Ejemplo n.º 1
0
    def test_get_dungeons(self):
        dungeon_model = Dungeon(db=self.db)
        dungeons = dungeon_model.get_dungeons()

        self.assertIsInstance(dungeons, list)
        self.assertTrue(len(dungeons) > 0)

        for dungeon in dungeons:
            self.assertIsInstance(dungeon, dict)
            self.assertTrue(dungeon["id"])
            self.assertTrue(dungeon["name"])
            self.assertTrue(dungeon["channel"])
            self.assertTrue("description" in dungeon)
            self.assertTrue("min_level" in dungeon)
            self.assertTrue("max_level" in dungeon)
Ejemplo n.º 2
0
    def _build_dungeons(self, **kwargs):
        """
        Add units from map/collection
        """
        self.log.info("Building dungeons")

        total_units = 0
        player_units = 0
        npc_units = 0
        dungeon_collection = DungeonCollection()
        unit_collection = kwargs["unit_collection"]
        dungeon_unit_map = kwargs["dungeon_unit_map"]
        dungeon_unit_models = kwargs["dungeon_unit_models"]

        dungeon_model = DungeonModel(db=self.db)
        dungeon_models = dungeon_model.get_dungeons()

        for dungeon_model in dungeon_models:
            dungeon_id = dungeon_model["id"]
            units = []

            for dungeon_unit_model in dungeon_unit_models:
                if dungeon_id in dungeon_unit_map:
                    unit_id_list = dungeon_unit_map[dungeon_id]
                    units = \
                        unit_collection.get_units_by_unit_id_list(unit_id_list)

            """
            Any units that are not in the dungeon (players)
            """
            other_units = unit_collection.units

            for ounit in other_units:
                if ounit not in units:
                    units.append(ounit)

            announcer = DungeonAnnouncer(irc=self.irc,
                                         ircutils=self.ircutils,
                                         ircmsgs=self.ircmsgs,
                                         destination="#spiffyrpg")

            dungeon = Dungeon(dungeon=dungeon_model,
                              announcer=announcer,
                              log=self.log)

            units = sorted(units, key=lambda x: x.level)

            for unit in units:
                added_unit = dungeon.add_unit(unit)

                if added_unit is not None:
                    total_units += 1

                    if unit.is_player:
                        player_units += 1

                    if unit.is_npc:
                        npc_units += 1

            dungeon_collection.add(dungeon)

        total_dungeons = len(dungeon_collection.dungeons)

        debug_msg = "SpiffyWorld: %s dungeons with " % total_dungeons
        debug_msg += "%s NPCs, %s players loaded" % \
            (npc_units, player_units)

        self.log.info(debug_msg)

        return dungeon_collection
    def test_build_world(self):
        """
        Utilize models to build the world.

        1. Get dungeons
        2. Get effects
        3. Get item effects
        4. Get items
        5. Get unit items
        6. Get units
        """

        """ Dungeons """
        dungeon_model = DungeonModel(db=self.db)
        dungeon_models = dungeon_model.get_dungeons()

        self.assertIsInstance(dungeon_models, list)
        self.assertTrue(dungeon_models)

        """ Effects """
        effects_model = EffectModel(db=self.db)
        effects_models = effects_model.get_effects()

        self.assertIsInstance(effects_models, list)
        self.assertTrue(effects_models)

        """ Unit effects map """
        unit_effects_model = UnitEffects(db=self.db)
        unit_effects = unit_effects_model.get_unit_effects()
        unit_effects_map = unit_effects_model._get_unit_effects_map(unit_effects)

        self.assertIsInstance(unit_effects_map, dict)
        self.assertTrue(unit_effects_map)

        """ Unit dialogue map """
        unit_dialogue_model = UnitDialogueModel(db=self.db)
        unit_dialogue = unit_dialogue_model.get_unit_dialogue()
        unit_dialogue_map = unit_dialogue_model._get_unit_dialogue_map(unit_dialogue)

        self.assertIsInstance(unit_dialogue, list)
        self.assertIsInstance(unit_dialogue_map, dict)
        self.assertTrue(unit_dialogue_map)

        """ Item effects map """
        item_effects_model = ItemEffects(db=self.db)
        item_effects = item_effects_model.get_item_effects()
        item_effects_map = item_effects_model._get_item_effects_map(item_effects)

        self.assertIsInstance(item_effects, list)
        self.assertIsInstance(item_effects_map, dict)
        self.assertTrue(item_effects_map)
        self.assertTrue(item_effects)

        """ Items """
        item_model = ItemModel(db=self.db)
        item_models = item_model.get_items()

        self.assertIsInstance(item_models, list)
        self.assertTrue(item_models)

        """ Unit items map """
        unit_items_model = UnitItems(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)

        self.assertIsInstance(unit_item_models, list)
        self.assertIsInstance(unit_items_map, dict)
        self.assertTrue(unit_items_map)
        self.assertTrue(unit_item_models)

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

        self.assertIsInstance(unit_models, list)
        self.assertTrue(unit_models)

        """ Assemble collections using models """
        item_collection = ItemCollection()
        effect_collection = EffectCollection()
        dialogue_collection = UnitDialogueCollection()

        for effect_model in effects_models:
            effect = Effect(effect=effect_model)
            effect_collection.add(effect)

        self.assertTrue(effect_collection.effects)

        item_builder = ItemBuilder()
        items = item_builder.build_items(item_models=item_models,
                                         item_effects_map=item_effects_map,
                                         effect_collection=effect_collection)
        self.assertIsInstance(items, list)
        self.assertTrue(items)

        for item in items:
            item_collection.add(item)

        self.assertTrue(item_collection.items)

        for ud in unit_dialogue:
            dialogue = UnitDialogue(dialogue=ud)
            dialogue_collection.add(dialogue)

        """ Build units using assembled collections """
        unit_builder = UnitBuilder()

        with LogCapture() as l:
            logger = logging.getLogger()
            units = unit_builder.build_units(unit_models=unit_models,
                                             effect_collection=effect_collection,
                                             item_collection=item_collection,
                                             dialogue_collection=dialogue_collection,
                                             unit_items_map=unit_items_map,
                                             unit_effects_map=unit_effects_map,
                                             unit_dialogue_map=unit_dialogue_map,
                                             log=logger)

        self.assertIsInstance(units, list)
        self.assertTrue(units)

        num_players = 0

        for unit in units:
            self.assertIsInstance(unit, Unit)

            if unit.is_player:
                num_players += 1

        self.assertTrue(num_players > 0)

        """
        Populate collections
        """
        unit_collection = UnitCollection()

        for unit in units:
            unit_collection.add(unit)

        self.assertEqual(len(unit_collection.units), len(unit_models))