コード例 #1
0
 def get_dummy_waiting_power_names(self, buffer_size, bot_token):
     """ Return names of dummy powers waiting for orders for current loaded games.
         This query is allowed only for bot tokens.
         :param buffer_size: maximum number of powers queried.
         :param bot_token: bot token
         :return: a dictionary mapping each game ID to a list of power names.
     """
     if self.users.get_name(bot_token) != constants.PRIVATE_BOT_USERNAME:
         raise exceptions.ResponseException('Invalid bot token %s' % bot_token)
     selected_size = 0
     selected_games = {}
     for game_id in sorted(list(self.games_with_dummy_powers.keys())):
         registered_token, registered_time = self.dispatched_dummy_powers[game_id]
         if registered_token is not None:
             time_elapsed_seconds = (common.timestamp_microseconds() - registered_time) / 1000000
             if time_elapsed_seconds > constants.PRIVATE_BOT_TIMEOUT_SECONDS or registered_token == bot_token:
                 # This game still has dummy powers but, either time allocated to previous bot token is over,
                 # or bot dedicated to this game is asking for current dummy powers of this game.
                 # Forget previous bot token.
                 registered_token = None
         if registered_token is None:
             # This game is not associated to any bot token.
             # Let current bot token handle it if buffer size is not reached.
             dummy_power_names = self.games_with_dummy_powers[game_id]
             nb_powers = len(dummy_power_names)
             if selected_size + nb_powers > buffer_size:
                 # Buffer size would be exceeded. We stop to collect games now.
                 break
             # Otherwise we collect this game.
             selected_games[game_id] = dummy_power_names
             selected_size += nb_powers
             self.dispatched_dummy_powers[game_id] = (bot_token, common.timestamp_microseconds())
     return selected_games
コード例 #2
0
def test_timestamp():
    """ Test timestamp generation. """

    timestamp1 = common.timestamp_microseconds()
    timestamp2 = common.timestamp_microseconds()
    timestamp3 = common.timestamp_microseconds()
    assert isinstance(timestamp1, int)
    assert isinstance(timestamp2, int)
    assert isinstance(timestamp3, int)
    assert timestamp1 > 1e6
    assert timestamp2 > 1e6
    assert timestamp3 > 1e6
    assert timestamp1 <= timestamp2 <= timestamp3, (timestamp1, timestamp2,
                                                    timestamp3)
コード例 #3
0
 def set_controlled(self, username):
     """ (Network Method) Control power with given username. Username may be None (meaning no controller). """
     if username is None or username == strings.DUMMY:
         if self.controller.last_value() != strings.DUMMY:
             self.controller.put(common.timestamp_microseconds(),
                                 strings.DUMMY)
             self.tokens.clear()
             self.wait = True
             self.vote = strings.NEUTRAL
     elif self.controller.last_value() == strings.DUMMY:
         self.controller.put(common.timestamp_microseconds(), username)
         self.wait = not self.game.real_time
     elif self.controller.last_value() != username:
         raise DiplomacyException(
             'Power already controlled by someone else. Kick previous controller before.'
         )
コード例 #4
0
    def attach_connection_handler(self, token, connection_handler):
        """ Associate given token with given connection handler if token is known.
            If there is a previous connection handler associated to given token, it should be
            the same as given connection handler, otherwise an error is raised
            (meaning previous connection handler was not correctly disconnected from given token.
            It should be a programming error).

            :param token: token
            :param connection_handler: connection handler
        """
        if self.has_token(token):
            previous_connection = self.get_connection_handler(token)
            if previous_connection:
                assert previous_connection == connection_handler, \
                    "A new connection handler cannot be attached to a token always connected to another handler."
            else:
                LOGGER.warning(
                    'Attaching a new connection handler to a token.')
                if connection_handler not in self.connection_handler_to_tokens:
                    self.connection_handler_to_tokens[
                        connection_handler] = set()
                self.token_to_connection_handler[token] = connection_handler
                self.connection_handler_to_tokens[connection_handler].add(
                    token)
                self.token_timestamp[token] = common.timestamp_microseconds()
