diff --git a/src/ttfrog/db/schema/__init__.py b/src/ttfrog/db/schema/__init__.py index d68feda..92e85e9 100644 --- a/src/ttfrog/db/schema/__init__.py +++ b/src/ttfrog/db/schema/__init__.py @@ -1,6 +1,7 @@ from .character import * from .classes import * from .constants import * +from .container import * from .log import * from .modifiers import * from .skill import * diff --git a/src/ttfrog/db/schema/character.py b/src/ttfrog/db/schema/character.py index eb8f080..fb24ac0 100644 --- a/src/ttfrog/db/schema/character.py +++ b/src/ttfrog/db/schema/character.py @@ -277,7 +277,6 @@ class Character(BaseObject, SlugMixin, ModifierMixin): def spells(self): return self.inventories[InventoryType.SPELL] - @property def prepared_spells(self): hashmap = dict([(mapping.item.name, mapping) for mapping in self.spells if mapping.prepared]) diff --git a/src/ttfrog/db/schema/container.py b/src/ttfrog/db/schema/container.py new file mode 100644 index 0000000..fdd93a3 --- /dev/null +++ b/src/ttfrog/db/schema/container.py @@ -0,0 +1,21 @@ +from sqlalchemy import ForeignKey +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from ttfrog.db.schema.inventory import Inventory, InventoryType +from ttfrog.db.schema.item import Item, ItemType + +__all__ = [ + "Container", +] + + +class Container(Item): + __tablename__ = "container" + __mapper_args__ = {"polymorphic_identity": ItemType.CONTAINER} + id: Mapped[int] = mapped_column(ForeignKey("item.id"), primary_key=True, init=False) + item_type: Mapped[ItemType] = ItemType.CONTAINER + inventory: Mapped["Inventory"] = relationship( + cascade="all,delete,delete-orphan", + lazy="immediate", + default_factory=lambda: Inventory(inventory_type=InventoryType.EQUIPMENT), + ) diff --git a/src/ttfrog/db/schema/inventory.py b/src/ttfrog/db/schema/inventory.py index f359890..cfdb870 100644 --- a/src/ttfrog/db/schema/inventory.py +++ b/src/ttfrog/db/schema/inventory.py @@ -32,16 +32,19 @@ def inventory_map_creator(fields): class Inventory(BaseObject): __tablename__ = "inventory" - __table_args__ = (UniqueConstraint("character_id", "inventory_type"),) + __table_args__ = (UniqueConstraint("character_id", "container_id", "inventory_type"),) id: Mapped[int] = mapped_column(init=False, primary_key=True, autoincrement=True) - character_id: Mapped[int] = mapped_column(ForeignKey("character.id")) inventory_type: Mapped[InventoryType] = mapped_column(nullable=False) items: Mapped[List["InventoryMap"]] = relationship( uselist=True, cascade="all,delete,delete-orphan", lazy="immediate", default_factory=lambda: [] ) + character_id: Mapped[int] = mapped_column(ForeignKey("character.id"), nullable=True, default=None) + container_id: Mapped[int] = mapped_column(ForeignKey("item.id"), nullable=True, default=None) + character = relationship("Character", init=False, viewonly=True, lazy="immediate") + container = relationship("Item", init=False, viewonly=True, lazy="immediate") def get(self, item): return self.get_all(item)[0] diff --git a/src/ttfrog/db/schema/item.py b/src/ttfrog/db/schema/item.py index d0c3acf..dcae91e 100644 --- a/src/ttfrog/db/schema/item.py +++ b/src/ttfrog/db/schema/item.py @@ -21,6 +21,7 @@ ITEM_TYPES = [ "WEAPON", "ARMOR", "SHIELD", + "CONTAINER", ] RECHARGE_TIMES = [