Exemplo n.º 1
0
 def test_add_item(self):
     """test that the add_item property works correctly"""
     coin_inv = inv.Inventory()
     self.assertEqual(coin_inv, self.empty)
     coin_inv.add_item(SilverCoin(), 7)
     self.assertTrue(coin_inv != self.coins)
     self.assertEqual(coin_inv, inv.Inventory((SilverCoin(), 7)))
     coin_inv.add_item(SilverCoin(), 3)
     self.assertEqual(coin_inv, self.coins)
     coin_inv.add_item(HealthPotion(3), 1)
     coin_inv.add_item(SilverCoin(), 10)
     self.assertTrue(coin_inv != self.coins)
     # test that items are always put into correct buckets
     # regardless of time of insert
     coin_inv.add_item(HealthPotion(50), 3)
     coin_inv.add_item(HealthPotion(10), 4)
     coin_inv.add_item(HealthPotion(100))
     coin_inv.add_item(HealthPotion(3), 2)
     coin_inv.add_item(HealthPotion(10), 1)
     coin_inv.add_item(HealthPotion(3), 2)
     coin_inv.add_item(HealthPotion(3), 2)
     coin_inv.add_item(HealthPotion(100))
     self.assertEqual(coin_inv, self.potion_seller)
     # check that the 'Health Potion' bucket is correct
     bucket = coin_inv._items["health potion"]
     self.assertEqual(len(bucket), 4)
     self.assertTrue(inv.ItemStack(HealthPotion, 7, {"hp": 3}) in bucket)
     self.assertTrue(inv.ItemStack(HealthPotion, 5, {"hp": 10}) in bucket)
     self.assertTrue(inv.ItemStack(HealthPotion, 3, {"hp": 50}) in bucket)
     self.assertTrue(inv.ItemStack(HealthPotion, 2, {"hp": 100}) in bucket)
Exemplo n.º 2
0
 def tearDown(self):
     self.bill.despawn()
     self.phil.despawn()
     self.dana.despawn()
     # clear the inventories for TEST_ROOM and TEST_EXIT
     TEST_ROOM.inv = inv.Inventory()
     TEST_OUT.inv = inv.Inventory()
Exemplo n.º 3
0
 def setUp(self):
     self.empty = inv.Inventory()
     self.coins = inv.Inventory((SilverCoin(), 10))
     self.rich = inv.Inventory((SilverCoin(), 15), (HealthPotion(10), 5),
                               (Sword(15, "steel"), 2))
     self.potion_seller = inv.Inventory(
         (SilverCoin(), 20), (HealthPotion(hp=10), 5),
         (HealthPotion(hp=3), 7), (HealthPotion(hp=100), 2),
         (HealthPotion(hp=50), 3))
Exemplo n.º 4
0
 def test_remove_item(self):
     """test that removing an item works"""
     with self.assertRaises(KeyError):
         self.empty.remove_item(SilverCoin())
     self.coins.remove_item(SilverCoin())
     self.assertEqual(self.coins, inv.Inventory((SilverCoin(), 9)))
     self.coins.remove_item(SilverCoin(), 3)
     self.assertEqual(self.coins, inv.Inventory((SilverCoin(), 6)))
     # Removing more coins than we have should cause a value error
     with self.assertRaises(ValueError):
         self.coins.remove_item(SilverCoin(), 10)
     # Stack should be removed once amount = 0
     self.coins.remove_item(SilverCoin(), 6)
     self.assertEqual(self.coins, self.empty)
     # testing with a more diverse inventory
     # items that almost match should not be removed
     with self.assertRaises(KeyError):
         self.rich.remove_item(Sword(15, "platinum"))
     # this should work correctly
     self.rich.remove_item(Sword(15, "steel"))
     self.assertEqual(
         self.rich,
         inv.Inventory((HealthPotion(10), 5), (SilverCoin(), 15),
                       (Sword(15, "steel"), 1)))
     self.rich.remove_item(Sword(15, "steel"))
     self.assertEqual(
         self.rich,
         inv.Inventory(
             (HealthPotion(10), 5),
             (SilverCoin(), 15),
         ))
     # manually check that bucket has been removed
     self.assertEqual(len(self.rich._items), 2)
     with self.assertRaises(KeyError):
         self.rich.remove_item(HealthPotion(3))
     # we can remove 4 potions...
     self.rich.remove_item(HealthPotion(10), 4)
     # ...but we can't remove 4 more
     with self.assertRaises(ValueError):
         self.rich.remove_item(HealthPotion(10), 4)
     # check the state of the inventory
     self.assertEqual(
         self.rich,
         inv.Inventory(
             (SilverCoin(), 15),
             (HealthPotion(10), 1),
         ))
     # now let's try to remove everything else
     self.rich.remove_item(HealthPotion(10))
     self.rich.remove_item(SilverCoin(), 15)
     self.assertEqual(self.rich, self.empty)
     self.assertEqual(self.rich._items, {})
