ft_transcendence/transcendence/abstract/AbstractRoom.py

52 lines
1.2 KiB
Python
Raw Permalink Normal View History

from channels.generic.websocket import WebsocketConsumer
2023-12-23 12:11:30 -05:00
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):
self._member_list.append(member)
member.accept()
2023-12-23 12:11:30 -05:00
def remove(self, member: AbstractRoomMember, code: int = 1000):
2023-12-23 12:11:30 -05:00
self._member_list.remove(member)
member.disconnect(code)
2023-12-23 12:11:30 -05:00
def empty(self):
2023-12-23 12:34:18 -05:00
for _ in self._member_list:
2023-12-23 12:11:30 -05:00
return False
return True
def get_users_id(self):
return [member.user_id for member in self._member_list]
2023-12-23 12:11:30 -05:00
def __len__(self):
return len(self._member_list)