1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-09 18:39:04 +08:00
AzurLaneAutoScript/module/ui/switch.py

149 lines
4.4 KiB
Python
Raw Normal View History

from module.base.base import ModuleBase
from module.base.button import Button
from module.base.timer import Timer
from module.exception import ScriptError
from module.logger import logger
class Switch:
"""
A wrapper to handle switches in game.
Set switch status with reties.
Examples:
# Definitions
submarine_hunt = Switch('Submarine_hunt', offset=120)
submarine_hunt.add_status('on', check_button=SUBMARINE_HUNT_ON)
submarine_hunt.add_status('off', check_button=SUBMARINE_HUNT_OFF)
# Change status to ON
submarine_view.set('on', main=self)
"""
def __init__(self, name='Switch', is_selector=False, offset=0):
2020-08-28 15:01:34 +08:00
"""
Args:
name (str):
is_selector (bool): True if this is a multi choice, click to choose one of the switches.
2020-08-28 15:01:34 +08:00
For example: | [Daily] | Urgent | -> click -> | Daily | [Urgent] |
False if this is a switch, click the switch itself, and it changed in the same position.
For example: | [ON] | -> click -> | [OFF] |
offset (bool, int, tuple): Global offset in current switch
2020-08-28 15:01:34 +08:00
"""
self.name = name
self.is_choice = is_selector
self.offset = offset
self.status_list = []
def add_status(self, status, check_button, click_button=None, offset=0):
"""
Args:
status (str):
check_button (Button):
click_button (Button):
offset (bool, int, tuple):
"""
self.status_list.append({
'status': status,
'check_button': check_button,
'click_button': click_button if click_button is not None else check_button,
'offset': offset if offset else self.offset
})
def appear(self, main):
"""
Args:
main (ModuleBase):
Returns:
bool
"""
for data in self.status_list:
if main.appear(data['check_button'], offset=data['offset']):
return True
return False
2020-07-28 14:56:17 +08:00
def get(self, main):
"""
Args:
main (ModuleBase):
Returns:
str: Status name or 'unknown'.
"""
for data in self.status_list:
if main.appear(data['check_button'], offset=data['offset']):
return data['status']
return 'unknown'
def get_data(self, status):
"""
Args:
status (str):
Returns:
dict: Dictionary in add_status
Raises:
ScriptError: If status invalid
"""
for row in self.status_list:
if row['status'] == status:
return row
logger.warning(f'Switch {self.name} received an invalid status {status}')
raise ScriptError(f'Switch {self.name} received an invalid status {status}')
def set(self, status, main, skip_first_screenshot=True):
"""
Args:
status (str):
main (ModuleBase):
skip_first_screenshot (bool):
Returns:
bool:
"""
self.get_data(status)
counter = 0
changed = False
warning_show_timer = Timer(5, count=10).start()
click_timer = Timer(1, count=3)
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
main.device.screenshot()
# Detect
2020-07-28 14:56:17 +08:00
current = self.get(main=main)
logger.attr(self.name, current)
# End
2020-07-28 14:56:17 +08:00
if current == status:
return changed
# Warning
if current == 'unknown':
if warning_show_timer.reached():
logger.warning(f'Unknown {self.name} switch')
warning_show_timer.reset()
if counter >= 1:
logger.warning(f'{self.name} switch {status} asset has evaluated to unknown too many times, '
f'asset should be re-verified')
return False
counter += 1
continue
# Click
if click_timer.reached():
click_status = status if self.is_choice else current
main.device.click(self.get_data(click_status)['click_button'])
click_timer.reset()
changed = True
return changed