Exemplo n.º 5
0
    def __init__(self, name=None):
        super().__init__()
        self._name = name
        self.location = None
        self.msgs = asyncio.Queue()

        # build dict from Commands collected by CharacterClass
        self.cmd_dict = ShadowDict()
        for (name, cmd) in self._commands.items():
            cmd = cmd.specify(self)
            # add command only if filter permits it
            if cmd.filter.permits(self):
                self.cmd_dict[name] = cmd
            # because sCommands are not bound properly like a normal
            # method, we must manually bind the methods
            # TODO: override getattribute__ to solve the super() issue?
            if isinstance(getattr(self, cmd.func.__name__), Command):
                setattr(self, cmd.func.__name__, cmd)

        # set up inventory and equipping items
        self.inv = inv.Inventory()
        self.equip_dict = inv.EquipTarget.make_dict(*self.equip_slots)

        # put character in default command parsing mode
        self._parser = self._command_parser
Exemplo n.º 6
0
 def test_eq(self):
     """testing the __eq__ method (mostly used for testing)"""
     # test equality with an empty list
     self.assertTrue(self.empty is not inv.Inventory())
     self.assertTrue(self.empty == inv.Inventory())
     self.assertTrue(inv.Inventory() == self.empty)
     # test with object of wrong type
     self.assertTrue(self.empty != "oops wrong type")
     # test with filled inventories
     other = inv.Inventory((SilverCoin(), 10))
     self.assertTrue(other is not self.coins)
     self.assertTrue(self.coins == other)
     rich = inv.Inventory((HealthPotion(10), 5), (SilverCoin(), 15),
                          (Sword(15, "steel"), 2))
     self.assertTrue(rich is not self.rich)
     self.assertTrue(rich == self.rich)
     self.assertTrue(self.rich != self.coins)
Exemplo n.º 7
0
    def test_unequip(self):
        """test that the Character.unequip method performs proper error
        checking and works as expected"""
        # TODO test that all EquipCommands are removed!
        # manually equip the hat and the mace
        # we mark the hat as from_inv=False, so it should not be returned
        # while the mace should be added back to inventory
        hat = Hat()
        mace = Mace()
        self.finn.equip_dict[hat.target] = hat, False
        self.finn.equip_dict[mace.target] = mace, True
        # copy a reference for comparisons
        ref = self.finn.equip_dict.copy()
        # clear finn's inventory for convenience
        self.finn.inv = inv.Inventory()

        # try unequipping a slot that does not exist
        self.finn.unequip(inv.EquipTarget("Foo"))
        self.assertEqual(self.finn.msgs.pop(),
                         "Human does not possess equip slot 'Foo'.")

        # unequip the item in the "Head" slot
        self.finn.unequip(inv.EquipTarget("head"))
        # hat should not be added to inventory since from_inv=False
        self.assertEqual(self.finn.inv, inv.Inventory())
        # equip dict should be updated
        ref[inv.EquipTarget("head")] = None
        self.assertEqual(self.finn.equip_dict, ref)
        # item's equip method should be called
        self.assertEqual(self.finn.msgs[-1], "unequip Hat")

        # unequip the item in the "Right Hand" slot
        self.finn.unequip(inv.EquipTarget("right hand"))
        # mace should be added to inventory since from_inv=False
        self.assertEqual(self.finn.inv, inv.Inventory((Mace(), 1)))
        # equip dict should be updated
        ref[inv.EquipTarget("right hand")] = None
        self.assertEqual(self.finn.equip_dict, ref)
        # item's equip method should be called
        self.assertEqual(self.finn.msgs[-1], "unequip Mace")

        # try to unequip from an empty slot
        self.finn.unequip(inv.EquipTarget("hEAD"))
        self.assertEqual(self.finn.msgs.pop(),
                         "No item equipped on target Head.")
