Example #1
0
def pull_generic_tag(tag_name, data, weight, archetype_tags,
                     archetype_weight_dict):
    """
    Adjusts the inputted weighting dictionary based on the occurrences of archetype tags in the inputted data set.
    :param tag_name: the singular name of the type of item being pulled, such as Spell
    :type tag_name: str
    :param data: the names of all data pieces to pull tags from, with their relevant data values, in a 2D array
    :type data: list
    :param weight: the weighting to be applied to recorded occurrences of the tags
    :type weight: int
    :param archetype_tags: the tags that apply to the archetype(s) selected in creating the chromosome
    :type archetype_tags: set
    :param archetype_weight_dict: a dictionary, linking an archetype tag to the (weighted) occurences of it so far
    :type archetype_weight_dict: dict
    :return: the updated archetype weight dictionary
    """
    for dataPiece, dataValue in data:
        tags = DataExtractor.get_names_from_connector(tag_name,
                                                      "GenericTag",
                                                      input_name=dataPiece)
        tagAmnt = Counter(tags)
        for match in set(tags).intersection(archetype_tags):
            # calculates the weighted value of the match, based on it's amount of occurrences, and adds this
            # to the archetype weighting dictionaries current value
            weightedWorth = (tagAmnt[match] * (weight + dataValue))
            archetype_weight_dict.update(
                {match: (archetype_weight_dict[match] + weightedWorth)})
    return archetype_weight_dict
Example #2
0
def create_race(race_name, chr_lvl, subrace_id=-1, is_subrace=False):
    """
    Creates and returns a race object, given the name and level of the race to use.
    :param race_name: the name of the race to create
    :type race_name: str
    :param chr_lvl: the level of the character being created
    :type chr_lvl: int
    :param subrace_id: the id of the subrace being built
    :type subrace_id: int, optional
    :param is_subrace: whether the current pass is creating the subrace
    :type is_subrace: bool, optional
    :return: a race object of the inputted race
    """
    # recursively builds a subrace from the data
    if subrace_id > -1 and is_subrace is False:
        subrace = create_race(race_name, chr_lvl, subrace_id, True)
        subrace_id = -1
    else:
        subrace = None

    # get basic variable data
    if subrace_id == -1:
        subraceStr = " IS NULL"
    else:
        subraceStr = "=" + str(subrace_id)

    raceId = Db.get_id(race_name, "Race")
    # get basic racial data
    Db.cursor.execute("SELECT size FROM Race WHERE raceId=" + str(raceId))
    size = Db.cursor.fetchone()[0]
    if subrace_id == -1:
        Db.cursor.execute("SELECT speed, darkvision, resistance FROM Race WHERE raceId=" + str(raceId))
    else:
        Db.cursor.execute("SELECT speed, darkvision, resistance FROM Subrace WHERE raceId="
                          + str(raceId) + " AND subraceId" + subraceStr)
    speed, darkvision, resistance = Db.cursor.fetchone()


    # gets the trait data
    traits = []
    traitNames = DataExtractor.get_names_from_connector("Race", "Trait", raceId)
    for trait in traitNames:
        Db.cursor.execute("SELECT subraceId FROM RaceTrait WHERE traitId=" + str(Db.get_id(trait, "Trait")))
        subId = Db.cursor.fetchone()[0]
        if (subId is None and subrace_id == -1) or (subId == subrace_id):
            Db.cursor.execute("SELECT traitDescription FROM Trait WHERE traitName='" + trait.replace("'", "''") + "'")
            traits.append((trait, Db.cursor.fetchone()[0]))
    traits = choose_trait_option(traits, race_name)

    # extracts data from options
    spells, modUsed, proficiencies, languages = collect_race_option_data(race_name, chr_lvl, subrace_id)

    # gets the ability score data
    Db.cursor.execute("SELECT abilityScore, scoreIncrease FROM RaceAbilityScore WHERE raceId="
                      + str(raceId) + " AND subraceId" + subraceStr)
    abilityScores = dict()
    for scoreName, scoreIncrease in Db.cursor.fetchall():
        abilityScores.update({scoreName: scoreIncrease})

    # converts data into the required formats
    darkvision = darkvision == 1
    if len(spells) == 0:
        spells = None
        modUsed = None

    if is_subrace:
        Db.cursor.execute("SELECT subraceName FROM Subrace WHERE subraceId=" + str(subrace_id))
        race_name = Db.cursor.fetchone()[0]

    return Race.Race(race_name, languages, proficiencies, abilityScores, traits, speed, size, darkvision,
                     spells, modUsed, resistance, subrace)
Example #3
0
    def pull_generic_tags(self):
        """
        Pulls all information linked to the GenericTag table from each archetype tag, and monitors how often
        it is used in the selected spells, equipment and traits.
        :return: the list information of all generic tags
        """
        # stores a set of the tags relevant to the selected archetype(s)
        archetypeTags = set()
        # stores a dictionary of (tag, value) pairs, where the pair is the weighted occurrences of the tag
        archetypeWeightDict = dict()
        # stores the weights of the generic tags, in [equipment, spell, trait] order
        weights = [1, 1, 1]

        # fills out the set and dictionary of the tags
        for tag in self.tags:
            archetypeTags.update(
                set(
                    DataExtractor.get_names_from_connector("Tag",
                                                           "GenericTag",
                                                           input_name=tag[0])))
        for tag in archetypeTags:
            archetypeWeightDict.update({tag: 0})

        # prepares lists of the names of each data group, with their related data values
        # they have easily adjustable data value weightings, with the base values typically being
        # their maximum dice values, plus 1 for every dice above 1, to represent the benefit of more dice
        equipment, spells, traits = [], [], []
        for eq in set(self.character.chrClass.equipment):
            if eq.armorClass != 0:
                equipVal = (eq.armorClass - 10) / 4.0
            elif eq.dice is not None:
                diceValues = eq.dice.split("d")
                equipVal = (int(diceValues[0]) *
                            int(diceValues[1])) + (int(diceValues[0]) - 1)
                equipVal /= 6.0
            else:
                equipVal = 0
            equipment.append([eq.name, equipVal])

        for spell in (self.character.magic.knownSpells +
                      self.character.magic.preparedSpellOptions):
            if spell.damage is None:
                spellDmg = 0
            else:
                spellDmgVals = spell.damage.split("d")
                spellDmg = (int(spellDmgVals[0]) *
                            int(spellDmgVals[1])) + (int(spellDmgVals[0]) - 1)
                spellDmg /= 6.0
            spells.append([spell.name, spellDmg])

        for trait in self.character.traits:
            traits.append([trait[0], 0])

        archetypeWeightDict = pull_generic_tag("Equipment", equipment,
                                               weights[0], archetypeTags,
                                               archetypeWeightDict)
        archetypeWeightDict = pull_generic_tag("Spell", spells, weights[1],
                                               archetypeTags,
                                               archetypeWeightDict)
        archetypeWeightDict = pull_generic_tag("Trait", traits, weights[2],
                                               archetypeTags,
                                               archetypeWeightDict)

        return archetypeWeightDict