Ejemplo n.º 1
0
class MyJsonable(Jsonable):
    """ Example of class derived from Jsonable. """
    __slots__ = ('field_a', 'field_b', 'field_c', 'field_d', 'field_e',
                 'field_f', 'field_g')

    model = {
        'field_a':
        bool,
        'field_b':
        str,
        'field_c':
        parsing.OptionalValueType(float),
        'field_d':
        parsing.DefaultValueType(str, 'super'),
        'field_e':
        parsing.SequenceType(int),
        'field_f':
        parsing.SequenceType(float, sequence_builder=SortedSet.builder(float)),
        'field_g':
        parsing.DefaultValueType(
            parsing.DictType(str, int, SortedDict.builder(str, int)),
            {'x': -1})
    }

    def __init__(self, **kwargs):
        """ Constructor """
        self.field_a = None
        self.field_b = None
        self.field_c = None
        self.field_d = None
        self.field_e = None
        self.field_f = None
        self.field_g = {}
        super(MyJsonable, self).__init__(**kwargs)
Ejemplo n.º 2
0
class ListGames(_AbstractChannelRequest):
    """ ListGames request.
        Expected response: responses.DataGames
        Expected response handler result: responses.DataGames
    """
    __slots__ = [
        'game_id', 'status', 'map_name', 'include_protected', 'for_omniscience'
    ]
    params = {
        strings.STATUS:
        OptionalValueType(parsing.EnumerationType(strings.ALL_GAME_STATUSES)),
        strings.MAP_NAME:
        OptionalValueType(str),
        strings.INCLUDE_PROTECTED:
        parsing.DefaultValueType(bool, True),
        strings.FOR_OMNISCIENCE:
        parsing.DefaultValueType(bool, False),
        strings.GAME_ID:
        OptionalValueType(str),
    }

    def __init__(self, **kwargs):
        self.game_id = None
        self.status = None
        self.map_name = None
        self.include_protected = None
        self.for_omniscience = None
        super(ListGames, self).__init__(**kwargs)
Ejemplo n.º 3
0
class _AbstractRequest(NetworkData):
    """ Abstract request class.

        Field **request_id** is auto-filled if not defined.

        Field **name** is auto-filled with snake case version of request class name.

        Field **re_sent** is False by default. It should be set to True if request is re-sent by client
        (currently done by Connection object when reconnecting).

        **Timestamp** field is auto-set with current local timestamp if not defined.
        For game request Synchronize, timestamp should be game latest timestamp instead
        (see method NetworkGame.synchronize()).
    """
    __slots__ = ['request_id', 're_sent']
    header = {
        strings.REQUEST_ID: str,
        strings.NAME: str,
        strings.RE_SENT: parsing.DefaultValueType(bool, False),
    }
    params = {}
    id_field = strings.REQUEST_ID
    level = None

    def __init__(self, **kwargs):
        """ Constructor. """
        self.request_id = None  # type: str
        self.re_sent = None  # type: bool
        super(_AbstractRequest, self).__init__(**kwargs)

    @classmethod
    def validate_params(cls):
        """ Hack: we just use it to validate level. """
        assert cls.level is None or cls.level in strings.ALL_COMM_LEVELS
Ejemplo n.º 4
0
class ListGames(_AbstractChannelRequest):
    """ Channel request to find games.

        :param game_id: if provided, look for games with game ID either containing or contained into this game ID.
        :param status: if provided, look for games with this status.
        :param map_name: if provided, look for games with this map name.
        :param include_protected: (default True) tell if we must look into games protected by a password
        :param for_omniscience: (default False) tell if we look for games where request user can be at least omniscient.
        :type game_id: str, optional
        :type status: str, optional
        :type map_name: str, optional
        :type include_protected: bool optional
        :type for_omniscience: bool, optional
        :return:

            - Server: :class:`.DataGames`
            - Client: a list of :class:`.DataGameInfo` objects, each containing
              a bunch of information about a game found. If no game found, list will be empty.
    """
    __slots__ = [
        'game_id', 'status', 'map_name', 'include_protected', 'for_omniscience'
    ]
    params = {
        strings.STATUS:
        OptionalValueType(parsing.EnumerationType(strings.ALL_GAME_STATUSES)),
        strings.MAP_NAME:
        OptionalValueType(str),
        strings.INCLUDE_PROTECTED:
        parsing.DefaultValueType(bool, True),
        strings.FOR_OMNISCIENCE:
        parsing.DefaultValueType(bool, False),
        strings.GAME_ID:
        OptionalValueType(str),
    }

    def __init__(self, **kwargs):
        self.game_id = None
        self.status = None
        self.map_name = None
        self.include_protected = None
        self.for_omniscience = None
        super(ListGames, self).__init__(**kwargs)
