2023-12-11 07:39:53 -05:00
|
|
|
from django.db import models
|
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
from channels.generic.websocket import WebsocketConsumer
|
|
|
|
import json
|
|
|
|
|
2023-12-11 07:39:53 -05:00
|
|
|
# Create your models here.
|
2023-12-23 06:54:33 -05:00
|
|
|
class Waiter:
|
|
|
|
|
|
|
|
def __init__(self, user_id: int, socket: WebsocketConsumer):
|
|
|
|
self.user_id: int = user_id
|
|
|
|
self.socket: WebsocketConsumer = socket
|
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
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))
|
2023-12-23 06:54:33 -05:00
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
self.socket.accept()
|
|
|
|
|
|
|
|
def disconnect(self):
|
2023-12-23 10:47:22 -05:00
|
|
|
self.socket.disconnect(200)
|
2023-12-23 06:54:33 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
def broadcast(self, detail: str, data: dict = {}):
|
2023-12-23 06:54:33 -05:00
|
|
|
for waiter in self._waiter_list:
|
|
|
|
waiter: Waiter
|
2023-12-23 10:47:22 -05:00
|
|
|
waiter.send(detail, data)
|
2023-12-23 06:54:33 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
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
|
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
def append(self, waiter: Waiter):
|
|
|
|
tmp: Waiter = self.get_waiter_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)
|
2023-12-23 06:54:33 -05:00
|
|
|
waiter.accept()
|
|
|
|
self._waiter_list.append(waiter)
|
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
def remove(self, waiter: Waiter):
|
|
|
|
self._waiter_list.remove(waiter)
|
|
|
|
waiter.disconnect()
|
2023-12-23 06:54:33 -05:00
|
|
|
|
|
|
|
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] = []
|
|
|
|
|
|
|
|
def get(self, mode: int):
|
|
|
|
for waiting_room in self._waiting_rooms:
|
|
|
|
waiting_room: WaitingRoom = waiting_room
|
|
|
|
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)
|
|
|
|
|
|
|
|
normal: WaitingRoomManager = WaitingRoomManager()
|