def find_match(max_players: int): """Find a match for the player to join.""" # Get the player. player: Optional[Player] = get_player_from_request() if not player: return ErrorResponse.no_player() # Match to put player in. match: Optional[Match] = None # If player is already in a match, kick them out. if player.match: prev_match: Match = player.match prev_match.remove_player(player) pusher.trigger(f"match-{prev_match.uid}", "update", prev_match.export()) # Let's find a match for player to join. for item in Match.list(Match): item: Match = item # Match must be in waiting status with same number of max players. if item.status is Match.Status.WAIT and item.max_players == max_players: match = item # Could not find any match to put player in. if not match: # Create a new match. match = Match(max_players) # Add the player to the match. match.add_player(player) # Trigger update. pusher.trigger(f"match-{match.uid}", "update", match.export()) # Return the match. return jsonify(match.export())
def test_finish_empty_match(self): match = Match(max_players=2) match.add_player(Player("Player 1")) match.add_player(Player("Player 2")) self.assertEqual(match.status, Match.Status.RUN) match.remove_player(match.players[0]) self.assertEqual(match.status, Match.Status.RUN) match.remove_player(match.players[1]) self.assertEqual(match.status, Match.Status.FINISH)
def test_winner_is_board_solver(self): match = Match(max_players=2) player1 = Player("Player 1") player2 = Player("Player 2") match.add_player(player1) match.add_player(player2) match.get_player_board(player1).solve() match.update_status() self.assertEqual(match.status, Match.Status.FINISH) self.assertEqual(match.winner, player1)
def test_status(self): match = Match(max_players=1) self.assertEqual(match.status, Match.Status.WAIT) player1 = Player("Player 1") match.add_player(player1) self.assertEqual(match.status, Match.Status.RUN) match.get_player_board(player1).solve() match.update_status() self.assertEqual(match.status, Match.Status.FINISH) self.assertIsNone(player1.match)
def test_player_count_and_capacity(self): match = Match(max_players=2) match.add_player(Player("Player 1")) self.assertEqual(len(match.players), 1) match.add_player(Player("Player 2")) self.assertEqual(len(match.players), 2)