diff --git a/Houdini/Data/Room.py b/Houdini/Data/Room.py index 145cbfe..1907749 100644 --- a/Houdini/Data/Room.py +++ b/Houdini/Data/Room.py @@ -1,4 +1,4 @@ -from Houdini.Data import db +from Houdini.Data import db, BaseCrumbsCollection class Room(db.Model): @@ -20,28 +20,31 @@ class Room(db.Model): super().__init__(*args, **kwargs) self.penguins = [] - def add_penguin(self, p): + self.tables = {} + self.waddles = {} + + async def add_penguin(self, p): if p.room: - p.room.remove_penguin(p) + await p.room.remove_penguin(p) self.penguins.append(p) p.room = self - p.send_xt('jr', self.get_string) - self.send_xt('ap', p.server.penguin_string_compiler.compile(p)) + await p.send_xt('jr', await self.get_string()) + await self.send_xt('ap', await p.server.penguin_string_compiler.compile(p)) - def remove_penguin(self, p): - self.send_xt('rp', p.data.ID) + async def remove_penguin(self, p): + await self.send_xt('rp', p.data.ID) self.penguins.remove(p) p.room = None - def get_string(self): - return '%'.join([p.server.penguin_string_compiler.compile(p) for p in self.penguins]) + async def get_string(self): + return '%'.join([await p.server.penguin_string_compiler.compile(p) for p in self.penguins]) - def send_xt(self, *data): + async def send_xt(self, *data): for penguin in self.penguins: - penguin.send_xt(*data) + await penguin.send_xt(*data) class RoomTable(db.Model): @@ -52,6 +55,58 @@ class RoomTable(db.Model): nullable=False) Game = db.Column(db.String(20), nullable=False) + GameClassMapping = { + + } + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.penguins = [] + self.room = None + + async def add(self, p): + self.penguins.append(p) + + seat_id = len(self.penguins) - 1 + + await p.send_xt("jt", self.ID, seat_id + 1) + await p.room.send_xt("ut", self.ID, len(self.penguins)) + p.table = self + + return seat_id + + async def remove(self, p): + self.penguins.remove(p) + + await p.send_xt("lt") + await p.room.send_xt("ut", self.ID, len(self.penguins)) + p.table = None + + async def reset(self): + for penguin in self.penguins: + penguin.table = None + + self.penguins = [] + await self.room.send_xt("ut", self.ID, 0) + + def get_seat_id(self, p): + return self.penguins.index(p) + + def get_string(self): + if len(self.penguins) == 0: + return str() + elif len(self.penguins) == 1: + player_one, = self.penguins + return "%".join([player_one.Nickname, str(), self.game.get_string()]) + player_one, player_two = self.penguins[:2] + if len(self.penguins) == 2: + return "%".join([player_one.Nickname, player_two.Nickname, self.game.get_string()]) + return "%".join([player_one.Nickname, player_two.Nickname, self.game.get_string(), "1"]) + + async def send_xt(self, *data): + for penguin in self.penguins: + await penguin.send_xt(*data) + class RoomWaddle(db.Model): __tablename__ = 'room_waddle' @@ -61,3 +116,61 @@ class RoomWaddle(db.Model): nullable=False) Seats = db.Column(db.SmallInteger, nullable=False, server_default=db.text("2")) Game = db.Column(db.String(20), nullable=False) + + GameClassMapping = { + + } + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.penguins = [] + + async def add(self, p): + if not self.penguins: + self.penguins = [None] * self.Seats + + seat_id = self.penguins.index(None) + self.penguins[seat_id] = p + await p.send_xt("jw", seat_id) + await p.room.send_xt("uw", self.ID, seat_id, p.Nickname) + + p.waddle = self + + if self.penguins.count(None) == 0: + await self.reset() + + async def remove(self, p): + seat_id = self.get_seat_id(p) + self.penguins[seat_id] = None + await self.room.send_xt("uw", self.ID, seat_id) + + p.waddle = None + + async def reset(self): + for seat_id, penguin in enumerate(self.penguins): + if penguin: + self.penguins[seat_id] = None + await penguin.room.send_xt("uw", self.ID, seat_id) + + def get_seat_id(self, p): + return self.penguins.index(p) + + +class RoomCrumbsCollection(BaseCrumbsCollection): + + def __init__(self): + super().__init__(model=Room, key='ID') + + def get_spawn_rooms(self): + return [room for room in self.values() if room.Spawn] + + async def setup_tables(self): + async with self._db.transaction(): + async for table in RoomTable.query.gino.iterate(): + self[table.RoomID].tables[table.ID] = table + + async def setup_waddles(self): + async with self._db.transaction(): + async for waddle in RoomWaddle.query.gino.iterate(): + self[waddle.RoomID].waddles[waddle.ID] = waddle