add ability to move items between inventories

This commit is contained in:
evilchili
2024-09-02 14:53:49 -07:00
parent d9b3c4500e
commit 17a951b1b2
3 changed files with 56 additions and 8 deletions
+36 -4
View File
@@ -161,13 +161,18 @@ def test_spell_slots(db, carl, wizard):
def test_containers(db, carl):
with db.transaction():
ten_foot_pole = Item(name="10ft. Pole", item_type=ItemType.ITEM, consumable=False)
rope = Item(name="50 ft. of Rope", item_type=ItemType.ITEM, consumable=True, count=50)
bag_of_holding = Container(name="Bag of Holding")
db.add_or_update([carl, ten_foot_pole, bag_of_holding])
db.add_or_update([carl, ten_foot_pole, rope, bag_of_holding])
# add the ten_foot_pole to the bag of holding
# add some items to the bag of holding
assert bag_of_holding.add(ten_foot_pole)
assert bag_of_holding.add(rope)
db.add_or_update(bag_of_holding)
pole_from_bag = bag_of_holding.get(ten_foot_pole)
rope_from_bag = bag_of_holding.get(rope)
assert pole_from_bag.item == ten_foot_pole
assert pole_from_bag in bag_of_holding
assert pole_from_bag not in carl.equipment
@@ -176,11 +181,38 @@ def test_containers(db, carl):
assert carl.equipment.add(bag_of_holding)
db.add_or_update(bag_of_holding)
assert pole_from_bag in carl.equipment
assert rope_from_bag in carl.equipment
# test equality of mappings
carls_bag = carl.equipment.get(bag_of_holding)
carls_pole = carl.equipment.get(ten_foot_pole)
carls_rope = carl.equipment.get(rope)
assert carls_pole == pole_from_bag
assert carls_rope == rope_from_bag
# remove the pole from the bag
assert carls_bag.remove(pole_from_bag)
# use some rope
carls_rope.consume(10)
assert carls_rope.count == 40
# move the rope out of the bag of holding, but not the pole
assert carls_rope.move_to(carl.equipment)
assert carls_rope not in carls_bag
assert carls_pole in carls_bag
db.add_or_update(carl)
# get the db record anew, in case the in-memory representation isn't
# what's recorded in the database. Then make sure we didn't break
# anything by asserting we still only have 40ft of rope.
carl = db.Character.filter_by(name="carl").one()
assert carls_rope in carl.equipment
assert carls_rope not in carl.equipment.get(bag_of_holding)
assert carls_rope.count == 40
# old references are still valid
assert rope_from_bag == carls_rope
# use the rest of the rope
assert carls_rope.consume(40) == 0
print(rope_from_bag.inventory)
assert rope_from_bag not in carl.equipment
assert rope_from_bag not in carl.equipment.get(bag_of_holding)