Ejemplo n.º 5
0
class CreateGame(_AbstractChannelRequest):
    """ CreateGame request.
        Expected response: responses.DataGame
        Expected response handler result: diplomacy.client.network_game.NetworkGame
    """
    __slots__ = [
        'game_id', 'power_name', 'state', 'map_name', 'rules', 'n_controls',
        'deadline', 'registration_password'
    ]
    params = {
        strings.GAME_ID:
        parsing.OptionalValueType(str),
        strings.N_CONTROLS:
        parsing.OptionalValueType(int),
        strings.DEADLINE:
        parsing.DefaultValueType(int, 300),  # 300 seconds. Must be >= 0.
        strings.REGISTRATION_PASSWORD:
        parsing.OptionalValueType(str),
        strings.POWER_NAME:
        parsing.OptionalValueType(str),
        strings.STATE:
        parsing.OptionalValueType(dict),
        strings.MAP_NAME:
        parsing.DefaultValueType(str, 'standard'),
        strings.RULES:
        parsing.OptionalValueType(
            parsing.SequenceType(str, sequence_builder=set)),
    }

    def __init__(self, **kwargs):
        self.game_id = ''
        self.n_controls = 0
        self.deadline = 0
        self.registration_password = ''
        self.power_name = ''
        self.state = {}
        self.map_name = ''
        self.rules = set()
        super(CreateGame, self).__init__(**kwargs)
Ejemplo n.º 6
0
class CreateGame(_AbstractChannelRequest):
    """ Channel request to create a game.

        :param game_id: game ID. If not provided, a game ID will be generated.
        :param n_controls: number of controlled powers required to start the game.
            A power becomes controlled when a player joins the game to control this power.
            Game won't start as long it does not have this number of controlled powers.
            Game will stop (to ``forming`` state) if the number of controlled powers decrease under
            this number (e.g. when powers are kicked, eliminated, or when a player controlling a power
            leaves the game). If not provided, set with the number of powers on the map (e.g. ``7``
            on standard map).
        :param deadline: (default ``300``) time (in seconds) for the game to wait before
            processing a phase. ``0`` means no deadline, ie. game won't process a phase until either
            all powers submit orders and turn off wait flag, or a game master forces game to process.
        :param registration_password: password required to join the game.
            If not provided, anyone can join the game.
        :param power_name: power to control once game is created.

            - If provided, the user who send this request will be joined to the game as a player
              controlling this power.
            - If not provided, the user who send this request will be joined to the game as an
              omniscient observer (ie. able to see everything in the game, including user messages).
              Plus, as game creator, user will also be a game master, ie. able to send master requests,
              e.g. to force game processing.

        :param state: game initial state (for expert users).
        :param map_name: (default ``'standard'``) map to play on.
            You can retrieve maps available on server by sending request :class:`GetAvailableMaps`.
        :param rules: list of strings - game rules (for expert users).
        :type game_id: str, optional
        :type n_controls: int, optional
        :type deadline: int, optional
        :type registration_password: str, optional
        :type power_name: str, optional
        :type state: dict, optional
        :type map_name: str, optional
        :type rules: list, optional
        :return:

            - Server: :class:`.DataGame`
            - Client: a :class:`.NetworkGame` object representing a client version of the
              game created and joined. Either a power game (if power name given) or an omniscient game.
    """
    __slots__ = [
        'game_id', 'power_name', 'state', 'map_name', 'rules', 'n_controls',
        'deadline', 'registration_password'
    ]
    params = {
        strings.GAME_ID:
        parsing.OptionalValueType(str),
        strings.N_CONTROLS:
        parsing.OptionalValueType(int),
        strings.DEADLINE:
        parsing.DefaultValueType(int, 300),  # 300 seconds. Must be >= 0.
        strings.REGISTRATION_PASSWORD:
        parsing.OptionalValueType(str),
        strings.POWER_NAME:
        parsing.OptionalValueType(str),
        strings.STATE:
        parsing.OptionalValueType(dict),
        strings.MAP_NAME:
        parsing.DefaultValueType(str, 'standard'),
        strings.RULES:
        parsing.OptionalValueType(
            parsing.SequenceType(str, sequence_builder=set)),
    }

    def __init__(self, **kwargs):
        self.game_id = ''
        self.n_controls = 0
        self.deadline = 0
        self.registration_password = ''
        self.power_name = ''
        self.state = {}
        self.map_name = ''
        self.rules = set()
        super(CreateGame, self).__init__(**kwargs)