Exemplo n.º 1
0
def test_sco():
    """ Tests the SCO response """
    daide_str = 'SCO ( AUS BUD TRI VIE ) ( ENG EDI LON LVP ) ( FRA BRE MAR PAR ) ' \
                '( GER BER KIE MUN ) ( ITA NAP ROM VEN ) ( RUS MOS SEV STP WAR ) ' \
                '( TUR ANK CON SMY ) ( UNO BEL BUL DEN GRE HOL NWY POR RUM SER SPA SWE TUN )'
    game = Game(map_name='standard')
    power_centers = {
        power.name: power.centers
        for power in game.powers.values()
    }
    response = responses.SCO(power_centers, map_name='standard')
    assert isinstance(response, responses.SCO), 'Expected a SCO response'
    assert bytes(response) == str_to_bytes(daide_str)
Exemplo n.º 2
0
def on_supply_centre_ownership_request(server, request, connection_handler,
                                       game):
    """ Manage SCO 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, request, connection_handler  # Unused args
    power_centers = {
        power.name: power.centers
        for power in game.powers.values()
    }
    return [responses.SCO(power_centers, game.map_name)]
Exemplo n.º 3
0
def on_history_request(server, request, connection_handler, game):
    """ Manage HST 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
    """
    history_responses = []

    _, _, _, power_name = utils.get_user_connection(server.users, game,
                                                    connection_handler)
    phase, current_phase = request.phase, game.get_current_phase()
    phase_order = game.order_history.get(phase, None)
    phase_result = game.result_history.get(phase, None)

    if phase_result is None:
        return [responses.REJ(bytes(request))]

    next_phase = game.map.phase_abbr(
        game.map.find_next_phase(game.map.phase_long(phase)))
    next_phase_state = game.state_history.get(next_phase, None)

    while next_phase_state is None and next_phase != current_phase:
        next_phase = game.map.phase_abbr(
            game.map.find_next_phase(game.map.phase_long(next_phase)))
        next_phase_state = game.state_history.get(next_phase, None)

    if next_phase == current_phase:
        next_phase_state = game.get_state()

    phase = splitter.PhaseSplitter(phase)
    next_phase = splitter.PhaseSplitter(next_phase)

    # ORD responses
    for order in phase_order[power_name]:
        order = splitter.OrderSplitter(order)

        # WAIVE
        if len(order) == 1:
            order.order_type = ' '.join([power_name, order.order_type])
            results = [OK]
        else:
            results = phase_result[order.unit]
            order.unit = ' '.join([power_name, order.unit])

        if order.supported_unit:
            order.supported_unit = ' '.join([power_name, order.supported_unit])

        order_bytes = parse_order_to_bytes(phase.phase_type, order)
        history_responses.append(
            notifications.ORD(phase.input_str, order_bytes,
                              [result.code for result in results]))

    # SCO response
    history_responses.append(
        responses.SCO(next_phase_state['centers'], game.map.name))

    # NOW response
    units = {
        power_name: [unit for unit in units if not unit.startswith('*')]
        for power_name, units in next_phase_state['units'].items()
    }
    retreats = next_phase_state['retreats'].copy()
    history_responses.append(
        responses.NOW(next_phase.input_str, units, retreats))

    return history_responses