示例#1
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(SendMessageRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        assert str(lead_token) == 'SND', 'Expected SND request'

        # Turn
        turn, daide_bytes = parse_bytes(Turn, daide_bytes, on_error='ignore')

        # Powers
        powers = []
        powers_group_bytes, daide_bytes = break_next_group(daide_bytes)
        powers_group_bytes = strip_parentheses(powers_group_bytes)
        while powers_group_bytes:
            power, powers_group_bytes = parse_bytes(Power, powers_group_bytes)
            powers += [power]
        assert powers, 'Expected a group of `power`. Request is malformed'

        # Press message or reply
        message_group_bytes, daide_bytes = break_next_group(daide_bytes)
        message_group_bytes = strip_parentheses(message_group_bytes)
        assert message_group_bytes, 'Expected a `press_message` or a `reply`. Request is malformed'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.phase = '' if not turn else str(turn)
        self.powers = [str(power) for power in powers]
        self.message_bytes = message_group_bytes
示例#2
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(IAmRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        assert str(lead_token) == 'IAM', 'Expected IAM request'

        # Power
        power_group_bytes, daide_bytes = break_next_group(daide_bytes)
        power_group_bytes = strip_parentheses(power_group_bytes)
        power, power_group_bytes = parse_bytes(Power, power_group_bytes)
        assert not power_group_bytes, '%s bytes remaining in power group. Request is malformed' % len(
            power_group_bytes)

        # Passcode
        passcode_group_bytes, daide_bytes = break_next_group(daide_bytes)
        passcode_group_bytes = strip_parentheses(passcode_group_bytes)
        passcode, passcode_group_bytes = parse_bytes(SingleToken,
                                                     passcode_group_bytes)
        assert not passcode_group_bytes, '%s bytes remaining in passcode group. Req. error' % len(
            passcode_group_bytes)
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(
            daide_bytes)

        # Setting properties
        self.power_name = str(power)
        self.passcode = str(passcode)
示例#3
0
    def _build_retreat_phase(self, power):
        """ Builds the missing orders response for a retreat phase """
        units_bytes_buffer = []

        units_with_no_order = {
            unit: retreat_provinces
            for unit, retreat_provinces in power.retreats.items()
        }

        # Removing units for which we have orders
        for key, value in power.orders.items():
            unit = key  # Regular game {e.g. 'A PAR': '- BUR')
            if key[0] in 'RIO':  # No-check game (key is INVALID, ORDER x, REORDER x)
                unit = ' '.join(value.split()[:2])
            if unit in units_with_no_order:
                del units_with_no_order[unit]

        # Sorting by the unit's province ASC so results are deterministic
        for unit, retreat_provinces in sorted(
                units_with_no_order.items(),
                key=lambda key_val: key_val[0].split()[-1]):
            unit_clause = parse_string(Unit, '%s %s' % (power.name, unit))
            retreat_clauses = [
                parse_string(Province, province)
                for province in retreat_provinces
            ]
            units_bytes_buffer += [
                add_parentheses(
                    strip_parentheses(bytes(unit_clause)) + bytes(tokens.MRT) +
                    add_parentheses(b''.join(
                        [bytes(province) for province in retreat_clauses])))
            ]

        self._bytes = bytes(tokens.MIS) + b''.join(units_bytes_buffer)
示例#4
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(ParenthesisErrorRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        message_bytes, daide_bytes = break_next_group(daide_bytes)
        if message_bytes:
            message_bytes = strip_parentheses(message_bytes)
        else:
            message_bytes = strip_parentheses(daide_bytes)
            daide_bytes = b''
        assert str(lead_token) == 'PRN', 'Expected PRN request'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.message_bytes = message_bytes
示例#5
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(RejectRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        response_bytes, daide_bytes = break_next_group(daide_bytes)
        response_bytes = strip_parentheses(response_bytes)
        assert str(lead_token) == 'REJ', 'Expected REJ request'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.response_bytes = response_bytes
示例#6
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(NotRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        request_group_bytes, daide_bytes = break_next_group(daide_bytes)
        assert str(lead_token) == 'NOT', 'Expected NOT request'

        # Request
        request_group_bytes = strip_parentheses(request_group_bytes)
        request = RequestBuilder.from_bytes(request_group_bytes)
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.request = request
示例#7
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(TimeToDeadlineRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        seconds_group_bytes, daide_bytes = break_next_group(daide_bytes)
        assert str(lead_token) == 'TME', 'Expected TME request'

        # Seconds
        if seconds_group_bytes:
            seconds_group_bytes = strip_parentheses(seconds_group_bytes)
            seconds, daide_bytes = parse_bytes(Number, seconds_group_bytes)
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.seconds = None if not seconds_group_bytes else int(seconds)
示例#8
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(DrawRequest, self).parse_bytes(daide_bytes)
        powers = []

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        assert str(lead_token) == 'DRW', 'Expected DRW request'

        # Powers
        powers_group_bytes, daide_bytes = break_next_group(daide_bytes)
        if powers_group_bytes:
            powers_group_bytes = strip_parentheses(powers_group_bytes)
            while powers_group_bytes:
                power, powers_group_bytes = parse_bytes(Power, powers_group_bytes)
                powers += [power]

        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.powers = [str(power) for power in powers]
示例#9
0
    def __init__(self, phase_name, powers_units, powers_retreats, **kwargs):
        """ Builds the response

            :param phase_name: The name of the current phase (e.g. 'S1901M')
            :param powers: A list of `diplomacy.engine.power.Power` objects
        """
        super(CurrentPositionResponse, self).__init__(**kwargs)
        units_bytes_buffer = []

        # Turn
        turn_clause = parse_string(Turn, phase_name)

        # Units
        for power_name, units in sorted(powers_units.items()):
            # Regular units
            for unit in units:
                unit_clause = parse_string(Unit, '%s %s' % (power_name, unit))
                units_bytes_buffer += [bytes(unit_clause)]

            # Dislodged units
            for unit, retreat_provinces in sorted(
                    powers_retreats[power_name].items()):
                unit_clause = parse_string(Unit, '%s %s' % (power_name, unit))
                retreat_clauses = [
                    parse_string(Province, province)
                    for province in retreat_provinces
                ]
                units_bytes_buffer += [
                    add_parentheses(
                        strip_parentheses(bytes(unit_clause)) +
                        bytes(tokens.MRT) + add_parentheses(b''.join(
                            [bytes(province)
                             for province in retreat_clauses])))
                ]

        # Storing full response
        self._bytes = bytes(
            tokens.NOW) + bytes(turn_clause) + b''.join(units_bytes_buffer)