added inventory management ui
This commit is contained in:
@@ -341,6 +341,18 @@ class Character(BaseObject, SlugMixin, ModifierMixin):
|
||||
def equipment(self):
|
||||
return [inv for inv in self._inventories if inv.inventory_type == InventoryType.EQUIPMENT][0]
|
||||
|
||||
def equip(self, mapping):
|
||||
if mapping.equipped:
|
||||
return False
|
||||
mapping.equipped = True
|
||||
return True
|
||||
|
||||
def unequip(self, mapping):
|
||||
if not mapping.equipped:
|
||||
return False
|
||||
mapping.equipped = False
|
||||
return True
|
||||
|
||||
@property
|
||||
def spells(self):
|
||||
return [inv for inv in self._inventories if inv.inventory_type == InventoryType.SPELL][0]
|
||||
|
||||
@@ -13,7 +13,7 @@ class InventoryType(EnumField):
|
||||
inventory_type_map = {
|
||||
InventoryType.EQUIPMENT: [
|
||||
ItemType.ITEM,
|
||||
ItemType.SPELL,
|
||||
ItemType.SCROLL,
|
||||
],
|
||||
InventoryType.SPELL: [
|
||||
ItemType.SPELL
|
||||
@@ -34,16 +34,47 @@ class Inventory(BaseObject):
|
||||
character_id: Mapped[int] = mapped_column(ForeignKey("character.id"))
|
||||
inventory_type: Mapped[InventoryType] = mapped_column(nullable=False)
|
||||
|
||||
_inventory_map = relationship("InventoryMap", lazy="immediate", uselist=True)
|
||||
_inventory_map = relationship("InventoryMap", lazy="immediate", uselist=True, cascade="all,delete,delete-orphan")
|
||||
inventory_map = association_proxy("_inventory_map", "id", creator=inventory_map_creator)
|
||||
|
||||
def get(self, item):
|
||||
return [mapping for mapping in self._inventory_map if mapping.item == item]
|
||||
|
||||
def add(self, item):
|
||||
if item.item_type not in inventory_type_map[self.inventory_type]:
|
||||
return False
|
||||
self.inventory_map.append(InventoryMap(inventory_id=self.id, item_id=item.id))
|
||||
mapping = InventoryMap(inventory_id=self.id, item_id=item.id)
|
||||
if item.consumable:
|
||||
mapping.count = item.count
|
||||
self.inventory_map.append(mapping)
|
||||
return mapping
|
||||
|
||||
def remove(self, mapping):
|
||||
if mapping.id not in self.inventory_map:
|
||||
return False
|
||||
self.inventory_map.remove(mapping.id)
|
||||
return True
|
||||
|
||||
def equip(self, mapping):
|
||||
if mapping.equipped:
|
||||
return False
|
||||
mapping.equipped = True
|
||||
return True
|
||||
|
||||
def unequip(self, mapping):
|
||||
if not mapping.equipped:
|
||||
return False
|
||||
mapping.equipped = False
|
||||
return True
|
||||
|
||||
def __contains__(self, obj):
|
||||
for mapping in self._inventory_map:
|
||||
if mapping.item == obj:
|
||||
return True
|
||||
return False
|
||||
|
||||
def __iter__(self):
|
||||
yield from [mapping.item for mapping in self._inventory_map]
|
||||
yield from self._inventory_map
|
||||
|
||||
|
||||
class InventoryMap(BaseObject):
|
||||
@@ -52,6 +83,20 @@ class InventoryMap(BaseObject):
|
||||
inventory_id: Mapped[int] = mapped_column(ForeignKey("inventory.id"))
|
||||
item_id: Mapped[int] = mapped_column(ForeignKey("item.id"))
|
||||
item: Mapped["Item"] = relationship(uselist=False, lazy="immediate", viewonly=True, init=False)
|
||||
inventory: Mapped["Inventory"] = relationship(uselist=False, viewonly=True, init=False)
|
||||
|
||||
equipped: Mapped[bool] = mapped_column(default=False)
|
||||
count: Mapped[int] = mapped_column(nullable=False, default=1)
|
||||
|
||||
def use(self, count=1):
|
||||
if count < 0:
|
||||
return False
|
||||
if not self.item.consumable:
|
||||
return False
|
||||
if self.count < count:
|
||||
return False
|
||||
self.count -= count
|
||||
if self.count == 0:
|
||||
self.inventory.remove(self)
|
||||
return 0
|
||||
return self.count
|
||||
|
||||
@@ -13,6 +13,7 @@ __all__ = [
|
||||
class ItemType(EnumField):
|
||||
ITEM = "ITEM"
|
||||
SPELL = "SPELL"
|
||||
SCROLL = "SCROLL"
|
||||
|
||||
|
||||
class Item(BaseObject, ConditionMixin):
|
||||
@@ -24,6 +25,7 @@ class Item(BaseObject, ConditionMixin):
|
||||
id: Mapped[int] = mapped_column(init=False, primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String(collation="NOCASE"), nullable=False, unique=True)
|
||||
consumable: Mapped[bool] = mapped_column(default=False)
|
||||
count: Mapped[int] = mapped_column(nullable=False, default=1)
|
||||
item_type: Mapped[ItemType] = mapped_column(default=ItemType.ITEM, nullable=False)
|
||||
|
||||
|
||||
@@ -32,6 +34,7 @@ class Spell(Item):
|
||||
__mapper_args__ = {
|
||||
"polymorphic_identity": ItemType.SPELL
|
||||
}
|
||||
id: Mapped[int] = mapped_column(ForeignKey("item.id"), primary_key=True)
|
||||
id: Mapped[int] = mapped_column(ForeignKey("item.id"), primary_key=True, init=False)
|
||||
level: Mapped[int] = mapped_column(nullable=False, info={"min": 0, "max": 9}, default=0)
|
||||
concentration: Mapped[bool] = mapped_column(default=False)
|
||||
item_type: Mapped[ItemType] = mapped_column(default=ItemType.SPELL, init=False)
|
||||
|
||||
Reference in New Issue
Block a user