1
0
mirror of https://github.com/0O0o0oOoO00/Alas.git synced 2026-05-14 09:09:26 +08:00

add: support game auto restart

This commit is contained in:
0O0o0oOoO00
2025-09-12 19:15:32 +08:00
parent cf0c93a766
commit 1918e56dbb
10 changed files with 100 additions and 117 deletions

129
alas.py
View File

@@ -587,27 +587,6 @@ class AzurLaneAutoScript:
from module.luahook.crack import *
from module.counter import *
class FailedTaskCounterManager:
def __init__(self, config: AzurLaneConfig):
self.task_counter = {}
if config.full_config.Restart_GameRestart_Enable:
self.counter_max_count = config.full_config.Restart_GameRestart_MaxRetryTimes
self.counter_class = MaxCounter
else:
self.counter_max_count = 0
self.counter_class = Counter
def count(self, task, throw=True) -> bool:
counter = self.task_counter.get(task, self.counter_class(self.counter_max_count))
return counter.count_once(throw=throw)
def reset(self, task):
counter = self.task_counter.get(task, self.counter_class(self.counter_max_count))
counter.reset()
class AzurLaneAutoScript(AzurLaneAutoScript):
@@ -623,7 +602,9 @@ class AzurLaneAutoScript(AzurLaneAutoScript):
elif self.class_name == "ArknightsAutoScript":
self.is_ark = True
self.failed_task_counter = FailedTaskCounterManager(self.config)
full_config = self.config.full_config
self.is_fatal_error_restart = full_config.Restart_GameRestart_FatalErrorRestart
self.max_retry_times_for_same_task_failed = full_config.Restart_GameRestart_MaxRetryTimesForSameTaskFailed
def pre_init(self):
from PIL import Image
@@ -680,12 +661,18 @@ class AzurLaneAutoScript(AzurLaneAutoScript):
if self.checker.is_available():
logger.critical('Game page unknown')
self.save_error_log()
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> GamePageUnknownError",
)
exit(1)
if self.is_fatal_error_restart:
logger.info("Fatal error occured, call restart")
self.config.task_call('Restart')
self.device.sleep(10)
return False
else:
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> GamePageUnknownError",
)
exit(1)
else:
self.checker.wait_until_available()
return False
@@ -693,31 +680,49 @@ class AzurLaneAutoScript(AzurLaneAutoScript):
def handle_ScriptError(self, e) -> bool:
logger.exception(e)
logger.critical('This is likely to be a mistake of developers, but sometimes just random issues')
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> ScriptError",
)
exit(1)
if self.is_fatal_error_restart:
logger.info("Fatal error occured, call restart")
self.config.task_call('Restart')
self.device.sleep(10)
return False
else:
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> ScriptError",
)
exit(1)
def handle_RequestHumanTakeover(self, e) -> bool:
logger.critical('Request human takeover')
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> RequestHumanTakeover",
)
exit(1)
if self.is_fatal_error_restart:
logger.info("Fatal error occured, call restart")
self.config.task_call('Restart')
self.device.sleep(10)
return False
else:
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> RequestHumanTakeover",
)
exit(1)
def handle_Exception(self, e) -> bool:
logger.exception(e)
self.save_error_log()
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> Exception occured",
)
exit(1)
if self.is_fatal_error_restart:
logger.info("Fatal error occured, call restart")
self.config.task_call('Restart')
self.device.sleep(10)
return False
else:
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> Exception occured",
)
exit(1)
def run(self, command, skip_first_screenshot=False):
try:
@@ -863,23 +868,25 @@ class AzurLaneAutoScript(AzurLaneAutoScript):
logger.info(f'Scheduler: End task `{task}`')
self.is_first_task = False
if not success:
if not self.failed_task_counter.count(task, throw=False):
logger.critical(f"Task `{task}` failed 3 or more times.")
logger.critical("Possible reason #1: You haven't used it correctly. "
"Please read the help text of the options.")
logger.critical("Possible reason #2: There is a problem with this task. "
"Please contact developers or try to fix it yourself.")
logger.critical('Request human takeover')
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> RequestHumanTakeover\nTask `{task}` failed 3 or more times.",
)
exit(1)
# Check failures
failed = deep_get(self.failure_record, keys=task, default=0)
failed = 0 if success else failed + 1
deep_set(self.failure_record, keys=task, value=failed)
if failed >= self.max_retry_times_for_same_task_failed:
logger.critical(f"Task `{task}` failed {self.max_retry_times_for_same_task_failed} or more times.")
logger.critical("Possible reason #1: You haven't used it correctly. "
"Please read the help text of the options.")
logger.critical("Possible reason #2: There is a problem with this task. "
"Please contact developers or try to fix it yourself.")
logger.critical('Request human takeover')
handle_notify(
self.config.Error_OnePushConfig,
title=f"Alas <{self.config_name}> crashed",
content=f"<{self.config_name}> RequestHumanTakeover\nTask `{task}` failed {self.max_retry_times_for_same_task_failed} or more times.",
)
exit(1)
if success:
self.failed_task_counter.reset(task)
del_cached_property(self, 'config')
continue
elif self.config.Error_HandleError: