113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Gleicht unsere Handbewertung mit der des Clients ab.
|
|
|
|
Der Client schickt bei passenden Gelegenheiten `game.hand_strength` mit
|
|
`handRank` - eine unabhängige Bewertung unserer Karten. Der Kern muss genau
|
|
dasselbe sagen. Null Toleranz: Bewertung ist reine Bestimmung.
|
|
|
|
python3 abgleich.py [dateien...]
|
|
"""
|
|
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 glob
|
|
import json
|
|
import os
|
|
import sys
|
|
import re
|
|
import sys
|
|
|
|
import decide
|
|
|
|
BASE = os.path.dirname(os.path.abspath(__file__))
|
|
BEAN = re.compile(r'"BeanData":"((?:[^"\\]|\\.)*)","Cmd":"(game\.[a-z_]+)","RoomName":"((?:[^"\\]|\\.)*)"')
|
|
STREETS = ("FLOP", "TURN", "RIVER")
|
|
|
|
|
|
def u(text):
|
|
try:
|
|
return json.loads('"' + text + '"')
|
|
except Exception:
|
|
return text
|
|
|
|
|
|
def karte(c):
|
|
return {"rank": c["value"], "suit": c["suit"]}
|
|
|
|
|
|
def board_von(raw):
|
|
if not isinstance(raw, dict):
|
|
return {}
|
|
return {s: [karte(c) for c in raw[s]] for s in STREETS if raw.get(s)}
|
|
|
|
|
|
def sammeln(pfade):
|
|
"""Je Hand: eigene Karten, letztes Board, letzter Pot - sowie die Bewertung des Clients."""
|
|
hole, board, pot, bewertung = {}, {}, {}, {}
|
|
for pfad in pfade:
|
|
for m in BEAN.finditer(open(pfad, errors="replace").read()):
|
|
cmd = m.group(2)
|
|
try:
|
|
d = json.loads(u(m.group(1)))
|
|
except Exception:
|
|
continue
|
|
if not isinstance(d, dict):
|
|
continue
|
|
hand = d.get("gameHandId")
|
|
if cmd == "game.hole_cards" and hand:
|
|
hole[hand] = d.get("holeCards")
|
|
elif cmd == "game.dealer_cards" and hand:
|
|
board[hand] = d.get("dealerCards")
|
|
elif cmd == "game.potInfo" and hand:
|
|
pot[hand] = d.get("totalPotAmount")
|
|
elif cmd == "game.hand_strength" and hand:
|
|
bestand = (d.get("userBestHand") or {})
|
|
if bestand.get("handRank"):
|
|
bewertung[hand] = (bestand["handRank"], bestand.get("bestHandCards"))
|
|
return hole, board, pot, bewertung
|
|
|
|
|
|
def main():
|
|
pfade = [a for a in sys.argv[1:] if not a.startswith("--")] or \
|
|
sorted(glob.glob(os.path.join(WURZEL, "archiv", "*.log")))
|
|
hole, board, pot, bewertung = sammeln(pfade)
|
|
print(f"Bewertungen des Clients im Korpus: {len(bewertung)}")
|
|
if not bewertung:
|
|
print("keine gefunden")
|
|
return
|
|
|
|
anfragen, erwartet = [], []
|
|
for hand, (rang, beste) in sorted(bewertung.items()):
|
|
if not hole.get(hand):
|
|
print(f" Hand {hand}: übersprungen (keine eigenen Karten)")
|
|
continue
|
|
b = board_von(board.get(hand))
|
|
street = next((s for s in reversed(STREETS) if b.get(s)), "PREFLOP")
|
|
anfragen.append(json.dumps({
|
|
"variant": "NLHE", "bigBlind": 0.1, "seats": 6, "heroSeat": 5,
|
|
"heroCards": [karte(c) for c in hole[hand]], "board": b, "street": street,
|
|
"pot": pot.get(hand) or 0.1, "toCall": 0.0, "heroStack": 10.0,
|
|
"legalActions": ["CHECK", "CALL"]}))
|
|
erwartet.append((hand, rang, beste))
|
|
|
|
antworten = decide.entscheide(anfragen)
|
|
treffer = 0
|
|
for (hand, rang, beste), antwort in zip(erwartet, antworten):
|
|
unser = antwort.get("category")
|
|
stimmt = unser == rang
|
|
treffer += stimmt
|
|
karten = " ".join(f"{c['value'][:2]}{c['suit'][:2]}" for c in (beste or [])[:5])
|
|
print(f" {'OK ' if stimmt else 'ABW'} Hand {hand}: Client {rang:<14} "
|
|
f"unser {str(unser):<14} | fünf beste: {karten}")
|
|
print(f"\nüberein: {treffer} von {len(erwartet)}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|