Add TRK set preview and refresh draft analysis
Track the TRK preview package and the newest value artifacts for FRA20261007, move the 17lands API notes under data, and ignore downloaded 17lands, card image and live directories.
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compute archetype matchup matrices from game results.
|
||||
|
||||
Our side = the template (cluster) the draft belongs to (via templates/_members.json);
|
||||
opponent side = opponent_colors collapsed to main pair.
|
||||
|
||||
NOTE: every deck here is a 7-win trophy deck, so these win rates are *conditional
|
||||
on our deck being a trophy deck* — read them as relative matchup strength.
|
||||
|
||||
Outputs:
|
||||
value/<SNAPSHOT>/matchups/FRA.PremierDraft.matrix.json
|
||||
value/<SNAPSHOT>/reports/matchups.md
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
from collections import Counter, defaultdict
|
||||
from pathlib import Path
|
||||
|
||||
PAIRS = ["WU", "UB", "BR", "RG", "WG", "WB", "BG", "UR", "UG", "WR"]
|
||||
Z = 1.959964
|
||||
|
||||
|
||||
RANK_ORDER = {"Bronze": 0, "Silver": 1, "Gold": 2, "Platinum": 3, "Diamond": 4, "Mythic": 5}
|
||||
RANK_MIN = 2 # Gold
|
||||
|
||||
|
||||
def rank_ok(s):
|
||||
return RANK_ORDER.get((s or "").split("-")[0], 99) >= RANK_MIN
|
||||
|
||||
|
||||
def load_jsonl(path: Path):
|
||||
with path.open(encoding="utf-8") as fh:
|
||||
for line in fh:
|
||||
line = line.strip()
|
||||
if line:
|
||||
yield json.loads(line)
|
||||
|
||||
|
||||
def wilson(w, n, z=Z):
|
||||
if not n:
|
||||
return [None, None]
|
||||
p = w / n
|
||||
d = 1 + z * z / n
|
||||
c = p + z * z / (2 * n)
|
||||
m = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n))
|
||||
return [round((c - m) / d, 3), round((c + m) / d, 3)]
|
||||
|
||||
|
||||
def main_pair(colors):
|
||||
if not colors:
|
||||
return "?"
|
||||
up = [c for c in colors if c in "WUBRG"]
|
||||
if len(up) >= 2:
|
||||
return "".join(sorted(up[:2], key="WUBRG".index))
|
||||
if len(up) == 1:
|
||||
return up[0]
|
||||
return "?"
|
||||
|
||||
|
||||
def build(snapshot: Path, out_dir: Path) -> None:
|
||||
_sm = re.match(r"([A-Za-z]+)", out_dir.name)
|
||||
SET = (_sm.group(1) if _sm else "FRA").upper()
|
||||
members = json.loads((out_dir / "templates/_members.json").read_text(encoding="utf-8"))
|
||||
index = json.loads((out_dir / "templates/_index.json").read_text(encoding="utf-8"))
|
||||
draft_to_slug = {did: slug for slug, ids in members.items() for did in ids}
|
||||
|
||||
cell = defaultdict(lambda: [0, 0]) # (slug, opp_pair) -> [n, wins]
|
||||
paircell = defaultdict(lambda: [0, 0]) # (our_pair, opp_pair) -> [n, wins]
|
||||
games = 0
|
||||
for rec in load_jsonl(snapshot / "drafts/event_details.jsonl"):
|
||||
slug = draft_to_slug.get(rec["draft_id"])
|
||||
our_pair = index.get(slug, {}).get("archetype")
|
||||
for m in rec.get("details", {}).get("match_results") or []:
|
||||
for g in m.get("game_results") or []:
|
||||
if not rank_ok(g.get("start_rank")):
|
||||
continue
|
||||
opp = main_pair(g.get("opponent_colors"))
|
||||
won = 1 if g.get("won") else 0
|
||||
games += 1
|
||||
if slug:
|
||||
c = cell[(slug, opp)]
|
||||
c[0] += 1
|
||||
c[1] += won
|
||||
if our_pair:
|
||||
c = paircell[(our_pair, opp)]
|
||||
c[0] += 1
|
||||
c[1] += won
|
||||
|
||||
def matrix(keys, cells, key_fn):
|
||||
out = {}
|
||||
for k in keys:
|
||||
row = {}
|
||||
for opp in PAIRS + ["?"]:
|
||||
c = cells.get(key_fn(k, opp))
|
||||
if c and c[0] >= 10:
|
||||
row[opp] = {"winrate": round(c[1] / c[0], 3), "ci": wilson(c[1], c[0]), "n": c[0]}
|
||||
out[k] = row
|
||||
return out
|
||||
|
||||
slug_keys = [s for s in index]
|
||||
m_full = matrix(slug_keys, cell, lambda s, o: (s, o))
|
||||
m_pair = matrix(PAIRS, paircell, lambda s, o: (s, o))
|
||||
|
||||
(out_dir / "matchups").mkdir(parents=True, exist_ok=True)
|
||||
(out_dir / f"matchups/{SET}.PremierDraft.matrix.json").write_text(json.dumps({
|
||||
"expansion": SET, "format": "PremierDraft",
|
||||
"note": "trophy-conditional: our side is always a 7-win deck; read as relative strength",
|
||||
"games": games,
|
||||
"archetypes": slug_keys,
|
||||
"matrix": m_full,
|
||||
"color_pair_matrix": m_pair,
|
||||
}, ensure_ascii=False, indent=1), encoding="utf-8")
|
||||
|
||||
# report
|
||||
rep = [f"# {SET} PremierDraft · 克制矩阵(相对强度)\n",
|
||||
f"{games} 局;我方均为 7 胜牌表 → 绝对胜率偏高,**看相对差异**。\n",
|
||||
"## 双色对 × 双色对(我方胜率 / n)\n",
|
||||
"| 我方 | " + " | ".join(PAIRS) + " |",
|
||||
"|---|" + "---|" * len(PAIRS)]
|
||||
for me in PAIRS:
|
||||
row = [me]
|
||||
for opp in PAIRS:
|
||||
c = paircell.get((me, opp))
|
||||
row.append(f"{c[1]/c[0]*100:.0f}% ({c[0]})" if c and c[0] >= 10 else "-")
|
||||
rep.append("| " + " | ".join(row) + " |")
|
||||
(out_dir / "reports").mkdir(parents=True, exist_ok=True)
|
||||
(out_dir / "reports/matchups.md").write_text("\n".join(rep), encoding="utf-8")
|
||||
filled = sum(1 for r in m_full.values() for _ in r)
|
||||
print(f"games={games}; template cells(>=10)={filled}; wrote matchups/{SET}.PremierDraft.matrix.json + reports/matchups.md")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--snapshot", required=True)
|
||||
ap.add_argument("--out", required=True)
|
||||
a = ap.parse_args()
|
||||
build(Path(a.snapshot), Path(a.out))
|
||||
Reference in New Issue
Block a user