dockered
This commit is contained in:
0
django/matchmaking/__init__.py
Normal file
0
django/matchmaking/__init__.py
Normal file
3
django/matchmaking/admin.py
Normal file
3
django/matchmaking/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
django/matchmaking/apps.py
Normal file
6
django/matchmaking/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MatchmakingConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'matchmaking'
|
53
django/matchmaking/consumers.py
Normal file
53
django/matchmaking/consumers.py
Normal file
@ -0,0 +1,53 @@
|
||||
from channels.generic.websocket import WebsocketConsumer
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from games.models import GameModel
|
||||
|
||||
import json
|
||||
|
||||
from .models import Waiter, WaitingRoom, waiting_room_manager
|
||||
|
||||
class MatchMaking(WebsocketConsumer):
|
||||
|
||||
def connect(self):
|
||||
|
||||
user: User = self.scope["user"]
|
||||
|
||||
if (user.is_anonymous or not user.is_authenticated):
|
||||
return
|
||||
|
||||
self.mode: int = int(self.scope['url_route']['kwargs']['mode'])
|
||||
self.game_type: str = self.scope['url_route']['kwargs']['game_type']
|
||||
self.group_name = self.mode
|
||||
|
||||
self.waiting_room: WaitingRoom = waiting_room_manager.get(self.game_type, self.mode)
|
||||
self.waiting_room.append(Waiter(user, self))
|
||||
|
||||
if (self.mode < 2 or self.mode > 4):
|
||||
data: dict = {
|
||||
"detail": "The mode must be > 1 and < 4.",
|
||||
}
|
||||
self.send(json.dumps(data))
|
||||
self.disconnect(1000)
|
||||
return
|
||||
|
||||
if (self.game_type not in ["tictactoe", "pong"]):
|
||||
data: dict = {
|
||||
"detail": "The game_type must 'pong' or 'tictactoe'.",
|
||||
}
|
||||
self.send(json.dumps(data))
|
||||
self.disconnect(1000)
|
||||
return
|
||||
|
||||
self.waiting_room.broadcast(f"{len(self.waiting_room)} / {self.waiting_room.mode}")
|
||||
if (len(self.waiting_room) == self.waiting_room.mode):
|
||||
game: GameModel = GameModel(game_type=self.game_type).create(self.waiting_room.get_members())
|
||||
self.waiting_room.broadcast("game_found", {"game_id": game.pk, "game_type": self.game_type})
|
||||
|
||||
def disconnect(self, close_code):
|
||||
super().disconnect(close_code)
|
||||
waiting_room: WaitingRoom = waiting_room_manager.get(self.game_type, self.mode)
|
||||
waiter: Waiter = waiting_room.get_member_by_socket(self)
|
||||
if (waiter is not None):
|
||||
waiting_room.remove(waiter)
|
56
django/matchmaking/models.py
Normal file
56
django/matchmaking/models.py
Normal file
@ -0,0 +1,56 @@
|
||||
from django.db import models
|
||||
|
||||
from channels.generic.websocket import WebsocketConsumer
|
||||
import json
|
||||
|
||||
from transcendence.abstract.AbstractRoom import AbstractRoom
|
||||
from transcendence.abstract.AbstractRoomManager import AbstractRoomManager
|
||||
from transcendence.abstract.AbstractRoomMember import AbstractRoomMember
|
||||
|
||||
# Create your models here.
|
||||
class Waiter(AbstractRoomMember):
|
||||
pass
|
||||
|
||||
class WaitingRoom(AbstractRoom):
|
||||
|
||||
def __init__(self, room_manager, game_type: str, mode: int):
|
||||
|
||||
super().__init__(room_manager)
|
||||
self._member_list: set[Waiter]
|
||||
|
||||
self.mode: int = mode
|
||||
self.game_type: str = game_type
|
||||
|
||||
def append(self, waiter: Waiter):
|
||||
|
||||
tmp: Waiter = self.get_member_by_user(waiter.user)
|
||||
if (tmp is not None):
|
||||
tmp.send("Connection close: Another connection open with the same user id.")
|
||||
self.remove(tmp)
|
||||
|
||||
waiter.socket.accept()
|
||||
|
||||
super().append(waiter)
|
||||
|
||||
class WaitingRoomManager(AbstractRoomManager):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self._room_list: set[WaitingRoom]
|
||||
|
||||
|
||||
def get(self, game_type: str, mode: int) -> WaitingRoom:
|
||||
|
||||
for waiting_room in self._room_list:
|
||||
waiting_room: WaitingRoom
|
||||
if (waiting_room.mode == mode and waiting_room.game_type == game_type):
|
||||
return waiting_room
|
||||
|
||||
tmp: WaitingRoom = WaitingRoom(self, game_type, mode)
|
||||
|
||||
super().append(tmp)
|
||||
|
||||
return tmp
|
||||
|
||||
waiting_room_manager: WaitingRoomManager = WaitingRoomManager()
|
6
django/matchmaking/routing.py
Normal file
6
django/matchmaking/routing.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.urls import re_path
|
||||
from . import consumers
|
||||
|
||||
websocket_urlpatterns = [
|
||||
re_path(r'ws/matchmaking/(?P<game_type>\w+)/(?P<mode>\d+)$', consumers.MatchMaking.as_asgi())
|
||||
]
|
3
django/matchmaking/tests.py
Normal file
3
django/matchmaking/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
3
django/matchmaking/views.py
Normal file
3
django/matchmaking/views.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
Reference in New Issue
Block a user