Add a InventoryHolder superclass for player and merchants

This commit is contained in:
Yohann D'ANELLO
2020-12-11 17:20:50 +01:00
parent 98b5dd64a8
commit 7179346e2b
4 changed files with 68 additions and 73 deletions

View File

@@ -1,18 +1,14 @@
from ..interfaces import FriendlyEntity
from ..interfaces import FriendlyEntity, InventoryHolder
from ..translations import gettext as _
from .player import Player
from .items import Item
from random import choice
from typing import Any
class Merchant(FriendlyEntity):
class Merchant(InventoryHolder, FriendlyEntity):
"""
The class for merchants in the dungeon
"""
inventory = list
hazel = int
def keys(self) -> list:
"""
Returns a friendly entitie's specific attributes
@@ -22,16 +18,9 @@ class Merchant(FriendlyEntity):
def __init__(self, name: str = "merchant", inventory: list = None,
hazel: int = 75, *args, **kwargs):
super().__init__(name=name, *args, **kwargs)
self.inventory = inventory or []
self.inventory = self.translate_inventory(inventory or [])
self.hazel = hazel
entity_classes = self.get_all_entity_classes_in_a_dict()
for i in range(len(self.inventory)):
if isinstance(self.inventory[i], dict):
item_class = entity_classes[self.inventory[i]["type"]]
self.inventory[i] = item_class(**self.inventory[i])
if not self.inventory:
for i in range(5):
self.inventory.append(choice(Item.get_all_items())())
@@ -41,29 +30,8 @@ class Merchant(FriendlyEntity):
This function is used to open the merchant's inventory in a menu,
and allow the player to buy/sell objects
"""
# TODO
return _("I don't sell any squirrel")
def save_state(self) -> dict:
"""
We save the inventory of the merchant formatted as JSON
"""
d = super().save_state()
d["inventory"] = [item.save_state() for item in self.inventory]
return d
def add_to_inventory(self, obj: Any) -> None:
"""
Adds an object to inventory
"""
self.inventory.append(obj)
def remove_from_inventory(self, obj: Any) -> None:
"""
Removes an object from the inventory
"""
self.inventory.remove(obj)
def change_hazel_balance(self, hz: int) -> None:
"""
Change the number of hazel the merchant has by hz.