2021-12-19 19:16:34 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Module to interact with the game."""
|
|
|
|
|
|
|
|
import platform
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
|
import pyautogui
|
|
|
|
|
|
|
|
PLATFORM_SYS = platform.system()
|
|
|
|
|
|
|
|
if PLATFORM_SYS == "Windows":
|
|
|
|
import d3dshot
|
|
|
|
|
|
|
|
|
|
|
|
class AbstractGameIO(ABC):
|
|
|
|
"""Base class for each platform."""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __init__(self):
|
|
|
|
"""Initializes components needed for the I/O to operate."""
|
|
|
|
self.loc = pyautogui.locateOnScreen("images/app.png")
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def fetch_sshot(self):
|
|
|
|
"""Creates a screenshot, and returns it in Pillow format."""
|
|
|
|
pass
|
|
|
|
|
2021-12-19 19:33:57 +00:00
|
|
|
# TODO: Implement the following two functions in descendants.
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def key_up(self, key: str):
|
|
|
|
"""Holds a key on a virtual keyboard."""
|
|
|
|
pass
|
|
|
|
|
2021-12-19 19:16:34 +00:00
|
|
|
@abstractmethod
|
2021-12-19 19:33:57 +00:00
|
|
|
def key_up(self, key: str):
|
|
|
|
"""Lets go of a key on a virtual keyboard."""
|
|
|
|
pass
|
|
|
|
|
2021-12-19 19:16:34 +00:00
|
|
|
def send_key(self, key: str):
|
|
|
|
"""Presses a key on a virtual keyboard."""
|
2021-12-19 19:33:57 +00:00
|
|
|
self.key_up(key)
|
|
|
|
self.key_down(key)
|
2021-12-19 19:16:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WindowsGameIO(AbstractGameIO):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(self)
|
|
|
|
self.d = d3dshot.create()
|
|
|
|
|
|
|
|
def fetch_sshot(self):
|
|
|
|
return self.d.screenshot() # TODO: Cut this to self.loc(x, y, w, h)
|
|
|
|
|
2021-12-19 19:33:57 +00:00
|
|
|
def key_up(self, key: str):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def key_up(self, key: str):
|
|
|
|
pass
|
2021-12-19 19:16:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LinuxGameIO(AbstractGameIO):
|
|
|
|
"""Base class for each platform."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(self)
|
|
|
|
|
|
|
|
def fetch_sshot(self):
|
|
|
|
return pyautogui.screenshot(region=self.loc)
|
|
|
|
|
2021-12-19 19:33:57 +00:00
|
|
|
def key_up(self, key: str):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def key_up(self, key: str):
|
|
|
|
pass
|