StarRailCopilot/module/base/base.py

96 lines
3.2 KiB
Python
Raw Normal View History

2020-03-28 17:22:46 +00:00
from module.base.button import Button
from module.base.timer import Timer
from module.config.config import AzurLaneConfig
from module.device.device import Device
class ModuleBase:
def __init__(self, config, device=None):
"""
Args:
config (AzurLaneConfig):
device (Device):
"""
self.config = config
self.interval_timer = {}
if device is not None:
self.device = device
else:
self.device = Device(config=config)
def appear(self, button, offset=0, interval=0, threshold=None):
2020-03-28 17:22:46 +00:00
"""
Args:
button (Button, Template):
offset (bool, int):
interval (int, float): interval between two active events.
threshold (int, float): 0 to 1 if use offset, bigger means more similar,
0 to 255 if not use offset, smaller means more similar
2020-03-28 17:22:46 +00:00
Returns:
bool:
"""
if interval:
if button.name not in self.interval_timer:
self.interval_timer[button.name] = Timer(interval)
if not self.interval_timer[button.name].reached():
return False
if offset:
if isinstance(offset, bool):
offset = self.config.BUTTON_OFFSET
appear = button.match(self.device.image, offset=offset,
threshold=self.config.BUTTON_MATCH_SIMILARITY if threshold is None else threshold)
2020-03-28 17:22:46 +00:00
else:
appear = button.appear_on(self.device.image,
threshold=self.config.COLOR_SIMILAR_THRESHOLD if threshold is None else threshold)
2020-03-28 17:22:46 +00:00
if appear and interval:
self.interval_timer[button.name].reset()
return appear
def appear_then_click(self, button, screenshot=False, genre='items', offset=0, interval=0):
appear = self.appear(button, offset=offset, interval=interval)
if appear:
if screenshot:
self.device.sleep(self.config.WAIT_BEFORE_SAVING_SCREEN_SHOT)
self.device.screenshot()
self.device.save_screenshot(genre=genre)
self.device.click(button)
return appear
2020-06-01 00:20:13 +00:00
def wait_until_appear(self, button, offset=0, skip_first_screenshot=False):
2020-03-28 17:22:46 +00:00
while 1:
2020-06-01 00:20:13 +00:00
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
2020-03-28 17:22:46 +00:00
if self.appear(button, offset=offset):
break
def wait_until_appear_then_click(self, button, offset=0):
self.wait_until_appear(button, offset=offset)
self.device.click(button)
def wait_until_disappear(self, button, offset=0):
while 1:
self.device.screenshot()
if not self.appear(button, offset=offset):
break
def image_area(self, button):
"""Extract the area from image.
Args:
button(Button): Button instance.
Returns:
PIL.Image.Image:
"""
return self.device.image.crop(button.area)
def interval_reset(self, button):
if button.name in self.interval_timer:
self.interval_timer[button.name].reset()