Example #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
Example #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)
Example #3
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(AdminMessageRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        adm_message, daide_bytes = parse_bytes(String, daide_bytes)
        assert str(lead_token) == 'ADM', 'Expected ADM request'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.adm_message = str(adm_message)
Example #4
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(NameRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        client_name, daide_bytes = parse_bytes(String, daide_bytes)
        client_version, daide_bytes = parse_bytes(String, daide_bytes)
        assert str(lead_token) == 'NME', 'Expected NME request'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.client_name = str(client_name)
        self.client_version = str(client_version)
Example #5
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(HistoryRequest, self).parse_bytes(daide_bytes)

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

        # Turn
        turn, daide_bytes = parse_bytes(Turn, daide_bytes)
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.phase = str(turn)
Example #6
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)
Example #7
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(GoFlagRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        assert str(lead_token) == 'GOF', 'Expected GOF request'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)
Example #8
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(CurrentPositionRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        assert str(lead_token) == 'NOW', 'Expected NOW request'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)
Example #9
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(SupplyCentreOwnershipRequest, self).parse_bytes(daide_bytes)

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        assert str(lead_token) == 'SCO', 'Expected SCO request'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)
Example #10
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(SubmitOrdersRequest, self).parse_bytes(daide_bytes)
        orders = []

        # Parsing
        lead_token, daide_bytes = parse_bytes(SingleToken, daide_bytes)
        turn, daide_bytes = parse_bytes(Turn, daide_bytes, on_error='ignore')
        while daide_bytes:
            order, daide_bytes = parse_bytes(Order, daide_bytes)
            orders += [order]
        assert str(lead_token) == 'SUB', 'Expected SUB request'
        assert not daide_bytes, '%s bytes remaining. Request is malformed' % len(daide_bytes)

        # Setting properties
        self.phase = '' if not turn else str(turn)
        self.power_name = None if not orders or not orders[0].power_name else str(orders[0].power_name)
        self.orders = [str(order) for order in orders]
Example #11
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]
Example #12
0
    def parse_bytes(self, daide_bytes):
        """ Builds the request from DAIDE bytes """
        super(SyntaxErrorRequest, self).parse_bytes(daide_bytes)

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

        # Setting properties
        self.message_bytes = message_bytes
Example #13
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
Example #14
0
def on_accept_request(server, request, connection_handler, game):
    """ Manage YES request

        :param server: server which receives the request
        :param request: request to manage
        :param connection_handler: connection handler from which the request was sent
        :param game: the game
        :return: the list of responses
    """
    _, daide_user, token, power_name = utils.get_user_connection(
        server.users, game, connection_handler)

    response = None
    accept_response = request.response_bytes
    lead_token, _ = parse_bytes(clauses.SingleToken, accept_response)

    if bytes(lead_token) == bytes(tokens.MAP):

        # Assigning a power to the user
        if not power_name:
            uncontrolled_powers = [
                pow_name for pow_name, power in game.powers.items()
                if not power.is_eliminated() and not power.is_controlled()
            ]
            if not uncontrolled_powers:
                return [responses.OFF()]

            # 1 - Trying to respect the choice specified by the DAIDE user
            # i.e. if the Daide client is 'FRA:Dumbbot', it wants to be assigned to a power starting with 'FRA'
            if ':' in daide_user.client_name:
                prefix = daide_user.client_name.split(':')[0].upper()
                selected_powers = [
                    pow_name for pow_name in uncontrolled_powers
                    if pow_name.upper().startswith(prefix)
                ]
                if selected_powers:
                    power_name = selected_powers[0]

            # 2 - Otherwise, assigning to the first uncontrolled power
            if not power_name:
                power_name = sorted(uncontrolled_powers)[0]

            join_game_request = internal_requests.JoinGame(
                game_id=game.game_id,
                power_name=power_name,
                registration_password=None,
                token=token)
            yield internal_request_managers.handle_request(
                server, join_game_request, connection_handler)

    return [response] if response else None
Example #15
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
Example #16
0
def on_reject_request(server, request, connection_handler, game):
    """ Manage REJ request
        :param server: server which receives the request
        :param request: request to manage
        :param connection_handler: connection handler from which the request was sent
        :param game: the game
        :return: the list of responses
    """
    del server, connection_handler, game  # Unused args
    response = None
    reject_response = request.response_bytes
    lead_token, _ = parse_bytes(clauses.SingleToken, reject_response)

    if bytes(lead_token) == bytes(tokens.MAP):
        response = responses.OFF()

    return [response] if response else None
Example #17
0
def on_submit_orders_request(server, request, connection_handler, game):
    """ Manage SUB request
        :param server: server which receives the request
        :param request: request to manage
        :param connection_handler: connection handler from which the request was sent
        :param game: the game
        :return: the list of responses
    """
    _, _, token, power_name = utils.get_user_connection(
        server.users, game, connection_handler)

    if request.phase and not request.phase == game.get_current_phase():
        return [responses.REJ(bytes(request))]

    request.token = token
    request.phase = game.get_current_phase()

    power = game.get_power(power_name)
    initial_power_adjusts = power.adjust[:]
    initial_power_orders = []
    initial_game_errors = game.error[:]

    order_responses = []

    # Parsing lead token and turn
    _, request_bytes = parse_bytes(clauses.SingleToken, bytes(request))
    _, request_bytes = parse_bytes(clauses.Turn,
                                   request_bytes,
                                   on_error='ignore')

    # Validate each order individually
    while request_bytes:
        daide_order, request_bytes = parse_bytes(clauses.Order, request_bytes)
        order = str(daide_order)

        set_orders_request = internal_requests.SetOrders(
            power_name=request.power_name,
            orders=[order],
            game_id=request.game_id,
            game_role=request.power_name,
            phase=request.phase,
            token=request.token)
        yield internal_request_managers.handle_request(server,
                                                       set_orders_request,
                                                       connection_handler)

        new_power_adjusts = [
            adjust for adjust in power.adjust
            if adjust not in initial_power_adjusts
        ]
        new_power_orders = {
            id: val
            for id, val in power.orders.items()
            if id not in initial_power_orders
        }
        new_game_errors = [
            error.code for error in game.error
            if error not in initial_game_errors
        ]

        if not new_power_adjusts and not new_power_orders and not new_game_errors:
            new_game_errors.append((err.GAME_ORDER_NOT_ALLOWED % order).code)

        order_responses.append(
            responses.THX(bytes(daide_order), new_game_errors))

    # Setting orders
    set_orders_request = internal_requests.SetOrders(
        power_name=request.power_name,
        orders=request.orders,
        game_id=request.game_id,
        game_role=request.power_name,
        phase=request.phase,
        token=request.token)
    yield internal_request_managers.handle_request(server, set_orders_request,
                                                   connection_handler)

    # Returning results and missing orders
    order_responses.append(responses.MIS(game.get_current_phase(), power))
    return order_responses