Add try except for mancala movement

This commit is contained in:
Zaseth 2019-12-27 18:54:16 +01:00
parent 438d083602
commit 5dfae648c4

View File

@ -26,30 +26,35 @@ async def handle_join_game(p):
@handlers.handler(XTPacket('zm', ext='z')) @handlers.handler(XTPacket('zm', ext='z'))
@table_handler(MancalaLogic) @table_handler(MancalaLogic)
async def handle_send_move(p, move: int): async def handle_send_move(p, move: int):
seat_id = p.table.get_seat_id(p) try:
is_player = seat_id < 2 seat_id = p.table.get_seat_id(p)
game_ready = len(p.table.penguins) > 1 is_player = seat_id < 2
game_ready = len(p.table.penguins) > 1
if is_player and game_ready: if is_player and game_ready:
hollow, = map(int, move) hollow, = map(int, move)
current_player = p.table.penguins[p.table.logic.current_player - 1] current_player = p.table.penguins[p.table.logic.current_player - 1]
if current_player != p: return if current_player != p:
if not p.table.logic.is_valid_move(hollow): return return
if not p.table.logic.is_valid_move(hollow):
return
move_result = p.table.logic.place_stone(hollow) move_result = p.table.logic.place_stone(hollow)
await p.table.send_xt('zm', seat_id, hollow, move_result) await p.table.send_xt('zm', seat_id, hollow, move_result)
opponent = p.table.penguins[1 if p.table.logic.current_player == 1 else 0] opponent = p.table.penguins[1 if p.table.logic.current_player == 1 else 0]
if p.table.logic.is_position_win(): if p.table.logic.is_position_win():
await p.add_coins(10) await p.add_coins(10)
await opponent.add_coins(5) await opponent.add_coins(5)
await p.table.reset() await p.table.reset()
return return
elif p.table.logic.is_position_tie(): elif p.table.logic.is_position_tie():
await p.add_coins(5) await p.add_coins(5)
await opponent.add_coins(5) await opponent.add_coins(5)
await p.table.reset() await p.table.reset()
return return
p.table.logic.current_player = 2 if p.table.logic.current_player == 1 else 1 p.table.logic.current_player = 2 if p.table.logic.current_player == 1 else 1
except (KeyError, ValueError):
p.logger.warn(f'Invalid mancala move made by {p}')