Пример #1
0
 def retrieve_game_port(self, host, port):
     """ Retreive and store the game's port
         :param host: the host
         :param port: the port
         :param game_id: the game id
     """
     connection = yield connect(host, port)
     self._game_port = yield connection.get_daide_port(self._game_id)
     yield connection.connection.close()
Пример #2
0
def main(case_data):
    """ Test real game environment with one game and all power controlled (no dummy powers).
        This method may be called form a non-test code to run a real game case.
        :param case_data: test data
        :type case_data: CaseData
    """
    # ================
    # Initialize test.
    # ================
    if case_data.admin_channel is None:
        LOGGER.info('Creating connection, admin channel and admin game.')
        case_data.connection = yield connect(case_data.hostname,
                                             case_data.port)
        case_data.admin_channel = yield case_data.connection.authenticate(
            'admin', 'password', create_user=False)
        # NB: For all test cases, first game state should be default game engine state when starting.
        # So, we don't need to pass game state of first expected phase when creating a server game.
        case_data.admin_game = yield case_data.admin_channel.create_game(
            map_name=case_data.map_name, rules=case_data.rules, deadline=0)
        assert case_data.admin_game.power_choice
        assert case_data.admin_game.real_time
        case_data.admin_game.data = ExpectedData(power_name='',
                                                 phases=case_data.phases,
                                                 phase_index=0)
        case_data.admin_game.add_on_game_status_update(
            on_admin_game_status_update)
        case_data.admin_game.add_on_game_processed(on_admin_game_phase_update)
        case_data.admin_game.add_on_game_phase_update(
            on_admin_game_state_update)
        case_data.admin_game.add_on_powers_controllers(
            on_admin_powers_controllers)

    # ==========
    # Test game.
    # ==========

    # Get available maps to retrieve map power names.
    available_maps = yield case_data.admin_channel.get_available_maps()
    print(
        'Map: %s, powers:' % case_data.map_name, ', '.join(
            power_name
            for power_name in sorted(available_maps[case_data.map_name])))
    # Load one game per power name.
    for power_name in available_maps[case_data.map_name]['powers']:
        case_data.future_games_ended[power_name] = Future()
        case_data.future_games_ended[power_name].add_done_callback(
            get_future_game_done_fn(power_name))
        case_data.io_loop.add_callback(get_user_game_fn(case_data, power_name))

    # Wait to let power games play.
    print('Running ...')
    yield case_data.future_games_ended
    print('All game terminated. Just wait a little ...')
    yield gen.sleep(2)
    print('End running.')
Пример #3
0
def get_client_channel():
    global _server
    global _client_channel

    client_channel = None

    if _client_channel is None:
        username = '******'
        password = '******'
        connection = yield connect(HOSTNAME, MAIN_PORT)
        _client_channel = yield connection.authenticate(
            username,
            password,
            create_user=not _server.users.has_user(username, password))

    return _client_channel
Пример #4
0
    def coroutine_func():
        """ Concrete call to main function. """
        port = random.randint(9000, 9999)

        while is_port_opened(port, HOSTNAME):
            port = random.randint(9000, 9999)

        nb_human_players = 1 if nb_daide_clients < 7 else 0

        server.start(port=port)
        server_game = ServerGame(map_name='standard',
                                 n_controls=nb_daide_clients +
                                 nb_human_players,
                                 rules=rules,
                                 server=server)

        # Register game on server.
        game_id = server_game.game_id
        server.add_new_game(server_game)
        server.start_new_daide_server(game_id)

        # Creating human player
        human_username = '******'
        human_password = '******'

        # Creating bot player to play for dummy powers
        bot_username = constants.PRIVATE_BOT_USERNAME
        bot_password = constants.PRIVATE_BOT_PASSWORD

        # Connecting
        connection = yield connect(HOSTNAME, port)
        human_channel = yield connection.authenticate(human_username,
                                                      human_password)
        bot_channel = yield connection.authenticate(bot_username, bot_password)

        # Joining human to game
        channels = {BOT_KEYWORD: bot_channel}
        if nb_human_players:
            yield human_channel.join_game(game_id=game_id,
                                          power_name='AUSTRIA')
            channels['AUSTRIA'] = human_channel

        comms_simulator = ClientsCommsSimulator(nb_daide_clients, csv_file,
                                                game_id, channels)
        yield comms_simulator.retrieve_game_port(HOSTNAME, port)

        # done_future is only used to prevent pylint E1101 errors on daide_future
        done_future = Future()
        daide_future = comms_simulator.execute()
        chain_future(daide_future, done_future)

        for _ in range(3 + nb_daide_clients):
            if done_future.done() or server_game.count_controlled_powers() >= (
                    nb_daide_clients + nb_human_players):
                break
            yield gen.sleep(2.5)
        else:
            raise TimeoutError()

        # Waiting for process to finish
        while not done_future.done() and server_game.status == strings.ACTIVE:
            yield gen.sleep(2.5)

        yield daide_future