Model methods for setting up and using tables & waddles

This commit is contained in:
Ben 2019-04-25 17:12:54 +01:00
parent 62b9531711
commit 70150b6b4e

View File

@ -1,4 +1,4 @@
from Houdini.Data import db from Houdini.Data import db, BaseCrumbsCollection
class Room(db.Model): class Room(db.Model):
@ -20,28 +20,31 @@ class Room(db.Model):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.penguins = [] self.penguins = []
def add_penguin(self, p): self.tables = {}
self.waddles = {}
async def add_penguin(self, p):
if p.room: if p.room:
p.room.remove_penguin(p) await p.room.remove_penguin(p)
self.penguins.append(p) self.penguins.append(p)
p.room = self p.room = self
p.send_xt('jr', self.get_string) await p.send_xt('jr', await self.get_string())
self.send_xt('ap', p.server.penguin_string_compiler.compile(p)) await self.send_xt('ap', await p.server.penguin_string_compiler.compile(p))
def remove_penguin(self, p): async def remove_penguin(self, p):
self.send_xt('rp', p.data.ID) await self.send_xt('rp', p.data.ID)
self.penguins.remove(p) self.penguins.remove(p)
p.room = None p.room = None
def get_string(self): async def get_string(self):
return '%'.join([p.server.penguin_string_compiler.compile(p) for p in self.penguins]) 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: for penguin in self.penguins:
penguin.send_xt(*data) await penguin.send_xt(*data)
class RoomTable(db.Model): class RoomTable(db.Model):
@ -52,6 +55,58 @@ class RoomTable(db.Model):
nullable=False) nullable=False)
Game = db.Column(db.String(20), 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): class RoomWaddle(db.Model):
__tablename__ = 'room_waddle' __tablename__ = 'room_waddle'
@ -61,3 +116,61 @@ class RoomWaddle(db.Model):
nullable=False) nullable=False)
Seats = db.Column(db.SmallInteger, nullable=False, server_default=db.text("2")) Seats = db.Column(db.SmallInteger, nullable=False, server_default=db.text("2"))
Game = db.Column(db.String(20), nullable=False) 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