- sammeln.sichern: O_EXCL-Sperre (Doppelaufruf kehrt leer zurueck, alte Sperre > 10 min faellt), Zwischendateien mit PID, main.log als EINE laufende Fassung (ersetzt; faellt weg, sobald eine Rotation sie ganz enthaelt) - der Ordner waechst mit den Rotationen, nicht mit den Aufrufen. Der Leser nimmt nur rohlog/*.log.gz. - betrieb/sicherung_haken.py: abgekoppelt, Idle, wirft nie, hoechstens alle 120 s, nie zwei zugleich, erzwungen beim Lauf-Ende; ROHLOG_SICHERN=0 schaltet ab, ROHLOG_SICHERN_S den Takt. - Specs: Doppelaufruf + Rotation mitten in der Hand zaehlt jede Hand einmal; 6 Faeden zugleich. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
262 lines
13 KiB
Python
262 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
"""Spezifikation des Sammlers (lesen/sammeln.py): jede Hand genau einmal, Held raus, Stakes getrennt.
|
|
|
|
python tests/sammeln_spec.py -v
|
|
"""
|
|
import glob
|
|
import gzip
|
|
import json
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
|
|
_w = os.path.dirname(os.path.abspath(__file__))
|
|
while _w != "/" and not os.path.isdir(os.path.join(_w, "lesen")):
|
|
_w = os.path.dirname(_w)
|
|
sys.path.insert(0, os.path.join(_w, "lesen"))
|
|
|
|
import sammeln
|
|
import zaehler
|
|
|
|
MICRO = "14R NL 0.01-0.02 EV-INRIT-(A) 1770001"
|
|
UEBUNG = "Big Ben 1770002"
|
|
SITZE = {1: ("9001", "mmintel"), 2: ("9002", "alpha"), 3: ("9003", "beta")}
|
|
AS = [{"suit": "SPADES", "value": "ACE"}, {"suit": "HEARTS", "value": "ACE"}]
|
|
BRETT = {"FLOP": [{"suit": "CLUBS", "value": "TWO"}, {"suit": "DIAMONDS", "value": "SEVEN"},
|
|
{"suit": "HEARTS", "value": "NINE"}],
|
|
"TURN": [{"suit": "SPADES", "value": "KING"}], "RIVER": [{"suit": "CLUBS", "value": "FOUR"}]}
|
|
|
|
|
|
def bean(cmd, raum, daten):
|
|
roh = json.dumps(daten).replace('"', '\\"')
|
|
return '{"cmd_bean":{"BeanData":"%s","Cmd":"%s","RoomName":"%s"}}\n' % (roh, cmd, raum)
|
|
|
|
|
|
def hand(nr, raum=MICRO, sitze=SITZE, zeigen=None):
|
|
"""Eine ganze Hand: 3 sitzt im BB, 2 erhoeht vorflop, 3 callt, Flop: 3 checkt, 2 setzt Pot, 3 callt,
|
|
Turn/River Check-Check; danach deckt `zeigen` ({sitz: karten}) auf. 1 (der Held) foldet vorflop."""
|
|
h = "%s%05d" % (raum.split()[-1], nr)
|
|
liste = [{"seatId": s, "userId": u, "userName": n, "userChips": 2.0} for s, (u, n) in sitze.items()]
|
|
t = bean("game.game_start", raum, {"gameHandId": h, "dealerSeatId": 1, "bbAmount": 0.02,
|
|
"seatResponseDataList": liste})
|
|
|
|
def seat(s, cap, betrag=None):
|
|
d = {"gameHandId": h, "seatId": s, "userName": sitze[s][1], "caption": cap, "userChips": 2.0}
|
|
if betrag is not None:
|
|
d["betAmout"] = betrag
|
|
return bean("game.seat", raum, d)
|
|
|
|
t += seat(2, "SB", 0.01) + seat(3, "BB", 0.02) + seat(1, "Fold") + seat(2, "Raise", 0.06) + seat(3, "Call", 0.06)
|
|
t += bean("game.dealer_cards", raum, {"gameHandId": h, "dealerCards": {"FLOP": BRETT["FLOP"]}})
|
|
t += seat(3, "Check") + seat(2, "Bet", 0.12) + seat(3, "Call", 0.12)
|
|
t += bean("game.dealer_cards", raum, {"gameHandId": h, "dealerCards": {k: BRETT[k] for k in ("FLOP", "TURN")}})
|
|
t += seat(3, "Check") + seat(2, "Check")
|
|
t += bean("game.dealer_cards", raum, {"gameHandId": h, "dealerCards": BRETT})
|
|
t += seat(3, "Check") + seat(2, "Check")
|
|
if zeigen:
|
|
t += bean("game.reveal_cards", raum, {"gameHandId": h, "userCardListMap": [
|
|
{"seatId": s, "cards": k} for s, k in zeigen.items()]})
|
|
return t
|
|
|
|
|
|
def schluss(raum=MICRO):
|
|
"""Ein Handanfang danach, damit die letzte Hand fertig gebucht wird."""
|
|
return bean("game.game_start", raum, {"gameHandId": "%s99999" % raum.split()[-1]})
|
|
|
|
|
|
class Haende(unittest.TestCase):
|
|
def test_eine_hand_zaehlt_die_gegner_nicht_den_held(self):
|
|
h = sammeln.haende_aus_text(hand(1) + schluss())
|
|
s = sammeln.sammeln(h)
|
|
sp = s["stufen"]["NL0.01-0.02"]
|
|
self.assertEqual(sorted(sp), ["9002", "9003"])
|
|
self.assertEqual(sp["9002"]["vpip"], [1, 1])
|
|
self.assertEqual(sp["9002"]["pfr"], [1, 1])
|
|
self.assertEqual(sp["9003"]["flopFoldToBet"], [1, 0])
|
|
self.assertEqual(s["bericht"]["held"], ["9001"])
|
|
|
|
def test_dieselbe_hand_zweimal_im_text_zaehlt_einmal(self):
|
|
t = hand(1) + hand(2) + schluss()
|
|
einmal = sammeln.sammeln(sammeln.haende_aus_text(t))["stufen"]
|
|
doppelt = sammeln.sammeln(sammeln.vereinen(sammeln.haende_aus_text(t), sammeln.haende_aus_text(t)))["stufen"]
|
|
self.assertEqual(einmal, doppelt)
|
|
self.assertEqual(einmal["NL0.01-0.02"]["9002"]["vpip"], [2, 2])
|
|
|
|
def test_stakes_bleiben_getrennt(self):
|
|
t = hand(1) + schluss() + hand(1, UEBUNG) + schluss(UEBUNG)
|
|
s = sammeln.sammeln(sammeln.haende_aus_text(t))
|
|
self.assertEqual(sorted(s["stufen"]), ["NL0.01-0.02", "uebung"])
|
|
self.assertEqual(s["stufen"]["uebung"]["9002"]["vpip"], [1, 1])
|
|
self.assertEqual(s["stufen"]["NL0.01-0.02"]["9002"]["vpip"], [1, 1])
|
|
self.assertEqual(s["haende_je_stufe"], {"NL0.01-0.02": 1, "uebung": 1})
|
|
|
|
def test_stufe_aus_namen_und_aus_bb_passen_zusammen(self):
|
|
self.assertEqual(sammeln.stufe(MICRO), sammeln.stufe_aus_bb(0.02))
|
|
self.assertEqual(sammeln.stufe("NL 0.05-0.10 X 1"), sammeln.stufe_aus_bb(0.1))
|
|
self.assertEqual(sammeln.stufe(UEBUNG), "uebung")
|
|
self.assertEqual(sammeln.stufe_aus_bb(2000), "uebung")
|
|
|
|
def test_aufgedeckt_mit_linie(self):
|
|
h = sammeln.haende_aus_text(hand(1, zeigen={2: AS, 1: AS}) + schluss())
|
|
g = sammeln.sammeln(h)["gezeigt"]
|
|
self.assertEqual(len(g), 1, "der Held zaehlt nicht")
|
|
self.assertEqual((g[0]["spieler"], g[0]["karten"], g[0]["linie"]), ("9002", ["As", "Ah"], "river-passiv"))
|
|
self.assertEqual(len(g[0]["board"]), 5)
|
|
|
|
def test_einsatzgroesse_aus_dem_topf(self):
|
|
h = sammeln.haende_aus_text(hand(1) + schluss())
|
|
v = next(iter(h.values()))["verlauf"]
|
|
# Topf vor dem Flop-Einsatz: 0,06 + 0,06 = 0,12; Einsatz 0,12 = 1,0 Topf -> "pot"
|
|
self.assertEqual(sammeln.einsatz_groessen(v), [(2, "FLOP", "pot")])
|
|
s = sammeln.sammeln(h)
|
|
self.assertEqual(s["tells"]["NL0.01-0.02"]["9002"]["sizing"], {"pot": 1})
|
|
|
|
|
|
class Dateien(unittest.TestCase):
|
|
def setUp(self):
|
|
self.d = tempfile.mkdtemp()
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.d, ignore_errors=True)
|
|
|
|
def _datei(self, name, text, packen=False):
|
|
p = os.path.join(self.d, name)
|
|
os.makedirs(os.path.dirname(p), exist_ok=True)
|
|
if packen:
|
|
with gzip.open(p, "wt", encoding="utf-8") as f:
|
|
f.write(text)
|
|
else:
|
|
with open(p, "w", encoding="utf-8") as f:
|
|
f.write(text)
|
|
return p
|
|
|
|
def test_gleicher_inhalt_in_zwei_ordnern_zaehlt_einmal(self):
|
|
t = "2026-11-14 10:00:00:000 x\n" + hand(1) + hand(2) + schluss()
|
|
a = self._datei("a/main.log", t)
|
|
b = self._datei("b/kopie.log.gz", t, packen=True)
|
|
haende, bericht = sammeln.rohlog_haende([a, b])
|
|
self.assertEqual(len(haende), 2)
|
|
self.assertTrue(any(x.get("doppelt_von") for x in bericht))
|
|
|
|
def test_ueberlappende_dateien_buchen_jede_hand_einmal(self):
|
|
a = self._datei("a/main.log", "2026-11-14 10:00:00:000 x\n" + hand(1) + hand(2) + hand(3) + schluss())
|
|
b = self._datei("b/main.log", "2026-11-14 10:05:00:000 x\n" + hand(2) + hand(3) + hand(4) + schluss())
|
|
haende, _ = sammeln.rohlog_haende([a, b])
|
|
self.assertEqual(sorted(h[-5:] for h in haende), ["00001", "00002", "00003", "00004"])
|
|
s = sammeln.sammeln(haende)
|
|
self.assertEqual(s["stufen"]["NL0.01-0.02"]["9002"]["vpip"], [4, 4])
|
|
|
|
def test_rotation_mitten_in_der_hand_bleibt_ganz(self):
|
|
ganz = hand(1) + hand(2) + schluss()
|
|
mitte = ganz.index("game.dealer_cards", ganz.index("177000100002"))
|
|
mitte = ganz.rindex("\n", 0, mitte) + 1
|
|
alt = self._datei("c/main.1.log.gz", "2026-11-14 10:00:00:000 x\n" + ganz[:mitte], packen=True)
|
|
neu = self._datei("c/main.log", "2026-11-14 10:07:00:000 x\n" + ganz[mitte:])
|
|
haende, _ = sammeln.rohlog_haende([neu, alt])
|
|
self.assertEqual(len(haende), 2, "die Hand ueber die Rotationsgrenze ist ganz")
|
|
|
|
def test_speicher_und_rohlog_doppeln_nicht(self):
|
|
haende = sammeln.haende_aus_text(hand(1) + hand(2) + hand(3) + schluss())
|
|
nr2 = [h for h in haende if h.endswith("00002")][0]
|
|
speicher = zaehler.hand_buchen({}, nr2, haende[nr2]["verlauf"], haende[nr2]["kennung"])
|
|
s = sammeln.sammeln(haende, [("alt", speicher)])
|
|
self.assertEqual(s["stufen"]["NL0.01-0.02"]["9002"]["vpip"], [3, 3], "Hand 2 nur einmal")
|
|
self.assertEqual(s["bericht"]["haende_gesamt"], 3)
|
|
self.assertEqual(s["bericht"]["rohlog_neu"], 2)
|
|
self.assertNotIn("9001", s["stufen"]["NL0.01-0.02"], "der Held faellt auch aus dem Speicher")
|
|
|
|
def test_zwei_speicher_mit_derselben_hand_werden_gemeldet(self):
|
|
haende = sammeln.haende_aus_text(hand(1) + schluss())
|
|
nr = next(iter(haende))
|
|
sp = zaehler.hand_buchen({}, nr, haende[nr]["verlauf"], haende[nr]["kennung"])
|
|
s = sammeln.sammeln({}, [("a", sp), ("b", sp)], {sammeln.tischnr(nr): "NL0.01-0.02"})
|
|
self.assertEqual(s["bericht"]["speicher"][1]["doppelt_speicher"], 1)
|
|
|
|
def test_sichern_legt_jede_rotation_einmal_ab(self):
|
|
quelle = os.path.join(self.d, "logs")
|
|
self._datei("logs/main.1.log.gz", "eins", packen=True)
|
|
self._datei("logs/main.log", "jetzt")
|
|
ziel = os.path.join(self.d, "rohlog")
|
|
self.assertEqual(sammeln.sichern(ziel, quelle), 2)
|
|
self.assertEqual(sammeln.sichern(ziel, quelle), 0)
|
|
self._datei("logs/main.log", "jetzt und mehr")
|
|
self.assertEqual(sammeln.sichern(ziel, quelle), 1)
|
|
|
|
|
|
def _gesichert(self, ziel):
|
|
haende, _ = sammeln.rohlog_haende(sorted(glob.glob(os.path.join(ziel, "*.log.gz"))))
|
|
return haende
|
|
|
|
def test_doppelaufruf_und_rotation_zaehlen_jede_hand_einmal(self):
|
|
"""Der Laeufer sichert alle paar Minuten; dazwischen rotiert der Client mitten in einer Hand."""
|
|
quelle, ziel = os.path.join(self.d, "logs"), os.path.join(self.d, "rohlog")
|
|
kopf = "2026-11-14 10:00:00:000 x\n"
|
|
ganz = kopf + hand(1) + hand(2) + hand(3) + hand(4) + schluss()
|
|
t2 = ganz.index("177000100002")
|
|
t3 = ganz.index("177000100003")
|
|
mitte = ganz.rindex("\n", 0, ganz.index("game.dealer_cards", ganz.index("177000100004"))) + 1
|
|
self._datei("logs/main.log", ganz[:t2]) # Hand 1 laeuft
|
|
self.assertEqual(sammeln.sichern(ziel, quelle), 1)
|
|
self._datei("logs/main.log", ganz[:t3]) # waechst
|
|
self.assertEqual(sammeln.sichern(ziel, quelle), 1)
|
|
self.assertEqual(sammeln.sichern(ziel, quelle), 0, "nichts Neues: nichts geschrieben")
|
|
self.assertEqual(len(glob.glob(os.path.join(ziel, "laufend-*"))), 1, "eine laufende Fassung, ersetzt")
|
|
# zweiter Aufruf, waehrend der erste sichert: die Sperre laesst ihn leer zurueckkehren
|
|
sperre = os.path.join(ziel, sammeln.SPERRE)
|
|
open(sperre, "w").close()
|
|
self._datei("logs/main.log", ganz[:mitte])
|
|
self.assertEqual(sammeln.sichern(ziel, quelle), 0)
|
|
os.remove(sperre)
|
|
# Rotation mitten in Hand 4: main.log -> main.1.log.gz, neues main.log mit dem Rest
|
|
os.remove(os.path.join(quelle, "main.log"))
|
|
self._datei("logs/main.1.log.gz", ganz[:mitte], packen=True)
|
|
self._datei("logs/main.log", "2026-11-14 10:07:00:000 x\n" + ganz[mitte:])
|
|
sammeln.sichern(ziel, quelle)
|
|
sammeln.sichern(ziel, quelle)
|
|
namen = sorted(os.path.basename(x) for x in os.listdir(ziel))
|
|
self.assertEqual(len(namen), 2, namen) # die Rotation + die neue laufende Fassung
|
|
self.assertEqual(sum(n.startswith("laufend-") for n in namen), 1)
|
|
haende = self._gesichert(ziel)
|
|
self.assertEqual(sorted(h[-5:] for h in haende), ["00001", "00002", "00003", "00004"])
|
|
self.assertEqual(sammeln.sammeln(haende)["stufen"]["NL0.01-0.02"]["9002"]["vpip"], [4, 4])
|
|
|
|
def test_gleichzeitige_aufrufe_werfen_nicht_und_doppeln_nicht(self):
|
|
import threading
|
|
quelle, ziel = os.path.join(self.d, "logs"), os.path.join(self.d, "rohlog")
|
|
self._datei("logs/main.1.log.gz", "2026-11-14 10:00:00:000 x\n" + hand(1) + hand(2), packen=True)
|
|
self._datei("logs/main.log", "2026-11-14 10:07:00:000 x\n" + hand(3) + schluss())
|
|
fehler = []
|
|
|
|
def lauf():
|
|
try:
|
|
sammeln.sichern(ziel, quelle)
|
|
except Exception as e: # noqa: BLE001
|
|
fehler.append(e)
|
|
faeden = [threading.Thread(target=lauf) for _ in range(6)]
|
|
for f in faeden:
|
|
f.start()
|
|
for f in faeden:
|
|
f.join()
|
|
sammeln.sichern(ziel, quelle)
|
|
self.assertEqual(fehler, [])
|
|
self.assertFalse(os.path.exists(os.path.join(ziel, sammeln.SPERRE)), "Sperre wieder frei")
|
|
self.assertEqual(sorted(h[-5:] for h in self._gesichert(ziel)), ["00001", "00002", "00003"])
|
|
self.assertEqual(len(os.listdir(ziel)), 2)
|
|
|
|
def test_alte_sperre_eines_abgestuerzten_laufs_blockiert_nicht(self):
|
|
quelle, ziel = os.path.join(self.d, "logs"), os.path.join(self.d, "rohlog")
|
|
self._datei("logs/main.log", "jetzt")
|
|
os.makedirs(ziel)
|
|
sperre = os.path.join(ziel, sammeln.SPERRE)
|
|
open(sperre, "w").close()
|
|
alt = time.time() - sammeln.SPERRE_ALT_S - 5
|
|
os.utime(sperre, (alt, alt))
|
|
self.assertEqual(sammeln.sichern(ziel, quelle), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|