コード例 #5
0
ファイル: server.py プロジェクト: satyaog/diplomacy
    def register_dummy_power_names(self, server_game):
        """ Update internal registry of dummy power names waiting for orders for given server games.

            :param server_game: server game to check
            :type server_game: ServerGame
        """
        if server_game.map.root_map != 'standard':
            # Bot does not currently support other maps.
            return
        dummy_power_names = []
        if server_game.is_game_active or server_game.is_game_paused:
            dummy_power_names = server_game.get_dummy_unordered_power_names()
            if dummy_power_names:
                # Update registry of dummy powers.
                self.games_with_dummy_powers[
                    server_game.game_id] = dummy_power_names
                # Every time we update registry of dummy powers,
                # then we also update bot time in registry of dummy powers associated to bot tokens.
                bot_token, _ = self.dispatched_dummy_powers.get(
                    server_game.game_id, (None, None))
                self.dispatched_dummy_powers[server_game.game_id] = (
                    bot_token, common.timestamp_microseconds())
        if not dummy_power_names:
            # No waiting dummy powers for this game, or game is not playable (canceled, completed, or forming).
            self.games_with_dummy_powers.pop(server_game.game_id, None)
            self.dispatched_dummy_powers.pop(server_game.game_id, None)
コード例 #6
0
 def token_is_alive(self, token):
     """ Return True if given token is known and still alive.
         A token is alive if elapsed time since last token usage does not exceed token lifetime
         (TOKEN_LIFETIME_SECONDS).
     """
     if self.has_token(token):
         current_time = common.timestamp_microseconds()
         elapsed_time_seconds = (current_time -
                                 self.token_timestamp[token]) / 1000000
         return elapsed_time_seconds <= TOKEN_LIFETIME_SECONDS
     return False
コード例 #7
0
 def connect_user(self, username, connection_handler):
     """ Connect given username to given connection handler with a new generated token, and return
         token generated.
         :param username: username to connect
         :param connection_handler: connection handler to link to user
         :return: a new token generated for connexion
     """
     token = self.create_token()
     user = self.users[username]
     if connection_handler not in self.connection_handler_to_tokens:
         self.connection_handler_to_tokens[connection_handler] = set()
     if user.username not in self.username_to_tokens:
         self.username_to_tokens[user.username] = set()
     self.token_to_username[token] = user.username
     self.token_to_connection_handler[token] = connection_handler
     self.username_to_tokens[user.username].add(token)
     self.connection_handler_to_tokens[connection_handler].add(token)
     self.token_timestamp[token] = common.timestamp_microseconds()
     return token
コード例 #8
0
    def __init__(self, game=None, name=None, **kwargs):
        """ Constructor """
        self.game = game
        self.abbrev = None
        self.adjust, self.centers, self.units, self.influence = [], [], [], []
        self.homes = None
        self.retreats = {}
        self.goner = self.civil_disorder = 0
        self.orders = {}

        self.name = ''
        self.role = ''
        self.controller = SortedDict(int, str)
        self.vote = ''
        self.order_is_set = 0
        self.wait = False
        self.tokens = set()
        super(Power, self).__init__(name=name, **kwargs)
        assert self.role in strings.ALL_ROLE_TYPES or self.role == self.name
        if not self.controller:
            self.controller.put(common.timestamp_microseconds(), strings.DUMMY)
コード例 #9
0
 def coroutine_func():
     """ Concrete call to main function. """
     yield main(case_data)
     case_data.io_loop.stop()
     print('Finished', case_data.case_name, 'at',
           common.timestamp_microseconds())
コード例 #10
0
 def relaunch_token(self, token):
     """ Update timestamp  of given token with current timestamp. """
     if self.has_token(token):
         self.token_timestamp[token] = common.timestamp_microseconds()