"""Old-world archive (Section 10). ``worlds/`` retains every generated world; ``worlds/index.json`` is the append-only index. Summaries feed the author as divergence context so new worlds don't reuse premises/settings/twists. """ from __future__ import annotations import json from pathlib import Path from typing import Any class Archive: def __init__(self, worlds_dir: Path) -> None: self.worlds_dir = worlds_dir self.index_path = worlds_dir / "index.json" self.worlds_dir.mkdir(parents=True, exist_ok=True) def entries(self) -> list[dict[str, Any]]: if not self.index_path.exists(): return [] data: list[dict[str, Any]] = json.loads(self.index_path.read_text("utf-8")) return data def append(self, entry: dict[str, Any]) -> None: entries = self.entries() entries.append(entry) self.index_path.write_text(json.dumps(entries, indent=2), "utf-8") def bump_play_count(self, world_id: str) -> None: entries = self.entries() for e in entries: if e.get("world_id") == world_id: e["play_count"] = int(e.get("play_count", 0)) + 1 self.index_path.write_text(json.dumps(entries, indent=2), "utf-8") def divergence_summaries(self) -> list[dict[str, str]]: return [ { "one_line": e.get("one_line", ""), "setting": e.get("setting", ""), "twist_tag": e.get("twist_tag", ""), } for e in self.entries() ]