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_abort(game_connection: GameConnection, game: Game, players):
    game_connection.player = players.hosting
    game_connection.game = game

    await game_connection.abort()

    game.remove_game_connection.assert_called_with(game_connection)
async def test_handle_action_GameState_launching_calls_launch(game_connection: GameConnection, players, game):
    game_connection.player = players.hosting
    game_connection.game = game
    game.launch = CoroMock()

    await game_connection.handle_action('GameState', ['Launching'])

    game.launch.assert_any_call()
async def test_handle_action_GameState_idle_non_searching_player_aborts(game_connection: GameConnection, players):
    game_connection.player = players.hosting
    game_connection.lobby = mock.Mock()
    game_connection.abort = mock.Mock()
    players.hosting.state = PlayerState.IDLE

    await game_connection.handle_action('GameState', ['Idle'])

    game_connection.abort.assert_any_call()
async def test_handle_action_GameState_launching_calls_launch(
        game: Game, game_connection: GameConnection, players):
    game_connection.player = players.hosting
    game_connection.game = game
    game.launch = CoroutineMock()

    await game_connection.handle_action("GameState", ["Launching"])

    game.launch.assert_any_call()
async def test_handle_action_GameState_idle_adds_connection(
        game: Game, game_connection: GameConnection, players):
    players.joining.game = game
    game_connection.player = players.hosting
    game_connection.game = game

    await game_connection.handle_action('GameState', ['Idle'])

    game.add_game_connection.assert_called_with(game_connection)
Exemple #7
0
async def test_handle_action_GameState_launching_calls_launch(
        game_connection: GameConnection, players, game):
    game_connection.player = players.hosting
    game_connection.game = game
    game.launch = CoroMock()

    await game_connection.handle_action('GameState', ['Launching'])

    game.launch.assert_any_call()
Exemple #8
0
async def test_handle_action_GameState_idle_non_searching_player_aborts(
        game_connection: GameConnection, players):
    game_connection.player = players.hosting
    game_connection.lobby = mock.Mock()
    game_connection.abort = mock.Mock()
    players.hosting.state = PlayerState.IDLE

    await game_connection.handle_action('GameState', ['Idle'])

    game_connection.abort.assert_any_call()
Exemple #9
0
 def make_connection(player, connectivity):
     lc = LobbyConnection(loop)
     lc.protocol = mock.Mock()
     conn = GameConnection(loop=loop,
                           lobby_connection=lc,
                           player_service=player_service,
                           games=game_service)
     conn.player = player
     conn.game = game
     conn._transport = transport
     conn._connectivity_state.set_result(connectivity)
     return conn
Exemple #10
0
 def make_connection(player, connectivity):
     lc = LobbyConnection(loop)
     lc.protocol = mock.Mock()
     conn = GameConnection(loop=loop,
                           lobby_connection=lc,
                           player_service=player_service,
                           games=game_service)
     conn.player = player
     conn.game = game
     conn._transport = transport
     conn._connectivity_state.set_result(connectivity)
     return conn
async def test_handle_action_GameState_lobby_calls_ConnectToHost(game_connection: GameConnection, players, game):
    game_connection.send_message = mock.MagicMock()
    game_connection.ConnectToHost = CoroMock()
    game_connection.player = players.joining
    players.joining.game = game
    game.host = players.hosting
    game.map_file_path = 'some_map'

    await game_connection.handle_action('GameState', ['Lobby'])
    # Give the connection coro time to run
    await asyncio.sleep(0.1)

    game_connection.ConnectToHost.assert_called_with(players.hosting.game_connection)
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_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()
Exemple #14
0
async def test_handle_action_GameState_lobby_calls_ConnectToHost(
        game_connection: GameConnection, players, game):
    game_connection.send_message = mock.MagicMock()
    game_connection.ConnectToHost = CoroMock()
    game_connection.player = players.joining
    players.joining.game = game
    game.host = players.hosting
    game.map_file_path = 'some_map'

    await game_connection.handle_action('GameState', ['Lobby'])
    # Give the connection coro time to run
    await asyncio.sleep(0.1)

    game_connection.ConnectToHost.assert_called_with(
        players.hosting.game_connection)
