34 lines
847 B
Python
34 lines
847 B
Python
# app/utils/spell_registry.py
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Dict, List, Optional
|
|
import yaml
|
|
|
|
@dataclass
|
|
class Spell:
|
|
id: str
|
|
name: str
|
|
school: str
|
|
element: Optional[str] = None
|
|
rank: int = 1
|
|
cost_mp: int = 0
|
|
description: str = ""
|
|
aoe: Optional[str] = None
|
|
|
|
class SpellRegistry:
|
|
def __init__(self) -> None:
|
|
self.by_id: Dict[str, Spell] = {}
|
|
|
|
def load_file(self, path: Path) -> None:
|
|
data = yaml.safe_load(path.read_text()) or []
|
|
for raw in data:
|
|
spell = Spell(**raw)
|
|
self.by_id[spell.id] = spell
|
|
|
|
def load_dir(self, root: Path) -> None:
|
|
for p in sorted(root.glob("*.y*ml")):
|
|
self.load_file(p)
|
|
|
|
def get(self, spell_id: str) -> Optional[Spell]:
|
|
return self.by_id.get(spell_id)
|