Exemplo n.º 8
0
    def test_cmd_unequip(self):
        """test that the unequip command works properly"""
        self.bill.inv.add_item(Sword())
        self.bill.inv.add_item(Hat())
        # equip the sword and hat
        self.bill.equip(Sword(), from_inv=True)
        self.bill.equip(Hat(), from_inv=True)
        # purge the equip messages
        self.bill.msgs.clear()
        # create a reference copy of the equip_dict for testing
        ref = self.bill.equip_dict.copy()
        # try to unequip a non-existent item
        self.bill.command("unequip flesh")
        self.assertEqual(self.bill.msgs.pop(),
                         "Could not find equipped item 'flesh'.")
        # equipped items should be unaffected
        self.assertEqual(ref, self.bill.equip_dict)
        # now try unequipping the hat
        self.bill.command("unequip hat")
        self.assertEqual(self.bill.msgs.pop(),
                         "unequip Hat")
        # hat should be removed from our equip_dict
        ref[inv.EquipTarget("head")] = None
        self.assertEqual(self.bill.equip_dict, ref)
        # hat should be added back to inventory
        self.assertEqual(self.bill.inv, inv.Inventory((Hat(), 1)))

        # now try unequipping the hat again
        self.bill.command("unequip hat")
        self.assertEqual(self.bill.msgs.pop(),
                         "Could not find equipped item 'hat'.")
        self.assertEqual(ref, self.bill.equip_dict)

        # try unequipping the sword (should be case-insensitive)
        self.bill.command("unequip SwOrD")
        self.assertEqual(self.bill.msgs.pop(),
                         "unequip Sword")
        # sword should be removed from our equip_dict
        ref[inv.EquipTarget("right hand")] = None
        self.assertEqual(self.bill.equip_dict, ref)
        # sword should be added back to inventory
        inv_items = [(Hat(), 1), (Sword(), 1)]
        self.assertEqual(self.bill.inv, inv.Inventory(*inv_items))
Exemplo n.º 9
0
 def test_iter(self):
     """test that __iter__ works properly"""
     self.assertEqual(len(list(self.empty)), 0)
     coin_list = list(self.coins)
     self.assertEqual(len(coin_list), 1)
     self.assertEqual(coin_list[0][1], 10)
     self.assertTrue(isinstance(coin_list[0][0], SilverCoin))
     inv_items = list(self.rich)
     cloned_inv = inv.Inventory(*inv_items)
     self.assertEqual(self.rich, cloned_inv)
Exemplo n.º 10
0
    def test_cmd_drop(self):
        """test that the 'drop' command works properly"""
        # try to drop an item when inventory is empty
        self.bill.command("drop coin")
        self.assertEqual(self.bill.msgs.pop(),
                         "Could not find item 'coin' to drop.")
        # now add a coin and some swords to bill's inventory
        self.bill.add_item(Coin(), 5)
        self.bill.add_item(Sword(), 1)
        # create a copy of TEST_ROOM's inv for reference
        loc_ref = inv.Inventory(*TEST_ROOM.inv)
        # create a copy of Bill's inv for reference
        bill_ref = inv.Inventory(*self.bill.inv)

        # try dropping a coin
        self.bill.command("drop coin")
        self.assertEqual(self.bill.msgs, [])
        # coin should be added to location's inventory
        loc_ref.add_item(Coin())
        self.assertEqual(TEST_ROOM.inv, loc_ref)
        # coin should be removed from bill's inventory
        bill_ref.remove_item(Coin())
        self.assertEqual(self.bill.inv, bill_ref)

        # try dropping a sword
        self.bill.command("drop sWORD")
        self.assertEqual(self.bill.msgs, [])
        # sword should be added to the location's inventory
        loc_ref.add_item(Sword())
        self.assertEqual(TEST_ROOM.inv, loc_ref)
        # sword should to be removed from bill's inventory
        bill_ref.remove_item(Sword())
        self.assertEqual(self.bill.inv, bill_ref)

        # try dropping a sword (when none are left)
        self.bill.command("drop Sword")
        self.assertEqual(self.bill.msgs.pop(),
                         "Could not find item 'sword' to drop.")
