Files
pokerfreak-shell/hand/click.py
T

121 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Klickt die Aktionsknöpfe des Tisches an - Positionen aus dem Fensterrahmen.
Die Knöpfe liegen immer an derselben Stelle im Fenster, deshalb werden die
Positionen als Anteil der Fenstergröße berechnet. Verschieben und Größe ändern
macht also nichts.
python3 click.py --where # Positionen anzeigen
python3 click.py fold # Fold klicken
python3 click.py call # Call klicken
python3 click.py raise # Raise klicken
python3 click.py size 33 # Einsatz-Voreinstellung 33% wählen
"""
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 ctypes
import subprocess
import sys
FW = "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices"
AS = ctypes.cdll.LoadLibrary(FW)
class CGPoint(ctypes.Structure):
_fields_ = [("x", ctypes.c_double), ("y", ctypes.c_double)]
AS.CGEventCreateMouseEvent.restype = ctypes.c_void_p
AS.CGEventCreateMouseEvent.argtypes = [ctypes.c_void_p, ctypes.c_uint32, CGPoint,
ctypes.c_uint32]
AS.CGEventPost.argtypes = [ctypes.c_uint32, ctypes.c_void_p]
MOVED, DOWN, UP = 5, 1, 2
# Anteil der Fensterbreite/-höhe (aus einem Bild vom Tisch abgelesen)
SPOTS = {
"fold": (0.659, 0.943),
"call": (0.796, 0.943),
"raise": (0.933, 0.943),
"size33": (0.616, 0.858),
"size50": (0.677, 0.858),
"size75": (0.735, 0.858),
"max": (0.784, 0.858),
"amount": (0.873, 0.855),
}
def osa(script):
r = subprocess.run(["osascript", "-e", script], capture_output=True, text=True)
return (r.stdout or r.stderr).strip()
def window_rect():
pids = subprocess.run(["pgrep", "-f", "CoinPoker Game"], capture_output=True,
text=True).stdout.split()
if not pids:
return None
proc = f'(first process whose unix id is {pids[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')
try:
x, y = [float(v) for v in pos.replace(" ", "").split(",")[:2]]
w, h = [float(v) for v in size.replace(" ", "").split(",")[:2]]
except Exception:
return None
return x, y, w, h
def point_for(name, rect):
if name not in SPOTS:
return None
x, y, w, h = rect
fx, fy = SPOTS[name]
return round(x + w * fx), round(y + h * fy)
def click(px, py):
for kind in (MOVED, DOWN, UP):
event = AS.CGEventCreateMouseEvent(None, kind, CGPoint(px, py), 0)
AS.CGEventPost(0, event)
AS.CGEventPostToPid(0, event) if False else None
import time as _t
_t.sleep(0.03)
def main():
args = sys.argv[1:]
rect = window_rect()
if not rect:
print("kein Tischfenster")
return 1
x, y, w, h = rect
print(f"Tischfenster: {int(x)},{int(y)} {int(w)}x{int(h)}")
if not args or args[0] == "--where":
for name in SPOTS:
px, py = point_for(name, rect)
print(f" {name:<7} -> {px},{py}")
return 0
key = args[0]
if key == "size":
key = "size" + args[1]
px, py = point_for(key, rect)
if px is None:
print(f"unbekannt: {key}; bekannt: {', '.join(SPOTS)}")
return 1
click(px, py)
print(f"Klick auf {key} bei {px},{py}")
return 0
if __name__ == "__main__":
sys.exit(main())