async def test_handle_action_GameResult_calls_add_result(
        game: Game, game_connection: GameConnection):
    game_connection.connect_to_host = CoroutineMock()

    await game_connection.handle_action('GameResult', [0, 'score -5'])
    game.add_result.assert_called_once_with(game_connection.player.id, 0,
                                            'score', -5)
Exemple #16
0
    def launch_game(self, game, port, is_host=False, use_map=None):
        # FIXME: Setting up a ridiculous amount of cyclic pointers here
        if self.game_connection:
            self.game_connection.abort("Player launched a new game")
        self.game_connection = GameConnection(self.loop, self,
                                              self.player_service,
                                              self.game_service)
        self.game_connection.player = self.player
        self.player.game_connection = self.game_connection
        self.game_connection.game = game
        if is_host:
            game.host = self.player

        self.player.state = PlayerState.HOSTING if is_host else PlayerState.JOINING
        self.player.game = game
        self.player.game_port = port
        cmd = {
            "command": "game_launch",
            "mod": game.game_mode,
            "uid": game.id,
            "args": ["/numgames " + str(self.player.numGames)]
        }
        if use_map:
            cmd['mapname'] = use_map
        self.sendJSON(cmd)
async def test_handle_action_OperationComplete_invalid(
        ugame: Game, game_connection: GameConnection, database):
    """
        Sends an OperationComplete action to handle action and verifies that
    the `coop_leaderboard` table is updated accordingly.

    Requires that the map from `game.map_file_path` exists in the database.
    """

    ugame.map_file_path = "maps/prothyon16.v0005.zip"
    ugame.validity = ValidityState.OTHER_UNRANK
    game_connection.game = ugame

    secondary = 1
    time_taken = '09:08:07.654321'
    await game_connection.handle_action('OperationComplete',
                                        ['1', secondary, time_taken])

    async with database.acquire() as conn:
        result = await conn.execute(
            "SELECT secondary, gameuid from `coop_leaderboard` where gameuid=%s",
            ugame.id)

        row = await result.fetchone()

    assert row is None
async def test_handle_action_GameMods_not_host(game: Game,
                                               game_connection: GameConnection,
                                               players):
    game_connection.player = players.joining
    mods = game.mods
    await game_connection.handle_action('GameMods', ['uids', 'foo baz'])
    assert game.mods == mods
async def test_handle_action_invalid(game_connection: GameConnection):
    game_connection.abort = CoroutineMock()

    await game_connection.handle_action('ThisDoesntExist', [1, 2, 3])

    game_connection.abort.assert_not_called()
    game_connection.protocol.send_message.assert_not_called()
Exemple #20
0
async def test_handle_action_GameResult_draw_ends_sim(
        game: Game, game_connection: GameConnection):
    game_connection.connect_to_host = CoroMock()
    await game_connection.handle_action('GameResult', [0, 'draw'])

    assert game_connection.finished_sim
    assert game.check_sim_end.called
async def test_handle_action_TeamkillHappened_AI(
        game: Game, game_connection: GameConnection, database):
    # Should fail with a sql constraint error if this isn't handled correctly
    game_connection.abort = CoroutineMock()
    await game_connection.handle_action("TeamkillHappened",
                                        ["200", 0, "Dostya", "0", "Rhiza"])
    game_connection.abort.assert_not_called()
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_TeamkillHappened_AI(
        game: Game, game_connection: GameConnection, database):
    # Should fail with a sql constraint error if this isn't handled correctly
    game_connection.abort = CoroutineMock()
    await game_connection.handle_action('TeamkillHappened',
                                        ['200', 0, 'Dostya', '0', 'Rhiza'])
    game_connection.abort.assert_not_called()
