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_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
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
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 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
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
async def test_game_end_when_no_more_connections(game: Game, mock_game_connection): game.state = GameState.LOBBY game.on_game_end = CoroMock() 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()
async def test_game_end_when_no_more_connections(game: Game, mock_game_connection): game.state = GameState.LOBBY game.on_game_end = CoroMock() 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 = 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
def add_connected_player(game: Game, player): game.game_service.player_service[player.id] = player gc = 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.state = GameState.LOBBY players = add_players(game, 2) obs = Player(id=3, login='******', global_rating=(1500, 500)) game.game_service.player_service[obs.id] = obs gc = 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)
async def test_players_exclude_observers(game: Game): game.state = GameState.LOBBY players = add_players(game, 2) obs = 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)
def test_add_game_connection(game: Game, players, game_connection): game.state = GameState.LOBBY game_connection.player = players.hosting game_connection.state = GameConnectionState.CONNECTED_TO_HOST game.add_game_connection(game_connection) assert players.hosting in game.players
def test_add_game_connection(game: Game, players, game_connection): game.state = GameState.LOBBY game_connection.player = players.hosting game_connection.state = GameConnectionState.CONNECTED_TO_HOST game.add_game_connection(game_connection) assert players.hosting in game.players