"""협상 카드 스크립트 Repository — JSON 파일 기반 (DB 없음).""" import json from pathlib import Path from typing import Dict, List, Optional from common.paths import RESOURCES_DIR class CardScriptRepository: """협상 카드 스크립트를 resources/cards/nego_card.json 에서 관리한다.""" def __init__(self, json_file_path: Optional[str] = None): if json_file_path is None: json_file_path = RESOURCES_DIR / "cards" / "nego_card.json" self.json_file_path = Path(json_file_path) self._ensure_file_exists() def _ensure_file_exists(self): if not self.json_file_path.exists(): self.json_file_path.parent.mkdir(parents=True, exist_ok=True) self._save_to_file({"cards": []}) def _load_from_file(self) -> Dict: with open(self.json_file_path, "r", encoding="utf-8") as f: return json.load(f) def _save_to_file(self, data: Dict): with open(self.json_file_path, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) def find_all(self) -> List[Dict]: return self._load_from_file().get("cards", []) def find_by_id(self, card_id: str) -> Optional[Dict]: for card in self.find_all(): if card.get("card_id") == card_id: return card return None def exists(self, card_id: str) -> bool: return self.find_by_id(card_id) is not None def count(self) -> int: return len(self.find_all())