<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from SpiffyWorld.models import Dungeon as DungeonModel, \
    Effect as EffectModel, ItemEffects, Item as ItemModel, \
    UnitEffects, DungeonUnits, UnitDialogue as UnitDialogueModel, \
    UnitItems as UnitItemsModel, Unit as UnitModel, UnitType as UnitTypeModel
from SpiffyWorld.collections import EffectCollection, ItemCollection, \
    UnitDialogueCollection, UnitCollection, DungeonCollection, \
    UnitTypeCollection
from SpiffyWorld import Effect, ItemBuilder, World, Dungeon, UnitDialogue, \
    UnitBuilder, UnitType, DungeonAnnouncer


class Worldbuilder:
    """
    Builds dependencies for the world based on DB models

    1. Get everything we need to build a unit:
        a. Items
        b. Effects
        c. Unit Effects
        d. dialogue
        e. unit titles
    2. Get everything we need to build a dungeon
        a. Units
    3. Get everything we need to build a world
        a. Dungeons with units
    """
    def __init__(self, **kwargs):
        self.db = kwargs["db"]
        self.irc = kwargs["irc"]
        self.ircmsgs = kwargs["ircmsgs"]
        self.ircutils = kwargs["ircutils"]
        self.log = kwargs["log"]

    def build_world(self):
        """
        Populate retrieves db info and build
        uses that to create objects
        """
        effect_collection = self._build_effects()
        item_collection = \
            self._build_items(effect_collection=effect_collection)

        dialogue_info = self._build_dialogue()
        dialogue_collection = dialogue_info["dialogue_collection"]
        unit_dialogue_map = dialogue_info["unit_dialogue_map"]

        unit_effects_map = self._build_unit_effects()
        dungeon_unit_info = self._build_dungeon_units()
        dungeon_unit_models = dungeon_unit_info["dungeon_unit_models"]
        dungeon_unit_map = dungeon_unit_info["dungeon_unit_map"]
        unit_items_map = self._build_unit_items()
        unit_type_collection = self._build_unit_types()

        unit_info = \
            self._build_units(item_collection=item_collection,
                              effect_collection=effect_collection,
                              dialogue_collection=dialogue_collection,
                              unit_effects_map=unit_effects_map,
                              unit_items_map=unit_items_map,
                              unit_dialogue_map=unit_dialogue_map)
        unit_collection = unit_info["unit_collection"]
        unit_model = unit_info["unit_model"]

        dungeon_collection = \
            self._build_dungeons(dungeon_unit_map=dungeon_unit_map,
                                 dungeon_unit_models=dungeon_unit_models,
                                 unit_collection=unit_collection)

        world = World(effect_collection=effect_collection,
                      item_collection=item_collection,
                      unit_collection=unit_collection,
                      dialogue_collection=dialogue_collection,
                      dungeon_collection=dungeon_collection,
                      unit_type_collection=unit_type_collection,
                      unit_model=unit_model,
                      irc=self.irc,
                      log=self.log)

        return world

    def _build_unit_types(self):
        self.log.info("Building unit types")

        unit_type_model = UnitTypeModel(db=self.db)
        unit_types = unit_type_model.get_unit_types()
        unit_type_collection = UnitTypeCollection()

        if unit_types:
            for ut in unit_types:
                unit_type = UnitType(unit_type=ut)
                unit_type_collection.add(unit_type)

        return unit_type_collection

    def _build_units(self, **kwargs):
        self.log.info("Building units")

        unit_model = UnitModel(db=self.db)
        unit_models = unit_model.get_units()

        item_collection = kwargs["item_collection"]
        effect_collection = kwargs["effect_collection"]
        dialogue_collection = kwargs["dialogue_collection"]
        unit_effects_map = kwargs["unit_effects_map"]
        unit_dialogue_map = kwargs["unit_dialogue_map"]
        unit_items_map = kwargs["unit_items_map"]

        unit_builder = UnitBuilder()
        unit_collection = UnitCollection()
        units = \
            unit_builder.build_units(unit_models=unit_models,
                                     item_collection=item_collection,
                                     effect_collection=effect_collection,
                                     dialogue_collection=dialogue_collection,
                                     unit_effects_map=unit_effects_map,
                                     unit_items_map=unit_items_map,
                                     unit_dialogue_map=unit_dialogue_map,
                                     log=self.log)

        for unit in units:
            unit_collection.add(unit)

        return {
            "unit_collection": unit_collection,
            "unit_model": unit_model
        }

    def _build_unit_items(self):
        self.log.info("Building unit items")

        unit_items_model = UnitItemsModel(db=self.db)
        unit_item_models = unit_items_model.get_unit_items()
        unit_items_map = unit_items_model._get_unit_items_map(unit_item_models)

        return unit_items_map

    def _build_dialogue(self, **kwargs):
        self.log.info("Building dialogue")

        dialogue_collection = UnitDialogueCollection()
        dialogue_model = UnitDialogueModel(db=self.db)
        dialogue_models = dialogue_model.get_unit_dialogue()
        unit_dialogue_map = \
            dialogue_model._get_unit_dialogue_map(dialogue_models)

        for dialogue_model in dialogue_models:
            dialogue = UnitDialogue(dialogue=dialogue_model)
            dialogue_collection.add(dialogue)

        return {
            "dialogue_collection": dialogue_collection,
            "unit_dialogue_map": unit_dialogue_map
        }

    def _build_items(self, **kwargs):
        """
        This requires a populated EffectCollection in order
        to build the items due to the fact that items can have
        associated effects.
        """
        self.log.info("Building items")

        effect_collection = kwargs["effect_collection"]
