def check_choicescarf(battle, msg_lines):
    def get_move_information(m):
        try:
            return m.split('|')[2], all_move_json[normalize_name(
                m.split('|')[3])]
        except KeyError:
            logger.debug(
                "Unknown move {} - using standard 0 priority move".format(
                    normalize_name(m.split('|')[3])))
            return m.split('|')[2], {
                constants.ID: "unknown",
                constants.PRIORITY: 0
            }

    moves = [
        get_move_information(m) for m in msg_lines if m.startswith('|move|')
    ]
    if len(moves) != 2 or moves[0][0].startswith(
            battle.user.name
    ) or moves[0][1][constants.PRIORITY] != moves[1][1][constants.PRIORITY]:
        return

    if (battle.opponent.active is None
            or battle.opponent.active.item != constants.UNKNOWN_ITEM
            or can_have_speed_modified(battle, battle.opponent.active)
            or can_have_priority_modified(battle, battle.opponent.active,
                                          moves[0][1][constants.ID])):
        return

    battle_copy = deepcopy(battle)
    battle_copy.user.from_json(battle_copy.request_json)
    if battle.battle_type == constants.RANDOM_BATTLE:
        battle_copy.opponent.active.set_spread(
            'serious',
            '85,85,85,85,85,85')  # random battles have known spreads
    else:
        if battle.trick_room:
            battle_copy.opponent.active.set_spread(
                'quiet',
                '0,0,0,0,0,0')  # assume as slow as possible in trickroom
        else:
            battle_copy.opponent.active.set_spread(
                'jolly', '0,0,0,0,0,252')  # assume as fast as possible
    state = battle_copy.create_state()
    opponent_effective_speed = get_effective_speed(state, state.opponent)
    bot_effective_speed = get_effective_speed(state, state.self)

    if battle.trick_room:
        has_scarf = opponent_effective_speed > bot_effective_speed
    else:
        has_scarf = bot_effective_speed > opponent_effective_speed

    if has_scarf:
        logger.debug(
            "Opponent {} could not have gone first - setting it's item to choicescarf"
            .format(battle.opponent.active.name))
        battle.opponent.active.item = 'choicescarf'
Exemple #2
0
def check_choicescarf(battle, msg_lines):
    def get_move_information(m):
        try:
            return m.split('|')[2], all_move_json[normalize_name(
                m.split('|')[3])]
        except KeyError:
            logger.debug(
                "Unknown move {} - using standard 0 priority move".format(
                    normalize_name(m.split('|')[3])))
            return m.split('|')[2], {constants.PRIORITY: 0}

    if battle.opponent.active is None or battle.opponent.active.item != constants.UNKNOWN_ITEM:
        return

    moves = [
        get_move_information(m) for m in msg_lines if m.startswith('|move|')
    ]
    if len(moves) != 2 or moves[0][0].startswith(
            battle.user.name
    ) or moves[0][1][constants.PRIORITY] != moves[1][1][constants.PRIORITY]:
        return

    battle_copy = deepcopy(battle)
    if battle.trick_room:
        battle_copy.opponent.active.set_spread(
            'quiet', '0,0,0,0,0,0')  # assume as slow as possible in trickroom
    else:
        battle_copy.opponent.active.set_spread(
            'jolly', '0,0,0,0,0,252')  # assume as fast as possible
    state = battle_copy.to_object()
    opponent_effective_speed = get_effective_speed(state, state.opponent)
    bot_effective_speed = get_effective_speed(state, state.self)

    if battle.trick_room:
        has_scarf = opponent_effective_speed > bot_effective_speed
    else:
        has_scarf = bot_effective_speed > opponent_effective_speed

    if has_scarf:
        logger.debug(
            "Opponent {} could not have gone first - setting it's item to choicescarf"
            .format(battle.opponent.active.name))
        battle.opponent.active.item = 'choicescarf'