diff --git a/alas.py b/alas.py index bb6c252e2..dd97241d8 100644 --- a/alas.py +++ b/alas.py @@ -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: diff --git a/config/template.json b/config/template.json index e2a0c5817..db5688af8 100644 --- a/config/template.json +++ b/config/template.json @@ -74,9 +74,8 @@ "ServerUpdate": "00:00" }, "GameRestart": { - "Enable": false, - "MaxRetryTimes": 5, - "Notify": false + "FatalErrorRestart": false, + "MaxRetryTimesForSameTaskFailed": 5 }, "InstanceRestart": { "Enable": false, diff --git a/module/config/argument/args.json b/module/config/argument/args.json index 7de994d00..a0432280a 100644 --- a/module/config/argument/args.json +++ b/module/config/argument/args.json @@ -414,17 +414,13 @@ } }, "GameRestart": { - "Enable": { + "FatalErrorRestart": { "type": "checkbox", "value": false }, - "MaxRetryTimes": { + "MaxRetryTimesForSameTaskFailed": { "type": "input", "value": 5 - }, - "Notify": { - "type": "checkbox", - "value": false } }, "InstanceRestart": { diff --git a/module/config/argument/argument.yaml b/module/config/argument/argument.yaml index bc85630d1..59374412c 100644 --- a/module/config/argument/argument.yaml +++ b/module/config/argument/argument.yaml @@ -844,9 +844,8 @@ PowerLimit: Raid: 25000 Ash: 25000 GameRestart: - Enable: false - MaxRetryTimes: 5 - Notify: false + FatalErrorRestart: false + MaxRetryTimesForSameTaskFailed: 5 InstanceRestart: Enable: false MaxRetryTimes: 5 diff --git a/module/config/config_generated.py b/module/config/config_generated.py index 6963ceba3..5c908d21a 100644 --- a/module/config/config_generated.py +++ b/module/config/config_generated.py @@ -522,9 +522,8 @@ class GeneratedConfig: PowerLimit_Ash = 25000 # Group `GameRestart` - GameRestart_Enable = False - GameRestart_MaxRetryTimes = 5 - GameRestart_Notify = False + GameRestart_FatalErrorRestart = False + GameRestart_MaxRetryTimesForSameTaskFailed = 5 # Group `InstanceRestart` InstanceRestart_Enable = False diff --git a/module/config/full_config_generated.py b/module/config/full_config_generated.py index 2e9603205..95fc7e412 100644 --- a/module/config/full_config_generated.py +++ b/module/config/full_config_generated.py @@ -57,9 +57,8 @@ class FullGeneratedConfig: Restart_Scheduler_SuccessInterval = None Restart_Scheduler_FailureInterval = None Restart_Scheduler_ServerUpdate = None - Restart_GameRestart_Enable = None - Restart_GameRestart_MaxRetryTimes = None - Restart_GameRestart_Notify = None + Restart_GameRestart_FatalErrorRestart = None + Restart_GameRestart_MaxRetryTimesForSameTaskFailed = None Restart_InstanceRestart_Enable = None Restart_InstanceRestart_MaxRetryTimes = None Restart_InstanceRestart_Notify = None diff --git a/module/config/i18n/en-US.json b/module/config/i18n/en-US.json index 4bade8955..1c8348e28 100644 --- a/module/config/i18n/en-US.json +++ b/module/config/i18n/en-US.json @@ -2922,17 +2922,13 @@ "name": "GameRestart._info.name", "help": "GameRestart._info.help" }, - "Enable": { - "name": "GameRestart.Enable.name", - "help": "GameRestart.Enable.help" + "FatalErrorRestart": { + "name": "GameRestart.FatalErrorRestart.name", + "help": "GameRestart.FatalErrorRestart.help" }, - "MaxRetryTimes": { - "name": "GameRestart.MaxRetryTimes.name", - "help": "GameRestart.MaxRetryTimes.help" - }, - "Notify": { - "name": "GameRestart.Notify.name", - "help": "GameRestart.Notify.help" + "MaxRetryTimesForSameTaskFailed": { + "name": "GameRestart.MaxRetryTimesForSameTaskFailed.name", + "help": "GameRestart.MaxRetryTimesForSameTaskFailed.help" } }, "InstanceRestart": { diff --git a/module/config/i18n/ja-JP.json b/module/config/i18n/ja-JP.json index 413343235..9ec58ed84 100644 --- a/module/config/i18n/ja-JP.json +++ b/module/config/i18n/ja-JP.json @@ -2922,17 +2922,13 @@ "name": "GameRestart._info.name", "help": "GameRestart._info.help" }, - "Enable": { - "name": "GameRestart.Enable.name", - "help": "GameRestart.Enable.help" + "FatalErrorRestart": { + "name": "GameRestart.FatalErrorRestart.name", + "help": "GameRestart.FatalErrorRestart.help" }, - "MaxRetryTimes": { - "name": "GameRestart.MaxRetryTimes.name", - "help": "GameRestart.MaxRetryTimes.help" - }, - "Notify": { - "name": "GameRestart.Notify.name", - "help": "GameRestart.Notify.help" + "MaxRetryTimesForSameTaskFailed": { + "name": "GameRestart.MaxRetryTimesForSameTaskFailed.name", + "help": "GameRestart.MaxRetryTimesForSameTaskFailed.help" } }, "InstanceRestart": { diff --git a/module/config/i18n/zh-CN.json b/module/config/i18n/zh-CN.json index e7d918465..e2619600b 100644 --- a/module/config/i18n/zh-CN.json +++ b/module/config/i18n/zh-CN.json @@ -2922,17 +2922,13 @@ "name": "游戏重启", "help": "该功能的启用和关闭需要重启这个Alas实例" }, - "Enable": { - "name": "启用", + "FatalErrorRestart": { + "name": "致命错误重启", "help": "" }, - "MaxRetryTimes": { - "name": "最大尝试重启次数", + "MaxRetryTimesForSameTaskFailed": { + "name": "同一个任务最多重启 X 次", "help": "" - }, - "Notify": { - "name": "重启时通知", - "help": "不建议开启,防止消息轰炸" } }, "InstanceRestart": { diff --git a/module/config/i18n/zh-TW.json b/module/config/i18n/zh-TW.json index aa1dffdc0..816977b6b 100644 --- a/module/config/i18n/zh-TW.json +++ b/module/config/i18n/zh-TW.json @@ -2922,17 +2922,13 @@ "name": "GameRestart._info.name", "help": "GameRestart._info.help" }, - "Enable": { - "name": "GameRestart.Enable.name", - "help": "GameRestart.Enable.help" + "FatalErrorRestart": { + "name": "GameRestart.FatalErrorRestart.name", + "help": "GameRestart.FatalErrorRestart.help" }, - "MaxRetryTimes": { - "name": "GameRestart.MaxRetryTimes.name", - "help": "GameRestart.MaxRetryTimes.help" - }, - "Notify": { - "name": "GameRestart.Notify.name", - "help": "GameRestart.Notify.help" + "MaxRetryTimesForSameTaskFailed": { + "name": "GameRestart.MaxRetryTimesForSameTaskFailed.name", + "help": "GameRestart.MaxRetryTimesForSameTaskFailed.help" } }, "InstanceRestart": {