284 lines
13 KiB
Python
284 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
"""Spezifikation Spieler-Vorwissen (lesen/vorwissen: kennung, scan, speicher, draht; entscheiden/decide.py).
|
|
|
|
Rein funktional, oeffnet keinen Client und keinen Tisch.
|
|
|
|
python tests/vorwissen_spec.py -v
|
|
"""
|
|
import gzip
|
|
import json
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
_w = os.path.dirname(os.path.abspath(__file__))
|
|
while not os.path.isdir(os.path.join(_w, "lesen")):
|
|
_w = os.path.dirname(_w)
|
|
for _u in ("lesen", os.path.join("lesen", "vorwissen"), "entscheiden"):
|
|
sys.path.insert(0, os.path.join(_w, _u))
|
|
|
|
import kennung # noqa: E402
|
|
import scan # noqa: E402
|
|
import speicher # noqa: E402
|
|
import draht # noqa: E402
|
|
|
|
HAND = """CoinPoker Hand #1: Hold'em No Limit (₮0.01/₮0.02 - Ante ₮0.01 CPCC) - 2026/11/16 11:40:39 +00
|
|
Table 'T' 6-max Seat #4 is the button
|
|
Seat 1: a (₮2.06 in chips)
|
|
Seat 2: b (₮2.42 in chips)
|
|
Seat 3: c (₮1.93 in chips)
|
|
Seat 4: d (₮1.90 in chips)
|
|
Seat 5: e (₮2.99 in chips)
|
|
Seat 6: f (₮0.71 in chips)
|
|
a: posts the ante ₮0.01
|
|
b: posts the ante ₮0.01
|
|
c: posts the ante ₮0.01
|
|
d: posts the ante ₮0.01
|
|
e: posts the ante ₮0.01
|
|
f: posts the ante ₮0.01
|
|
e: posts small blind ₮0.01
|
|
f: posts big blind ₮0.02
|
|
*** HOLE CARDS ***
|
|
a: raises ₮0.03 to ₮0.05
|
|
b: calls ₮0.05
|
|
c: folds
|
|
d: folds
|
|
e: folds
|
|
f: folds
|
|
*** FLOP *** [Tc Ad Qd]
|
|
a: checks
|
|
b: bets ₮0.02
|
|
a: calls ₮0.02
|
|
*** TURN *** [Tc Ad Qd] [8d]
|
|
a: bets ₮0.17
|
|
b: raises ₮0.19 to ₮0.36
|
|
a: calls ₮0.19
|
|
*** RIVER *** [Tc Ad Qd 8d] [Ac]
|
|
a: checks
|
|
b: checks
|
|
*** SHOW DOWN ***
|
|
a: shows [8c 8h] (Full House)
|
|
b: mucks hand
|
|
a collected ₮0.90 from pot
|
|
*** SUMMARY ***
|
|
Total pot ₮0.93 | Rake ₮0.03
|
|
Board [ Tc Ad Qd 8d Ac ]"""
|
|
|
|
|
|
class Kennung(unittest.TestCase):
|
|
def setUp(self):
|
|
self.d = tempfile.mkdtemp()
|
|
self.salz = os.path.join(self.d, "salz.bin")
|
|
with open(self.salz, "wb") as f:
|
|
f.write(b"x" * 32)
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.d)
|
|
|
|
def test_gesalzen_und_stabil(self):
|
|
a = kennung.schluessel("spieler", self.salz)
|
|
self.assertEqual(a, kennung.schluessel("spieler", self.salz))
|
|
self.assertEqual(len(a), 16)
|
|
anders = os.path.join(self.d, "s2.bin")
|
|
with open(anders, "wb") as f:
|
|
f.write(b"y" * 32)
|
|
self.assertNotEqual(a, kennung.schluessel("spieler", anders))
|
|
|
|
def test_ohne_salz_kein_schluessel(self):
|
|
self.assertIsNone(kennung.schluessel("spieler", os.path.join(self.d, "fehlt.bin")))
|
|
|
|
|
|
class Scan(unittest.TestCase):
|
|
def test_hand_wie_live_gezaehlt(self):
|
|
h = scan.hand_lesen(HAND.splitlines())
|
|
self.assertIsNotNone(h)
|
|
self.assertEqual(h["pos"][1], "UTG")
|
|
self.assertEqual(h["pos"][4], "BTN")
|
|
# Netto = Gesammeltes - Eingezahltes; in den Ante-Haenden (CPCC) fehlt mehr als der Rake im Topf
|
|
self.assertAlmostEqual(sum(h["netto"].values()), -2.5, places=6)
|
|
z = scan.zaehle_hand(h)
|
|
self.assertEqual(z[1]["open_UTG"], [1, 1])
|
|
self.assertEqual(z[2]["kalt_HJ"], [1, 1])
|
|
self.assertEqual(z[2]["raiseToBet"], [1, 1]) # Turn-Raise gegen den Einsatz
|
|
self.assertEqual(z[1]["turnFoldToBet"], [1, 0])
|
|
self.assertEqual(z[1]["turnFoldToBet_k"], [1, 0]) # 0,19 auf 0,48+0,17: klein
|
|
self.assertEqual(z[1]["flopFoldToBet_k"], [1, 0])
|
|
self.assertEqual(z[1]["checkRaise"], [1, 0])
|
|
self.assertAlmostEqual(z[1]["_bb"], (0.90 - 0.01 - 0.05 - 0.02 - 0.36) / 0.02, places=3)
|
|
|
|
|
|
class Draht(unittest.TestCase):
|
|
SP = {"v": 1, "stichtag": "2026-10-19", "reads": ["raiseToBet", "riverFoldToBet"],
|
|
"pool": {"raiseToBet": [1.0, 9.0]}, "spieler": {"abcdabcdabcdabcd": [120.4, 300, -12.0, 3, {"0": [40.0, 4.0], "1": [10.0, 7.0]}]}}
|
|
|
|
def test_ohne_vw_unveraendert(self):
|
|
z = json.dumps({"villains": [{"seat": 1, "stack": 1, "folded": False}]})
|
|
self.assertEqual(draht.anreichern(z, self.SP), z)
|
|
|
|
def test_unbekannt_unveraendert(self):
|
|
z = json.dumps({"villains": [{"seat": 1, "stack": 1, "folded": False, "vw": "ffff0000ffff0000"}]})
|
|
self.assertEqual(draht.anreichern(z, self.SP), z)
|
|
|
|
def test_live_und_vorwissen_addiert(self):
|
|
z = json.dumps({"villains": [{"seat": 1, "stack": 1, "folded": False, "vw": "abcdabcdabcdabcd",
|
|
"profile": {"hands": 30, "vpip": 0.3, "reads": {"raiseToBet": {"n": 5, "k": 2}}}}]})
|
|
p = json.loads(draht.anreichern(z, self.SP))["villains"][0]["profile"]
|
|
self.assertEqual(p["reads"]["raiseToBet"], {"n": 45.0, "k": 6.0, "prior": [1.0, 9.0]})
|
|
self.assertEqual(p["reads"]["riverFoldToBet"], {"n": 10.0, "k": 7.0})
|
|
self.assertEqual(p["hands"], 120)
|
|
self.assertEqual(p["vpip"], 0.3)
|
|
self.assertEqual(p["vorwissen"]["roh"], 300)
|
|
|
|
def test_roh_ohne_prior_und_neues_profil(self):
|
|
z = json.dumps({"villains": [{"seat": 1, "stack": 1, "folded": False, "vw": "abcdabcdabcdabcd"}]})
|
|
p = json.loads(draht.anreichern(z, self.SP, "roh"))["villains"][0]["profile"]
|
|
self.assertNotIn("prior", p["reads"]["raiseToBet"])
|
|
|
|
def test_zweimal_gleich(self):
|
|
z = json.dumps({"villains": [{"seat": 1, "stack": 1, "folded": False, "vw": "abcdabcdabcdabcd"}]})
|
|
a = draht.anreichern(z, self.SP)
|
|
self.assertEqual(draht.anreichern(a, self.SP), a)
|
|
|
|
|
|
def test_ohne_laesst_read_weg(self):
|
|
z = json.dumps({"villains": [{"seat": 1, "stack": 1, "folded": False, "vw": "abcdabcdabcdabcd"}]})
|
|
p = json.loads(draht.anreichern(z, self.SP, "pool", ("raiseToBet",)))["villains"][0]["profile"]
|
|
self.assertNotIn("raiseToBet", p["reads"])
|
|
|
|
def test_art_fold_nur_fold_reads(self):
|
|
"""VORWISSEN_ART=fold: nur <strasse>FoldToBet_<klasse>, Live-Profil sonst unveraendert (auch hands)."""
|
|
sp = dict(self.SP, spieler={"abcdabcdabcdabcd": [120.4, 300, -12.0, 3, {"0": [40.0, 4.0], "1": [10.0, 7.0]}]},
|
|
reads=["raiseToBet", "turnFoldToBet_m"], pool={"turnFoldToBet_m": [5.0, 5.0]})
|
|
z = json.dumps({"villains": [{"seat": 1, "stack": 1, "folded": False, "vw": "abcdabcdabcdabcd",
|
|
"profile": {"hands": 30, "vpip": 0.3}}]})
|
|
p = json.loads(draht.anreichern(z, sp, "fold"))["villains"][0]["profile"]
|
|
self.assertEqual(p["reads"], {"turnFoldToBet_m": {"n": 10.0, "k": 7.0, "prior": [5.0, 5.0]}})
|
|
self.assertEqual(p["hands"], 30)
|
|
self.assertEqual(p["vpip"], 0.3)
|
|
self.assertTrue(draht.nur_fold_read("riverFoldToBet_u") and not draht.nur_fold_read("riverFoldToBet"))
|
|
|
|
def test_rel_pool_mittel_auf_kern_anker(self):
|
|
pool = {"aggression": [31.4, 68.6], "raiseToBet": [8.4, 91.6]}
|
|
f = draht.relativ({"n": 1000.0, "k": 314.0, "prior": [31.4, 68.6]}, "aggression", pool)
|
|
self.assertAlmostEqual(f["k"] / f["n"], draht.ANKER["aggression"], places=3)
|
|
self.assertAlmostEqual(f["prior"][0] / sum(f["prior"]), draht.ANKER["aggression"], places=3)
|
|
g = draht.relativ({"n": 100.0, "k": 9.0}, "raiseToBet", pool) # ohne Anker: unveraendert
|
|
self.assertEqual(g["k"], 9.0)
|
|
|
|
|
|
class Grund(unittest.TestCase):
|
|
def test_reads_in_begruendung(self):
|
|
z = json.dumps({"street": "TURN", "villains": [
|
|
{"seat": 3, "folded": False, "profile": {"reads": {"turnFoldToBet_m": {"n": 90.0, "k": 45.0, "prior": [5.0, 5.0]}}}},
|
|
{"seat": 4, "folded": True, "profile": {"reads": {"turnFoldToBet_m": {"n": 9.0, "k": 1.0}}}}]})
|
|
self.assertEqual(draht.grund(z), "Vorwissen S3 turnFold m.50 (n 90)")
|
|
self.assertEqual(draht.grund(z, fold=False), "")
|
|
|
|
def test_fold_setzen_nur_ohne_offenen_einsatz(self):
|
|
sys.path.insert(0, os.path.join(_w, "entscheiden"))
|
|
import decide
|
|
alt_v, alt_env = decide._vorwissen, dict(os.environ)
|
|
try:
|
|
decide._vorwissen = lambda: (object(), "rel")
|
|
prof = {"reads": {"turnFoldToBet_m": {"n": 90.0, "k": 45.0, "prior": [5.0, 5.0]}}}
|
|
frei = json.dumps({"street": "TURN", "toCall": 0.0, "villains": [{"seat": 3, "profile": prof}]})
|
|
offen = json.dumps({"street": "TURN", "toCall": 0.4, "villains": [{"seat": 3, "profile": prof}]})
|
|
for art, erwartet in (("setzen", [True, False]), ("wert", [True, False]), ("1", [True, True])):
|
|
os.environ["VORWISSEN_FOLD"] = art
|
|
os.environ.pop("VORWISSEN_BLUFF", None)
|
|
antw = [{"action": "CHECK", "reason": "r"}, {"action": "CALL", "reason": "r"}]
|
|
decide._vorwissen_grund([frei, offen], antw)
|
|
self.assertEqual(["turnFold" in a["reason"] for a in antw], erwartet, art)
|
|
os.environ["VORWISSEN_FOLD"] = "0"
|
|
antw = [{"action": "CHECK", "reason": "r"}]
|
|
decide._vorwissen_grund([frei], antw)
|
|
self.assertEqual(antw[0]["reason"], "r")
|
|
finally:
|
|
decide._vorwissen = alt_v
|
|
os.environ.clear()
|
|
os.environ.update(alt_env)
|
|
|
|
|
|
class Laufzeit(unittest.TestCase):
|
|
"""Live-Pfad: fehlt oder klemmt der Speicher, faellt alles still auf aus (nie Fehler, nie Warten)."""
|
|
|
|
def test_quelle_fehlt_kaputt_und_wartet_nicht(self):
|
|
d = tempfile.mkdtemp()
|
|
try:
|
|
self.assertIsNone(draht.Quelle(os.path.join(d, "fehlt.json.gz")).speicher(True))
|
|
kaputt = os.path.join(d, "kaputt.json.gz")
|
|
with open(kaputt, "wb") as f:
|
|
f.write(b"kein gzip")
|
|
self.assertIsNone(draht.Quelle(kaputt).speicher(True))
|
|
gut = os.path.join(d, "gut.json.gz")
|
|
with gzip.open(gut, "wt", encoding="utf-8") as f:
|
|
json.dump(Draht.SP, f)
|
|
import datetime
|
|
q = draht.Quelle(gut, heute=datetime.date(2026, 10, 20))
|
|
import time
|
|
t0 = time.perf_counter()
|
|
q.speicher() # stoesst das Laden an, wartet nicht
|
|
self.assertLess(time.perf_counter() - t0, 0.5)
|
|
self.assertIsNotNone(q.speicher(True))
|
|
# veraltet (Stichtag 19.10., 31 Tage > 30) oder ohne Stichtag: still aus
|
|
self.assertIsNone(draht.Quelle(gut, heute=datetime.date(2026, 11, 19)).speicher(True))
|
|
self.assertIsNotNone(draht.Quelle(gut, 60, heute=datetime.date(2026, 11, 19)).speicher(True))
|
|
ohne_tag = os.path.join(d, "ohne-tag.json.gz")
|
|
with gzip.open(ohne_tag, "wt", encoding="utf-8") as f:
|
|
json.dump(dict(Draht.SP, stichtag=None), f)
|
|
self.assertIsNone(draht.Quelle(ohne_tag, heute=datetime.date(2026, 10, 20)).speicher(True))
|
|
finally:
|
|
shutil.rmtree(d)
|
|
|
|
def test_decide_ohne_speicher_unveraendert(self):
|
|
sys.path.insert(0, os.path.join(_w, "entscheiden"))
|
|
import decide
|
|
alt_q, alt_env = decide.VORWISSEN_QUELLE, dict(os.environ)
|
|
try:
|
|
decide.VORWISSEN_QUELLE = None
|
|
os.environ["VORWISSEN"] = os.path.join(tempfile.gettempdir(), "vorwissen-fehlt-%d.json.gz" % os.getpid())
|
|
os.environ["VORWISSEN_FOLD"] = "wert"
|
|
heute = json.dumps({"street": "RIVER", "toCall": 0.0, "villains": [{"seat": 2}]})
|
|
z = [json.dumps({"street": "RIVER", "toCall": 0.0, "villains": [{"seat": 2, "vw": "abcdabcdabcdabcd"}]})]
|
|
self.assertEqual(decide.mit_vorwissen(z), [heute]) # textgleich mit dem Betrieb ohne VORWISSEN
|
|
self.assertEqual(decide.mit_vorwissen([heute]), [heute])
|
|
antw = [{"action": "CHECK", "reason": "r"}]
|
|
decide._vorwissen_grund(z, antw)
|
|
self.assertEqual(antw[0]["reason"], "r")
|
|
finally:
|
|
decide.VORWISSEN_QUELLE = alt_q
|
|
os.environ.clear()
|
|
os.environ.update(alt_env)
|
|
|
|
|
|
class Speicher(unittest.TestCase):
|
|
def test_zeitgewicht_und_stichtag(self):
|
|
d = tempfile.mkdtemp()
|
|
try:
|
|
os.makedirs(os.path.join(d, "tage"))
|
|
for tag, n in (("2026-10-01", 10), ("2026-10-18", 10), ("2026-10-19", 99)):
|
|
with gzip.open(os.path.join(d, "tage", tag + "_X.json.gz"), "wt", encoding="utf-8") as f:
|
|
json.dump({"spieler": {"s": {"_h": n, "_bb": 0.0, "vpip": [n, n // 2]}}}, f)
|
|
sp = speicher.bauen(d, "2026-10-19", halbwert=18.0)
|
|
e = speicher.eintrag(sp, "s")
|
|
self.assertEqual(e["h_roh"], 20) # der Stichtag selbst zaehlt nicht
|
|
self.assertAlmostEqual(e["h"], 10 * 0.5 ** (17.5 / 18) + 10 * 0.5 ** (0.5 / 18), places=1)
|
|
self.assertEqual(e["tage"], 1)
|
|
finally:
|
|
shutil.rmtree(d)
|
|
|
|
|
|
class Schalter(unittest.TestCase):
|
|
def test_in_bekannten_schaltern(self):
|
|
with open(os.path.join(_w, "entscheiden", "decide.py"), encoding="utf-8") as f:
|
|
src = f.read()
|
|
for k in ("\"VORWISSEN\"", "\"VORWISSEN_ART\"", "\"VORWISSEN_OHNE\"", "\"VORWISSEN_KERN\"", "\"VORWISSEN_FOLD\"",
|
|
"\"VORWISSEN_BLUFF\"", "\"VORWISSEN_ALTER\""):
|
|
self.assertIn(k, src)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|