Files

82 lines
4.3 KiB
Python

"""Aus geloggten Turn/River-Spots die frueheren Postflop-Entscheidungen des Helden nachbauen."""
import json, copy
STR = ["PREFLOP", "FLOP", "TURN", "RIVER"]
BL = {"FLOP": 3, "TURN": 4, "RIVER": 5}
AGG = ("BET", "RAISE", "ALL_IN")
def beitraege(hist):
"""je (strasse, sitz) der 'bis'-Betrag (amounts sind Strassen-Summen)."""
b = {}
for r in hist:
if r.get("splash"): # totes Geld (spot_from_log): zaehlt im Topf, nicht beim Sitz
continue
if r.get("amount") is not None and r["action"] != "FOLD":
k = (r["street"], r["seat"])
b[k] = max(b.get(k, 0.0), float(r["amount"]))
return b
def summe(b, seat=None, street=None):
return sum(v for (s, se), v in b.items() if (seat is None or se == seat) and (street is None or s == street))
def rueckbau(spot):
hist = spot["history"]; held = spot["heroSeat"]; bb = spot["bigBlind"]
alle = beitraege(hist)
aus = []
for i, r in enumerate(hist):
if r["seat"] != held or r["street"] == "PREFLOP":
continue
vor = hist[:i]; b = beitraege(vor); st = r["street"]
s = copy.deepcopy(spot)
s["history"] = vor; s["street"] = st
s["board"] = {k: v for k, v in spot["board"].items() if STR.index(k) <= STR.index(st)}
s["pot"] = round(summe(b) + sum(float(x.get("amount") or 0) for x in hist if x.get("splash")), 4)
hoch = max([v for (x, _), v in b.items() if x == st] + [0.0])
mein = b.get((st, held), 0.0)
stack = spot["heroStack"] + summe(alle, held) - summe(b, held)
s["heroStack"] = round(stack, 4)
s["toCall"] = round(min(hoch - mein, stack), 4)
if s["toCall"] > 0:
s["legalActions"] = ["FOLD", "CALL"] + (["RAISE"] if stack > s["toCall"] else [])
else:
s["legalActions"] = ["FOLD", "CHECK", "RAISE"]
# Mindest-Raise: hoechster Betrag + letzte Erhoehung (mind. 1 BB); hoechstens alles
stufen = sorted({v for (x, _), v in b.items() if x == st} | {0.0})
schritt = max(bb, (stufen[-1] - stufen[-2]) if len(stufen) > 1 else bb)
s["minRaiseTo"] = round(min(hoch + schritt, mein + stack), 4); s["maxRaiseTo"] = round(mein + stack, 4)
gefoldet = {x["seat"] for x in vor if x["action"] == "FOLD"}
for v in s["villains"]:
v["folded"] = v["seat"] in gefoldet
v["stack"] = round(v["stack"] + summe(alle, v["seat"]) - summe(b, v["seat"]), 4)
gegen_raise = any(x["seat"] == held and x["street"] == st and x["action"] in AGG for x in vor) and s["toCall"] > 0
aus.append({"idx": i, "street": st, "live": r["action"], "live_betrag": r.get("amount"), "gegen_raise": gegen_raise, "spot": s})
return aus
if __name__ == "__main__":
import sys, subprocess, os
exe = os.path.abspath(sys.argv[1]); env = dict(os.environ); env.update(dict(kv.split("=") for kv in sys.argv[2:]))
bbv = 0.02; netto = {}
for z in open("archiv/lauf/haende.jsonl", encoding="utf-8"):
if '14R NL' in z and '2026-11-14' in z:
h = json.loads(z); netto[h["hand"]] = h["netto"] / bbv
zs = [json.loads(z) for z in open("archiv/lauf/zuege.jsonl", encoding="utf-8") if '2026-11-14' in z and '14R NL' in z]
letzte = {}
for e in zs:
if isinstance(e.get("spot"), dict):
letzte[e["hand"]] = e # laengste Historie je Hand
punkte = []
for h, e in letzte.items():
for p in rueckbau(e["spot"]):
if p["street"] == e["street"] and p["idx"] >= len(e["spot"]["history"]):
continue
p["hand"] = h; punkte.append(p)
punkte = [p for p in punkte if p["street"] in ("FLOP", "TURN")]
inp = "\n".join(json.dumps(p["spot"]) for p in punkte) + "\n"
r = subprocess.run([exe], input=inp.encode(), stdout=subprocess.PIPE, env=env, creationflags=0x08000040)
out = [json.loads(o) for o in r.stdout.decode().splitlines()]
norm = lambda a: {"BET": "RAISE", "ALL_IN": "RAISE"}.get(a, a)
gleich = sum(norm(p["live"]) == o["action"] for p, o in zip(punkte, out))
print(f"{len(punkte)} nachgebaute Flop/Turn-Punkte, {gleich} wie live")
for p, o in zip(punkte, out):
if p["gegen_raise"]:
print(f" {p['hand']} {p['street']:5s} gegen Erhoehung: live {p['live']:6s} -> {o['action']:5s} eq {o.get('equity')} | Hand {netto.get(p['hand'],0):+7.1f} | {o['reason'][:70]}")