Give room objects penguins_by_id and penguins_by_username attributes

This commit is contained in:
Ben 2019-09-09 22:07:51 +01:00
parent 4495779bf1
commit 8018a7c21e
3 changed files with 22 additions and 13 deletions

View File

@ -19,7 +19,9 @@ class Room(db.Model):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.penguins = [] self.penguins_by_id = {}
self.penguins_by_username = {}
self.penguins_by_character_id = {}
self.igloo = False self.igloo = False
@ -29,7 +31,11 @@ class Room(db.Model):
async def add_penguin(self, p): async def add_penguin(self, p):
if p.room: if p.room:
await p.room.remove_penguin(p) 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 p.room = self
if p.client_type == ClientType.Vanilla: if p.client_type == ClientType.Vanilla:
@ -50,7 +56,12 @@ class Room(db.Model):
else: else:
await self.send_xt('rp', p.data.id) 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.room = None
p.frame = 1 p.frame = 1
p.toy = None 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)) 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)) await p.send_xt('grs', self.id, await self.get_string(p))
async def get_string(self, p): async def get_string(self):
if p.client_type == ClientType.Vanilla: return '%'.join([await p.string for p in self.penguins_by_id.values()])
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 send_xt(self, *data): async def send_xt(self, *data):
for penguin in self.penguins: for penguin in self.penguins_by_id.values():
await penguin.send_xt(*data) await penguin.send_xt(*data)

View File

@ -10,9 +10,9 @@ async def handle_send_message(p, penguin_id: int, message: str):
return await p.close() return await p.close()
if p.muted: if p.muted:
for room_player in p.room.penguins: for penguin in p.room.penguins_by_id.values():
if room_player.data.moderator: if penguin.data.moderator:
await room_player.sendXt("mm", message, penguin_id) await penguin.send_xt("mm", message, penguin_id)
return return
if has_command_prefix(message): if has_command_prefix(message):

View File

@ -17,7 +17,7 @@ async def handle_close_book(p):
@handlers.handler(XTPacket('j', 'jr'), client=ClientType.Legacy) @handlers.handler(XTPacket('j', 'jr'), client=ClientType.Legacy)
async def handle_join_room_toy(p): 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: if penguin.toy is not None:
await p.send_xt('at', penguin.data.id, penguin.toy) 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.handler(XTPacket('j', 'crl'), client=ClientType.Vanilla)
@handlers.depends_on_packet(XTPacket('j', 'jr')) @handlers.depends_on_packet(XTPacket('j', 'jr'))
async def handle_client_room_loaded_toy(p): 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: if penguin.toy is not None:
await p.send_xt('at', penguin.data.id, penguin.toy) await p.send_xt('at', penguin.data.id, penguin.toy)