Exemplo n.º 11
0
    def test_cmd_pickup(self):
        """test that the 'pickup' command works properly"""
        # try to pickup an item when there are none available
        self.bill.command("pickup coin")
        self.assertEqual(self.bill.msgs.pop(),
                         "Could not find item 'coin' to pick up.")
        # now add a coin and some swords
        TEST_ROOM.add_item(Coin(), 5)
        TEST_ROOM.add_item(Sword(), 1)
        # create a copy of TEST_ROOM's inv for reference
        loc_ref = inv.Inventory(*TEST_ROOM.inv)
        # create a copy of Bill's inv for reference
        bill_ref = inv.Inventory(*self.bill.inv)
        # try to pickup an item with a bad name
        self.bill.command("pickup foo")
        self.assertEqual(self.bill.msgs.pop(),
                         "Could not find item 'foo' to pick up.")
        # try looting a coin
        self.bill.command("pickup coin")
        self.assertEqual(self.bill.msgs, [])
        # coin should be removed from location
        loc_ref.remove_item(Coin())
        self.assertEqual(TEST_ROOM.inv, loc_ref)
        # coin should be added to character
        bill_ref.add_item(Coin())
        self.assertEqual(self.bill.inv, bill_ref)

        # try looting a sword
        self.bill.command("pickup sword")
        self.assertEqual(self.bill.msgs, [])
        # coin should be removed from location
        loc_ref.remove_item(Sword())
        self.assertEqual(TEST_ROOM.inv, loc_ref)
        # coin should be added to character
        bill_ref.add_item(Sword())
        self.assertEqual(self.bill.inv, bill_ref)
Exemplo n.º 12
0
 def test_fire_staff(self):
     brute, seller, wizard = self.brute, self.seller, self.wizard
     # put down three fire staffs
     tavern.add_item(FireStaff())
     tavern.add_item(FireStaff())
     tavern.add_item(FireStaff())
     # each character picks one up
     brute.command("pickup fire staff")
     seller.command("pickup fire staff")
     wizard.command("pickup fire staff")
     inv_with_staff = inventory.Inventory(
         (FireStaff(), 1)
     )
     self.assertEqual(brute.inv, inv_with_staff)
     self.assertEqual(seller.inv, inv_with_staff)
     self.assertEqual(wizard.inv, inv_with_staff)
     # each player should be able to equip the staff
     brute.command("equip fire staff")
     seller.command("equip fire staff")
     wizard.command("equip fire staff")
     rh = inventory.EquipTarget("Right hand")
     self.assertTrue(isinstance(brute.equip_dict[rh][0], FireStaff))
     self.assertTrue(isinstance(seller.equip_dict[rh][0], FireStaff))
     self.assertTrue(isinstance(wizard.equip_dict[rh][0], FireStaff))
     # now brute and wizard should have updated commands
     self.assertTrue("hit" in brute.cmd_dict and
                     "fireball" not in brute.cmd_dict)
     self.assertTrue("hit" not in seller.cmd_dict and
                     "fireball" not in seller.cmd_dict)
     self.assertTrue("hit" not in wizard.cmd_dict and
                     "fireball" in wizard.cmd_dict)
     # test that the commands can be used
     brute.command("hit adam")
     self.assertEqual(brute.msgs.get_nowait(),
                      f"You hit {seller} with a staff.")
     self.assertEqual(seller.msgs.get_nowait(),
                      f"{brute} hit you with a staff.")
     wizard.command("fireball adam")
     self.assertEqual(wizard.msgs.get_nowait(),
                      f"You hit {seller} with a fireball.")
     self.assertEqual(seller.msgs.get_nowait(),
                      f"{wizard} hit you with a fireball.")
