diff options
Diffstat (limited to 'swiftstory/game.py')
-rw-r--r-- | swiftstory/game.py | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/swiftstory/game.py b/swiftstory/game.py index 4569a5c..c46df4b 100644 --- a/swiftstory/game.py +++ b/swiftstory/game.py @@ -124,59 +124,62 @@ class Game: return self.try_view_played_cards(player) - def try_designate_card(self, player: Player, card_id: int) -> str: + def try_designate_card(self, player: Player, card_id: Optional[int]) -> str: if self.state is not GameState.WAITING_DESIGNATION: raise WrongAction('Not now, moron !') if self.judge is not player: raise WrongAction('Who do you think you are !?') - if card_id is None and len(self.board.played_cards) > 0: - raise WrongAction('There are cards on the board, pick one !') + def move_on() -> str: + self.judge = None - if card_id is not None or len(self.board.played_cards) > 0: - # if there are cards on the board - # TODO check exception - try: - card, winner = self.board.played_cards[card_id] - except IndexError: - return error('Invalid card') + for p in self.players: + if p is not player: + p.register_notification({'op': 'judge_needed'}) - winner.inc_score() + self.state = GameState.WAITING_NEW_JUDGE + return success(None) - # put the cards back in the deck - self.board.recycle_played_cards() + if not self.board.played_cards: + return move_on() - # reset the state of the players - for p in self.players: - if p.has_played: - idx = p.receive_card(self.board.pick_white_card()) - - p.register_notification({ - 'op': 'received_card', - 'content': { - 'card': { - 'id': idx, - 'desc': p.cards[idx][1], - }, - }, - }) - p.has_played = False + if card_id is None: + raise WrongAction('There are cards on the board, pick one !') + + # TODO check exception + try: + card, winner = self.board.played_cards[card_id] + except IndexError: + return error('Invalid card') + + winner.inc_score() - self.judge = None + # put the cards back in the deck + self.board.recycle_played_cards() + # reset the state of the players for p in self.players: - if p is not player: - p.register_notification({'op': 'judge_needed'}) + if p.has_played: + idx = p.receive_card(self.board.pick_white_card()) - self.state = GameState.WAITING_NEW_JUDGE + p.register_notification({ + 'op': 'received_card', + 'content': { + 'card': { + 'id': idx, + 'desc': p.cards[idx][1], + }, + }, + }) + p.has_played = False - return success(None) + return move_on() def try_view_player_cards(self, player: Player) -> str: return success([(idx, desc) for idx, (_, desc) in player.cards.items()]) - def try_view_played_cards(self, player: Player): + def try_view_played_cards(self, player: Player) -> str: if self.state is not GameState.WAITING_DESIGNATION: raise WrongAction('Not now, moron !') |