Skip to content

Commit

Permalink
检测连续等待并重启游戏
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoZuohong committed Nov 3, 2023
1 parent 3b28f15 commit 5f15989
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion arknights_mower/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def simulate():
if conf["close_simulator_when_idle"]:
restart_simulator(conf["simulator"], start=False)
elif conf["exit_game_when_idle"]:
base_scheduler.device.exit(base_scheduler.package_name)
base_scheduler.device.exit()
body = task_template.render(
tasks=[
obj.format(timezone_offset) for obj in base_scheduler.tasks
Expand Down
10 changes: 5 additions & 5 deletions arknights_mower/solvers/base_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def overtake_room(self):

def handle_error(self, force=False):
if self.scene() == Scene.UNKNOWN:
self.device.exit(self.package_name)
self.device.exit()
if self.error or force:
# 如果没有任何时间小于当前时间的任务才生成空任务
if find_next_task(self.tasks, datetime.now()) is None:
Expand Down Expand Up @@ -2347,7 +2347,7 @@ def maa_plan_solver(self, tasks='All', one_time=False):
logger.info(hard_stop_msg)
self.send_message(hard_stop_msg)
time.sleep(180)
self.device.exit(self.package_name)
self.device.exit()
elif not one_time:
logger.info(f"记录MAA 本次执行时间")
self.maa_config['last_execution'] = datetime.now()
Expand Down Expand Up @@ -2424,7 +2424,7 @@ def maa_plan_solver(self, tasks='All', one_time=False):
break
else:
time.sleep(0)
self.device.exit(self.package_name)
self.device.exit()
# 生息演算逻辑 结束
if one_time:
if len(self.tasks) > 0:
Expand All @@ -2446,15 +2446,15 @@ def maa_plan_solver(self, tasks='All', one_time=False):
if self.close_simulator_when_idle:
restart_simulator(self.simulator, start=False)
elif self.exit_game_when_idle:
self.device.exit(self.package_name)
self.device.exit()
time.sleep(remaining_time)
if self.close_simulator_when_idle:
restart_simulator(self.simulator, stop=False)
self.MAA = None
except Exception as e:
logger.exception(e)
self.MAA = None
self.device.exit(self.package_name)
self.device.exit()
self.send_message(str(e), "Maa调用出错!")
remaining_time = (self.tasks[0].time - datetime.now()).total_seconds()
if remaining_time > 0:
Expand Down
6 changes: 4 additions & 2 deletions arknights_mower/utils/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def run(self, cmd: str) -> Optional[bytes]:

def launch(self) -> None:
""" launch the application """
logger.info("明日方舟,启动!")

tap = config.TAP_TO_LAUNCH["enable"]
x = config.TAP_TO_LAUNCH["x"]
y = config.TAP_TO_LAUNCH["y"]
Expand All @@ -89,10 +91,10 @@ def launch(self) -> None:
else:
self.run(f'am start -n {config.APPNAME}/{config.APP_ACTIVITY_NAME}')

def exit(self, app: str) -> None:
def exit(self) -> None:
""" exit the application """
logger.info("退出游戏")
self.run(f'am force-stop {app}')
self.run(f'am force-stop {config.APPNAME}')

def send_keyevent(self, keycode: int) -> None:
""" send a key event """
Expand Down
28 changes: 28 additions & 0 deletions arknights_mower/utils/recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Recognizer(object):
def __init__(self, device: Device, screencap: bytes = None) -> None:
self.device = device
self.start(screencap)
self.loading_time = 0
self.LOADING_TIME_LIMIT = 5

def start(self, screencap: bytes = None, build: bool = True) -> None:
""" init with screencap, build matcher """
Expand Down Expand Up @@ -220,6 +222,19 @@ def get_scene(self) -> int:
if config.SCREENSHOT_PATH is not None:
self.save_screencap(self.scene)
logger.info(f'Scene: {self.scene}: {SceneComment[self.scene]}')

if self.scene == Scene.CONNECTING:
self.loading_time += 1
if self.loading_time > 1:
logger.debug(f"检测到连续等待{self.loading_time}次")
else:
self.loading_time = 0
if self.loading_time > self.LOADING_TIME_LIMIT:
logger.info(f"检测到连续等待{self.loading_time}次")
self.device.exit()
time.sleep(3)
self.device.check_current_focus()

return self.scene

def get_infra_scene(self)-> int:
Expand Down Expand Up @@ -265,6 +280,19 @@ def get_infra_scene(self)-> int:
if config.SCREENSHOT_PATH is not None:
self.save_screencap(self.scene)
logger.info(f'Scene: {self.scene}: {SceneComment[self.scene]}')

if self.scene == Scene.CONNECTING:
self.loading_time += 1
if self.loading_time > 1:
logger.debug(f"检测到连续等待{self.loading_time}次")
else:
self.loading_time = 0
if self.loading_time > self.LOADING_TIME_LIMIT:
logger.info(f"检测到连续等待{self.loading_time}次")
self.device.exit()
time.sleep(3)
self.device.check_current_focus()

return self.scene

def is_black(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions arknights_mower/utils/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def back_to_reclamation_algorithm(self):
self.recog.update()
while self.find('index_terminal') is None:
if self.scene() == Scene.UNKNOWN:
self.device.exit('com.hypergryph.arknights')
self.device.exit()
self.back_to_index()
logger.info('导航至生息演算')
self.tap_themed_element('index_terminal')
Expand Down Expand Up @@ -404,7 +404,7 @@ def waiting_solver(self, scenes, wait_count=20, sleep_time=3):
return True
wait_count -= 1
logger.warning("同一等待界面等待超时,重启方舟。")
self.device.exit(self.package_name)
self.device.exit()
time.sleep(3)
self.device.check_current_focus()
return False
Expand Down

0 comments on commit 5f15989

Please sign in to comment.