Exemplo n.º 13
0
    def test_simple_tree(self):
        """test that load_tree can load in a simple tree"""
        mudworld.load_tree(self.simple_tree, self.simple_objs, {})

        abra = self.simple_objs["Abra"]
        grug = self.simple_objs["Grug"]
        ring = self.simple_objs["ring"]
        house = self.simple_objs["Boring House"]
        interior = self.simple_objs["Boring House Interior"]

        # check that abra and grug are in the proper places
        self.assertEqual([abra], list(interior.characters))
        self.assertEqual([grug], list(house.characters))

        # no entities should have been added to the houses
        self.assertEqual([], list(house.entities))
        self.assertEqual([], list(interior.entities))

        # grug should have nothing in his inventory
        self.assertEqual([], list(grug.inv))
        # abra should have one thing (the cursed ring)
        self.assertEqual(
            abra.inv, inv.Inventory((self.simple_classes["CursedRing"](), 1)))
Exemplo n.º 14
0
    def test_equip(self):
        """test that the equip method throws proper exceptions and
        works as expected"""
        #TODO: test that all equip_commands are added!
        # Finn's equip dict should be empty to start with
        self.assertEqual(self.finn.equip_dict, self.ref)
        # equip Sword without removing one from the inventory
        sword = Sword()
        self.finn.equip(sword, from_inv=False)
        # sword should not be removed from inventory
        self.assertEqual(self.finn.inv, inv.Inventory(
            (Sword(), 1),
            (Coin(), 5),
            (HealthPotion(10), 3)
        ))
        # equip dict should be updated
        self.ref[inv.EquipTarget("Right Hand")] = sword, False
        self.assertEqual(self.finn.equip_dict, self.ref)
        # item's equip method should be called
        self.assertEqual(self.finn.msgs[-1], "equip Sword")

        # try to equip an item that cannot be equipped
        self.finn.equip(HealthPotion(5), False)
        self.assertEqual(self.finn.msgs.pop(),
                         "Health Potion cannot be equipped.")

        # try to equip an item for which we don't have proper slots
        self.finn.equip(Bow(), False)
        self.assertEqual(self.finn.msgs[-1],
                         "Cannot equip item Bow to Left hand.")


        # try to equip a hat, but this time pull it from the inventory
        hat = Hat()
        self.finn.equip(hat, True)
        self.assertEqual(self.finn.msgs.pop(),
                         "Cannot equip Hat-not found in inventory.")
        # give finn a hat and try again
        self.finn.add_item(Hat())
        self.finn.equip(hat)
        # hat should be removed from inventory
        self.assertEqual(self.finn.inv, inv.Inventory(
            (Sword(), 1),
            (Coin(), 5),
            (HealthPotion(10), 3)
        ))
        # equip dict should be updated
        self.ref[inv.EquipTarget("Head")] = hat, True
        self.assertEqual(self.finn.equip_dict, self.ref)
        # item's equip method should be called
        self.assertEqual(self.finn.msgs[-1], "equip Hat")

        # try to equip a Mace, which implicitly unequips the Sword
        mace = Mace()
        self.finn.equip(mace, from_inv=False)
        # mace should not be removed from inventory and
        # old sword should NOT be returned since from_inv=False
        self.assertEqual(self.finn.inv, inv.Inventory(
            (Sword(), 1),
            (Coin(), 5),
            (HealthPotion(10), 3)
        ))
        # equip dict should be updated
        self.ref[inv.EquipTarget("Right Hand")] = mace, False
        self.assertEqual(self.finn.equip_dict, self.ref)
        # item's equip method should be called
        self.assertEqual(self.finn.msgs[-1], "equip Mace")
Exemplo n.º 15
0
 def add_item_stack(self):
     """test that either items or ItemStacks can be given to players"""
     self.default.add_item(inv.ItemStack.from_item(Sword(), 5))
     self.assertEqual(self.default.inv, inv.Inventory((Sword(), 5)))