<commit_msg>Adds Battlemaster and populates unit items with base items<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from SpiffyWorld.models import Dungeon as DungeonModel, \
    Effect as EffectModel, ItemEffects, Item as ItemModel, \
    UnitEffects, DungeonUnits, UnitDialogue as UnitDialogueModel, \
    UnitItems as UnitItemsModel, Unit as UnitModel, UnitType as UnitTypeModel
from SpiffyWorld.collections import EffectCollection, ItemCollection, \
    UnitDialogueCollection, UnitCollection, DungeonCollection, \
    UnitTypeCollection
from SpiffyWorld import Effect, ItemBuilder, World, Dungeon, UnitDialogue, \
    UnitBuilder, UnitType, DungeonAnnouncer, Battlemaster


class Worldbuilder:
    """
    Builds dependencies for the world based on DB models

    1. Get everything we need to build a unit:
        a. Items
        b. Effects
        c. Unit Effects
        d. dialogue
        e. unit titles
    2. Get everything we need to build a dungeon
        a. Units
    3. Get everything we need to build a world
        a. Dungeons with units
    """
    def __init__(self, **kwargs):
        self.db = kwargs["db"]
        self.irc = kwargs["irc"]
        self.ircmsgs = kwargs["ircmsgs"]
        self.ircutils = kwargs["ircutils"]
        self.log = kwargs["log"]

    def build_world(self):
        """
        Populate retrieves db info and build
        uses that to create objects
        """
        effect_collection = self._build_effects()
        item_collection = \
            self._build_items(effect_collection=effect_collection)

        dialogue_info = self._build_dialogue()
        dialogue_collection = dialogue_info["dialogue_collection"]
        unit_dialogue_map = dialogue_info["unit_dialogue_map"]

        unit_effects_map = self._build_unit_effects()
        dungeon_unit_info = self._build_dungeon_units()
        dungeon_unit_models = dungeon_unit_info["dungeon_unit_models"]
        dungeon_unit_map = dungeon_unit_info["dungeon_unit_map"]
        unit_items_map = self._build_unit_items(item_collection)
        unit_type_collection = self._build_unit_types()

        unit_info = \
            self._build_units(item_collection=item_collection,
                              effect_collection=effect_collection,
                              dialogue_collection=dialogue_collection,
                              unit_effects_map=unit_effects_map,
                              unit_items_map=unit_items_map,
                              unit_dialogue_map=unit_dialogue_map)
        unit_collection = unit_info["unit_collection"]
        unit_model = unit_info["unit_model"]

        dungeon_collection = \
            self._build_dungeons(dungeon_unit_map=dungeon_unit_map,
                                 dungeon_unit_models=dungeon_unit_models,
                                 unit_collection=unit_collection)

        battlemaster = Battlemaster()

        world = World(effect_collection=effect_collection,
                      item_collection=item_collection,
                      unit_collection=unit_collection,
                      dialogue_collection=dialogue_collection,
                      dungeon_collection=dungeon_collection,
                      unit_type_collection=unit_type_collection,
                      unit_model=unit_model,
                      battlemaster=battlemaster,
                      irc=self.irc,
                      log=self.log)

        return world

    def _build_unit_types(self):
        self.log.info("Building unit types")

        unit_type_model = UnitTypeModel(db=self.db)
        unit_types = unit_type_model.get_unit_types()
        unit_type_collection = UnitTypeCollection()

        if unit_types:
            for ut in unit_types:
                unit_type = UnitType(unit_type=ut)
                unit_type_collection.add(unit_type)

        return unit_type_collection

    def _build_units(self, **kwargs):
        self.log.info("Building units")

        unit_model = UnitModel(db=self.db)
        unit_models = unit_model.get_units()

        item_collection = kwargs["item_collection"]
        effect_collection = kwargs["effect_collection"]
        dialogue_collection = kwargs["dialogue_collection"]
        unit_effects_map = kwargs["unit_effects_map"]
        unit_dialogue_map = kwargs["unit_dialogue_map"]
        unit_items_map = kwargs["unit_items_map"]

        unit_builder = UnitBuilder()
        unit_collection = UnitCollection()
        units = \
            unit_builder.build_units(unit_models=unit_models,
                                     item_collection=item_collection,
                                     effect_collection=effect_collection,
                                     dialogue_collection=dialogue_collection,
                                     unit_effects_map=unit_effects_map,
                                     unit_items_map=unit_items_map,
                                     unit_dialogue_map=unit_dialogue_map,
                                     log=self.log)

        for unit in units:
            unit_collection.add(unit)

        return {
            "unit_collection": unit_collection,
            "unit_model": unit_model
        }

    def _build_unit_items(self, item_collection):
        self.log.info("Building unit items")

        unit_items_model = UnitItemsModel(db=self.db)
        unit_item_models = unit_items_model.get_unit_items()
        unit_items_map = unit_items_model._get_unit_items_map(unit_item_models)

        """
        Each unit starts with a set of base items.
        """
        base_items = item_collection.get_base_items()

        for unit_id in unit_items_map:
            for base_item in base_items:
                unit_items_map[unit_id].append(base_item.id)

        return unit_items_map

    def _build_dialogue(self, **kwargs):
        self.log.info("Building dialogue")

        dialogue_collection = UnitDialogueCollection()
        dialogue_model = UnitDialogueModel(db=self.db)
        dialogue_models = dialogue_model.get_unit_dialogue()
        unit_dialogue_map = \
            dialogue_model._get_unit_dialogue_map(dialogue_models)

        for dialogue_model in dialogue_models:
            dialogue = UnitDialogue(dialogue=dialogue_model)
            dialogue_collection.add(dialogue)

        return {
            "dialogue_collection": dialogue_collection,
            "unit_dialogue_map": unit_dialogue_map
        }

    def _build_items(self, **kwargs):
        """
        This requires a populated EffectCollection in order
        to build the items due to the fact that items can have
        associated effects.
        """
        self.log.info("Building items")

        effect_collection = kwargs["effect_collection"]

{'fraction_non_alphanumeric': 0.0677836431795578, 'fraction_numerical': 0.0006120419248718537, 'mean_word_length': 2.1658997335916688, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '393827', 'n_tokens_mistral': 3555, 'n_tokens_neox': 3459, 'n_words': 661}