diff --git a/houdini/data/room.py b/houdini/data/room.py index 3399449..2e8073c 100644 --- a/houdini/data/room.py +++ b/houdini/data/room.py @@ -19,7 +19,9 @@ class Room(db.Model): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.penguins = [] + self.penguins_by_id = {} + self.penguins_by_username = {} + self.penguins_by_character_id = {} self.igloo = False @@ -29,7 +31,11 @@ class Room(db.Model): async def add_penguin(self, p): if p.room: await p.room.remove_penguin(p) - self.penguins.append(p) + self.penguins_by_id[p.data.id] = p + self.penguins_by_username[p.data.username] = p + + if p.data.character: + self.penguins_by_character_id[p.data.character] = p p.room = self if p.client_type == ClientType.Vanilla: @@ -50,7 +56,12 @@ class Room(db.Model): else: await self.send_xt('rp', p.data.id) - self.penguins.remove(p) + del self.penguins_by_id[p.data.id] + del self.penguins_by_username[p.data.username] + + if p.data.character: + del self.penguins_by_character_id[p.data.character] + p.room = None p.frame = 1 p.toy = None @@ -60,13 +71,11 @@ class Room(db.Model): return await p.send_xt('grs', self.id, (await self.get_string(p) + await p.string)) await p.send_xt('grs', self.id, await self.get_string(p)) - async def get_string(self, p): - if p.client_type == ClientType.Vanilla: - return '%'.join([await p.string for p in self.penguins if not p.data.stealth_moderator]) - return '%'.join([await p.string for p in self.penguins]) + async def get_string(self): + return '%'.join([await p.string for p in self.penguins_by_id.values()]) async def send_xt(self, *data): - for penguin in self.penguins: + for penguin in self.penguins_by_id.values(): await penguin.send_xt(*data) diff --git a/houdini/handlers/play/message.py b/houdini/handlers/play/message.py index fec41af..38bcdef 100644 --- a/houdini/handlers/play/message.py +++ b/houdini/handlers/play/message.py @@ -10,9 +10,9 @@ async def handle_send_message(p, penguin_id: int, message: str): return await p.close() if p.muted: - for room_player in p.room.penguins: - if room_player.data.moderator: - await room_player.sendXt("mm", message, penguin_id) + for penguin in p.room.penguins_by_id.values(): + if penguin.data.moderator: + await penguin.send_xt("mm", message, penguin_id) return if has_command_prefix(message): diff --git a/houdini/handlers/play/toy.py b/houdini/handlers/play/toy.py index ce1bd3f..d124fa3 100644 --- a/houdini/handlers/play/toy.py +++ b/houdini/handlers/play/toy.py @@ -17,7 +17,7 @@ async def handle_close_book(p): @handlers.handler(XTPacket('j', 'jr'), client=ClientType.Legacy) async def handle_join_room_toy(p): - for penguin in p.room.penguins: + for penguin in p.room.penguins_by_id.values(): if penguin.toy is not None: await p.send_xt('at', penguin.data.id, penguin.toy) @@ -25,6 +25,6 @@ async def handle_join_room_toy(p): @handlers.handler(XTPacket('j', 'crl'), client=ClientType.Vanilla) @handlers.depends_on_packet(XTPacket('j', 'jr')) async def handle_client_room_loaded_toy(p): - for penguin in p.room.penguins: + for penguin in p.room.penguins_by_id.values(): if penguin.toy is not None: await p.send_xt('at', penguin.data.id, penguin.toy)