Files

85 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""Macht Bilder vom Tisch - und markiert, bei welchem es dein Zug war.
python3 shot.py [sekunden]
"""
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", "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 subprocess
import sys
import time
BASE = os.path.dirname(os.path.abspath(__file__))
LOG = os.path.expanduser("~/Library/Logs/CoinPoker/main.log")
SHOTS = os.path.join(BASE, "scratch", "shots")
EVENT = re.compile(r'"BeanData":"((?:[^"\\]|\\.)*)","Cmd":"game\.user_turn"')
USER = re.compile(r"username=([A-Za-z0-9_]+)")
os.makedirs(SHOTS, exist_ok=True)
duration = float(sys.argv[1]) if len(sys.argv) > 1 else 45.0
def osa(script):
r = subprocess.run(["osascript", "-e", script], capture_output=True, text=True)
return (r.stdout or r.stderr).strip()
pid = subprocess.run(["pgrep", "-f", "CoinPoker Game"], capture_output=True,
text=True).stdout.split()
if not pid:
print("kein Tischfenster")
sys.exit(1)
proc = f'(first process whose unix id is {pid[0]})'
pos = osa(f'tell application "System Events" to tell {proc} to get position of window 1')
size = osa(f'tell application "System Events" to tell {proc} to get size of window 1')
x, y = [int(float(v)) for v in pos.replace(" ", "").split(",")[:2]]
w, h = [int(float(v)) for v in size.replace(" ", "").split(",")[:2]]
rect = f"{x},{y},{w},{h}"
print("Fenster:", rect)
# Namen des Spielers einmal aus der ganzen Datei holen
_whole = open(LOG, errors="replace").read()
_names = re.findall(r"username=([A-Za-z0-9_]+)", _whole)
me = _names[-1] if _names else None
print("Spieler:", me, "| letzte eigene Züge:", len(_whole) - _whole.rfind('game.user_turn') if 'game.user_turn' in _whole else 0)
log = open(LOG, errors="replace")
log.seek(0, 2)
last_own = 0.0
end = time.time() + duration
index = 0
while time.time() < end:
index += 1
out = os.path.join(SHOTS, f"s{index:02d}.png")
subprocess.run(["screencapture", "-x", f"-R{rect}", out], capture_output=True)
while True:
line = log.readline()
if not line:
break
who = USER.search(line)
if who:
me = who.group(1)
hit = EVENT.search(line)
if not hit:
continue
try:
d = json.loads(json.loads('"' + hit.group(1) + '"'))
except Exception:
continue
if {s: v for s, v in (d.get("userTurnOptions") or {}).items() if v}:
me = d.get("whoseTurn") or me
if me and d.get("whoseTurn") == me:
last_own = time.time()
tag = "DEIN ZUG" if time.time() - last_own < 8 else ""
print(f" {out} {tag}")
time.sleep(1.2)