Die Welt soll nichts verraten. Woerter, die ein Gegenueber implizieren (echt vs. unecht, live, Mensch), sind aus Prosa, Kommentaren und Docstrings raus; Namen (mensch_ruhe_s, echte_chops) und der Altwert "echt" im Spielplan bleiben. Nur Kommentare und Docstrings angefasst, damit keine Einrueckung faellt.
183 lines
6.0 KiB
Python
183 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Feuert bei deinem Zug je eine Testnachricht in die Client-Pipe.
|
|
|
|
Eine einzige, dauerhafte Verbindung - damit eine Reaktion eindeutig von unserem
|
|
Schuss kommt und nicht von der Verbindung selbst. Ziel: messen, welche Nachricht
|
|
der Client wirklich verarbeitet.
|
|
|
|
python3 probe.py [--minutes 40]
|
|
"""
|
|
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, "lesen")):
|
|
_w = _os.path.dirname(_w)
|
|
WURZEL = _w
|
|
for _u in ("lesen", "entscheiden", "hand", "client", "lobby", "korpus", "betrieb", "mock"):
|
|
_q = _os.path.join(_w, _u)
|
|
if _os.path.isdir(_q) and _q not in _sys.path:
|
|
_sys.path.insert(0, _q)
|
|
import json
|
|
import os
|
|
import re
|
|
import socket
|
|
import struct
|
|
import sys
|
|
import time
|
|
|
|
LOG = os.path.expanduser("~/Library/Logs/CoinPoker/main.log")
|
|
PIPE = "/tmp/lobby-pipe"
|
|
BASE = os.path.dirname(os.path.abspath(__file__))
|
|
OUT = os.path.join(BASE, "scratch", "probe-log.txt")
|
|
|
|
EVENT = re.compile(
|
|
r'"BeanData":"((?:[^"\\]|\\.)*)","Cmd":"([a-zA-Z_.]+)","RoomName":"((?:[^"\\]|\\.)*)"'
|
|
)
|
|
# Woran eine Reaktion erkennbar ist (nicht an der Verbindung selbst)
|
|
REACTION = ("token", "position", "settings", "user_action", "HUD", "hud", "action",
|
|
"extension", "IPC", "error", "Error")
|
|
|
|
|
|
def candidates(room, tag):
|
|
"""Kandidaten mit unuebersehbarer Wirkung, aufsteigend nach Aufwand."""
|
|
return [
|
|
("1 HUD-Fenster oeffnen",
|
|
{"EventName": "send_to_table", "RequestId": tag,
|
|
"Data": {"room_name": room, "event_type": "action",
|
|
"action": "open_hud_stats_window"}}),
|
|
("2 Lobby in den Vordergrund",
|
|
{"EventName": "send_to_table", "RequestId": tag,
|
|
"Data": {"room_name": room, "event_type": "action", "action": "focus_lobby"}}),
|
|
("3 Tischposition anfragen",
|
|
{"EventName": "send_to_table", "RequestId": tag,
|
|
"Data": {"room_name": room, "event_type": "action", "action": "table_layout",
|
|
"layout_request_type": "table_position_request"}}),
|
|
("4 wie 2, aber an alle Tische",
|
|
{"EventName": "send_to_all_tables", "RequestId": tag,
|
|
"Data": {"event_type": "action", "action": "focus_lobby"}}),
|
|
("5 als Bruxcke getarnt: receive_from_table",
|
|
{"EventName": "receive_from_table", "RequestId": tag,
|
|
"Data": {"room_name": room, "event_type": "action", "action": "focus_lobby"}}),
|
|
]
|
|
|
|
|
|
def connect():
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
sock.settimeout(0.2)
|
|
sock.connect(PIPE)
|
|
drain(sock, 1.2)
|
|
return sock
|
|
|
|
|
|
def drain(sock, seconds):
|
|
end = time.time() + seconds
|
|
while time.time() < end:
|
|
try:
|
|
if sock.recv(65536) == b"":
|
|
return
|
|
except socket.timeout:
|
|
time.sleep(0.05)
|
|
except Exception:
|
|
return
|
|
|
|
|
|
def send(sock, event):
|
|
payload = json.dumps(event).encode()
|
|
sock.sendall(struct.pack(">I", len(payload)) + payload)
|
|
|
|
|
|
def main():
|
|
minutes = 40.0
|
|
if "--minutes" in sys.argv:
|
|
minutes = float(sys.argv[sys.argv.index("--minutes") + 1])
|
|
|
|
out = open(OUT, "w")
|
|
def note(text):
|
|
out.write(text + "\n")
|
|
out.flush()
|
|
|
|
try:
|
|
sock = connect()
|
|
except Exception as exc:
|
|
note(f"# keine Verbindung: {exc}")
|
|
return
|
|
note(f"# Probe gestartet {time.strftime('%H:%M:%S')}, eine Verbindung, {minutes} min "
|
|
f"(Verbindungsrauschen vor dem ersten Schuss ist geleert)")
|
|
|
|
handle = open(LOG, errors="replace")
|
|
handle.seek(0, 2)
|
|
|
|
shot = 0
|
|
last_hand = None
|
|
end = time.time() + minutes * 60
|
|
while time.time() < end:
|
|
line = handle.readline()
|
|
if not line:
|
|
time.sleep(0.15)
|
|
continue
|
|
hit = EVENT.search(line)
|
|
if not hit or hit.group(2) != "game.user_turn":
|
|
continue
|
|
try:
|
|
data = json.loads(json.loads('"' + hit.group(1) + '"'))
|
|
except Exception:
|
|
continue
|
|
if not {s: v for s, v in (data.get("userTurnOptions") or {}).items() if v}:
|
|
continue
|
|
hand = data.get("gameHandId")
|
|
if hand == last_hand:
|
|
continue
|
|
last_hand = hand
|
|
|
|
room = hit.group(3)
|
|
label, event = candidates(room, f"zcode-{shot}")[shot % 5]
|
|
shot += 1
|
|
note(f"\n=== Schuss {shot} | {time.strftime('%H:%M:%S')} | Hand {hand} | {room}")
|
|
note(f" Call {data.get('callAmount')} Pot-Raise {data.get('potRaiseValue')} "
|
|
f"Zeit {data.get('turnTime')}s")
|
|
note(f" Kandidat {label}")
|
|
note(f" gesendet: {json.dumps(event, ensure_ascii=False)[:170]}")
|
|
try:
|
|
drain(sock, 0.3)
|
|
started = time.time()
|
|
send(sock, event)
|
|
note(" gesendet OK")
|
|
except Exception as exc:
|
|
note(f" SENDEFEHLER: {exc} - neu verbinden")
|
|
try:
|
|
sock = connect()
|
|
except Exception:
|
|
pass
|
|
continue
|
|
|
|
reactions = []
|
|
while time.time() - started < 1.6:
|
|
try:
|
|
raw = sock.recv(65536)
|
|
except socket.timeout:
|
|
raw = b""
|
|
except Exception:
|
|
break
|
|
if raw:
|
|
for m in re.finditer(rb'\{"trigger', raw):
|
|
pass
|
|
text = raw.decode("utf-8", "replace")
|
|
for part in re.findall(r'\{"EventName":"[^"]+"', text):
|
|
reactions.append(f"+{int((time.time()-started)*1000)}ms PIPE {part[14:60]}")
|
|
more = handle.readline()
|
|
if not more:
|
|
time.sleep(0.05)
|
|
continue
|
|
low = more.strip()
|
|
if any(w in low for w in REACTION):
|
|
reactions.append(f"+{int((time.time()-started)*1000)}ms LOG {low[:180]}")
|
|
note(f" Reaktionen: {len(reactions)}")
|
|
for r in reactions[:10]:
|
|
note(f" {r}")
|
|
|
|
note(f"\n# Probe beendet {time.strftime('%H:%M:%S')}, {shot} Schuesse")
|
|
out.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|