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
+2 -1
View File
@@ -1,8 +1,9 @@
from typing import Union
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import base as sa_base
from sqlalchemy.orm import mapped_column, relationship
from ttfrog.db.schema.inventory import Inventory, InventoryMap, InventoryType
from ttfrog.db.schema.item import Item, ItemType
+18 -3
View File
@@ -1,8 +1,9 @@
from typing import List, Union
from sqlalchemy import ForeignKey, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import base as sa_base
from sqlalchemy.orm import mapped_column, relationship
from ttfrog.db.base import BaseObject, EnumField
from ttfrog.db.schema.item import Item, ItemProperty, ItemType
@@ -145,11 +146,25 @@ class InventoryMap(BaseObject):
self.attuned = False
return True
def move_to(self, inventory):
if inventory == self.inventory:
return False
self.inventory.remove(self)
self.inventory = inventory
inventory.item_map.append(self)
return True
def __getattr__(self, name: str):
if name == sa_base.DEFAULT_STATE_ATTR:
raise AttributeError()
return getattr(self.item, name)
def __contains__(self, obj):
if self.item.item_type == ItemType.CONTAINER:
return obj in self.item.inventory
raise RuntimeException("Item {self.item.name} is not a container.")
class Inventory(BaseObject):
__tablename__ = "inventory"
__table_args__ = (UniqueConstraint("character_id", "container_id", "inventory_type"),)
@@ -177,6 +192,7 @@ class Inventory(BaseObject):
yield mapping
if mapping.item.item_type == ItemType.CONTAINER:
yield from inventory_contents(mapping.item.inventory)
yield from inventory_contents(self)
@property
@@ -186,6 +202,7 @@ class Inventory(BaseObject):
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):
@@ -222,8 +239,6 @@ class Inventory(BaseObject):
yield from self.all_items
class Charge(BaseObject):
__tablename__ = "charge"
id: Mapped[int] = mapped_column(init=False, primary_key=True, autoincrement=True)