106 lines
3.7 KiB
Python
106 lines
3.7 KiB
Python
import importlib.util
|
|
import json
|
|
import os
|
|
import sys
|
|
from typing import Callable
|
|
|
|
from base_mob import BaseMob
|
|
|
|
|
|
class CharacterBuilder:
|
|
save_character_impl: Callable[[BaseMob], str]
|
|
load_character_impl: Callable[[str], None]
|
|
|
|
@classmethod
|
|
def build_character(cls):
|
|
"""Demande à l'utilisateur de saisir les attributs du personnage."""
|
|
print("Création d'un nouveau personnage :")
|
|
name = input("Nom du personnage : ")
|
|
max_pv = int(input("Points de vie maximum : "))
|
|
strength = int(input("Force : "))
|
|
protection = int(input("Protection : "))
|
|
speed = int(input("Vitesse : "))
|
|
mob_type = 0
|
|
return BaseMob(name, max_pv, strength, protection, speed, mob_type)
|
|
|
|
@classmethod
|
|
def save_character(cls, character: BaseMob) -> None:
|
|
"""Enregistre un personnage dans un fichier JSON."""
|
|
filename = cls.save_character_impl(character)
|
|
print(f"Personnage enregistré dans {filename}.")
|
|
|
|
@classmethod
|
|
def load_character(cls, name: str) -> BaseMob | None:
|
|
"""Charge un personnage à partir d'un fichier JSON."""
|
|
try:
|
|
return cls.load_character_impl(name)
|
|
except Exception as e:
|
|
return None
|
|
|
|
|
|
def save_character(character: BaseMob) -> str:
|
|
"""Enregistre un personnage dans un fichier JSON."""
|
|
character_data = {
|
|
"name": character.name,
|
|
"max_pv": character.max_pv,
|
|
"current_pv": character.current_pv,
|
|
"strength": character.strength,
|
|
"protection": character.protection,
|
|
"speed": character.speed,
|
|
"mob_type": character.mob_type
|
|
}
|
|
filename = character.name + ".json"
|
|
with open(filename, "w", encoding='UTF-8') as file:
|
|
json.dump(character_data, file, indent=4)
|
|
return filename
|
|
|
|
|
|
def load_character(name: str) -> BaseMob | None:
|
|
"""Charge un personnage à partir d'un fichier JSON."""
|
|
filename = name + ".json"
|
|
if not os.path.exists(filename):
|
|
print(f"Erreur : Le fichier {filename} n'existe pas.")
|
|
return None
|
|
|
|
try:
|
|
with open(filename, "r", encoding='UTF-8') as file:
|
|
character_data = json.load(file)
|
|
|
|
# Vérification des clés essentielles dans le JSON pour éviter les erreurs
|
|
required_keys = ["name", "max_pv", "current_pv",
|
|
"strength", "protection", "speed", "mob_type"]
|
|
if not all(key in character_data for key in required_keys):
|
|
print(f"Erreur : Le fichier {filename} ne contient pas toutes les clés nécessaires.")
|
|
return None
|
|
|
|
# Construction du personnage
|
|
return BaseMob(
|
|
name=character_data["name"],
|
|
max_pv=character_data["max_pv"],
|
|
strength=character_data["strength"],
|
|
protection=character_data["protection"],
|
|
speed=character_data["speed"],
|
|
mob_type=character_data["mob_type"]
|
|
)
|
|
except json.JSONDecodeError:
|
|
print(f"Erreur : Le fichier {filename} n'est pas un JSON valide.")
|
|
return None
|
|
|
|
|
|
pydantic = None
|
|
name = 'pydantic'
|
|
if name in sys.modules:
|
|
pydantic = sys.modules[name]
|
|
elif (spec := importlib.util.find_spec(name)) is not None:
|
|
pydantic = importlib.util.module_from_spec(spec)
|
|
sys.modules[name] = pydantic
|
|
spec.loader.exec_module(pydantic)
|
|
|
|
if pydantic and os.environ.get('ELIRON_BUILDER') == 'pydantic':
|
|
pydantic_character_builder = importlib.import_module('pydantic_character_builder')
|
|
CharacterBuilder.save_character_impl = pydantic_character_builder.save_character
|
|
CharacterBuilder.load_character_impl = pydantic_character_builder.load_character
|
|
else:
|
|
CharacterBuilder.save_character_impl = save_character
|
|
CharacterBuilder.load_character_impl = load_character
|