ft_transcendence/transcendence/abstract/AbstractRoom.py
2023-12-23 18:12:13 +01:00

53 lines
1.3 KiB
Python

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)