Files
pokerfreak/sicht/roentgen_seite.py
mmintelandClaude Opus 5.5 e545670292 Engine-Roentgen: je Lauf wer entschied, Read-Wirkung (Nachspiel mit/ohne Profil), Stichprobe, Laeufer
lesen/roentgen.py (CLI, --json), tests/roentgen_spec.py, Cockpit-Seite /roentgen und /api/roentgen
(sicht/roentgen_seite.py, Nachspiel im Hintergrundfaden, Kern-Kopie in Prioritaet Idle, Cache je Lauf).
Lauf 4 (pid 43432, ab 18:57): Read aendert 7 von 2060 Entscheidungen (0,3 %), Preflop-CFR 60,9 %,
E3 7,2 % der Turn-Zuege, kein Veto, kein MDF-Boden; Nachspiel = live 99,9 %.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 20:32:57 +01:00

132 lines
6.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Engine-Röntgen im Cockpit-Server (lesen/roentgen.py): eine Seite und ein JSON-Endpunkt.
/roentgen Seite: Lauf wählen, Bericht als Tabelle (pollt, solange gerechnet wird)
/api/roentgen?pid=..|start=.. {"status": "rechnet"|"fertig"|"fehler", "text", "bericht", "stand"}
/api/roentgen/laeufe [{pid, von, bis, entscheidungen}]
Das Nachspielen (zweimal jede Anfrage durch den Kern, Priorität Idle) läuft in EINEM Hintergrundfaden,
nie im Anfrage-Faden: die erste Antwort kommt sofort mit dem schnellen Teil (ohne Read-Wirkung),
die Read-Wirkung folgt. Je Lauf zwischengespeichert; ein späterer Blick rechnet nur neue Züge nach
(roentgen.py hält den Plattencache je Anfrage). Datenquelle: ROENTGEN_WURZEL, sonst dieser Baum.
"""
import json
import os
import sys
import threading
import time
from urllib.parse import parse_qs
_w = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_l = os.path.join(_w, "lesen")
if _l not in sys.path:
sys.path.append(_l)
NEU_NACH_S = 120.0 # ein fertiger Bericht gilt so lange; danach rechnet der nächste Blick nach
_sperre = threading.Lock()
_stand = {} # schluessel -> {"status", "text", "bericht", "stand", "fehler"}
_faden = None
def _wurzel():
import roentgen
return os.environ.get("ROENTGEN_WURZEL") or roentgen.BAUM
def _rechnen(schluessel, pid, start):
import roentgen
try:
schnell = roentgen.rechnen(_wurzel(), pid=pid, start=start, nachspiel=False)
with _sperre:
_stand[schluessel] = {"status": "rechnet", "bericht": schnell, "text": roentgen.text(schnell),
"stand": time.time()}
voll = roentgen.rechnen(_wurzel(), pid=pid, start=start, nachspiel=True)
with _sperre:
_stand[schluessel] = {"status": "fertig", "bericht": voll, "text": roentgen.text(voll),
"stand": time.time()}
except Exception as e: # noqa: BLE001 - die Seite zeigt den Fehler, der Server bleibt stehen
with _sperre:
alt = _stand.get(schluessel) or {}
_stand[schluessel] = {**alt, "status": "fehler", "fehler": f"{type(e).__name__}: {e}",
"stand": time.time()}
def anfordern(pid=None, start=None):
"""Stand für einen Lauf; stößt das Rechnen an, wenn nichts da oder veraltet ist."""
global _faden
schluessel = f"{pid or ''}|{start or ''}"
with _sperre:
st = dict(_stand.get(schluessel) or {})
laeuft = _faden is not None and _faden.is_alive()
alt = not st or (st.get("status") != "rechnet" and time.time() - st.get("stand", 0) > NEU_NACH_S)
if alt and not laeuft:
_faden = threading.Thread(target=_rechnen, args=(schluessel, pid, start), daemon=True,
name="roentgen")
_faden.start()
if not st:
st = {"status": "rechnet", "text": "rechnet ...", "bericht": None, "stand": None}
else:
st["status"] = "rechnet"
elif alt and laeuft and not st:
st = {"status": "wartet", "text": "ein anderer Lauf wird gerade gerechnet ...", "bericht": None}
return st
def laeufe():
import roentgen
return roentgen.laeufe(roentgen.daten(_wurzel())["schatten"])
SEITE = """<!doctype html>
<html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Engine-Röntgen</title>
<style>
:root{--bg:#fff;--fg:#1d1d1f;--mut:#6e6e73;--rand:#d2d2d7;--akzent:#0a62c9}
@media (prefers-color-scheme:dark){:root{--bg:#1c1c1e;--fg:#f2f2f7;--mut:#98989d;--rand:#3a3a3c;--akzent:#64a8ff}}
body{margin:0;padding:16px;background:var(--bg);color:var(--fg);font:14px/1.4 -apple-system,Segoe UI,sans-serif}
h1{font-size:18px;margin:0 0 12px}
select,button{font:inherit;padding:4px 8px;background:var(--bg);color:var(--fg);border:1px solid var(--rand);border-radius:6px}
#status{color:var(--mut);margin:8px 0}
pre{font:12px/1.45 ui-monospace,Consolas,monospace;overflow-x:auto;border-top:1px solid var(--rand);padding-top:12px}
</style></head><body>
<h1>Engine-Röntgen</h1>
<label>Lauf <select id="lauf"></select></label> <button id="neu">neu rechnen</button>
<div id="status"></div><pre id="text">lädt ...</pre>
<script>
const $=id=>document.getElementById(id);let uhr=null;
async function laeufe(){
const l=await (await fetch('/api/roentgen/laeufe')).json();
$('lauf').innerHTML=l.filter(r=>r.entscheidungen>0).reverse().map(r=>
`<option value="${r.pid}">pid ${r.pid} · ${r.von} – ${r.bis.slice(11)} · ${r.entscheidungen} Entsch.</option>`).join('');
}
async function holen(){
clearTimeout(uhr);
const pid=$('lauf').value;
try{
const r=await (await fetch('/api/roentgen'+(pid?'?pid='+encodeURIComponent(pid):''))).json();
$('text').textContent=r.text||'';
const t=r.stand?new Date(r.stand*1000).toLocaleTimeString():'';
$('status').textContent=r.status==='fertig'?`fertig (${t})`:r.status==='fehler'?`Fehler: ${r.fehler}`:
r.status==='rechnet'?'Read-Wirkung wird nachgespielt (Kern, Priorität Idle) ...':r.status;
if(r.status==='rechnet'||r.status==='wartet') uhr=setTimeout(holen,3000);
}catch(e){$('status').textContent='Server nicht erreichbar';uhr=setTimeout(holen,5000);}
}
$('lauf').onchange=holen;$('neu').onclick=holen;
laeufe().then(holen);
</script></body></html>"""
def antwort(pfad, query=""):
"""(code, daten, art) für unsere Pfade, sonst None."""
if pfad == "/roentgen":
return 200, SEITE.encode("utf-8"), "text/html; charset=utf-8"
if pfad == "/api/roentgen/laeufe":
return 200, json.dumps(laeufe(), ensure_ascii=False).encode("utf-8"), "application/json; charset=utf-8"
if pfad == "/api/roentgen":
q = parse_qs(query or "")
pid = (q.get("pid") or [None])[0]
start = (q.get("start") or [None])[0]
st = anfordern(pid, start)
return 200, json.dumps(st, ensure_ascii=False, default=str).encode("utf-8"), "application/json; charset=utf-8"
return None