def test_get_unit_status_distribution(self):
        dungeon_model = self._get_dungeon_model()
        with LogCapture():
            logger = logging.getLogger()
            dungeon = Dungeon(dungeon=dungeon_model,
                              announcer={},
                              log=logger)

        """ living hostile player """
        unit_generator = UnitGenerator()
        living_player = unit_generator.generate(is_player=True,
                                                combat_status="hostile")
        dungeon.add_unit(living_player)

        """ dead friendly NPC """
        dead_friendly_unit = unit_generator.generate(combat_status="friendly")
        dead_friendly_unit.kill()
        dungeon.add_unit(dead_friendly_unit)

        """ living friendly NPC """
        living_npc = unit_generator.generate(combat_status="friendly")
        dungeon.add_unit(living_npc)

        """ living hostile NPC """
        hostile_living_npc = unit_generator.generate(combat_status="hostile")
        dungeon.add_unit(hostile_living_npc)

        # TODO: add undead here
        actual = dungeon.get_unit_status_distribution()

        expected = {
            "players": {
                "living": 1,
                "dead": 0,
                "undead": 0
            },
            "npc": {
                "hostile": {
                    "living": 1,
                    "dead": 0,
                    "undead": 0
                },
                "friendly": {
                    "living": 1,
                    "dead": 1,
                    "undead": 0
                }
            }
        }

        self.assertEqual(expected, actual)
    def test_get_unit_by_user_id(self):
        dungeon_model = self._get_dungeon_model()
        with LogCapture():
            logger = logging.getLogger()
            dungeon = Dungeon(dungeon=dungeon_model,
                              announcer={},
                              log=logger)
        user_id = 42
        unit_generator = UnitGenerator()
        unit = unit_generator.generate(user_id=user_id)

        dungeon.add_unit(unit)

        actual = dungeon.get_unit_by_user_id(user_id)

        self.assertIsInstance(actual, Unit)
    def test_add_dungeon_units(self):
        dungeon_model = self._get_dungeon_model()
        with LogCapture():
            logger = logging.getLogger()
            dungeon = Dungeon(dungeon=dungeon_model,
                              announcer={},
                              log=logger)

        unit_generator = UnitGenerator()
        unit = unit_generator.generate()

        dungeon.add_unit(unit)

        dungeon_unit = dungeon.units[0]

        self.assertIsInstance(dungeon_unit, Unit)
    def test_get_random_living_unit(self):
        dungeon_model = self._get_dungeon_model()
        with LogCapture():
            logger = logging.getLogger()
            dungeon = Dungeon(dungeon=dungeon_model,
                              announcer={},
                              log=logger)

        unit_generator = UnitGenerator()

        for j in range(0, 5):
            hostile_living_npc = unit_generator.generate(combat_status="hostile")
            dungeon.add_unit(hostile_living_npc)

        random_unit = dungeon.get_random_living_unit()

        self.assertIsInstance(random_unit, Unit)
    def test_get_unit_by_name(self):
        dungeon_model = self._get_dungeon_model()
        with LogCapture():
            logger = logging.getLogger()
            dungeon = Dungeon(dungeon=dungeon_model,
                              announcer={},
                              log=logger)

        unit_name = "quux"

        unit_generator = UnitGenerator()
        unit = unit_generator.generate(unit_name=unit_name)

        dungeon.add_unit(unit)

        self.assertTrue(len(dungeon.units), 0)

        # test exact match
        actual = dungeon.get_unit_by_name(unit_name)
        self.assertIsInstance(actual, Unit)

        # test invalid match
        actual = dungeon.get_unit_by_name("foo")
        self.assertIsNone(actual)

        # test case sensitivity
        actual = dungeon.get_unit_by_name(unit_name.upper())
        self.assertIsInstance(actual, Unit)

        # test startswith
        actual = dungeon.get_unit_by_name(unit_name[0:1])
        self.assertIsInstance(actual, Unit)
    def test_get_living_players(self):
        dungeon_model = self._get_dungeon_model()
        with LogCapture():
            logger = logging.getLogger()
            dungeon = Dungeon(dungeon=dungeon_model,
                              announcer={},
                              log=logger)

        unit_generator = UnitGenerator()

        unit = unit_generator.generate(is_player=True)

        dungeon.add_unit(unit)

        dead_unit = unit_generator.generate()
        dead_unit.kill()

        dungeon.add_unit(dead_unit)

        # test typical use case
        actual = dungeon.get_living_players()
        self.assertEqual(len(actual), 1)

        dead_players = dungeon.get_dead_players()
        self.assertEqual(len(dead_players), 0)
    def test_get_living_unit_by_name(self):
        dungeon_model = self._get_dungeon_model()
        with LogCapture():
            logger = logging.getLogger()
            dungeon = Dungeon(dungeon=dungeon_model,
                              announcer={},
                              log=logger)

        unit_name = "baz"
        unit_generator = UnitGenerator()
        unit = unit_generator.generate(unit_name=unit_name)

        dungeon.add_unit(unit)

        # test typical use case
        actual = dungeon.get_living_unit_by_name(unit.get_name())
        self.assertIsInstance(actual, Unit)

        unit.kill()

        actual = dungeon.get_living_unit_by_name(unit.get_name())
        self.assertIsNone(actual)
    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