async def test_game_visible_for_rating(game: Game, players): game.enforce_rating_range = True game.displayed_rating_range = InclusiveRange(2000, None) game.host = players.hosting players.joining.ratings[RatingType.GLOBAL] = (1500, 1) assert not game.is_visible_to_player(players.joining) players.joining.ratings[RatingType.GLOBAL] = (2100, 1) assert game.is_visible_to_player(players.joining)
async def test_game_is_invalid_due_to_desyncs(game: Game, players): game.state = GameState.LOBBY add_connected_players(game, [players.hosting, players.joining]) game.host = players.hosting await game.launch() game.desyncs = 30 await game.on_game_end() assert game.validity is ValidityState.TOO_MANY_DESYNCS
async def test_game_sim_ends_when_no_more_connections(game: Game, players): game.state = GameState.LOBBY host_conn = add_connected_player(game, players.hosting) join_conn = add_connected_player(game, players.joining) game.host = players.hosting await game.launch() await game.remove_game_connection(host_conn) await game.remove_game_connection(join_conn) assert game.ended
def add_connected_players(game: Game, players): """ Utility to add players with army and StartSpot indexed by a list """ for army, player in enumerate(players): add_connected_player(game, player) game.set_player_option(player.id, 'Army', army) game.set_player_option(player.id, 'StartSpot', army) game.set_player_option(player.id, 'Team', army) game.set_player_option(player.id, 'Faction', 0) game.set_player_option(player.id, 'Color', 0) game.host = players[0]
def add_connected_players(game: Game, players): """ Utility to add players with army and StartSpot indexed by a list """ for army, player in enumerate(players): add_connected_player(game, player) game.set_player_option(player.id, "Army", army) game.set_player_option(player.id, "StartSpot", army) game.set_player_option(player.id, "Team", army) game.set_player_option(player.id, "Faction", 0) game.set_player_option(player.id, "Color", 0) game.host = players[0]
async def test_game_sim_ends_when_connections_ended_sim(game: Game, players): game.state = GameState.LOBBY host_conn = add_connected_player(game, players.hosting) join_conn = add_connected_player(game, players.joining) game.host = players.hosting await game.launch() host_conn.finished_sim = True join_conn.finished_sim = True await game.check_sim_end() assert game.ended
async def test_game_launch_freezes_players(game: Game, players): game.state = GameState.LOBBY host_conn = add_connected_player(game, players.hosting) game.host = players.hosting add_connected_player(game, players.joining) await game.launch() assert game.state is GameState.LIVE assert game.players == {players.hosting, players.joining} await game.remove_game_connection(host_conn) assert game.players == {players.hosting, players.joining}
async def test_handle_action_GameState_lobby_calls_abort( game: Game, game_connection: GameConnection, event_loop, players): game_connection.send = CoroutineMock() game_connection.abort = CoroutineMock() game_connection.player = players.joining players.joining.game = game game.host = players.hosting game.host.state = PlayerState.IDLE game.map_file_path = 'maps/some_map.zip' game.map_folder_name = 'some_map' await game_connection.handle_action('GameState', ['Lobby']) await exhaust_callbacks(event_loop) game_connection.abort.assert_called_once()
async def test_handle_action_GameState_lobby_calls_ConnectToHost( game: Game, game_connection: GameConnection, event_loop, players): game_connection.send = CoroutineMock() game_connection.connect_to_host = CoroutineMock() game_connection.player = players.joining players.joining.game = game game.host = players.hosting game.map_file_path = "maps/some_map.zip" game.map_folder_name = "some_map" await game_connection.handle_action("GameState", ["Lobby"]) await exhaust_callbacks(event_loop) game_connection.connect_to_host.assert_called_with( players.hosting.game_connection)
async def test_handle_lobby_state_handles_GameError( real_game: Game, game_connection: GameConnection, event_loop, players): game_connection.abort = CoroutineMock() game_connection.connect_to_host = CoroutineMock() game_connection.player = players.joining game_connection.game = real_game players.joining.game = real_game real_game.host = players.hosting real_game.state = GameState.ENDED await game_connection.handle_action('GameState', ['Lobby']) await exhaust_callbacks(event_loop) game_connection.abort.assert_called_once()
async def test_handle_action_GameState_lobby_calls_ConnectToHost( game: Game, game_connection: GameConnection, players): game_connection.send_message = mock.MagicMock() game_connection.connect_to_host = CoroMock() game_connection.player = players.joining players.joining.game = game game.host = players.hosting game.map_file_path = 'maps/some_map.zip' game.map_folder_name = 'some_map' await game_connection.handle_action('GameState', ['Lobby']) # Give the connection coro time to run await asyncio.sleep(0.1) game_connection.connect_to_host.assert_called_with( players.hosting.game_connection)
async def test_handle_action_GameState_lobby_calls_ConnectToPeer( game: Game, game_connection: GameConnection, event_loop, players): game_connection.send = CoroutineMock() game_connection.connect_to_host = CoroutineMock() game_connection.connect_to_peer = CoroutineMock() game_connection.player = players.joining players.joining.game = game game.host = players.hosting game.map_file_path = 'maps/some_map.zip' game.map_folder_name = 'some_map' peer_conn = mock.Mock() players.peer.game_connection = peer_conn game.connections = [peer_conn] await game_connection.handle_action('GameState', ['Lobby']) await exhaust_callbacks(event_loop) game_connection.connect_to_peer.assert_called_with(peer_conn)
async def test_game_visible_to_friends(game: Game, players): game.host = players.hosting game.visibility = VisibilityState.FRIENDS players.hosting.friends.add(players.joining.id) assert game.is_visible_to_player(players.joining)
async def test_game_not_visible_to_foes(game: Game, players): game.host = players.hosting players.hosting.foes.add(players.joining.id) assert not game.is_visible_to_player(players.joining)
async def test_game_visible_to_players(game: Game, players): game.host = players.hosting game.visibility = None # Ensure that visibility is not checked game.state = GameState.LOBBY add_connected_player(game, players.joining) assert game.is_visible_to_player(players.joining)
async def test_game_visible_to_host(game: Game, players): game.host = players.hosting game.visibility = None # Ensure that visibility is not checked assert game.is_visible_to_player(players.hosting)