99 lines
4.5 KiB
Python
99 lines
4.5 KiB
Python
"""Bruecke Vertrag (docs/VERTRAG.md) -> SHARINGAN (markokraemer/poker-sharingan, Apache-2.0), ohne Netz.
|
|
|
|
Startet `sharingan-stdio` (unser Zusatz in seinem Baum, sharingan-1/serve/src/bin/sharingan-stdio.rs: liest JSON-Zeilen,
|
|
ruft seinen Core::decide, kein Socket). Je Vertrags-Anfrage eine ActRequest-Zeile (seine TABLE_API: Chips ganzzahlig,
|
|
BB = 100, Sitze 0..5, `to` = Einsatz dieser Strasse), Antwort zurueck als FOLD/CHECK/CALL/RAISE mit size = raise-to in Geld.
|
|
Aufruf: sh6.py <sharingan-stdio.exe> <blueprint.plcp> <buckets-ordner> <sizing> [weitere Flags, z. B. --no-search]
|
|
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"}
|
|
|
|
|
|
def karte(k):
|
|
if isinstance(k, str):
|
|
return k[0].upper() + k[1].lower()
|
|
r = str(k["rank"])
|
|
w = ("TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE")
|
|
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, st, auf = [], [0] * n, None, {}
|
|
for h in a.get("history") or []:
|
|
x = h["action"]
|
|
if x == "POST_ANTE":
|
|
raise ValueError("Antes kennt SHARINGAN nicht")
|
|
s = int(h["seat"]) - 1
|
|
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]} 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": 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="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, creationflags=0x08004000)
|
|
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:
|
|
q = anfrage(a, nr)
|
|
p.stdin.write(json.dumps(q, separators=(",", ":")) + "\n")
|
|
p.stdin.flush()
|
|
r = json.loads(p.stdout.readline())
|
|
except Exception as e: # noqa: BLE001
|
|
r = {"ok": False, "error": {"code": "bruecke", "message": str(e)}}
|
|
if r.get("ok"):
|
|
o = r["response"]
|
|
akt = o["action"]
|
|
t = akt["type"]
|
|
w = {"action": t.upper(), "size": round(akt["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()
|