Turnier-Duplikat: Bruecke zu SHARINGAN mit Antes als totem Geld je Platz und 32-Bit-Saat, auch am Mac

Spec: tests/turnier_duplikat_spec.py 13 OK (vorher 2 rot: Modul fehlte); saat32 gegen den bekannten splitmix64-Wert von 0,
stack_start mit Ante und Einsaetzen, keine post_ante im Verlauf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 16:06:53 +01:00
co-authored by Claude Opus 5.5
parent dbec003599
commit 11c0ecd974
2 changed files with 138 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
#!/usr/bin/env python3
"""Bruecke Vertrag (docs/VERTRAG.md) -> SHARINGAN fuer das Turnier-Duplikat, ohne Netz (wie messung/motor/pruef6/sharingan/sh6.py).
Unterschiede zu sh6.py: Antes gehen als totes Geld je Platz in die Anfrage (`players[].dead`, SHARINGAN-Zweig pokerfreak-splash
d5b9a6f); die Saat geht als 32-Bit-Wert an ihn (saat32 wie entscheiden/motor/src/sharingan.rs, mit 64-Bit-Saaten zieht er
verzerrt); laeuft auch am Mac. Das Feld `tournament` kennt er nicht: er spielt Chips.
sh_turnier.py <sharingan-stdio> <blueprint.plcp> <buckets-ordner> <sizing> [weitere Flags, z. B. --set iters=200000]
SH_PROTOKOLL=<datei>: je Antwort eine Zeile `source fallback_reason strasse ms`.
"""
import json
import os
import subprocess
import sys
PROTOKOLL = os.environ.get("SH_PROTOKOLL")
ART = {"POST_SB": "post_sb", "POST_BB": "post_bb", "FOLD": "fold", "CHECK": "check", "CALL": "call", "BET": "bet",
"RAISE": "raise", "ALL_IN": "all_in"}
M64 = (1 << 64) - 1
def saat32(x):
z = (x + 0x9E3779B97F4A7C15) & M64
z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & M64
z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & M64
z ^= z >> 31
return (z ^ (z >> 32)) & 0xFFFFFFFF
def karte(k):
if isinstance(k, str):
return k[0].upper() + k[1].lower()
w = ("TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE")
r = str(k["rank"])
r = "23456789TJQKA"[w.index(r)] if r in w else r[0]
return r + str(k["suit"])[0].lower()
def anfrage(a, nr):
bb = float(a["bigBlind"])
c = lambda x: int(round(float(x) / bb * 100))
n = int(a["seats"])
hist, drin, tot, st, auf = [], [0] * n, [0] * n, None, {}
for h in a.get("history") or []:
x, s = h["action"], int(h["seat"]) - 1
if x == "POST_ANTE":
tot[s] += c(h["amount"])
continue
if h.get("street") != st:
st, auf = h.get("street"), {}
e = {"seat": s, "type": ART[x]}
if x in ("POST_SB", "POST_BB", "BET", "RAISE", "ALL_IN", "CALL") and h.get("amount") is not None:
to = c(h["amount"])
drin[s] += to - auf.get(s, 0)
auf[s] = to
if x != "CALL":
e["to"] = to
hist.append(e)
stand = {int(a["heroSeat"]) - 1: c(a["heroStack"])}
for v in a.get("villains") or []:
stand[int(v["seat"]) - 1] = c(v["stack"])
spieler = [{"seat": s, "stack_start": stand[s] + drin[s] + tot[s], "dead": tot[s]} for s in range(n)]
brett = [karte(k) for s in ("FLOP", "TURN", "RIVER") for k in (a.get("board") or {}).get(s, [])]
return {"id": nr, "small_blind": 50, "big_blind": 100, "button_seat": int(a["buttonSeat"]) - 1,
"hero_seat": int(a["heroSeat"]) - 1, "hole_cards": [karte(k) for k in a["heroCards"]], "board": brett,
"players": spieler, "history": hist, "options": {"seed": saat32(int(a.get("seed") or nr)), "include_probs": False}}
def main():
exe, bp, bk, sz, *rest = sys.argv[1:]
env = dict(os.environ, STDIO_THREADS=os.environ.get("STDIO_THREADS", "1"))
p = subprocess.Popen([exe, "--blueprint", bp, "--buckets", bk, "--sizing", sz, "--bind", "127.0.0.1:0"] + rest,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env, text=True, encoding="utf-8", bufsize=1)
prot = open(PROTOKOLL, "a", encoding="utf-8") if PROTOKOLL else None
nr = 0
for zeile in sys.stdin:
if not zeile.strip():
continue
nr += 1
a = json.loads(zeile)
bb = float(a["bigBlind"])
try:
p.stdin.write(json.dumps(anfrage(a, nr), separators=(",", ":")) + "\n")
p.stdin.flush()
r = json.loads(p.stdout.readline())
except Exception as e: # noqa: BLE001 - jeder Fehler der Bruecke wird zur Antwort mit Grund, der Tisch laeuft weiter
r = {"ok": False, "error": {"code": "bruecke", "message": str(e)}}
if r.get("ok"):
o = r["response"]
t = o["action"]["type"]
w = {"action": t.upper(), "size": round(o["action"]["to"] * bb / 100, 6) if t == "raise" else None,
"confidence": 1, "reason": "sharingan %s %s" % (o["source"], o.get("fallback_reason") or "")}
if prot:
prot.write("%s %s %s %.0f\n" % (o["source"], (o.get("fallback_reason") or "-").split(":")[0].replace(" ", "_"),
o["street"], o["timing"]["total_ms"]))
else:
w = {"action": "CHECK" if "CHECK" in (a.get("legalActions") or []) else "FOLD", "size": None, "confidence": 0,
"reason": "sharingan Fehler %s" % json.dumps(r.get("error"))[:200]}
if prot:
prot.write("fehler %s - 0\n" % (r.get("error") or {}).get("code"))
if prot:
prot.flush()
print(json.dumps(w), flush=True)
if __name__ == "__main__":
main()
+31
View File
@@ -188,5 +188,36 @@ class Lauf(unittest.TestCase):
"unter 50/30/20 muss Preisgeld von Chips abweichen")
class SharinganAnfrage(unittest.TestCase):
"""Bruecke Vertrag -> SHARINGAN fuer das Turnier-Duplikat (messung/turnier/duplikat/sh_turnier.py), ohne seinen Prozess."""
def setUp(self):
import sh_turnier
self.S = sh_turnier
def test_saat32_wie_im_motor(self):
# entscheiden/motor/src/sharingan.rs saat32 (splitmix64, gefaltet): Werte aus der Rust-Formel
for x in (0, 1, 3399975668225400732, 2**64 - 1, 1 << 63):
self.assertLessEqual(self.S.saat32(x), 2**32 - 1)
self.assertNotEqual(self.S.saat32(3399975668225400732), self.S.saat32(3399975668225400733))
self.assertNotEqual(self.S.saat32(5), self.S.saat32(5 | (1 << 40)))
self.assertEqual(self.S.saat32(0), 0xE220A839 ^ 0x7B1DCDAF)
def test_antes_sind_totes_geld_je_platz(self):
a = {"bigBlind": 0.02, "seats": 3, "heroSeat": 3, "buttonSeat": 3, "heroStack": 0.5, "seed": 77,
"heroCards": ["As", "Kd"], "board": {},
"villains": [{"seat": 1, "stack": 0.29, "folded": False}, {"seat": 2, "stack": 1.0, "folded": False}],
"history": [{"street": "PREFLOP", "seat": s, "action": "POST_ANTE", "amount": 0.0024} for s in (1, 2, 3)]
+ [{"street": "PREFLOP", "seat": 1, "action": "POST_SB", "amount": 0.01},
{"street": "PREFLOP", "seat": 2, "action": "POST_BB", "amount": 0.02}]}
q = self.S.anfrage(a, 5)
self.assertEqual([p["dead"] for p in q["players"]], [12, 12, 12])
# stack_start = jetzt + in dieser Hand gesetzt + Ante
self.assertEqual([p["stack_start"] for p in q["players"]], [1450 + 50 + 12, 5000 + 100 + 12, 2500 + 12])
self.assertTrue(all(h["type"] != "post_ante" for h in q["history"]))
self.assertEqual(q["options"]["seed"], self.S.saat32(77))
self.assertEqual((q["hero_seat"], q["button_seat"]), (2, 2))
if __name__ == "__main__":
unittest.main()