2019-04-25 16:12:54 +00:00
|
|
|
from Houdini.Data import db, BaseCrumbsCollection
|
2019-03-18 15:58:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Room(db.Model):
|
|
|
|
__tablename__ = 'room'
|
|
|
|
|
|
|
|
ID = db.Column(db.Integer, primary_key=True)
|
|
|
|
InternalID = db.Column(db.Integer, nullable=False, unique=True,
|
|
|
|
server_default=db.text("nextval('\"room_InternalID_seq\"'::regclass)"))
|
|
|
|
Name = db.Column(db.String(50), nullable=False)
|
|
|
|
Member = db.Column(db.Boolean, nullable=False, server_default=db.text("false"))
|
|
|
|
MaxUsers = db.Column(db.SmallInteger, nullable=False, server_default=db.text("80"))
|
|
|
|
RequiredItem = db.Column(db.ForeignKey('item.ID', ondelete='CASCADE', onupdate='CASCADE'))
|
|
|
|
Game = db.Column(db.Boolean, nullable=False, server_default=db.text("false"))
|
|
|
|
Blackhole = db.Column(db.Boolean, nullable=False, server_default=db.text("false"))
|
|
|
|
Spawn = db.Column(db.Boolean, nullable=False, server_default=db.text("false"))
|
|
|
|
StampGroup = db.Column(db.ForeignKey('stamp_group.ID', ondelete='CASCADE', onupdate='CASCADE'))
|
|
|
|
|
2019-04-10 23:55:48 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.penguins = []
|
|
|
|
|
2019-04-25 16:12:54 +00:00
|
|
|
self.tables = {}
|
|
|
|
self.waddles = {}
|
|
|
|
|
|
|
|
async def add_penguin(self, p):
|
2019-04-10 23:55:48 +00:00
|
|
|
if p.room:
|
2019-04-25 16:12:54 +00:00
|
|
|
await p.room.remove_penguin(p)
|
2019-04-10 23:55:48 +00:00
|
|
|
self.penguins.append(p)
|
|
|
|
|
|
|
|
p.room = self
|
|
|
|
|
2019-04-25 16:12:54 +00:00
|
|
|
await p.send_xt('jr', await self.get_string())
|
|
|
|
await self.send_xt('ap', await p.server.penguin_string_compiler.compile(p))
|
2019-04-10 23:55:48 +00:00
|
|
|
|
2019-04-25 16:12:54 +00:00
|
|
|
async def remove_penguin(self, p):
|
|
|
|
await self.send_xt('rp', p.data.ID)
|
2019-04-10 23:55:48 +00:00
|
|
|
|
|
|
|
self.penguins.remove(p)
|
|
|
|
p.room = None
|
|
|
|
|
2019-04-25 16:12:54 +00:00
|
|
|
async def get_string(self):
|
|
|
|
return '%'.join([await p.server.penguin_string_compiler.compile(p) for p in self.penguins])
|
2019-04-10 23:55:48 +00:00
|
|
|
|
2019-04-25 16:12:54 +00:00
|
|
|
async def send_xt(self, *data):
|
2019-04-10 23:55:48 +00:00
|
|
|
for penguin in self.penguins:
|
2019-04-25 16:12:54 +00:00
|
|
|
await penguin.send_xt(*data)
|
2019-04-10 23:55:48 +00:00
|
|
|
|
2019-03-18 15:58:21 +00:00
|
|
|
|
|
|
|
class RoomTable(db.Model):
|
|
|
|
__tablename__ = 'room_table'
|
|
|
|
|
|
|
|
ID = db.Column(db.Integer, primary_key=True, nullable=False)
|
|
|
|
RoomID = db.Column(db.ForeignKey('room.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True,
|
|
|
|
nullable=False)
|
|
|
|
Game = db.Column(db.String(20), nullable=False)
|
|
|
|
|
2019-04-25 16:12:54 +00:00
|
|
|
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)
|
|
|
|
|
2019-03-18 15:58:21 +00:00
|
|
|
|
|
|
|
class RoomWaddle(db.Model):
|
|
|
|
__tablename__ = 'room_waddle'
|
|
|
|
|
|
|
|
ID = db.Column(db.Integer, primary_key=True, nullable=False)
|
|
|
|
RoomID = db.Column(db.ForeignKey('room.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True,
|
|
|
|
nullable=False)
|
|
|
|
Seats = db.Column(db.SmallInteger, nullable=False, server_default=db.text("2"))
|
|
|
|
Game = db.Column(db.String(20), nullable=False)
|
2019-04-25 16:12:54 +00:00
|
|
|
|
|
|
|
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
|