make inventories recursive
This commit is contained in:
@@ -19,14 +19,16 @@ inventory_type_map = {
|
||||
ItemType.SHIELD,
|
||||
ItemType.ITEM,
|
||||
ItemType.SCROLL,
|
||||
ItemType.CONTAINER,
|
||||
],
|
||||
InventoryType.SPELL: [ItemType.SPELL],
|
||||
}
|
||||
|
||||
|
||||
def inventory_map_creator(fields):
|
||||
if isinstance(fields, InventoryMap):
|
||||
return fields
|
||||
# if isinstance(fields, InventoryMap):
|
||||
# return fields
|
||||
# return InventoryMap(**fields)
|
||||
return InventoryMap(**fields)
|
||||
|
||||
|
||||
@@ -36,7 +38,7 @@ class Inventory(BaseObject):
|
||||
id: Mapped[int] = mapped_column(init=False, primary_key=True, autoincrement=True)
|
||||
inventory_type: Mapped[InventoryType] = mapped_column(nullable=False)
|
||||
|
||||
items: Mapped[List["InventoryMap"]] = relationship(
|
||||
item_map: Mapped[List["InventoryMap"]] = relationship(
|
||||
uselist=True, cascade="all,delete,delete-orphan", lazy="immediate", default_factory=lambda: []
|
||||
)
|
||||
|
||||
@@ -46,11 +48,33 @@ class Inventory(BaseObject):
|
||||
character = relationship("Character", init=False, viewonly=True, lazy="immediate")
|
||||
container = relationship("Item", init=False, viewonly=True, lazy="immediate")
|
||||
|
||||
@property
|
||||
def items(self):
|
||||
return [mapping.item for mapping in self.item_map]
|
||||
|
||||
@property
|
||||
def all_items(self):
|
||||
def inventory_contents(inventory):
|
||||
for mapping in inventory.item_map:
|
||||
yield mapping
|
||||
if mapping.item.item_type == ItemType.CONTAINER:
|
||||
yield from inventory_contents(mapping.item.inventory)
|
||||
yield from inventory_contents(self)
|
||||
|
||||
@property
|
||||
def all_item_maps(self):
|
||||
def inventory_map(inventory):
|
||||
for mapping in inventory.item_map:
|
||||
yield mapping
|
||||
if mapping.item.item_type == ItemType.CONTAINER:
|
||||
yield from inventory_map(mapping.item.inventory)
|
||||
yield from inventory_map(self)
|
||||
|
||||
def get(self, item):
|
||||
return self.get_all(item)[0]
|
||||
|
||||
def get_all(self, item):
|
||||
return [mapping for mapping in self.items if mapping.item == item]
|
||||
return [mapping for mapping in self.all_item_maps if mapping.item == item]
|
||||
|
||||
def add(self, item):
|
||||
if item.item_type not in inventory_type_map[self.inventory_type]:
|
||||
@@ -60,23 +84,23 @@ class Inventory(BaseObject):
|
||||
mapping.count = item.count
|
||||
if item.charges:
|
||||
mapping.charges = [Charge(inventory_map_id=mapping.id) for i in range(item.charges)]
|
||||
self.items.append(mapping)
|
||||
self.item_map.append(mapping)
|
||||
return mapping
|
||||
|
||||
def remove(self, mapping):
|
||||
if mapping not in self.items:
|
||||
return False
|
||||
self.items.remove(mapping)
|
||||
return True
|
||||
if mapping in self.item_map:
|
||||
self.item_map.remove(mapping)
|
||||
return True
|
||||
return False
|
||||
|
||||
def __contains__(self, obj):
|
||||
for mapping in self.items:
|
||||
if mapping.item == obj:
|
||||
for item in self.all_items:
|
||||
if item == obj:
|
||||
return True
|
||||
return False
|
||||
|
||||
def __iter__(self):
|
||||
yield from self.items
|
||||
yield from self.all_items
|
||||
|
||||
|
||||
class InventoryMap(BaseObject):
|
||||
@@ -138,7 +162,7 @@ class InventoryMap(BaseObject):
|
||||
charges = item_property.charge_cost
|
||||
if len(avail) < charges:
|
||||
return False
|
||||
for charge in avail:
|
||||
for charge in avail[:charges]:
|
||||
charge.expended = True
|
||||
return True
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ class ItemProperty(BaseObject):
|
||||
id: Mapped[int] = mapped_column(init=False, primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String(collation="NOCASE"), nullable=False, unique=True)
|
||||
description: Mapped[str] = mapped_column(String, nullable=True, default=None)
|
||||
charge_cost: Mapped[int] = mapped_column(nullable=True, info={"min": 0}, default=None)
|
||||
charge_cost: Mapped[int] = mapped_column(nullable=True, info={"min": 1}, default=None)
|
||||
item_id: Mapped[int] = mapped_column(ForeignKey("item.id"), default=0)
|
||||
|
||||
# action/reaction/bonus
|
||||
|
||||
Reference in New Issue
Block a user