Files
pokerfreak-shell/hand/fenster.py
T

114 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Findet die Tischfenster und ordnet sie dem Raumnamen zu.
Tischfenster gehören zum Spielprozess ("CoinPoker Game"); das Lobbyfenster
gehört zur App. Die Zuordnung läuft zuerst über die Tischnummer im Titel -
Übungstische tragen aber nur "CoinPoker" als Titel, deshalb gibt es als
Rückfall: gibt es genau ein Tischfenster, ist es das richtige.
python3 fenster.py --list
"""
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 re
import subprocess
import sys
NUMMER = re.compile(r"\b(\d{5,})\b")
def osa(script):
r = subprocess.run(["osascript", "-e", script], capture_output=True, text=True)
return (r.stdout or r.stderr).strip()
def prozesse():
r = subprocess.run(["pgrep", "-f", "CoinPoker"], capture_output=True, text=True)
return [int(p) for p in r.stdout.split()]
def ist_spielprozess(pid):
r = subprocess.run(["ps", "-o", "command=", "-p", str(pid)],
capture_output=True, text=True)
return "CoinPoker Game" in r.stdout
def _zahl(text, i):
try:
return float(text.replace(" ", "").split(",")[i])
except Exception:
return None
def fenster_von(pid):
proc = f'(first process whose unix id is {pid})'
try:
anzahl = int(osa(f'tell application "System Events" to tell {proc} to count of windows'))
except Exception:
return []
ergebnis = []
for i in range(1, anzahl + 1):
titel = osa(f'tell application "System Events" to tell {proc} to get name of window {i}')
if not titel or "error" in titel.lower():
continue
pos = osa(f'tell application "System Events" to tell {proc} to get position of window {i}')
groesse = osa(f'tell application "System Events" to tell {proc} to get size of window {i}')
if "," not in pos or "," not in groesse:
continue
x, y = _zahl(pos, 0), _zahl(pos, 1)
w, h = _zahl(groesse, 0), _zahl(groesse, 1)
if None in (x, y, w, h):
continue
treffer = NUMMER.search(titel)
ergebnis.append({"pid": pid, "titel": titel, "rect": (x, y, w, h),
"tisch": treffer.group(1) if treffer else None,
"tischfenster": ist_spielprozess(pid)})
return ergebnis
def alle():
ergebnis = []
for pid in prozesse():
ergebnis += fenster_von(pid)
return ergebnis
def tischfenster():
return [f for f in alle() if f["tischfenster"]]
def tisch_id(raum):
treffer = NUMMER.findall(raum or "")
return treffer[-1] if treffer else None
def rect_fuer(raum):
"""Rahmen des Fensters zu diesem Raum - oder None."""
nummer = tisch_id(raum)
fenster = tischfenster()
if nummer:
for f in fenster:
if f["tisch"] == nummer:
return f["rect"]
if len(fenster) == 1:
return fenster[0]["rect"]
return None
if __name__ == "__main__":
fenster = alle()
print(f"{len(fenster)} Fenster gefunden")
for f in fenster:
art = "Tischfenster" if f["tischfenster"] else "Lobby"
kennung = f" (Nr {f['tisch']})" if f["tisch"] else ""
print(f" pid {f['pid']} {art:<12}{kennung} {f['rect']} {f['titel'][:40]}")
for raum in ("Big Ben 1663167", "NLH 1662471 - x"):
print(f" rect_fuer({raum!r}) -> {rect_fuer(raum)}")