#!/usr/bin/env python """Build a per-draft "cards that were offered" index from 17lands draftdata. For each replay in /draftdata/.json we union every entry of ``picks[].available[]`` (the pack contents the seat actually saw) into a set of card names, then cache ``{draft_id: [names...]}`` to value//offer_index.json. This lets the template builder compute a rarity-fair inclusion: P(card is played | card was offered to this seat) which removes the bias that rare/mythic cards are simply never passed to you. """ from __future__ import annotations import argparse import json from pathlib import Path def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--snapshot", required=True, help="e.g. data/17lands/FRA20261007") ap.add_argument("--out", required=True, help="e.g. value/FRA20261007") ap.add_argument("--force", action="store_true") args = ap.parse_args() snapshot = Path(args.snapshot) out = Path(args.out) dest = out / "offer_index.json" if dest.exists() and not args.force: print(f"exists, skip: {dest} (use --force to rebuild)") return 0 ddir = snapshot / "draftdata" files = sorted(ddir.glob("*.json")) index: dict[str, list[str]] = {} for i, f in enumerate(files, 1): try: d = json.loads(f.read_text(encoding="utf-8")) except Exception as e: # noqa: BLE001 print(f" !! {f.name}: {e}") continue seen: set[str] = set() for pk in d.get("picks") or []: for c in pk.get("available") or []: nm = c.get("name") if nm: seen.add(nm) if seen: index[f.stem] = sorted(seen) if i % 100 == 0: print(f" {i}/{len(files)}") out.mkdir(parents=True, exist_ok=True) dest.write_text(json.dumps(index, ensure_ascii=False), encoding="utf-8") avg = (sum(len(v) for v in index.values()) / len(index)) if index else 0 print(f"wrote {len(index)} drafts (avg {avg:.1f} cards offered) -> {dest}") return 0 if __name__ == "__main__": raise SystemExit(main())