async def test_disconnect_all_peers(game_connection: GameConnection, real_game: Game, players): real_game.state = GameState.LOBBY game_connection.player = players.hosting game_connection.game = real_game disconnect_done = mock.Mock() async def fake_send_dc(player_id): await asyncio.sleep(1) # Take some time disconnect_done.success() return "OK" # Set up a peer that will disconnect without error ok_disconnect = asynctest.create_autospec(GameConnection) ok_disconnect.state = GameConnectionState.CONNECTED_TO_HOST ok_disconnect.send_DisconnectFromPeer = fake_send_dc # Set up a peer that will throw an exception fail_disconnect = asynctest.create_autospec(GameConnection) fail_disconnect.send_DisconnectFromPeer.return_value = Exception( "Test exception") fail_disconnect.state = GameConnectionState.CONNECTED_TO_HOST # Add the peers to the game real_game.add_game_connection(fail_disconnect) real_game.add_game_connection(ok_disconnect) await game_connection.disconnect_all_peers() disconnect_done.success.assert_called_once()
async def test_add_game_connection_twice(game: Game, players, mock_game_connection): """ When a player disconnects and reconnects to the same game, they should not be considered as 'in-lobby' until the new PlayerOptions are received. """ game.state = GameState.LOBBY mock_game_connection.player = players.hosting mock_game_connection.state = GameConnectionState.CONNECTED_TO_HOST # Connect the host game.add_game_connection(mock_game_connection) game.set_player_option(players.hosting.id, "Team", 1) assert game.players == {players.hosting} # Join a new player join_conn = add_connected_player(game, players.joining) assert game.players == {players.hosting, players.joining} # Player leaves await game.remove_game_connection(join_conn) assert game.players == {players.hosting} # Player joins again game.add_game_connection(join_conn) assert game.to_dict()["num_players"] == 1 assert game.players == {players.hosting} game.set_player_option(players.joining.id, "Team", 1) assert game.players == {players.hosting, players.joining} assert game.to_dict()["num_players"] == 2
async def test_remove_game_connection(game: Game, players, mock_game_connection): game.state = GameState.LOBBY mock_game_connection.player = players.hosting mock_game_connection.state = GameConnectionState.CONNECTED_TO_HOST game.add_game_connection(mock_game_connection) await game.remove_game_connection(mock_game_connection) assert players.hosting not in game.players
async def test_add_game_connection_throws_if_not_lobby_state( game: Game, players, mock_game_connection): game.state = GameState.INITIALIZING mock_game_connection.player = players.hosting mock_game_connection.state = GameConnectionState.CONNECTED_TO_HOST with pytest.raises(GameError): game.add_game_connection(mock_game_connection) assert players.hosting not in game.players
async def test_add_game_connection_throws_if_not_connected_to_host( game: Game, players, mock_game_connection): game.state = GameState.LOBBY mock_game_connection.player = players.hosting mock_game_connection.state = GameConnectionState.INITIALIZED with pytest.raises(GameError): game.add_game_connection(mock_game_connection) assert players.hosting not in game.players
def add_connected_player(game: Game, player): game.game_service.player_service[player.id] = player gc = make_mock_game_connection(state=GameConnectionState.CONNECTED_TO_HOST, player=player) game.set_player_option(player.id, 'Army', 0) game.set_player_option(player.id, 'StartSpot', 0) game.set_player_option(player.id, 'Team', 0) game.set_player_option(player.id, 'Faction', 0) game.set_player_option(player.id, 'Color', 0) game.add_game_connection(gc) return gc
async def test_game_end_when_no_more_connections(game: Game, mock_game_connection): game.state = GameState.LOBBY game.on_game_end = CoroutineMock() mock_game_connection.state = GameConnectionState.CONNECTED_TO_HOST game.add_game_connection(mock_game_connection) await game.remove_game_connection(mock_game_connection) game.on_game_end.assert_any_call()
def add_connected_player(game: Game, player): game.game_service.player_service[player.id] = player gc = make_mock_game_connection(state=GameConnectionState.CONNECTED_TO_HOST, player=player) game.set_player_option(player.id, "Army", 0) game.set_player_option(player.id, "StartSpot", 0) game.set_player_option(player.id, "Team", 0) game.set_player_option(player.id, "Faction", 0) game.set_player_option(player.id, "Color", 0) game.add_game_connection(gc) return gc
async def test_add_game_connection(game: Game, players, mock_game_connection): game.state = GameState.LOBBY mock_game_connection.player = players.hosting mock_game_connection.state = GameConnectionState.CONNECTED_TO_HOST game.add_game_connection(mock_game_connection) # Players should not be considered as 'in lobby' until the host has sent # "PlayerOption" configuration for them assert game.to_dict()["num_players"] == 0 assert game.players == set() game.set_player_option(players.hosting.id, "Team", 1) assert players.hosting in game.players
async def test_players_exclude_observers(game: Game, game_add_players, player_factory): game.state = GameState.LOBBY players = game_add_players(game, 2) obs = player_factory(player_id=3, login="******", global_rating=(1500, 500)) game.game_service.player_service[obs.id] = obs gc = make_mock_game_connection(state=GameConnectionState.CONNECTED_TO_HOST, player=obs) game.set_player_option(obs.id, "Army", -1) game.set_player_option(obs.id, "StartSpot", -1) game.set_player_option(obs.id, "Team", 0) game.set_player_option(obs.id, "Faction", 0) game.set_player_option(obs.id, "Color", 0) game.add_game_connection(gc) await game.launch() assert game.players == frozenset(players)