mirror of
https://github.com/0O0o0oOoO00/Alas.git
synced 2026-05-14 12:29:25 +08:00
add: support game auto restart
This commit is contained in:
61
alas.py
61
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,6 +661,12 @@ class AzurLaneAutoScript(AzurLaneAutoScript):
|
||||
if self.checker.is_available():
|
||||
logger.critical('Game page unknown')
|
||||
self.save_error_log()
|
||||
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",
|
||||
@@ -693,6 +680,12 @@ 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')
|
||||
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",
|
||||
@@ -702,6 +695,12 @@ class AzurLaneAutoScript(AzurLaneAutoScript):
|
||||
|
||||
def handle_RequestHumanTakeover(self, e) -> bool:
|
||||
logger.critical('Request human takeover')
|
||||
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",
|
||||
@@ -712,6 +711,12 @@ class AzurLaneAutoScript(AzurLaneAutoScript):
|
||||
def handle_Exception(self, e) -> bool:
|
||||
logger.exception(e)
|
||||
self.save_error_log()
|
||||
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",
|
||||
@@ -863,9 +868,12 @@ 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.")
|
||||
# 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. "
|
||||
@@ -874,12 +882,11 @@ class AzurLaneAutoScript(AzurLaneAutoScript):
|
||||
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.",
|
||||
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:
|
||||
|
||||
@@ -74,9 +74,8 @@
|
||||
"ServerUpdate": "00:00"
|
||||
},
|
||||
"GameRestart": {
|
||||
"Enable": false,
|
||||
"MaxRetryTimes": 5,
|
||||
"Notify": false
|
||||
"FatalErrorRestart": false,
|
||||
"MaxRetryTimesForSameTaskFailed": 5
|
||||
},
|
||||
"InstanceRestart": {
|
||||
"Enable": false,
|
||||
|
||||
@@ -414,17 +414,13 @@
|
||||
}
|
||||
},
|
||||
"GameRestart": {
|
||||
"Enable": {
|
||||
"FatalErrorRestart": {
|
||||
"type": "checkbox",
|
||||
"value": false
|
||||
},
|
||||
"MaxRetryTimes": {
|
||||
"MaxRetryTimesForSameTaskFailed": {
|
||||
"type": "input",
|
||||
"value": 5
|
||||
},
|
||||
"Notify": {
|
||||
"type": "checkbox",
|
||||
"value": false
|
||||
}
|
||||
},
|
||||
"InstanceRestart": {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -2922,17 +2922,13 @@
|
||||
"name": "游戏重启",
|
||||
"help": "该功能的启用和关闭需要重启这个Alas实例"
|
||||
},
|
||||
"Enable": {
|
||||
"name": "启用",
|
||||
"FatalErrorRestart": {
|
||||
"name": "致命错误重启",
|
||||
"help": ""
|
||||
},
|
||||
"MaxRetryTimes": {
|
||||
"name": "最大尝试重启次数",
|
||||
"MaxRetryTimesForSameTaskFailed": {
|
||||
"name": "同一个任务最多重启 X 次",
|
||||
"help": ""
|
||||
},
|
||||
"Notify": {
|
||||
"name": "重启时通知",
|
||||
"help": "不建议开启,防止消息轰炸"
|
||||
}
|
||||
},
|
||||
"InstanceRestart": {
|
||||
|
||||
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user