1
0
mirror of https://github.com/TheFunny/ArisuAutoSweeper synced 2026-06-24 18:15:01 +00:00

refactor: auto-mission

This commit is contained in:
RedDeadDepresso
2024-01-21 19:12:58 +00:00
parent 91bdc79fa9
commit 78c12a21dc
32 changed files with 496 additions and 229 deletions
@@ -183,6 +183,24 @@ ENTER_SUB = ButtonWrapper(
button=(553, 490, 712, 538),
),
)
LAYOUT = ButtonWrapper(
name='LAYOUT',
jp=None,
en=Button(
file='./assets/en/auto_mission/LAYOUT.png',
area=(1179, 461, 1226, 504),
search=(1159, 441, 1246, 524),
color=(189, 198, 210),
button=(1179, 461, 1226, 504),
),
zht=Button(
file='./assets/zht/auto_mission/LAYOUT.png',
area=(1179, 461, 1226, 504),
search=(1159, 441, 1246, 524),
color=(189, 198, 210),
button=(1179, 461, 1226, 504),
),
)
MISSION_COMPLETE = ButtonWrapper(
name='MISSION_COMPLETE',
jp=None,
@@ -327,6 +345,204 @@ ONE_STAR = ButtonWrapper(
button=(148, 349, 183, 390),
),
)
PRESET1_OFF = ButtonWrapper(
name='PRESET1_OFF',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET1_OFF.png',
area=(52, 143, 197, 181),
search=(32, 123, 217, 201),
color=(253, 253, 254),
button=(52, 143, 197, 181),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET1_OFF.png',
area=(52, 143, 197, 181),
search=(32, 123, 217, 201),
color=(253, 253, 254),
button=(52, 143, 197, 181),
),
)
PRESET1_ON = ButtonWrapper(
name='PRESET1_ON',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET1_ON.png',
area=(54, 146, 200, 182),
search=(34, 126, 220, 202),
color=(48, 77, 115),
button=(54, 146, 200, 182),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET1_ON.png',
area=(54, 146, 200, 182),
search=(34, 126, 220, 202),
color=(48, 77, 115),
button=(54, 146, 200, 182),
),
)
PRESET2_OFF = ButtonWrapper(
name='PRESET2_OFF',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET2_OFF.png',
area=(214, 144, 355, 180),
search=(194, 124, 375, 200),
color=(252, 253, 253),
button=(214, 144, 355, 180),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET2_OFF.png',
area=(214, 144, 355, 180),
search=(194, 124, 375, 200),
color=(252, 253, 253),
button=(214, 144, 355, 180),
),
)
PRESET2_ON = ButtonWrapper(
name='PRESET2_ON',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET2_ON.png',
area=(211, 147, 355, 182),
search=(191, 127, 375, 202),
color=(50, 78, 116),
button=(211, 147, 355, 182),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET2_ON.png',
area=(211, 147, 355, 182),
search=(191, 127, 375, 202),
color=(50, 78, 116),
button=(211, 147, 355, 182),
),
)
PRESET3_OFF = ButtonWrapper(
name='PRESET3_OFF',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET3_OFF.png',
area=(369, 143, 516, 186),
search=(349, 123, 536, 206),
color=(251, 252, 252),
button=(369, 143, 516, 186),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET3_OFF.png',
area=(369, 143, 516, 186),
search=(349, 123, 536, 206),
color=(251, 252, 252),
button=(369, 143, 516, 186),
),
)
PRESET3_ON = ButtonWrapper(
name='PRESET3_ON',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET3_ON.png',
area=(365, 146, 517, 184),
search=(345, 126, 537, 204),
color=(49, 78, 116),
button=(365, 146, 517, 184),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET3_ON.png',
area=(365, 146, 517, 184),
search=(345, 126, 537, 204),
color=(49, 78, 116),
button=(365, 146, 517, 184),
),
)
PRESET4_OFF = ButtonWrapper(
name='PRESET4_OFF',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET4_OFF.png',
area=(527, 146, 675, 183),
search=(507, 126, 695, 203),
color=(252, 252, 253),
button=(527, 146, 675, 183),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET4_OFF.png',
area=(527, 146, 675, 183),
search=(507, 126, 695, 203),
color=(252, 252, 253),
button=(527, 146, 675, 183),
),
)
PRESET4_ON = ButtonWrapper(
name='PRESET4_ON',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET4_ON.png',
area=(528, 148, 676, 178),
search=(508, 128, 696, 198),
color=(50, 78, 117),
button=(528, 148, 676, 178),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET4_ON.png',
area=(528, 148, 676, 178),
search=(508, 128, 696, 198),
color=(50, 78, 117),
button=(528, 148, 676, 178),
),
)
PRESET_FIRST_ITEM = ButtonWrapper(
name='PRESET_FIRST_ITEM',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET_FIRST_ITEM.png',
area=(28, 184, 1252, 397),
search=(8, 164, 1272, 417),
color=(214, 224, 231),
button=(28, 184, 1252, 397),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET_FIRST_ITEM.png',
area=(28, 184, 1252, 397),
search=(8, 164, 1272, 417),
color=(214, 224, 231),
button=(28, 184, 1252, 397),
),
)
PRESET_LIST = ButtonWrapper(
name='PRESET_LIST',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET_LIST.png',
area=(556, 85, 726, 120),
search=(536, 65, 746, 140),
color=(197, 205, 213),
button=(556, 85, 726, 120),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET_LIST.png',
area=(567, 83, 708, 120),
search=(547, 63, 728, 140),
color=(177, 187, 197),
button=(567, 83, 708, 120),
),
)
PRESET_WINDOW = ButtonWrapper(
name='PRESET_WINDOW',
jp=None,
en=Button(
file='./assets/en/auto_mission/PRESET_WINDOW.png',
area=(28, 184, 1252, 649),
search=(8, 164, 1272, 669),
color=(215, 226, 233),
button=(28, 184, 1252, 649),
),
zht=Button(
file='./assets/zht/auto_mission/PRESET_WINDOW.png',
area=(28, 184, 1252, 649),
search=(8, 164, 1272, 669),
color=(215, 226, 233),
button=(28, 184, 1252, 649),
),
)
RANK = ButtonWrapper(
name='RANK',
jp=None,
+167 -64
View File
@@ -7,60 +7,34 @@ from module.base.timer import Timer
from module.exception import RequestHumanTakeover
from module.logger import logger
from tasks.item.data_update import DataUpdate
from module.base.decorator import cached_property
import re
class AutoMissionStatus(Enum):
AP = 0 # Calculate AP and decide to terminate Auto-Mission module or not
NAVIGATE = 1 # Navigate to the area and select mode
ENTER = 2 # Enter the first stage in the stage list
CHECK = 3 # Check stages and find a stage that requires to be completed
START = 4 # Start the stage
FORMATION = 5 # Select units based on the types required by the stage
FIGHT = 6 # Fight the stage
STAGES_DATA = 1
NAVIGATE = 2 # Navigate to the area and select mode
ENTER = 3 # Enter the first stage in the stage list
CHECK = 4 # Check stages and find a stage that requires to be completed
START = 5 # Start the stage
FORMATION = 6 # Select units based on the types required by the stage
FIGHT = 7 # Fight the stage
END = 8
FINISH = -1 # Indicate termination of Auto-Mission module
class AutoMission(AutoMissionUI, Mission):
@property
def mission_info(self) -> list:
valid = True
mode = ("N", "H")
enable = (self.config.Normal_Enable, self.config.Hard_Enable)
area = (self.config.Normal_Area, self.config.Hard_Area)
stages_data = [None, None]
completion_level = (self.config.Normal_Completion, self.config.Hard_Completion)
for index in range(2):
if enable[index]:
stages_data[index] = self.get_stages_data(mode[index], area[index])
valid = valid if self.check_formation(mode[index], area[index], stages_data[index]) else False
if valid:
info = zip(mode, area, stages_data, completion_level)
return list(filter(lambda x: x[2], info))
def check_formation(self, mode, area, stages_data):
mode_name = "Normal" if mode == "N" else "Hard"
if stages_data:
for stage, info in stages_data.items():
if "start" in info:
types = info["start"]
list_unit = []
list_type = []
for type in types:
list_type.append(type)
unit = self.type_to_unit[type]
if unit in list_unit:
logger.error(f"Mission {mode_name} {area} requires {list_type} but they are both set to unit {unit}")
return False
list_unit.append(unit)
if list_unit and list_unit[0] > unit:
logger.error(f"Mission {mode_name} {area} requires {list_type} but they are set to units {list_unit} respectively.\
Due to Auto-Mission's implementation, the first unit's index must be smaller than the second unit's index.")
return False
return True
return False
def __init__(self, config, device):
super().__init__(config, device)
self.task = None
self.previous_mode = None
self.previous_area = None
self._stage = None
self.stages_data = None
self.default_type_to_preset = self.get_default_type_to_preset()
self.current_type_to_preset = None
@cached_property
def type_to_unit(self):
return {
def get_default_type_to_preset(self):
type_to_preset = {
"burst1": self.config.Formation_burst1,
"burst2": self.config.Formation_burst2,
"pierce1": self.config.Formation_pierce1,
@@ -68,6 +42,83 @@ class AutoMission(AutoMissionUI, Mission):
"mystic1": self.config.Formation_mystic1,
"mystic2": self.config.Formation_mystic2
}
valid = True
for type, preset in type_to_preset.items():
preset_list = []
if isinstance(preset, str):
preset = re.sub(r'[ \t\r\n]', '', preset)
preset = preset.split("-")
if len(preset) == 2:
column = preset[0]
row = preset[1]
if (column.isdigit() and 1 <= int(column) <= 4) and (row.isdigit() and 1 <= int(row) <= 5):
preset_list = [int(num) for num in preset]
if not preset_list:
logger.error(f"Failed to read {type}'s preset settings")
valid = False
continue
type_to_preset[type] = preset_list
if not valid:
raise RequestHumanTakeover
return type_to_preset
def validate_area(self, mode, area_input):
area_list = []
if isinstance(area_input, str):
area_input = re.sub(r'[ \t\r\n]', '', area_input)
area_input = (re.sub(r'[>﹥›˃ᐳ❯]', '>', area_input)).split('>')
# tried to convert to set to remove duplicates but doesn't maintain order
[area_list.append(x) for x in area_input if x not in area_list]
elif isinstance(area_input, int):
area_list = [str(area_input)]
if area_list and len([x for x in area_list if x.isdigit()]) == len(area_list):
return area_list
mode_name = "Normal" if mode == "N" else "H"
logger.error(f"Failed to read Mission {mode_name}'s area settings")
return None
def find_alternative(self, type, preset_list):
if not self.config.Formation_Substitute:
return None
alternatives_dictionary = {
'pierce1': ['pierce2', 'burst1', 'burst2', 'mystic1', 'mystic2'],
'pierce2': ['burst1', 'burst2', 'mystic1', 'mystic2'],
'burst1': ['burst2', 'pierce1', 'pierce2', 'mystic1', 'mystic2'],
'burst2': ['pierce1', 'pierce2', 'mystic1', 'mystic2'],
'mystic1': ['mystic2', 'burst1', 'burst2', 'pierce1', 'pierce2'],
'mystic2': ['burst1', 'burst2', 'pierce1', 'pierce2'],
}
alternatives = alternatives_dictionary[type]
for alternative in alternatives:
alternative_preset = self.default_type_to_preset[alternative]
if alternative_preset not in preset_list:
preset_list.append(alternative_preset)
logger.warning(f"{type} was replaced by {alternative}")
return preset_list
logger.error(f"Unable to find replacements for {type}")
return None
@property
def mission_info(self) -> list:
valid = True
mode = ("N", "H")
enable = (self.config.Normal_Enable, self.config.Hard_Enable)
area = (self.config.Normal_Area, self.config.Hard_Area)
area_list = [None, None]
completion_level = (self.config.Normal_Completion, self.config.Hard_Completion)
for index in range(2):
if enable[index]:
area_list[index] = self.validate_area(mode[index], area[index])
valid = valid if area_list[index] else False
if valid:
info = zip(mode, area_list, completion_level)
return list(filter(lambda x: x[1], info))
return None
@property
def current_mode(self):
@@ -75,7 +126,7 @@ class AutoMission(AutoMissionUI, Mission):
@property
def current_area(self):
return self.task[0][1]
return int(self.task[0][1][0])
@property
def current_stage(self):
@@ -85,20 +136,67 @@ class AutoMission(AutoMissionUI, Mission):
def current_stage(self, value):
self._stage = value
@property
def current_stages_data(self):
return self.task[0][2]
@property
def current_completion_level(self):
return self.task[0][3]
return self.task[0][2]
@property
def current_count(self):
return 1
def update_stages_data(self):
if [self.previous_mode, self.previous_area] != [self.current_mode, self.current_area]:
self.stages_data = self.get_stages_data(self.current_mode, self.current_area)
if self.stages_data:
return True
return False
def update_current_type_to_preset(self):
if [self.previous_mode, self.previous_area] == [self.current_mode, self.current_area]:
self.current_type_to_preset = None
return True
mode_name = "Normal" if self.current_mode == "N" else "Hard"
use_alternative = False
for stage, info in self.stages_data.items():
if "start" not in info:
continue
list_preset = []
list_type = []
for type in info["start"]:
preset = self.default_type_to_preset[type]
list_type.append(type)
if preset not in list_preset:
list_preset.append(preset)
continue
logger.error(f"Mission {mode_name} {self.current_area} requires {list_type} but they are both set to preset {preset}")
list_preset = self.find_alternative(type, list_preset)
use_alternative = True
if list_preset:
continue
return False
if use_alternative:
d = {}
for index in range(len(list_type)):
type, preset = list_type[index], list_preset[index]
d[type] = preset
self.current_type_to_preset = d
else:
self.current_type_to_preset = self.default_type_to_preset
return True
return False
def update_task(self):
self.task.pop(0)
self.previous_mode = self.current_mode
self.previous_area = self.current_area
area_list = self.task[0][1]
area_list.pop(0)
if not area_list:
self.task.pop(0)
def handle_auto_mission(self, status):
match status:
@@ -106,42 +204,49 @@ class AutoMission(AutoMissionUI, Mission):
if self.task:
self.realistic_count = self.get_realistic_count()
if self.realistic_count != 0:
return AutoMissionStatus.NAVIGATE
return AutoMissionStatus.STAGES_DATA
return AutoMissionStatus.FINISH
case AutoMissionStatus.STAGES_DATA:
if self.update_stages_data() and self.update_current_type_to_preset():
return AutoMissionStatus.NAVIGATE
return AutoMissionStatus.END
case AutoMissionStatus.NAVIGATE:
switch = SWITCH_NORMAL if self.current_mode == "N" else SWITCH_HARD
self.navigate(self.previous_mode, self.current_mode)
if self.select_area(self.current_area) and self.select_mode(switch):
return AutoMissionStatus.ENTER
raise RequestHumanTakeover
case AutoMissionStatus.ENTER:
if self.wait_mission_info(self.current_mode, open_task=True):
return AutoMissionStatus.CHECK
raise RequestHumanTakeover
case AutoMissionStatus.CHECK:
self.current_stage = self.check_stages(*self.task[0])
self.current_stage = self.check_stages(self.current_mode, self.current_area, self.stages_data, self.current_completion_level)
if self.current_stage:
return AutoMissionStatus.START
self.update_task()
return AutoMissionStatus.AP
return AutoMissionStatus.END
case AutoMissionStatus.START:
self.start_stage(self.current_stage)
return AutoMissionStatus.FORMATION
case AutoMissionStatus.FORMATION:
self.formation(self.current_stage, self.type_to_unit)
self.formation(self.current_stage, self.current_type_to_preset)
return AutoMissionStatus.FIGHT
case AutoMissionStatus.FIGHT:
self.fight(self.current_stage, manual_boss=self.config.ManualBoss_Enable)
# Return to the previous region to prevent map unlock card recognition
self.select_area(self.current_area - 1)
self.update_ap()
self.previous_mode = self.current_mode
self.previous_area = self.current_area
return AutoMissionStatus.AP
case AutoMissionStatus.END:
self.update_task()
return AutoMissionStatus.AP
case AutoMissionStatus.FINISH:
@@ -153,8 +258,6 @@ class AutoMission(AutoMissionUI, Mission):
return status
def run(self):
self.previous_mode = None
self._stage = None
self.task = self.valid_task
if self.task:
action_timer = Timer(0.5, 1)
+29 -5
View File
@@ -1,12 +1,16 @@
from module.base.timer import Timer
from module.logger import logger
from module.ui.switch import Switch
from module.ui.scroll_select import ScrollSelect
from module.ocr.ocr import Digit
from tasks.base.ui import UI
from tasks.base.assets.assets_base_page import MISSION_CHECK
from tasks.auto_mission.assets.assets_auto_mission import *
from tasks.auto_mission.stage import StageState
SCROLL_SELECT = ScrollSelect(PRESET_WINDOW, PRESET_FIRST_ITEM, MOBILIZE, clickx=1150, responsey=60, swipeoffsetx=-600)
PRESETS = [PRESET1_ON, PRESET2_ON, PRESET3_ON, PRESET4_ON]
SWITCH_UNIT1 = Switch('Unit1_Switch')
SWITCH_UNIT1.add_state('on', UNIT1_ON)
SWITCH_UNIT1.add_state('off', UNIT1_OFF)
@@ -74,8 +78,21 @@ class Copilot(UI):
return True
"""Formation methods"""
def choose_unit(self, type, type_to_unit):
unit_index = type_to_unit[type] - 1
def choose_from_preset(self, type, type_to_preset):
preset, row = type_to_preset[type]
preset_index = preset - 1
row_index = row - 1
self.select_then_check(LAYOUT, PRESET_LIST)
#self.set_switch(PRESET_SWITCHES[preset_index])
PRESET = PRESETS[preset_index]
while not self.match_color(PRESET, threshold=50):
self.device.screenshot()
self.click_with_interval(PRESET, interval=1)
clickoffsety = [90, 85, 0, -90, 0]
SCROLL_SELECT.select_index(main=self, target_index=row_index, clickoffsety=clickoffsety[row_index])
def choose_unit(self, unit):
unit_index = unit - 1
unit_switch = UNIT_SWITCHES[unit_index]
self.set_switch(unit_switch)
@@ -86,16 +103,23 @@ class Copilot(UI):
return True
self.click(*start_coords, interval=2)
def formation(self, stage, type_to_unit):
def formation(self, stage, type_to_preset):
if stage.state == StageState.SUB:
# Select a unit to start the battle
self.choose_unit(stage.formation_info, type_to_unit)
self.choose_unit(1)
if type_to_preset:
type = stage.formation_info
self.choose_from_preset(type, type_to_preset)
self.click_with_interval(MOBILIZE, interval=1)
else:
unit = 1
for type, start_coords in stage.formation_start_info:
self.goto_formation_page(start_coords)
self.choose_unit(type, type_to_unit)
self.choose_unit(unit)
if type_to_preset:
self.choose_from_preset(type, type_to_preset)
self.select_then_check(MOBILIZE, MISSION_INFO)
unit += 1
"""Fight methods"""
def begin_mission(self):
+2 -1
View File
@@ -23,7 +23,8 @@ class AutoMissionUI(Copilot):
# Get stage_data data from the module
return stage_data
except ModuleNotFoundError:
logger.error(f"Exploration not supported for area {area}, under development...")
mode_name = "Normal" if mode == "N" else "Hard"
logger.error(f"Exploration not supported for Mission {mode_name} area {area}, under development...")
return None
def wait_mission_info(self, mode, open_task=False, max_retry=99999):