All room-type objects are now consistent in method naming

This commit is contained in:
Ben 2020-02-14 21:56:52 +00:00
parent b4b1abba1c
commit 5c5b89884e
3 changed files with 61 additions and 28 deletions

View File

@ -208,23 +208,30 @@ class RoomTable(db.Model):
self.room = None self.room = None
self.logic = None self.logic = None
async def add(self, p): async def add_penguin(self, p):
self.penguins.append(p) self.penguins.append(p)
seat_id = len(self.penguins) - 1 seat_id = len(self.penguins) - 1
await p.send_xt("jt", self.id, seat_id + 1) await p.send_xt('jt', self.id, seat_id + 1)
await p.room.send_xt("ut", self.id, len(self.penguins)) await p.room.send_xt('ut', self.id, len(self.penguins))
p.table = self p.table = self
return seat_id return seat_id
async def remove(self, p): async def remove_penguin(self, p):
self.penguins.remove(p) seat_id = self.get_seat_id(p)
is_player = seat_id < 2
game_ready = len(self.penguins) > 1
if is_player and game_ready:
await self.send_xt('cz', p.safe_name)
await self.reset()
else:
self.penguins.remove(p)
await p.send_xt("lt") await p.send_xt('lt')
await self.room.send_xt("ut", self.id, len(self.penguins)) await self.room.send_xt('ut', self.id, len(self.penguins))
p.table = None p.table = None
async def reset(self): async def reset(self):
for penguin in self.penguins: for penguin in self.penguins:
@ -232,7 +239,7 @@ class RoomTable(db.Model):
self.logic = type(self.logic)() self.logic = type(self.logic)()
self.penguins = [] self.penguins = []
await self.room.send_xt("ut", self.id, 0) await self.room.send_xt('ut', self.id, 0)
def get_seat_id(self, p): def get_seat_id(self, p):
return self.penguins.index(p) return self.penguins.index(p)
@ -242,11 +249,11 @@ class RoomTable(db.Model):
return str() return str()
elif len(self.penguins) == 1: elif len(self.penguins) == 1:
player_one, = self.penguins player_one, = self.penguins
return "%".join([player_one.safe_name, str(), self.logic.get_string()]) return '%'.join([player_one.safe_name, str(), self.logic.get_string()])
player_one, player_two = self.penguins[:2] player_one, player_two = self.penguins[:2]
if len(self.penguins) == 2: if len(self.penguins) == 2:
return "%".join([player_one.safe_name, player_two.safe_name, self.logic.get_string()]) return '%'.join([player_one.safe_name, player_two.safe_name, self.logic.get_string()])
return "%".join([player_one.safe_name, player_two.safe_name, self.logic.get_string(), "1"]) return '%'.join([player_one.safe_name, player_two.safe_name, self.logic.get_string(), '1'])
async def send_xt(self, *data): async def send_xt(self, *data):
for penguin in self.penguins: for penguin in self.penguins:
@ -263,7 +270,7 @@ class RoomWaddle(db.Model):
game = db.Column(db.String(20), nullable=False) game = db.Column(db.String(20), nullable=False)
GameClassMapping = { GameClassMapping = {
'sled': SledRacingLogic
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -271,7 +278,7 @@ class RoomWaddle(db.Model):
self.penguins = [] self.penguins = []
async def add(self, p): async def add_penguin(self, p):
if not self.penguins: if not self.penguins:
self.penguins = [None] * self.seats self.penguins = [None] * self.seats
@ -283,9 +290,12 @@ class RoomWaddle(db.Model):
p.waddle = self p.waddle = self
if self.penguins.count(None) == 0: if self.penguins.count(None) == 0:
game_instance = RoomWaddle.GameClassMapping[self.game](self)
await game_instance.start()
await self.reset() await self.reset()
async def remove(self, p): async def remove_penguin(self, p):
seat_id = self.get_seat_id(p) seat_id = self.get_seat_id(p)
self.penguins[seat_id] = None self.penguins[seat_id] = None
await p.room.send_xt("uw", self.id, seat_id) await p.room.send_xt("uw", self.id, seat_id)

View File

@ -19,29 +19,33 @@ async def handle_get_waddle_population(p):
@handlers.handler(XTPacket('a', 'jt')) @handlers.handler(XTPacket('a', 'jt'))
async def handle_join_table(p, table_id: int): async def handle_join_table(p, table_id: int):
table = p.room.tables[table_id] try:
await table.add(p) table = p.room.tables[table_id]
await table.add_penguin(p)
except KeyError:
p.logger.warn(f'{p.username} tried to join a table that doesn\'t exist')
@handlers.handler(XTPacket('a', 'lt')) @handlers.handler(XTPacket('a', 'lt'))
async def handle_leave_table(p): async def handle_leave_table(p):
if p.table is not None: if p.table:
await p.table.remove(p) await p.table.remove_penguin(p)
@handlers.handler(XTPacket('j', 'jr'), after=handle_join_room) @handlers.handler(XTPacket('j', 'jr'), after=handle_join_room)
async def handle_join_room_table(p): async def handle_join_room_table(p):
if p.table is not None: if p.table:
await p.table.remove(p) await p.table.remove_penguin(p)
@handlers.handler(XTPacket('j', 'jp'), after=handle_join_player_room) @handlers.handler(XTPacket('j', 'jp'), after=handle_join_player_room)
async def handle_join_player_room_table(p): async def handle_join_player_room_table(p):
if p.table is not None: if p.table:
await p.table.remove(p) await p.table.remove_penguin(p)
@handlers.disconnected @handlers.disconnected
@handlers.player_attribute(joined_world=True)
async def handle_disconnect_table(p): async def handle_disconnect_table(p):
if p.table is not None: if p.table:
await p.table.remove(p) await p.table.remove_penguin(p)

View File

@ -12,12 +12,31 @@ async def handle_get_waddle_population(p):
async def handle_join_waddle(p, waddle_id: int): async def handle_join_waddle(p, waddle_id: int):
try: try:
waddle = p.room.waddles[waddle_id] waddle = p.room.waddles[waddle_id]
await waddle.add(p) await waddle.add_penguin(p)
except KeyError: except KeyError:
pass p.logger.warn(f'{p.username} tried to join a waddle that doesn\'t exist')
@handlers.handler(XTPacket('lw', ext='z')) @handlers.handler(XTPacket('lw', ext='z'))
async def handle_leave_waddle(p): async def handle_leave_waddle(p):
if p.waddle: if p.waddle:
await p.waddle.remove(p) await p.waddle.remove_penguin(p)
@handlers.handler(XTPacket('j', 'jr'), after=handle_join_room)
async def handle_join_room_waddle(p):
if p.waddle:
await p.waddle.remove_penguin(p)
@handlers.handler(XTPacket('j', 'jp'), after=handle_join_player_room)
async def handle_join_player_room_waddle(p):
if p.waddle:
await p.waddle.remove_penguin(p)
@handlers.disconnected
@handlers.player_attribute(joined_world=True)
async def handle_disconnect_waddle(p):
if p.waddle:
await p.waddle.remove_penguin(p)