diff --git a/Houdini/Data/Igloo.py b/Houdini/Data/Igloo.py index 315aa8c..ac18b7c 100644 --- a/Houdini/Data/Igloo.py +++ b/Houdini/Data/Igloo.py @@ -1,4 +1,5 @@ -from Houdini.Data import db +from Houdini.Data import db, BaseCrumbsCollection +from Houdini.Data.Penguin import PenguinIgloo, PenguinLocation, PenguinFurniture class Flooring(db.Model): @@ -66,3 +67,30 @@ class Location(db.Model): Name = db.Column(db.String(50), nullable=False) Cost = db.Column(db.Integer, nullable=False, server_default=db.text("0")) Patched = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) + + +class IglooCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, inventory_id=None): + super().__init__(model=Igloo, key='ID', inventory_model=PenguinIgloo, + inventory_id=inventory_id) + + +class LocationCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, inventory_id=None): + super().__init__(model=Location, key='ID', inventory_model=PenguinLocation, + inventory_id=inventory_id) + + +class FurnitureCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, inventory_id=None): + super().__init__(model=Furniture, key='ID', inventory_model=PenguinFurniture, + inventory_id=inventory_id) + + +class FlooringCrumbsCollection(BaseCrumbsCollection): + + def __init__(self): + super().__init__(model=Flooring, key='ID') diff --git a/Houdini/Data/Item.py b/Houdini/Data/Item.py index c421ddc..a5a7d50 100644 --- a/Houdini/Data/Item.py +++ b/Houdini/Data/Item.py @@ -1,4 +1,5 @@ -from Houdini.Data import db +from Houdini.Data import db, BaseCrumbsCollection +from Houdini.Data.Penguin import PenguinItem class Item(db.Model): @@ -14,3 +15,10 @@ class Item(db.Model): EPF = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) Tour = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) ReleaseDate = db.Column(db.Date, nullable=False, server_default=db.text("now()")) + + +class ItemCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, model=Item, key='ID', inventory_model=PenguinItem, inventory_id=None): + super().__init__(model=model, key=key, inventory_model=inventory_model, + inventory_id=inventory_id) diff --git a/Houdini/Data/Mail.py b/Houdini/Data/Mail.py index 8fa3510..68864da 100644 --- a/Houdini/Data/Mail.py +++ b/Houdini/Data/Mail.py @@ -1,4 +1,5 @@ -from Houdini.Data import db +from Houdini.Data import db, BaseCrumbsCollection +from Houdini.Data.Penguin import PenguinPostcard class Postcard(db.Model): @@ -10,3 +11,8 @@ class Postcard(db.Model): Enabled = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) +class PostcardCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, inventory_id=None): + super().__init__(model=Postcard, key='ID', inventory_model=PenguinPostcard, + inventory_id=inventory_id) diff --git a/Houdini/Data/Ninja.py b/Houdini/Data/Ninja.py index 76b052a..9af7dcd 100644 --- a/Houdini/Data/Ninja.py +++ b/Houdini/Data/Ninja.py @@ -1,4 +1,5 @@ -from Houdini.Data import db +from Houdini.Data import db, BaseCrumbsCollection +from Houdini.Data.Penguin import PenguinCard class Card(db.Model): @@ -12,3 +13,10 @@ class Card(db.Model): Color = db.Column(db.CHAR(1), nullable=False, server_default=db.text("'b'::bpchar")) Value = db.Column(db.SmallInteger, nullable=False, server_default=db.text("2")) Description = db.Column(db.String(255), nullable=False, server_default=db.text("''::character varying")) + + +class CardCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, inventory_id=None): + super().__init__(model=Card, key='ID', inventory_model=PenguinCard, + inventory_id=inventory_id) diff --git a/Houdini/Data/Penguin.py b/Houdini/Data/Penguin.py index 5885236..bf4a131 100644 --- a/Houdini/Data/Penguin.py +++ b/Houdini/Data/Penguin.py @@ -86,31 +86,31 @@ class PuffleQuest(db.Model): CoinsCollected = db.Column(db.Boolean, nullable=False, server_default=db.text("false")) -t_penguin_igloo = db.Table( - 'penguin_igloo', db, - db.Column('PenguinID', db.ForeignKey('penguin.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False), - db.Column('IglooID', db.ForeignKey('igloo.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False) -) +class PenguinIgloo(db.Model): + __tablename__ = 'penguin_igloo' + + PenguinID = db.Column(db.ForeignKey('penguin.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) + IglooID = db.Column(db.ForeignKey('igloo.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) -t_penguin_item = db.Table( - 'penguin_item', db, - db.Column('PenguinID', db.ForeignKey('penguin.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False), - db.Column('ItemID', db.ForeignKey('item.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False) -) +class PenguinItem(db.Model): + __tablename__ = 'penguin_item' + + PenguinID = db.Column(db.ForeignKey('penguin.ID', ondelete='CASCADE', onupdate='CASCADE'), + primary_key=True, nullable=False) + ItemID = db.Column(db.ForeignKey('item.ID', ondelete='CASCADE', onupdate='CASCADE'), + primary_key=True, nullable=False) -t_penguin_location = db.Table( - 'penguin_location', db, - db.Column('PenguinID', db.ForeignKey('penguin.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False), - db.Column('LocationID', db.ForeignKey('location.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False) -) +class PenguinLocation(db.Model): + __tablename__ = 'penguin_location' + + PenguinID = db.Column(db.ForeignKey('penguin.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) + LocationID = db.Column(db.ForeignKey('location.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) class PenguinStamp(db.Model): diff --git a/Houdini/Data/Pet.py b/Houdini/Data/Pet.py index bf4f011..778f0d0 100644 --- a/Houdini/Data/Pet.py +++ b/Houdini/Data/Pet.py @@ -1,4 +1,5 @@ -from Houdini.Data import db +from Houdini.Data import db, BaseCrumbsCollection +from Houdini.Data.Penguin import PenguinPuffle, PenguinPuffleItem class Puffle(db.Model): @@ -32,28 +33,42 @@ class PuffleItem(db.Model): CleanEffect = db.Column(db.SmallInteger, nullable=False, server_default=db.text("0")) -t_puffle_treasure_furniture = db.Table( - 'puffle_treasure_furniture', db, - db.Column('PuffleID', db.ForeignKey('puffle.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False), - db.Column('FurnitureID', db.ForeignKey('furniture.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False) -) +class PuffleTreasureFurniture(db.Model): + __tablename__ = 'puffle_treasure_furniture' + + PuffleID = db.Column(db.ForeignKey('puffle.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) + FurnitureID = db.Column(db.ForeignKey('furniture.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) -t_puffle_treasure_item = db.Table( - 'puffle_treasure_item', db, - db.Column('PuffleID', db.ForeignKey('puffle.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False), - db.Column('ItemID', db.ForeignKey('item.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False) -) +class PuffleTreasureItem(db.Model): + __tablename__ = 'puffle_treasure_item' + + PuffleID = db.Column(db.ForeignKey('puffle.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) + ItemID = db.Column(db.ForeignKey('item.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) -t_puffle_treasure_puffle_item = db.Table( - 'puffle_treasure_puffle_item', db, - db.Column('PuffleID', db.ForeignKey('puffle.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False), - db.Column('PuffleItemID', db.ForeignKey('puffle_item.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, - nullable=False) -) \ No newline at end of file +class PuffleTreasurePuffleItem(db.Model): + __tablename__ = 'puffle_treasure_puffle_item' + + PuffleID = db.Column(db.ForeignKey('puffle.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) + PuffleItemID = db.Column(db.ForeignKey('puffle_item.ID', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, + nullable=False) + + +class PuffleCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, inventory_id=None): + super().__init__(model=Puffle, key='ID', inventory_model=PenguinPuffle, + inventory_id=inventory_id) + + +class PuffleItemCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, inventory_id=None): + super().__init__(model=PuffleItem, key='ID', inventory_model=PenguinPuffleItem, + inventory_id=inventory_id) diff --git a/Houdini/Data/Stamp.py b/Houdini/Data/Stamp.py index e738018..972531e 100644 --- a/Houdini/Data/Stamp.py +++ b/Houdini/Data/Stamp.py @@ -1,4 +1,5 @@ -from Houdini.Data import db +from Houdini.Data import db, BaseCrumbsCollection +from Houdini.Data.Penguin import PenguinStamp class Stamp(db.Model): @@ -32,4 +33,11 @@ class CoverStamp(db.Model): Y = db.Column(db.SmallInteger, nullable=False, server_default=db.text("0")) Type = db.Column(db.SmallInteger, nullable=False, server_default=db.text("0")) Rotation = db.Column(db.SmallInteger, nullable=False, server_default=db.text("0")) - Depth = db.Column(db.SmallInteger, nullable=False, server_default=db.text("0")) \ No newline at end of file + Depth = db.Column(db.SmallInteger, nullable=False, server_default=db.text("0")) + + +class StampCrumbsCollection(BaseCrumbsCollection): + + def __init__(self, inventory_id=None): + super().__init__(model=Stamp, key='ID', inventory_model=PenguinStamp, + inventory_id=inventory_id) diff --git a/Houdini/Data/__init__.py b/Houdini/Data/__init__.py index 0a70952..f614f6f 100644 --- a/Houdini/Data/__init__.py +++ b/Houdini/Data/__init__.py @@ -2,3 +2,45 @@ from gino import Gino db = Gino() + +class BaseCrumbsCollection(dict): + + def __init__(self, *args, **kwargs): + super().__init__() + + self._db = db + self._model = kwargs.get('model') + self._key = kwargs.get('key') + self._inventory_model = kwargs.get('inventory_model') + self._inventory_id = kwargs.get('inventory_id') + + self._is_inventory = self._inventory_model is not None and self._inventory_id is not None + + async def get(self, k): + if self._is_inventory: + return self[k] + try: + return self[k] + except KeyError as e: + result = await self._model.query.where(getattr(self._model, self._key) == k).gino.first() + if result: + self[k] = result + return result + raise e + + async def __collect(self): + query = self._model.load(parent=self._inventory_model).where( + self._inventory_model.PenguinID == self._inventory_id + ) if self._is_inventory else self._model.query + + async with db.transaction(): + collected = query.gino.iterate() + self.update( + {getattr(model_instance, self._key): model_instance async for model_instance in collected} + ) + + @classmethod + async def get_collection(cls, *args, **kwargs): + cc = cls(*args, **kwargs) + await cc.__collect() + return cc