#!/usr/bin/env python """Append a per-card REAL cast/play rate table to the report's data_review.md. Reads value//cast_stats.json (produced by build_cast_stats.py) and writes a "## 单卡施放率" section into value//reports/data_review.md, replacing any previous one (idempotent, delimited by markers). """ from __future__ import annotations import argparse import json from pathlib import Path MARK_BEGIN = "" MARK_END = "" def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--snapshot", required=True) ap.add_argument("--out", required=True) args = ap.parse_args() snapshot = Path(args.snapshot) out = Path(args.out) stats = json.loads((out / "cast_stats.json").read_text(encoding="utf-8")) cards = {c["name"]: c for c in json.loads((snapshot / "stats/card_data.json").read_text(encoding="utf-8"))["data"]} rows = [] for nm, s in stats.items(): c = cards.get(nm) or {} is_land = any("Land" in t for t in (c.get("types") or [])) use = s.get("play", 0) if is_land else s.get("cast", 0) g = s.get("games", 0) if not g: continue games_with = s.get("play_games", 0) if is_land else s.get("cast_games", 0) rows.append((nm, c.get("rarity") or "", "地" if is_land else "咒语", use, g, use / g, games_with / g)) rows.sort(key=lambda r: -r[5]) lines = [MARK_BEGIN, "", "## 5. 单卡施放率(REPLAY 实测)", "", "由 `scripts/build_cast_stats.py` 解析 17lands GRE replays 得到:", "**施放** = `GameObjectType_Card` 进入 Stack(zoneId 27,我方);", "**地** = 地牌进入 Battlefield(zoneId 28)。", "", "公式:`次/局 = 使用次数 / 该牌被带进比赛的局数`(REPLAY 实测,共 " f"{len(rows)} 张牌)。", "「出现局%」= 至少有 1 次施放/落场的对局占比。", "", "| # | 牌 | 稀有度 | 类型 | 使用次数 | 带进局数 | 次/局 | 出现局% |", "|---:|---|---|---|---:|---:|---:|---:|"] for i, (nm, rar, kind, use, g, rate, pct) in enumerate(rows, 1): lines.append(f"| {i} | {nm} | {rar} | {kind} | {use} | {g} | {rate:.2f} | {pct*100:.0f}% |") lines += ["", MARK_END, ""] section = "\n".join(lines) rp = out / "reports" / "data_review.md" text = rp.read_text(encoding="utf-8") if rp.exists() else "# 数据体检\n" if MARK_BEGIN in text and MARK_END in text: head = text[:text.index(MARK_BEGIN)] tail = text[text.index(MARK_END) + len(MARK_END):] text = head + section + tail else: text = text.rstrip() + "\n\n" + section + "\n" rp.parent.mkdir(parents=True, exist_ok=True) rp.write_text(text, encoding="utf-8") print(f"wrote {len(rows)} rows -> {rp}") return 0 if __name__ == "__main__": raise SystemExit(main())