<commit_before>import math
import random

from twisted.internet.defer import inlineCallbacks, returnValue
from battlesnake.outbound_commands.think_fn_wrappers import get_map_dimensions
from battlesnake.plugins.contrib.ai.outbound_commands import start_unit_ai
from battlesnake.plugins.contrib.factions.api import get_faction
from battlesnake.plugins.contrib.factions.defines import ATTACKER_FACTION_DBREF

from battlesnake.plugins.contrib.pg_db.api import get_db_connection

# All of our max wave BV2 values are based off of this starter value. This
# assumes one player on wave one with a 1.0 difficulty mod (normal difficulty).
# From here, we multiply based on all of those previously mentioned variables.
from battlesnake.plugins.contrib.unit_spawning.outbound_commands import \
    create_unit


@inlineCallbacks
def pick_refs_for_wave(wave_num, opposing_bv2, difficulty_modifier):
    """
    :param int wave_num: A wave number. Starting at 1.
    :param int opposing_bv2: The total BV2 for the opposing force.
    :param float difficulty_modifier: A difficulty modifier that reduces
        the BV. Values 0...1
    :rtype: list
    :returns: A list of unit ref strings to spawn for this wave.
    """

    print "OPPOSING BV2", opposing_bv2
    first_wave_bv2 = opposing_bv2 * difficulty_modifier
    # Successive waves are slightly harder than the first wave.
    max_wave_bv2 = first_wave_bv2 + (first_wave_bv2 * math.log(wave_num))
    print "MAX WAVE BV2", max_wave_bv2

    conn = yield get_db_connection()
    results = yield conn.runQuery(
        'SELECT reference, battle_value2 FROM unit_library_unit '
        '  WHERE is_ai_spawnable=True AND battle_value2<=%s ORDER BY random()',
        (max_wave_bv2,)
    )

    wave_bv = 0
    refs = []
    for row in results:
        if wave_bv > max_wave_bv2:
            break<commit_msg>Prevent BV2 underruns with Easy difficulty and super-low BV2 units.<commit_after>import math
import random

from twisted.internet.defer import inlineCallbacks, returnValue
from battlesnake.outbound_commands.think_fn_wrappers import get_map_dimensions
from battlesnake.plugins.contrib.ai.outbound_commands import start_unit_ai
from battlesnake.plugins.contrib.factions.api import get_faction
from battlesnake.plugins.contrib.factions.defines import ATTACKER_FACTION_DBREF

from battlesnake.plugins.contrib.pg_db.api import get_db_connection

# All of our max wave BV2 values are based off of this starter value. This
# assumes one player on wave one with a 1.0 difficulty mod (normal difficulty).
# From here, we multiply based on all of those previously mentioned variables.
from battlesnake.plugins.contrib.unit_spawning.outbound_commands import \
    create_unit


@inlineCallbacks
def find_lowest_bv2_ai_unit():
    """
    Finds the lowest possible BV2 unit that the AI can use.

    :rtype: int
    :returns: The lowest BV2 value unit.
    """

    conn = yield get_db_connection()
    results = yield conn.runQuery(
        'SELECT battle_value2 FROM unit_library_unit '
        '  WHERE is_ai_spawnable=True ORDER BY battle_value2 ASC LIMIT 1',
    )
    for row in results:
        returnValue(row['battle_value2'])


@inlineCallbacks
def pick_refs_for_wave(wave_num, opposing_bv2, difficulty_modifier):
    """
    :param int wave_num: A wave number. Starting at 1.
    :param int opposing_bv2: The total BV2 for the opposing force.
    :param float difficulty_modifier: A difficulty modifier that reduces
        the BV. Values 0...1
    :rtype: list
    :returns: A list of unit ref strings to spawn for this wave.
    """

    print "Total Defender BV2", opposing_bv2
    min_wave_bv2 = yield find_lowest_bv2_ai_unit()
    # Cap the lowest possible BV2 to make sure the wave is always spawnable.
    first_wave_bv2 = max(min_wave_bv2, opposing_bv2 * difficulty_modifier)
    # Successive waves are slightly harder than the first wave.
    max_wave_bv2 = first_wave_bv2 + (first_wave_bv2 * math.log(wave_num))
    print "Max Attacker BV2", max_wave_bv2

    conn = yield get_db_connection()
    results = yield conn.runQuery(
        'SELECT reference, battle_value2 FROM unit_library_unit '
        '  WHERE is_ai_spawnable=True AND battle_value2<=%s ORDER BY random()',
        (max_wave_bv2,)
    )

    wave_bv = 0
    refs = []
    for row in results:
        if wave_bv > max_wave_bv2:
            break
{'fraction_non_alphanumeric': 0.06361556064073226, 'fraction_numerical': 0.013501144164759725, 'mean_word_length': 3.7980241492864986, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6964500', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1313, 'n_words': 427}