# Path: hubcave/game/models.py
# class InventoryItem(models.Model):
#     inventory = models.ForeignKey(Inventory, related_name="items")
#     item = models.ForeignKey(Item, related_name="i_instances")
# 
#     objects = InheritanceManager()
# 
# class StackableInventoryItem(InventoryItem):
#     count = models.IntegerField()


from hubcave.game.models import InventoryItem, StackableInventoryItem
"""
This is a hacky way of having items do arbitrary things
"""

def add_to_inventory(ns, item):
    def check_size():
        if len(InventoryItem.objects.filter(inventory=ns.inventory)) > 9:
            raise Exception("Can't add any more")


    if not item.item.stackable:
        check_size()
        new_item = InventoryItem.objects.create(inventory=ns.inventory,
                                                item=item.item)
    else:
        try:
            new_item = StackableInventoryItem.objects.get(
                inventory=ns.inventory,
                item=item.item)
            new_item.count += 1
            new_item.save()
        except StackableInventoryItem.DoesNotExist:
            if len(InventoryItem.objects.filter(inventory=ns.inventory)) + 1 > 9:
                check_size()

# Path: hubcave/game/models.py
# class InventoryItem(models.Model):
#     inventory = models.ForeignKey(Inventory, related_name="items")
#     item = models.ForeignKey(Item, related_name="i_instances")
# 
#     objects = InheritanceManager()
# 
# class StackableInventoryItem(InventoryItem):
#     count = models.IntegerField()


# Path: hubcave/game/item_effects.py
from hubcave.game.models import InventoryItem, StackableInventoryItem

"""
This is a hacky way of having items do arbitrary things
"""

def add_to_inventory(ns, item):
    def check_size():
        if len(InventoryItem.objects.filter(inventory=ns.inventory)) > 9:
            raise Exception("Can't add any more")


    if not item.item.stackable:
        check_size()
        new_item = InventoryItem.objects.create(inventory=ns.inventory,
                                                item=item.item)
    else:
        try:
            new_item = StackableInventoryItem.objects.get(
                inventory=ns.inventory,
                item=item.item)
            new_item.count += 1
            new_item.save()
        except StackableInventoryItem.DoesNotExist:
            if len(InventoryItem.objects.filter(inventory=ns.inventory)) + 1 > 9:
                check_size()

                new_item = StackableInventoryItem.objects.create(