Merchant inventory is well-saved

This commit is contained in:
Yohann D'ANELLO 2020-12-07 21:48:56 +01:00
parent 099508d4c0
commit 2bb99a707e
3 changed files with 23 additions and 4 deletions

View File

@ -23,8 +23,17 @@ class Merchant(FriendlyEntity):
super().__init__(name=name, *args, **kwargs)
self.inventory = inventory or []
self.hazel = hazel
for i in range(5):
self.inventory.append(choice(Item.get_all_items())())
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())())
def talk_to(self, player: Player) -> str:
"""
@ -34,6 +43,14 @@ class Merchant(FriendlyEntity):
# 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
class Sunflower(FriendlyEntity):
"""

View File

@ -30,9 +30,9 @@ class Player(FightingEntity):
self.current_xp = current_xp
self.max_xp = max_xp
self.inventory = inventory if inventory else list()
entity_classes = self.get_all_entity_classes_in_a_dict()
for i in range(len(self.inventory)):
if isinstance(self.inventory[i], dict):
entity_classes = self.get_all_entity_classes_in_a_dict()
item_class = entity_classes[self.inventory[i]["type"]]
self.inventory[i] = item_class(**self.inventory[i])
self.paths = dict()

View File

@ -362,7 +362,8 @@ class Entity:
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \
TeddyBear
from squirrelbattle.entities.friendly import Merchant, Sunflower
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, \
Heart, Sword
return {
"Tiger": Tiger,
"Bomb": Bomb,
@ -374,6 +375,7 @@ class Entity:
"Player": Player,
"Merchant": Merchant,
"Sunflower": Sunflower,
"Sword": Sword,
}
def save_state(self) -> dict: