From 0db73216b3a21543f30a326dbbf6c6a5363ae99c Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 23 Dec 2023 18:11:30 +0100 Subject: [PATCH] rename the project --- manage.py | 2 +- {trancendence => transcendence}/__init__.py | 0 transcendence/abstract/AbstractRoom.py | 53 +++++++++++++++++++ transcendence/abstract/AbstractRoomManager.py | 18 +++++++ transcendence/abstract/AbstractRoomMember.py | 16 ++++++ {trancendence => transcendence}/asgi.py | 0 {trancendence => transcendence}/settings.py | 8 +-- {trancendence => transcendence}/urls.py | 0 {trancendence => transcendence}/wsgi.py | 0 9 files changed, 92 insertions(+), 5 deletions(-) rename {trancendence => transcendence}/__init__.py (100%) create mode 100644 transcendence/abstract/AbstractRoom.py create mode 100644 transcendence/abstract/AbstractRoomManager.py create mode 100644 transcendence/abstract/AbstractRoomMember.py rename {trancendence => transcendence}/asgi.py (100%) rename {trancendence => transcendence}/settings.py (95%) rename {trancendence => transcendence}/urls.py (100%) rename {trancendence => transcendence}/wsgi.py (100%) diff --git a/manage.py b/manage.py index dd64115..9294fec 100755 --- a/manage.py +++ b/manage.py @@ -6,7 +6,7 @@ import sys def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings') + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'transcendence.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/trancendence/__init__.py b/transcendence/__init__.py similarity index 100% rename from trancendence/__init__.py rename to transcendence/__init__.py diff --git a/transcendence/abstract/AbstractRoom.py b/transcendence/abstract/AbstractRoom.py new file mode 100644 index 0000000..2a2b439 --- /dev/null +++ b/transcendence/abstract/AbstractRoom.py @@ -0,0 +1,53 @@ +from .AbstractRoomMember import AbstractRoomMember + +class AbstractRoom: + + def __init__(self, room_manager): + self._member_list: [AbstractRoomMember] = [] + self.room_manager = room_manager + + def broadcast(self, detail: str, data: dict = {}): + for member in self._member_list: + member: AbstractRoomMember + member.send(detail, data) + + def clear(self): + self._member_list.clear() + + def get_member_by_socket(self, socket: WebsocketConsumer): + for member in self._member_list: + member: AbstractRoomMember + if (member.socket is socket): + return member + return None + + def get_member_by_user_id(self, user_id: int): + for member in self._member_list: + member: AbstractRoomMember + if (member.user_id == user_id): + return member + return None + + + 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) + + def remove(self, member: AbstractRoomMember): + self._member_list.remove(member) + 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) \ No newline at end of file diff --git a/transcendence/abstract/AbstractRoomManager.py b/transcendence/abstract/AbstractRoomManager.py new file mode 100644 index 0000000..58ad97f --- /dev/null +++ b/transcendence/abstract/AbstractRoomManager.py @@ -0,0 +1,18 @@ +from .AbstractRoom import AbstractRoom + +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 diff --git a/transcendence/abstract/AbstractRoomMember.py b/transcendence/abstract/AbstractRoomMember.py new file mode 100644 index 0000000..743ec60 --- /dev/null +++ b/transcendence/abstract/AbstractRoomMember.py @@ -0,0 +1,16 @@ +class AbstractRoomMember: + + def __init__(self, user_id: int, socket: WebsocketConsumer): + self.user_id: int = user_id + self.socket: WebsocketConsumer = socket + + 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) \ No newline at end of file diff --git a/trancendence/asgi.py b/transcendence/asgi.py similarity index 100% rename from trancendence/asgi.py rename to transcendence/asgi.py diff --git a/trancendence/settings.py b/transcendence/settings.py similarity index 95% rename from trancendence/settings.py rename to transcendence/settings.py index 3080286..b75cedb 100644 --- a/trancendence/settings.py +++ b/transcendence/settings.py @@ -1,5 +1,5 @@ """ -Django settings for trancendence project. +Django settings for transcendence project. Generated by 'django-admin startproject' using Django 4.2.6. @@ -61,7 +61,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', ] -ASGI_APPLICATION = 'trancendence.asgi.application' +ASGI_APPLICATION = 'transcendence.asgi.application' CHANNEL_LAYERS = { 'default' :{ @@ -81,7 +81,7 @@ MIDDLEWARE = [ 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] -ROOT_URLCONF = 'trancendence.urls' +ROOT_URLCONF = 'transcendence.urls' TEMPLATES = [ { @@ -99,7 +99,7 @@ TEMPLATES = [ }, ] -WSGI_APPLICATION = 'trancendence.wsgi.application' +WSGI_APPLICATION = 'transcendence.wsgi.application' # Database diff --git a/trancendence/urls.py b/transcendence/urls.py similarity index 100% rename from trancendence/urls.py rename to transcendence/urls.py diff --git a/trancendence/wsgi.py b/transcendence/wsgi.py similarity index 100% rename from trancendence/wsgi.py rename to transcendence/wsgi.py