Skip to content

Commit

Permalink
TEMP: Feat autowho
Browse files Browse the repository at this point in the history
  • Loading branch information
Amund211 committed Sep 14, 2024
1 parent 7caf965 commit 8e724c4
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/prism/overlay/real_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
get_estimated_winstreaks,
get_playerdata,
)
from prism.overlay.controller import ERROR_DURING_PROCESSING, ProcessingError
from prism.overlay.controller import (
ERROR_DURING_PROCESSING,
OverlayController,
ProcessingError,
)
from prism.overlay.player import MISSING_WINSTREAKS
from prism.ratelimiting import RateLimiter
from prism.ssl_errors import MissingLocalIssuerSSLError
Expand Down Expand Up @@ -65,6 +69,8 @@ def __init__(
else None
)

AutoWhoThread(self).start()

def get_uuid(self, username: str) -> str | None | ProcessingError:
try:
uuid = get_uuid(username)
Expand Down Expand Up @@ -133,3 +139,38 @@ def get_estimated_winstreaks(

def store_settings(self) -> None:
self.settings.flush_to_disk()


class AutoWhoThread(threading.Thread): # pragma: nocover
"""Thread that types /who on request, unless cancelled"""

def __init__(self, controller: OverlayController) -> None:
super().__init__(daemon=True) # Don't block the process from exiting
self.controller = controller

def run(self) -> None:
"""Wait for requests, check if not cancelled, then type /who"""
try:
while True:
self.controller.autowho_event.wait()
# The bedwars game start event is parsed also at the end of a game.
# We cancel this by parsing a bedwars game end event right after, but
# need to give a bit of time for the overlay to process this event so
# we don't send /who at the end of a game as well.
# time.sleep(self.controller.settings.autowho_delay + 0.1)
time.sleep(2)
if not self.controller.autowho_event.is_set():
continue

self.controller.autowho_event.clear()

from pynput.keyboard import Controller, Key

keyboard = Controller()
keyboard.type("u")
time.sleep(0.1)
keyboard.type("/who")
keyboard.tap(Key.enter)
except Exception:
logger.exception("Exception caught in autowho thread. Exiting.")
# We let the overlay keep working if the autowho thread dies

0 comments on commit 8e724c4

Please sign in to comment.