71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Schickt Anfragen an den Entscheidungskern (pokerfreak) und liest Antworten.
|
|
|
|
from decide import entscheide
|
|
"""
|
|
import os as _os, sys as _sys
|
|
_w = _os.path.dirname(_os.path.abspath(__file__))
|
|
while _w != "/" and not _os.path.isdir(_os.path.join(_w, "hand")):
|
|
_w = _os.path.dirname(_w)
|
|
WURZEL = _w
|
|
for _u in ("lesen", "entscheiden", "hand", "lobby", "korpus", "betrieb"):
|
|
_q = _os.path.join(_w, _u)
|
|
if _os.path.isdir(_q) and _q not in _sys.path:
|
|
_sys.path.insert(0, _q)
|
|
import json
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
|
|
BASE = os.path.dirname(os.path.abspath(__file__))
|
|
BINARY = os.environ.get("COINPOKER_KERN", os.path.join(BASE, "..", "..", "pokerfreak", "target", "release", "pokerfreak"))
|
|
QUELLE = os.environ.get("COINPOKER_KERN_QUELLE", os.path.join(BASE, "..", "..", "pokerfreak"))
|
|
|
|
|
|
def bereit():
|
|
return os.path.exists(BINARY)
|
|
|
|
|
|
def baue():
|
|
"""Baut den Kern, falls nötig."""
|
|
r = subprocess.run(["cargo", "build", "--release"], cwd=QUELLE,
|
|
capture_output=True, text=True, timeout=600)
|
|
return r.returncode == 0, (r.stdout + r.stderr)[-400:]
|
|
|
|
|
|
def entscheide(anfragen, timeout=30):
|
|
"""Liste von Anfrage-Zeilen -> Liste von Entscheidungen (dict)."""
|
|
if not anfragen:
|
|
return []
|
|
r = subprocess.run([BINARY], input="\n".join(anfragen) + "\n",
|
|
capture_output=True, text=True, timeout=timeout)
|
|
if r.returncode != 0:
|
|
raise RuntimeError(f"pokerfreak scheiterte: {r.stderr.strip()[:300]}")
|
|
antworten = []
|
|
for zeile in r.stdout.strip().split("\n"):
|
|
if not zeile.strip():
|
|
continue
|
|
try:
|
|
antworten.append(json.loads(zeile))
|
|
except Exception:
|
|
antworten.append({"action": "ABSTAIN", "reason": "Antwort unlesbar"})
|
|
return antworten
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
if not bereit():
|
|
ok, ausgabe = baue()
|
|
print("gebaut:", ok)
|
|
if not ok:
|
|
print(ausgabe)
|
|
sys.exit(1)
|
|
probe = json.dumps({
|
|
"variant": "NLHE", "bigBlind": 0.1, "seats": 6, "heroSeat": 5,
|
|
"heroCards": [{"rank": "ACE", "suit": "SPADES"}, {"rank": "ACE", "suit": "HEARTS"}],
|
|
"board": {}, "street": "PREFLOP", "pot": 0.15, "toCall": 0.1,
|
|
"heroStack": 10.0, "legalActions": ["FOLD", "CALL", "RAISE"],
|
|
"allowedSizes": [0.3, 0.5],
|
|
"villains": [{"seat": 2, "stack": 10.0, "folded": False}]})
|
|
print(entscheide([probe])[0])
|