From 699abc9d1bc99114cad7165c4dc47cd6da836fca Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 23 Dec 2023 18:30:45 +0100 Subject: [PATCH] core: use abstract room, member, roommanager --- matchmaking/consumers.py | 6 +- matchmaking/models.py | 91 ++++--------------- transcendence/abstract/AbstractRoom.py | 16 ++-- transcendence/abstract/AbstractRoomManager.py | 16 +--- transcendence/abstract/AbstractRoomMember.py | 4 + 5 files changed, 36 insertions(+), 97 deletions(-) diff --git a/matchmaking/consumers.py b/matchmaking/consumers.py index 791d6ef..2087c54 100644 --- a/matchmaking/consumers.py +++ b/matchmaking/consumers.py @@ -28,14 +28,14 @@ class MatchMaking(WebsocketConsumer): waiting_room: WaitingRoom = normal.get(self.mode) waiting_room.append(Waiter(user.pk, self)) - waiting_room.broadcast(f"{len(waiting_room)} / {waiting_room._mode}") - if (len(waiting_room) == self.mode): + waiting_room.broadcast(f"{len(waiting_room)} / {waiting_room.mode}") + if (len(waiting_room) == waiting_room.mode): game_id: int = GameModel().create(waiting_room.get_users_id()) waiting_room.broadcast("game_found", {"game_id": game_id}) waiting_room.clear() def disconnect(self, close_code): waiting_room: WaitingRoom = normal.get(self.mode) - waiter: Waiter = waiting_room.get_waiter_by_socket(self) + waiter: Waiter = waiting_room.get_member_by_socket(self) if (waiter is not None): waiting_room.remove(waiter) \ No newline at end of file diff --git a/matchmaking/models.py b/matchmaking/models.py index a1f3fc5..8ce235d 100644 --- a/matchmaking/models.py +++ b/matchmaking/models.py @@ -3,94 +3,37 @@ 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: +class Waiter(AbstractRoomMember): + pass - def __init__(self, user_id: int, socket: WebsocketConsumer): - self.user_id: int = user_id - self.socket: WebsocketConsumer = socket +class WaitingRoom(AbstractRoom): - def send(self, detail: str, data: dict = {}): - raw_data: dict = {"detail": detail} - raw_data.update(data) - self.socket.send(text_data=json.dumps(raw_data)) - - def accept(self): - self.socket.accept() - - def disconnect(self): - self.socket.disconnect(200) - -class WaitingRoom: - - def __init__(self, waiting_room_manager, mode: int): - self._waiter_list: [Waiter] = [] - self._mode: int = mode - self._waiting_room_manager = waiting_room_manager - - def broadcast(self, detail: str, data: dict = {}): - for waiter in self._waiter_list: - waiter: Waiter - waiter.send(detail, data) - - def clear(self): - self._waiter_list.clear() - - def get_waiter_by_socket(self, socket: WebsocketConsumer): - for waiter in self._waiter_list: - waiter: Waiter - if (waiter.socket is socket): - return waiter - return None - - def get_waiter_by_user_id(self, user_id: int): - for waiter in self._waiter_list: - waiter: Waiter - if (waiter.user_id == user_id): - return waiter - return None - + def __init__(self, room_manager,mode): + super().__init__(room_manager) + self.mode = mode def append(self, waiter: Waiter): - tmp: Waiter = self.get_waiter_by_user_id(waiter.user_id) + tmp: Waiter = self.get_member_by_user_id(waiter.user_id) if (tmp is not None): tmp.send("Connection close: Another connection open with the same user id.") self.remove(tmp) waiter.accept() - self._waiter_list.append(waiter) + self._member_list.append(waiter) - def remove(self, waiter: Waiter): - self._waiter_list.remove(waiter) - waiter.disconnect() - - def empty(self): - for _ in self._waiter_list: - return False - return True - - def get_users_id(self): - return [waiter.user_id for waiter in self._waiter_list] - - def __len__(self): - return len(self._waiter_list) - - - -class WaitingRoomManager: - - def __init__(self): - self._waiting_rooms: [WaitingRoom] = [] +class WaitingRoomManager(AbstractRoomManager): def get(self, mode: int): - for waiting_room in self._waiting_rooms: - waiting_room: WaitingRoom = waiting_room - if (waiting_room._mode == mode): + for waiting_room in self._room_list: + waiting_room: WaitingRoom + if (waiting_room.mode == mode): return waiting_room tmp: WaitingRoom = WaitingRoom(self, mode) - self._waiting_rooms.append(tmp) + super().append(tmp) return tmp - - def remove(self, waiting_room: WaitingRoom): - self._waiting_rooms.remove(waiting_room) normal: WaitingRoomManager = WaitingRoomManager() \ No newline at end of file diff --git a/transcendence/abstract/AbstractRoom.py b/transcendence/abstract/AbstractRoom.py index 2a2b439..aceb9af 100644 --- a/transcendence/abstract/AbstractRoom.py +++ b/transcendence/abstract/AbstractRoom.py @@ -1,3 +1,5 @@ +from channels.generic.websocket import WebsocketConsumer + from .AbstractRoomMember import AbstractRoomMember class AbstractRoom: @@ -30,24 +32,20 @@ class AbstractRoom: def append(self, member: AbstractRoomMember): - tmp: AbstractRoomMember = self.get_member_by_user_id(member.user_id) - if (tmp is not None): - tmp.send("Connection close: Another connection open with the same user id.") - self.remove(tmp) - member.accept() self._member_list.append(waiter) + member.accept() def remove(self, member: AbstractRoomMember): self._member_list.remove(member) - waiter.disconnect() + member.disconnect() def empty(self): - for _ in self._waiter_list: + for _ in self._member_list: return False return True def get_users_id(self): - return [waiter.user_id for waiter in self._waiter_list] + return [member.user_id for member in self._member_list] def __len__(self): - return len(self._waiter_list) \ No newline at end of file + return len(self._member_list) \ No newline at end of file diff --git a/transcendence/abstract/AbstractRoomManager.py b/transcendence/abstract/AbstractRoomManager.py index 58ad97f..578102d 100644 --- a/transcendence/abstract/AbstractRoomManager.py +++ b/transcendence/abstract/AbstractRoomManager.py @@ -5,14 +5,8 @@ class AbstractRoomManager: def __init__(self): self._room_list: [AbstractRoom] = [] - def get(self, mode: int): - for room in self._room_list: - room: A - if (waiting_room._mode == mode): - return waiting_room - tmp: WaitingRoom = WaitingRoom(self, mode) - self._waiting_rooms.append(tmp) - return tmp - - def remove(self, waiting_room: WaitingRoom): - self._waiting_rooms.remove(waiting_room) \ No newline at end of file + def append(self, room: AbstractRoom): + self._room_list.append(room) + + def remove(self, room: AbstractRoom): + self._room_list.remove(room) \ No newline at end of file diff --git a/transcendence/abstract/AbstractRoomMember.py b/transcendence/abstract/AbstractRoomMember.py index 743ec60..1e0b506 100644 --- a/transcendence/abstract/AbstractRoomMember.py +++ b/transcendence/abstract/AbstractRoomMember.py @@ -1,3 +1,7 @@ +from channels.generic.websocket import WebsocketConsumer + +import json + class AbstractRoomMember: def __init__(self, user_id: int, socket: WebsocketConsumer):