Exemple #24
0
def game_connection(request, game, loop, player_service, players, game_service, transport):
    from server import GameConnection, LobbyConnection
    conn = GameConnection(loop=loop,
                          lobby_connection=mock.create_autospec(LobbyConnection(loop)),
                          player_service=player_service,
                          games=game_service)
    conn._transport = transport
    conn.player = players.hosting
    conn.game = game
    conn.lobby = mock.Mock(spec=LobbyConnection)

    def fin():
        conn.abort()

    request.addfinalizer(fin)
    return conn
async def test_handle_action_GameState_lobby_sends_HostGame(
        game: Game, game_connection: GameConnection, event_loop, players):
    game_connection.player = 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)

    assert_message_sent(game_connection, "HostGame", [game.map_folder_name])
async def test_handle_action_GameState_lobby_sends_HostGame(
        game: Game, game_connection: GameConnection, event_loop, players):
    game_connection.player = 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)

    assert_message_sent(game_connection, 'HostGame', [game.map_folder_name])
async def test_handle_action_GameState_lobby_sends_HostGame(game_connection: GameConnection, loop, players, game):
    game_connection.player = 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)

    assert_message_sent(game_connection, 'HostGame', [game.map_folder_name])
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)
Exemple #29
0
async def test_handle_action_GameState_lobby_calls_ConnectToPeer(
        game: Game, game_connection: GameConnection, players):
    game_connection.send_message = mock.MagicMock()
    game_connection.connect_to_host = CoroMock()
    game_connection.connect_to_peer = 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'
    game.connections = [players.peer.game_connection]

    await game_connection.handle_action('GameState', ['Lobby'])
    # Give the connection coro time to run
    await asyncio.sleep(0.1)

    game_connection.connect_to_peer.assert_called_with(
        players.peer.game_connection)
Exemple #30
0
async def test_handle_action_GameState_lobby_sends_HostGame(
        game_connection: GameConnection, loop, players, game):
    game_connection.player = 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)

    assert_message_sent(game_connection, 'HostGame', [game.map_folder_name])
Exemple #31
0
def game_connection(request, game, loop, player_service, players, game_service,
                    transport):
    from server import GameConnection, LobbyConnection
    conn = GameConnection(loop=loop,
                          lobby_connection=mock.create_autospec(
                              LobbyConnection(loop)),
                          player_service=player_service,
                          games=game_service)
    conn._transport = transport
    conn.player = players.hosting
    conn.game = game
    conn.lobby = mock.Mock(spec=LobbyConnection)

    def fin():
        conn.abort()

    request.addfinalizer(fin)
    return conn
async def test_handle_action_ClearSlot_not_host(
        game: Game, game_connection: GameConnection, players):
    game_connection.player = players.joining
    await game_connection.handle_action('ClearSlot', [1])
    game.clear_slot.assert_not_called()
async def test_handle_action_GameState_ended_calls_on_connection_lost(
        game_connection: GameConnection):
    game_connection.on_connection_lost = CoroutineMock()
    await game_connection.handle_action('GameState', ['Ended'])
    game_connection.on_connection_lost.assert_called_once_with()
async def test_handle_action_PlayerOption_not_host(
        game: Game, game_connection: GameConnection, players):
    game_connection.player = players.joining
    await game_connection.handle_action('PlayerOption', [1, 'Color', 2])
    game.set_player_option.assert_not_called()
async def test_handle_action_AIOption_not_host(game: Game,
                                               game_connection: GameConnection,
                                               players):
    game_connection.player = players.joining
    await game_connection.handle_action('AIOption', ['QAI', 'StartSpot', 1])
    game.set_ai_option.assert_not_called()
async def test_handle_action_GameOption_not_host(
        game: Game, game_connection: GameConnection, players):
    game_connection.player = players.joining
    game.gameOptions = {"Victory": "asdf"}
    await game_connection.handle_action('GameOption', ['Victory', 'sandbox'])
    assert game.gameOptions == {"Victory": "asdf"}