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,6 +23,15 @@ class Merchant(FriendlyEntity):
super().__init__(name=name, *args, **kwargs) super().__init__(name=name, *args, **kwargs)
self.inventory = inventory or [] self.inventory = inventory or []
self.hazel = hazel 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): for i in range(5):
self.inventory.append(choice(Item.get_all_items())()) self.inventory.append(choice(Item.get_all_items())())
@ -34,6 +43,14 @@ class Merchant(FriendlyEntity):
# TODO # TODO
return _("I don't sell any squirrel") 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): class Sunflower(FriendlyEntity):
""" """

View File

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

View File

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