Просмотр исходного кода

feat(Restart): Deal with soul overflow

ryland 1 год назад
Родитель
Сommit
8eb30842ac

+ 19 - 7
script.py

@@ -351,9 +351,17 @@ class Script:
         self.device.release_during_wait()
         return self.wait_until(next_run)
 
+    def exception_handler(self, e: Exception, command: str) -> None:
+        # 处理御魂溢出
+        from tasks.Utils.post_diagnotor import PostDiagnotor, AnalyzeType
+        image = getattr(self.device, 'image', None)
+        analyse_type = PostDiagnotor().handle(e=e, command=command, image=image)
+        if analyse_type == AnalyzeType.SoulOverflow:
+            self.config.task_call('SoulsTidy')
+            time.sleep(1)
+
     def run(self, command: str) -> bool:
         """
-
         :param command:  大写驼峰命名的任务名字
         :return:
         """
@@ -371,11 +379,13 @@ class Script:
             return True
         except GameNotRunningError as e:
             logger.warning(e)
+            self.exception_handler(e=e, command=command)
             self.config.task_call('Restart')
             return True
         except (GameStuckError, GameTooManyClickError) as e:
             logger.error(e)
             self.save_error_log()
+            self.exception_handler(e=e, command=command)
             logger.warning(f'Game stuck, {self.device.package} will be restarted in 10 seconds')
             logger.warning('If you are playing by hand, please stop Alas')
             self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> GameStuckError or GameTooManyClickError")
@@ -385,32 +395,37 @@ class Script:
         except GameBugError as e:
             logger.warning(e)
             self.save_error_log()
+            self.exception_handler(e=e, command=command)
             logger.warning('An error has occurred in Azur Lane game client, Alas is unable to handle')
             logger.warning(f'Restarting {self.device.package} to fix it')
             self.config.task_call('Restart')
             self.device.sleep(10)
             return False
-        except GamePageUnknownError:
+        except GamePageUnknownError as e:
             logger.info('Game server may be under maintenance or network may be broken, check server status now')
             # 这个还不重要 留着坑填
             logger.critical('Game page unknown')
             self.save_error_log()
+            self.exception_handler(e=e, command=command)
             self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> GamePageUnknownError")
             self.config.task_call('Restart')
             self.device.sleep(10)
             return False
         except ScriptError as e:
             logger.critical(e)
+            self.exception_handler(e=e, command=command)
             logger.critical('This is likely to be a mistake of developers, but sometimes just random issues')
             self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> ScriptError")
             exit(1)
         except RequestHumanTakeover as e:
             logger.critical(e)
+            self.exception_handler(e=e, command=command)
             logger.critical('Request human takeover')
             self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> RequestHumanTakeover")
             exit(1)
         except Exception as e:
             logger.exception(e)
+            self.exception_handler(e=e, command=command)
             self.save_error_log()
             self.config.notifier.push(title=f'{I18n.trans_zh_cn(command)}{command}', content=f"<{self.config_name}> Exception occured")
             exit(1)
@@ -460,8 +475,6 @@ class Script:
 
             # Get task
             task = self.get_next_task()
-            # 更新 gui的任务
-            # Init device and change server
             _ = self.device
             # Skip first restart
             if self.is_first_task and task == 'Restart':
@@ -526,6 +539,5 @@ class Script:
 
 
 if __name__ == "__main__":
-    script = Script("oas1")
-    print(script.gui_task_list())
-    print(script.config.gui_menu)
+    script = Script("oas2")
+    script.loop()

+ 5 - 2
tasks/Script/config_error.py

@@ -14,8 +14,11 @@ class Error(BaseModel):
                                description='是否多次出错关闭模拟器')
     save_error: bool = Field(default=True,
                              description='')
-    screenshot_length: int = Field(default=1,
-                                   description='')
+    screenshot_length: int = Field(
+        default=1,
+        description='',
+        ge=1,
+    )
     notify_enable: bool = Field(default=False,
                                 description='')
     notify_config: MultiLine = Field(default='provider: null', description='notify_config_help')

+ 2 - 0
tasks/SoulsTidy/assets.py

@@ -48,6 +48,8 @@ class SoulsTidyAssets:
 	I_ST_LEVEL_0 = RuleImage(roi_front=(85,235,45,30), roi_back=(85,235,45,30), threshold=0.9, method="Template matching", file="./tasks/SoulsTidy/simple/simple_st_level_0.png")
 	# 御魂奉纳后获得的金币 
 	I_ST_GOLD = RuleImage(roi_front=(170,100,950,500), roi_back=(170,100,950,500), threshold=0.9, method="Template matching", file="./tasks/SoulsTidy/simple/simple_st_gold.png")
+	# 御魂溢出 
+	I_ST_SOUL_OVERFLOW = RuleImage(roi_front=(447,260,384,42), roi_back=(430,232,417,100), threshold=0.8, method="Template matching", file="./tasks/SoulsTidy/simple/simple_st_soul_overflow.png")
 
 
 	# Long Click Rule Assets

+ 2 - 2
tasks/SoulsTidy/config.py

@@ -9,8 +9,8 @@ from tasks.Component.config_base import ConfigBase
 
 class SimpleTidy(BaseModel):
     # 贪吃鬼和招财猫
-    enable_greed: bool = Field(default=False, description="是否启用贪吃鬼")
-    enable_maneki: bool = Field(default=False, description="是否启用奉纳")
+    enable_greed: bool = Field(default=True, description="是否启用贪吃鬼")
+    enable_maneki: bool = Field(default=True, description="是否启用奉纳")
 
 
 class SoulsTidy(ConfigBase):

+ 9 - 0
tasks/SoulsTidy/simple/image.json

@@ -133,5 +133,14 @@
     "method": "Template matching",
     "threshold": 0.9,
     "description": "御魂奉纳后获得的金币"
+  },
+  {
+    "itemName": "st_soul_overflow",
+    "imageName": "simple_st_soul_overflow.png",
+    "roiFront": "447,260,384,42",
+    "roiBack": "430,232,417,100",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "御魂溢出"
   }
 ]

BIN
tasks/SoulsTidy/simple/simple_st_soul_overflow.png


+ 24 - 0
tasks/Utils/post_diagnotor.py

@@ -0,0 +1,24 @@
+import numpy as np
+from enum import Enum
+
+from module.logger import logger
+from module.exception import *
+from module.atom.image import RuleImage
+
+from tasks.GlobalGame.assets import GlobalGameAssets
+from tasks.SoulsTidy.assets import SoulsTidyAssets
+
+
+class AnalyzeType(str, Enum):
+    NONE = "none"
+    SoulOverflow = "soul_overflow"
+
+
+class PostDiagnotor(GlobalGameAssets, SoulsTidyAssets):
+    def handle(self, e: Exception, command: str, image: np.ndarray) -> AnalyzeType:
+        if self.I_UI_CONFIRM_SAMLL.match(image) and \
+                self.I_ST_SOUL_OVERFLOW.match(image):
+            logger.warning(f"Detect soul overflow, command: {command}")
+            return AnalyzeType.SoulOverflow
+
+        return AnalyzeType.NONE