New crumbs collections classes for easy crumbs & inventory management

This commit is contained in:
Ben 2019-04-25 17:12:29 +01:00
parent b0f9e2125c
commit 62b9531711
8 changed files with 164 additions and 49 deletions

View File

@ -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')

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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,
class PenguinIgloo(db.Model):
__tablename__ = 'penguin_igloo'
PenguinID = db.Column(db.ForeignKey('penguin.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,
IglooID = db.Column(db.ForeignKey('igloo.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,
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)
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):

View File

@ -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,
class PuffleTreasureFurniture(db.Model):
__tablename__ = 'puffle_treasure_furniture'
PuffleID = db.Column(db.ForeignKey('puffle.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,
FurnitureID = db.Column(db.ForeignKey('furniture.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,
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)
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)

View File

@ -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):
@ -33,3 +34,10 @@ class CoverStamp(db.Model):
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"))
class StampCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None):
super().__init__(model=Stamp, key='ID', inventory_model=PenguinStamp,
inventory_id=inventory_id)

View File

@ -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