Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 464adfa338 | |||
| f571603866 | |||
| abb1631414 | |||
| 9e7968b6b3 | |||
| 3d43f3ee66 | |||
| 4583fcdea6 | |||
| 73b918f2f1 | |||
| 1c26070798 | |||
| 35ec79e7e5 | |||
| 4cfa6b72ff | |||
| 0d41da6544 | |||
| e8e6c02756 | |||
| f36c8f6b17 | |||
| 3bed4f9903 | |||
| 1f1b928fd7 | |||
| 4a06728381 | |||
| bd55060b97 | |||
| 32306f041a |
+2
-2
@@ -52,5 +52,5 @@ __pycache__/
|
|||||||
.spyproject/
|
.spyproject/
|
||||||
.pydevproject
|
.pydevproject
|
||||||
|
|
||||||
|
*.egg-info
|
||||||
|
build
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
# Changelog
|
||||||
@@ -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
|
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
isort $1
|
isort src tests
|
||||||
autopep8 -i --max-line-length=120 $1
|
autopep8 src tests
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
|
|
||||||
# def main_loop(breaker:bool):
|
|
||||||
# pass
|
|
||||||
|
|
||||||
# def menu ():
|
|
||||||
# print("Nouvelle partie :\t1")
|
|
||||||
# print("Charger :\t\t2")
|
|
||||||
# print("Option :\t\t3")
|
|
||||||
# print("Quitter :\t\t4")
|
|
||||||
# return int(input("Choix : "))
|
|
||||||
|
|
||||||
|
|
||||||
# choix = menu()
|
|
||||||
|
|
||||||
# match choix:
|
|
||||||
# case 1:
|
|
||||||
# print("Nouvelle partie")
|
|
||||||
# case 2:
|
|
||||||
# print("Charger")
|
|
||||||
# case 3:
|
|
||||||
# print("Option")
|
|
||||||
# case 4:
|
|
||||||
# print("Quitter")
|
|
||||||
# case _:
|
|
||||||
# print("Mauvais choix")
|
|
||||||
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
import random
|
|
||||||
from base_mob import BaseMob
|
|
||||||
|
|
||||||
# Créer des instances de BaseMob
|
|
||||||
yanaar = BaseMob("Yanaar", 15, 2, 1, 3, 2)
|
|
||||||
ulnarain = BaseMob("Ulnarain", 20, 3, 3, 2, 2)
|
|
||||||
skroll = BaseMob("Skroll", 25, 5, 3, 1, 2)
|
|
||||||
|
|
||||||
# Liste d'instances de BaseMob (au lieu d'un ensemble pour autoriser des doublons)
|
|
||||||
mobs_list = [yanaar, skroll, ulnarain]
|
|
||||||
|
|
||||||
|
|
||||||
def generate_random_mob_list(max_mobs):
|
|
||||||
"""
|
|
||||||
Génère une liste aléatoire de mobs à partir de la liste des instances disponibles.
|
|
||||||
|
|
||||||
:param max_mobs: Le nombre maximum de mobs dans la liste générée.
|
|
||||||
:return: Une liste de mobs sélectionnés aléatoirement.
|
|
||||||
"""
|
|
||||||
if max_mobs <= 0:
|
|
||||||
return []
|
|
||||||
|
|
||||||
num_mobs = random.randint(2, max_mobs)
|
|
||||||
selected_mobs = [random.choice(mobs_list) for _ in range(num_mobs)]
|
|
||||||
return selected_mobs
|
|
||||||
|
|
||||||
|
|
||||||
def generate_random_rooms(mobs_in_level, num_rooms):
|
|
||||||
"""
|
|
||||||
Génère un nombre spécifié de pièces et y distribue les mobs disponibles.
|
|
||||||
|
|
||||||
:param mobs_in_level: Liste des mobs disponibles pour le niveau.
|
|
||||||
:param num_rooms: Nombre total de pièces à générer.
|
|
||||||
:return: Une liste de pièces, chaque pièce contenant un ou plusieurs mobs, ou étant vide si aucun mob n'est disponible.
|
|
||||||
"""
|
|
||||||
rooms = []
|
|
||||||
|
|
||||||
for _ in range(num_rooms):
|
|
||||||
if len(mobs_in_level) > 0:
|
|
||||||
# Si des mobs sont disponibles, ajouter le premier mob à la pièce
|
|
||||||
new_room = []
|
|
||||||
for x in range(random.randint(1, len(mobs_in_level))):
|
|
||||||
new_room.append(mobs_in_level.pop(0))
|
|
||||||
rooms.append(new_room)
|
|
||||||
else:
|
|
||||||
# Ajouter une pièce vide si aucun mob n'est disponible
|
|
||||||
rooms.append(None)
|
|
||||||
|
|
||||||
return rooms
|
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
[project]
|
||||||
|
name = "elironwars-python"
|
||||||
|
version = "0.0.0"
|
||||||
|
description = "Lovely Spam! Wonderful Spam!"
|
||||||
|
readme = "README.md"
|
||||||
|
keywords = []
|
||||||
|
requires-python = ">= 3.14"
|
||||||
|
dependencies = [
|
||||||
|
"pydantic >= 2.13.4",
|
||||||
|
]
|
||||||
|
authors = [
|
||||||
|
{ name = "Raggaroth", email = "b.baudouin@pm.me" },
|
||||||
|
{ name = "Toucan", email = "anthony.melin@laposte.net" },
|
||||||
|
]
|
||||||
|
maintainers = [
|
||||||
|
{ name = "Raggaroth", email = "b.baudouin@pm.me" }
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.urls]
|
||||||
|
Homepage = "https://gitea.raggaroth-factory.fr/public/elironwars-python"
|
||||||
|
Documentation = "https://gitea.raggaroth-factory.fr/public/elironwars-python/wiki"
|
||||||
|
Repository = "https://gitea.raggaroth-factory.fr/public/elironwars-python.git"
|
||||||
|
Issues = "https://gitea.raggaroth-factory.fr/public/elironwars-python/issues"
|
||||||
|
Changelog = "https://github.com/me/spam/blob/master/CHANGELOG.md"
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["setuptools >= 77.0.3"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project.scripts]
|
||||||
|
elironwars = "elironwars.app:app"
|
||||||
|
|
||||||
|
[tool.isort]
|
||||||
|
profile = "black"
|
||||||
|
src_paths = ["src", "tests"]
|
||||||
|
known_first_party = ["elironwars"]
|
||||||
|
|
||||||
|
[tool.autopep8]
|
||||||
|
max_line_length = 120
|
||||||
|
in-place = true
|
||||||
|
recursive = true
|
||||||
@@ -1,53 +1,8 @@
|
|||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
from enum import Enum
|
|
||||||
|
|
||||||
from base_mob import BaseMob
|
from .level import Level
|
||||||
from character_builder import CharacterBuilder
|
from .menu import Action, action_menu
|
||||||
from level import Level
|
from .mob import BaseMob, load_character
|
||||||
|
|
||||||
|
|
||||||
class Action(Enum):
|
|
||||||
PIECE_SUIVANTE = (1, "Pièce suivante")
|
|
||||||
COMBAT = (2, "Combat")
|
|
||||||
FUIR = (3, "Fuir")
|
|
||||||
QUITTER = (4, "Quitter")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def code(self):
|
|
||||||
return self.value[0]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def libelle(self):
|
|
||||||
return self.value[1]
|
|
||||||
|
|
||||||
|
|
||||||
def clear_console() -> None:
|
|
||||||
os.system('cls' if os.name == 'nt' else 'clear')
|
|
||||||
|
|
||||||
|
|
||||||
def action_menu(actions_autorisees: list[Action] = None) -> Action:
|
|
||||||
"""Affiche le menu et retourne une action valide."""
|
|
||||||
|
|
||||||
actions_autorisees = list(Action) if actions_autorisees is None else actions_autorisees
|
|
||||||
|
|
||||||
while True:
|
|
||||||
print("\n------ Menu Action ------\n")
|
|
||||||
|
|
||||||
for action in actions_autorisees:
|
|
||||||
print(f"{action.libelle:<20} : {action.code}")
|
|
||||||
|
|
||||||
try:
|
|
||||||
choix = int(input("\nChoix : "))
|
|
||||||
clear_console()
|
|
||||||
for action in actions_autorisees:
|
|
||||||
if action.code == choix:
|
|
||||||
return action
|
|
||||||
|
|
||||||
print("Mauvais choix")
|
|
||||||
except ValueError:
|
|
||||||
clear_console()
|
|
||||||
print("Veuillez saisir un nombre.")
|
|
||||||
|
|
||||||
|
|
||||||
def play_room(room: list[BaseMob], level: Level) -> int:
|
def play_room(room: list[BaseMob], level: Level) -> int:
|
||||||
@@ -87,14 +42,22 @@ def play_donjon(level: Level) -> None:
|
|||||||
print(f"Au revoir {level.player.name} !")
|
print(f"Au revoir {level.player.name} !")
|
||||||
|
|
||||||
|
|
||||||
def app(character_name: str) -> None:
|
def app() -> None:
|
||||||
loaded_character = CharacterBuilder.load_character(character_name)
|
loaded_character = load_character(sys.argv[1] if len(sys.argv) > 1 else "Jamin")
|
||||||
if not loaded_character:
|
if not loaded_character:
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
print(loaded_character)
|
print(loaded_character)
|
||||||
|
|
||||||
level = Level(2, loaded_character)
|
# Créer des instances de BaseMob
|
||||||
|
yanaar = BaseMob("Yanaar", 15, 2, 1, 3, 2)
|
||||||
|
ulnarain = BaseMob("Ulnarain", 20, 3, 3, 2, 2)
|
||||||
|
skroll = BaseMob("Skroll", 25, 5, 3, 1, 2)
|
||||||
|
|
||||||
|
# Liste d'instances de BaseMob (au lieu d'un ensemble pour autoriser des doublons)
|
||||||
|
mobs_list = [yanaar, skroll, ulnarain]
|
||||||
|
|
||||||
|
level = Level(2, loaded_character, mobs_list)
|
||||||
print(f"Nombre de pièce dans le donjon : {len(level.rooms)}")
|
print(f"Nombre de pièce dans le donjon : {len(level.rooms)}")
|
||||||
|
|
||||||
play_donjon(level)
|
play_donjon(level)
|
||||||
@@ -102,4 +65,4 @@ def app(character_name: str) -> None:
|
|||||||
|
|
||||||
# Exemple d'utilisation
|
# Exemple d'utilisation
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app(character_name=sys.argv[1] if len(sys.argv) > 1 else "Jamin")
|
app()
|
||||||
@@ -1,19 +1,16 @@
|
|||||||
""" Class Level """
|
from .random_utils import generate_random_mob_list, generate_random_rooms
|
||||||
import random
|
|
||||||
|
|
||||||
import mobs_utils
|
|
||||||
|
|
||||||
|
|
||||||
class Level:
|
class Level:
|
||||||
""" Class Level """
|
""" Class Level """
|
||||||
|
|
||||||
def __init__(self, difficulty, player):
|
def __init__(self, difficulty, player, mob_list):
|
||||||
""" Chargement du niveau """
|
""" Chargement du niveau """
|
||||||
print("Loading Level")
|
print("Loading Level")
|
||||||
self.mobs_in_level = mobs_utils.generate_random_mob_list(random.randint(2, difficulty))
|
self.mobs_in_level = generate_random_mob_list(mob_list, difficulty)
|
||||||
self.exit_level = True
|
self.exit_level = True
|
||||||
self.player = player
|
self.player = player
|
||||||
self.rooms = mobs_utils.generate_random_rooms(self.mobs_in_level, difficulty) # Liste des pièces du niveau
|
self.rooms = generate_random_rooms(self.mobs_in_level, difficulty) # Liste des pièces du niveau
|
||||||
self.current_room_index = 0 # Index de la pièce actuelle
|
self.current_room_index = 0 # Index de la pièce actuelle
|
||||||
|
|
||||||
print("Level Loaded")
|
print("Level Loaded")
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import os
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
|
class Action(Enum):
|
||||||
|
PIECE_SUIVANTE = (1, "Pièce suivante")
|
||||||
|
COMBAT = (2, "Combat")
|
||||||
|
FUIR = (3, "Fuir")
|
||||||
|
QUITTER = (4, "Quitter")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def code(self):
|
||||||
|
return self.value[0]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def libelle(self):
|
||||||
|
return self.value[1]
|
||||||
|
|
||||||
|
|
||||||
|
def clear_console() -> None:
|
||||||
|
os.system('cls' if os.name == 'nt' else 'clear')
|
||||||
|
|
||||||
|
|
||||||
|
def action_menu(actions_autorisees: list[Action] = None) -> Action:
|
||||||
|
"""Affiche le menu et retourne une action valide."""
|
||||||
|
|
||||||
|
actions_autorisees = list(Action) if actions_autorisees is None else actions_autorisees
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("\n------ Menu Action ------\n")
|
||||||
|
|
||||||
|
for action in actions_autorisees:
|
||||||
|
print(f"{action.libelle:<20} : {action.code}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
choix = int(input("\nChoix : "))
|
||||||
|
clear_console()
|
||||||
|
for action in actions_autorisees:
|
||||||
|
if action.code == choix:
|
||||||
|
return action
|
||||||
|
|
||||||
|
print("Mauvais choix")
|
||||||
|
except ValueError:
|
||||||
|
clear_console()
|
||||||
|
print("Veuillez saisir un nombre.")
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -2,7 +2,7 @@ import pathlib
|
|||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from base_mob import BaseMob
|
from .base_mob import BaseMob
|
||||||
|
|
||||||
|
|
||||||
class CharacterModel(BaseModel):
|
class CharacterModel(BaseModel):
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
def generate_random_mob_list(mob_list, max_mobs):
|
||||||
|
"""
|
||||||
|
Génère une liste aléatoire de mobs à partir de la liste fournie.
|
||||||
|
|
||||||
|
:param mob_list: Liste de mob a choisir aleatoirement.
|
||||||
|
:param max_mobs: Le nombre maximum de mobs dans la liste générée.
|
||||||
|
:return: Une liste de mobs sélectionnés aléatoirement.
|
||||||
|
"""
|
||||||
|
if max_mobs <= 0:
|
||||||
|
return []
|
||||||
|
|
||||||
|
num_mobs = random.randint(2, max_mobs)
|
||||||
|
selected_mobs = [random.choice(mob_list) for _ in range(num_mobs)]
|
||||||
|
return selected_mobs
|
||||||
|
|
||||||
|
|
||||||
|
def generate_random_rooms(mobs_in_level, num_rooms):
|
||||||
|
"""
|
||||||
|
Génère un nombre spécifié de pièces et y distribue les mobs disponibles.
|
||||||
|
|
||||||
|
:param mobs_in_level: Liste des mobs disponibles pour le niveau.
|
||||||
|
:param num_rooms: Nombre total de pièces à générer.
|
||||||
|
:return: Une liste de pièces, chaque pièce contenant un ou plusieurs mobs, ou étant vide si aucun mob n'est disponible.
|
||||||
|
"""
|
||||||
|
rooms = []
|
||||||
|
|
||||||
|
for _ in range(num_rooms):
|
||||||
|
if len(mobs_in_level) > 0:
|
||||||
|
# Si des mobs sont disponibles, ajouter le premier mob à la pièce
|
||||||
|
new_room = []
|
||||||
|
for x in range(random.randint(1, len(mobs_in_level))):
|
||||||
|
new_room.append(mobs_in_level.pop(0))
|
||||||
|
rooms.append(new_room)
|
||||||
|
else:
|
||||||
|
# Ajouter une pièce vide si aucun mob n'est disponible
|
||||||
|
rooms.append(None)
|
||||||
|
|
||||||
|
return rooms
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
import unittest
|
|
||||||
from unittest.mock import patch
|
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from base_mob import BaseMob
|
from elironwars.mob import BaseMob, save_character, load_character
|
||||||
from character_builder import CharacterBuilder
|
from elironwars.mob.default_builder import build_character
|
||||||
|
|
||||||
ARTHUR_JSON = """{
|
ARTHUR_JSON = """{
|
||||||
"name": "Arthur",
|
"name": "Arthur",
|
||||||
@@ -37,7 +37,7 @@ class TestCharacterBuilder(unittest.TestCase):
|
|||||||
]
|
]
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
character = CharacterBuilder.build_character()
|
character = build_character()
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.assertIsInstance(character, BaseMob)
|
self.assertIsInstance(character, BaseMob)
|
||||||
@@ -60,7 +60,7 @@ class TestCharacterBuilder(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
CharacterBuilder.save_character(character)
|
save_character(character)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.assertTrue(ARTHUR_FILE.exists())
|
self.assertTrue(ARTHUR_FILE.exists())
|
||||||
@@ -68,7 +68,7 @@ class TestCharacterBuilder(unittest.TestCase):
|
|||||||
|
|
||||||
def test_load_character_file_not_found(self):
|
def test_load_character_file_not_found(self):
|
||||||
# Act
|
# Act
|
||||||
result = CharacterBuilder.load_character("Lancelot")
|
result = load_character("Lancelot")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
@@ -78,7 +78,7 @@ class TestCharacterBuilder(unittest.TestCase):
|
|||||||
ARTHUR_FILE.write_text(ARTHUR_JSON, encoding='utf-8')
|
ARTHUR_FILE.write_text(ARTHUR_JSON, encoding='utf-8')
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
character = CharacterBuilder.load_character("Arthur")
|
character = load_character("Arthur")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.assertIsNotNone(character)
|
self.assertIsNotNone(character)
|
||||||
@@ -101,7 +101,7 @@ class TestCharacterBuilder(unittest.TestCase):
|
|||||||
invalid_dict), encoding='utf-8')
|
invalid_dict), encoding='utf-8')
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
result = CharacterBuilder.load_character("Arthur")
|
result = load_character("Arthur")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
@@ -111,7 +111,7 @@ class TestCharacterBuilder(unittest.TestCase):
|
|||||||
ARTHUR_FILE.write_text(ARTHUR_JSON[1:], encoding='utf-8')
|
ARTHUR_FILE.write_text(ARTHUR_JSON[1:], encoding='utf-8')
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
result = CharacterBuilder.load_character("Arthur")
|
result = load_character("Arthur")
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
Reference in New Issue
Block a user