Files
pokerfreak/tests/schatten_vorrechnen_spec.py

146 lines
6.4 KiB
Python

"""Spezifikation: Pondering MOTOR_VORRECHNEN_STRASSE (29.11., Standard aus).
* Leser: bei der Turnkarte ein Anfrage-Abbild (Leser.vor_abholen) = die echte Turn-Anfrage derselben Hand ohne die Turn-Zuege
(sonst traefe der Merker des Motors nie: Schluessel Topf, Stack, Brett, Ranges aus dem Verlauf). Aus: kein Abbild.
* Schatten: eigene Bahn `vor` (dritter Motor). Scheiben tragen vorrechnen_strasse, tisch, frist_rest_s = Scheibe; der groesste
Topf zuerst; bei Lastregel eng keine Scheibe. Der echte Turn-Zug eines Tisches mit warmem Spiel geht in die vor-Bahn (mit
`tisch`), ohne warmes Spiel in die tief-Bahn (ohne `tisch`, Vorrechnung weg). vor_ende und ein Fold beenden die Vorrechnung.
* Aus: keine dritte Bahn, keine neuen Felder.
Startet nur Ersatzmotoren (python), keinen Client, keine Tische.
"""
import json
import os
import sys
import tempfile
import time
H = os.path.dirname(os.path.abspath(__file__))
W = os.path.join(H, "..")
for p in ("lesen", "betrieb", "hand", "client", "entscheiden"):
sys.path.insert(0, os.path.join(W, p))
import schatten as S # noqa: E402
import spot_from_log # noqa: E402
for k in [k for k in os.environ if k.startswith("MOTOR_")]:
del os.environ[k]
# --- Leser ---
text = open(os.path.join(W, "mock", "vorlage", "hand-beispiel.txt"), encoding="utf-8", errors="replace").read()
leser = spot_from_log.Leser()
leser.lesen(text, ende=True)
assert leser.vor_abholen() == ([], []), "aus: kein Abbild"
os.environ["MOTOR_VORRECHNEN_STRASSE"] = "1"
leser = spot_from_log.Leser()
spots = leser.lesen(text, ende=True)
vor, aus = leser.vor_abholen()
assert len(vor) >= 1, vor
assert leser.vor_abholen() == ([], []), "abgeholt ist abgeholt"
zeile, raum, hand = vor[0]
v = json.loads(zeile)
assert v["street"] == "TURN", v["street"]
echt = next(json.loads(z) for z, *_ in spots if json.loads(z)["street"] == "TURN")
assert v["history"] == [x for x in echt["history"] if x["street"] != "TURN"], (v["history"], echt["history"])
for k in ("villains", "heroStack", "pot", "board", "heroCards", "heroSeat", "seats", "street", "bigBlind"):
assert v.get(k) == echt.get(k), (k, v.get(k), echt.get(k))
del os.environ["MOTOR_VORRECHNEN_STRASSE"]
# --- Schatten mit Ersatzmotor ---
d = tempfile.mkdtemp(prefix="schatten-vorrechnen-")
fragen, ziel, ersatz = os.path.join(d, "fragen.jsonl"), os.path.join(d, "log.jsonl"), os.path.join(d, "motor.py")
open(ersatz, "w").write(
"import sys, json, time, os\n"
"n = {}\n"
"for z in sys.stdin:\n"
" q = json.loads(z)\n"
f" open({fragen!r}, 'a').write(json.dumps(dict(q, _pid=os.getpid())) + chr(10))\n"
" a = {'action': q.get('_antwort', 'CHECK'), 'size': None, 'confidence': 1.0, 'reason': 'motor ersatz'}\n"
" if q.get('vorrechnen_strasse'):\n"
" time.sleep(0.05)\n"
" t = q['tisch']; n[t] = n.get(t, 0) + 1\n"
" a.update(vorgerechnet=True, stufe='schmal' if n[t] == 1 else 'warm', fertig=n[t] >= int(q.get('_scheiben', 3)))\n"
" print(json.dumps(a), flush=True)\n")
def gelesen():
return [json.loads(z) for z in open(fragen, encoding="utf-8")] if os.path.exists(fragen) else []
def warte(bed, s=10.0):
ende = time.time() + s
while time.time() < ende and not bed():
time.sleep(0.05)
assert bed()
def log():
return [json.loads(z) for z in open(ziel, encoding="utf-8")] if os.path.exists(ziel) else []
turn = dict(echt, legalActions=["FOLD", "CHECK", "RAISE"])
S.vorrang.heben = lambda pid, klasse=None: None # kein Prioritaetswechsel im Test
os.environ.update(MOTOR_VORRECHNEN_STRASSE="1", MOTOR_NACH_BREIT="1", MOTOR_NACH_BREIT_FRIST="4", MOTOR_VORRECHNEN_SCHEIBE="0.25")
assert S.vorrechnen_an()
m = S.Schatten([sys.executable, ersatz], ziel=ziel, spielt=True)
assert m.vor_bahn is not None and len(m.faeden) == 3
# groesster Topf zuerst; eng: keine Scheibe
m.vor_last(True)
m.vorrechnen(json.dumps(dict(v, totalPot=1.0, _scheiben=3)), "KLEIN", hand)
m.vorrechnen(json.dumps(dict(v, totalPot=9.0, _scheiben=3)), "GROSS", hand)
time.sleep(0.8)
assert gelesen() == [], "eng: keine Scheibe"
m.vor_last(False)
warte(lambda: sum(1 for f in gelesen() if f.get("vorrechnen_strasse")) >= 4)
sch = [f for f in gelesen() if f.get("vorrechnen_strasse")]
assert [f["tisch"] for f in sch[:3]] == ["GROSS"] * 3 and sch[3]["tisch"] == "KLEIN", [f["tisch"] for f in sch]
assert all(f["frist_rest_s"] == 0.25 for f in sch), sch
vor_pid = sch[0]["_pid"]
warte(lambda: m.vor["KLEIN"]["fertig"])
assert m.vor["GROSS"]["fertig"] and m.vor["GROSS"]["stufe"] == "warm"
# echter Zug mit warmem Spiel: vor-Bahn, mit tisch
n0 = len(gelesen())
r = m.zug(json.dumps(turn), "GROSS", hand, frist=5)
assert r and r["action"] == "CHECK", r
warte(lambda: log() and "bahn" in log()[-1])
f = gelesen()[n0]
assert f["_pid"] == vor_pid and f["tisch"] == "GROSS" and "vorrechnen_strasse" not in f and 3.0 <= f["frist_rest_s"] <= 4.7, f
assert log()[-1]["bahn"] == "vor" and log()[-1]["gespielt"] == "motor", log()[-1]
assert "GROSS" in m.vor, "nach einem Zug ohne Fold bleibt die Vorrechnung"
# Fold beendet sie
m.zug(json.dumps(dict(turn, _antwort="FOLD")), "GROSS", hand, frist=5)
warte(lambda: any("vorrechnen" in x and x["raum"] == "GROSS" for x in log()))
assert "GROSS" not in m.vor
ende = [x["vorrechnen"] for x in log() if "vorrechnen" in x]
assert ende and ende[-1]["ende"] == "Hand zu Ende" and ende[-1]["echt"] == 2, ende
# echter Zug ohne warmes Spiel (andere Hand): tief-Bahn ohne tisch, Vorrechnung weg
n0 = len(gelesen())
m.zug(json.dumps(turn), "KLEIN", "andere-hand", frist=5)
f = gelesen()[n0]
assert f["_pid"] != vor_pid and "tisch" not in f, f
assert "KLEIN" not in m.vor
# vor_ende
m.vorrechnen(json.dumps(dict(v, totalPot=5.0, _scheiben=99)), "T3", hand)
warte(lambda: m.vor["T3"]["warm"])
m.vor_ende("T3", hand)
assert "T3" not in m.vor
n0 = len(gelesen())
time.sleep(0.5)
assert all(f.get("tisch") != "T3" for f in gelesen()[n0 + 1:]), "nach vor_ende keine Scheibe mehr"
# --- aus: keine dritte Bahn, keine neuen Felder ---
for k in ("MOTOR_VORRECHNEN_STRASSE", "MOTOR_NACH_BREIT", "MOTOR_NACH_BREIT_FRIST", "MOTOR_VORRECHNEN_SCHEIBE"):
os.environ.pop(k)
os.remove(fragen)
m2 = S.Schatten([sys.executable, ersatz], ziel=ziel, spielt=True)
assert m2.vor_bahn is None and len(m2.faeden) == 2
m2.vorrechnen(zeile, "T1", hand)
assert m2.zug(json.dumps(turn), "T1", hand, frist=5)
f = gelesen()
assert len(f) == 1 and not ({"vorrechnen_strasse", "tisch", "frist_rest_s"} & set(f[0])), f
assert "bahn" not in log()[-1]
print("schatten_vorrechnen_spec: gruen")