🏗️ ♻️ restructuration du projet et refactoring #3
@@ -2,8 +2,7 @@ import sys
|
||||
|
||||
from .level import Level
|
||||
from .menu import Action, action_menu
|
||||
from .mob.base_mob import BaseMob
|
||||
from .mob.character_builder import CharacterBuilder
|
||||
from .mob import BaseMob, load_character, save_character
|
||||
|
||||
|
||||
def play_room(room: list[BaseMob], level: Level) -> int:
|
||||
@@ -44,7 +43,7 @@ def play_donjon(level: Level) -> None:
|
||||
|
||||
|
||||
def app() -> None:
|
||||
loaded_character = CharacterBuilder.load_character(sys.argv[1] if len(sys.argv) > 1 else "Jamin")
|
||||
loaded_character = load_character(sys.argv[1] if len(sys.argv) > 1 else "Jamin")
|
||||
if not loaded_character:
|
||||
sys.exit(-1)
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import importlib.util
|
||||
import os
|
||||
import sys
|
||||
|
||||
from .base_mob import BaseMob
|
||||
|
||||
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':
|
||||
from .pydantic_builder import load_character, save_character
|
||||
else:
|
||||
from .default_builder import load_character, save_character
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
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
|
||||
@@ -0,0 +1,65 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
from .base_mob import BaseMob
|
||||
|
||||
|
||||
def build_character():
|
||||
"""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)
|
||||
|
||||
|
||||
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
|
||||
@@ -4,7 +4,7 @@ import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from elironwars.mob.base_mob import BaseMob
|
||||
from elironwars.mob.character_builder import CharacterBuilder
|
||||
from elironwars.mob.default_builder import CharacterBuilder
|
||||
|
||||
ARTHUR_JSON = """{
|
||||
"name": "Arthur",
|
||||
|
||||
Reference in New Issue
Block a user