Przeglądaj źródła

perf(task): Each screenshot is verified to see if the invitation appears

huangrunheng 2 lat temu
rodzic
commit
05fba5de1d
3 zmienionych plików z 98 dodań i 69 usunięć
  1. 37 0
      dev_tools/decorator.py
  2. 1 1
      tasks/GlobalGame/config_emergency.py
  3. 60 68
      tasks/base_task.py

+ 37 - 0
dev_tools/decorator.py

@@ -0,0 +1,37 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import timeit
+import time
+from datetime import datetime
+from module.logger import logger
+
+
+class usage_time:
+    """
+    Test the time of the code block
+    Use example:
+    with usage_time('test'):
+        print('running')
+    """
+
+    def __init__(self, name: str = 'Timer'):
+        self.name = name
+
+    def __enter__(self):
+        self.start_time = datetime.now()
+        self.start_time = time.perf_counter()
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.end_time = datetime.now()
+        self.end_time = time.perf_counter()
+        execution_time = self.end_time - self.start_time
+        execution_time = (self.end_time - self.start_time) * 1000000  # 微秒
+        logger.info(f'Execute[{str(self.name)}]: {execution_time:.2f} microseconds')
+
+
+if __name__ == '__main__':
+    with usage_time('test'):
+        print('test1')
+    print('end')

+ 1 - 1
tasks/GlobalGame/config_emergency.py

@@ -25,7 +25,7 @@ class WhenNetworkError(str, Enum):
 # 也可以是左边的邀请什么的
 class Emergency(BaseModel):
     friend_invitation: FriendInvitation = Field(default=FriendInvitation.ACCEPT,description='friend_invitation_help')
-    invitation_detect_interval: int = Field(default=5, description='invitation_detect_interval_help')
+    # invitation_detect_interval: int = Field(default=5, description='invitation_detect_interval_help')
     when_network_abnormal: WhenNetworkAbnormal = Field(default=WhenNetworkAbnormal.WAIT_10S, description='when_network_abnormal_help')
     when_network_error: WhenNetworkError = Field(default=WhenNetworkError.RESTART, description='when_network_error_help')
     home_client_clear: bool = Field(default=True, description='home_client_clear_help')

+ 60 - 68
tasks/base_task.py

@@ -7,6 +7,8 @@ from time import sleep
 from datetime import datetime, timedelta, time
 from typing import Union
 
+from dev_tools.decorator import usage_time
+
 from module.config.utils import convert_to_underscore
 from module.atom.image import RuleImage
 from module.atom.click import RuleClick
@@ -61,6 +63,53 @@ class BaseTask(GlobalGameAssets, CostumeBase):
         # 战斗次数相关
         self.current_count = 0  # 战斗次数
 
+    def _burst(self) -> bool:
+        """
+        游戏界面突发异常检测
+        :return: 没有出现返回False, 其他True
+        """
+        image = self.device.image
+        # appear_invitation = self.appear(self.I_G_ACCEPT)
+        appear_invitation = False
+        if not appear_invitation:
+            return False
+        logger.info('Invitation appearing')
+        invite_type = self.config.global_game.emergency.friend_invitation
+        detect_record = self.device.detect_record
+        match invite_type:
+            case FriendInvitation.ACCEPT:
+                logger.info(f"Accept friend invitation")
+                click_button = self.I_G_ACCEPT
+            case FriendInvitation.REJECT:
+                logger.info(f"Reject friend invitation")
+                click_button = self.I_G_REJECT
+            case FriendInvitation.ONLY_JADE:
+                # 勾协
+                logger.info(f"Only accept jade invitation")
+                if self.appear(self.I_G_JADE):
+                    click_button = self.I_G_ACCEPT
+                else:
+                    click_button = self.I_G_IGNORE
+            case FriendInvitation.JADE_AND_FOOD:
+                # 如果是接受勾协和粮协
+                logger.info(f"Accept jade and food invitation")
+                if self.appear(self.I_G_JADE) or self.appear(self.I_G_CAT_FOOD) or self.appear(self.I_G_DOG_FOOD):
+                    click_button = self.I_G_ACCEPT
+                else:
+                    click_button = self.I_G_IGNORE
+            case FriendInvitation.IGNORE:
+                # 如果是忽略
+                logger.info(f"Ignore friend invitation")
+                click_button = self.I_G_IGNORE
+            case _:
+                raise ScriptError(f'Unknown friend invitation type: {invite_type}')
+        if not click_button:
+            raise ScriptError(f'Unknown click button type: {invite_type}')
+        self.ui_click_until_disappear(click_button)
+        # 有的时候长战斗 点击后会取消战斗状态
+        self.device.detect_record = detect_record
+        return True
+
     def screenshot(self):
         """
         截图 引入中间函数的目的是 为了解决如协作的这类突发的事件
@@ -68,76 +117,19 @@ class BaseTask(GlobalGameAssets, CostumeBase):
         """
         self.device.screenshot()
         # 判断勾协
-        if self.friend_timer and self.friend_timer.reached():
-            self.friend_timer.reset()
-            invite = self.appear(self.I_G_ACCEPT)
-            detect_record = self.device.detect_record
-            # 如果是全部接受
-            if invite and self.config.global_game.emergency.friend_invitation == FriendInvitation.ACCEPT:
-                # 如果是接受邀请
-                logger.info(f"Accept friend invitation")
-                while 1:
-                    self.device.screenshot()
-                    if self.appear_then_click(self.I_G_ACCEPT, interval=1):
-                        continue
-                    if not self.appear(self.I_G_ACCEPT):
-                        break
-            # 如果是全部拒绝
-            elif invite and self.config.global_game.emergency.friend_invitation == FriendInvitation.REJECT:
-                logger.info(f"Reject friend invitation")
-                while 1:
-                    self.device.screenshot()
-                    if self.appear_then_click(self.I_G_REJECT, interval=1):
-                        continue
-                    if not self.appear(self.I_G_REJECT):
-                        break
-            # 如果是仅接受勾协
-            elif invite and self.config.global_game.emergency.friend_invitation == FriendInvitation.ONLY_JADE:
-                logger.info(f"Accept jade invitation")
-                while 1:
-                    self.device.screenshot()
-                    if self.appear(self.I_G_JADE):
-                        if self.appear_then_click(self.I_G_ACCEPT, interval=1):
-                            continue
-                    elif self.appear_then_click(self.I_G_IGNORE, interval=1):
-                        continue
-                    if not self.appear(self.I_G_ACCEPT):
-                        break
-            # 如果是接受勾协和粮协
-            elif invite and self.config.global_game.emergency.friend_invitation == FriendInvitation.JADE_AND_FOOD:
-                logger.info(f"Accept jade and food invitation")
-                while 1:
-                    self.device.screenshot()
-                    if self.appear(self.I_G_JADE) or self.appear(self.I_G_CAT_FOOD) or self.appear(self.I_G_DOG_FOOD):
-                        if self.appear_then_click(self.I_G_ACCEPT, interval=1):
-                            continue
-                    elif self.appear_then_click(self.I_G_IGNORE, interval=1):
-                        continue
-                    if not self.appear(self.I_G_ACCEPT):
-                        break
-            # 如果是全部忽略
-            elif invite and self.config.global_game.emergency.friend_invitation == FriendInvitation.IGNORE:
-                logger.info(f"Ignore friend invitation")
-                while 1:
-                    self.device.screenshot()
-                    if self.appear_then_click(self.I_G_IGNORE, interval=1):
-                        continue
-                    if not self.appear(self.I_G_IGNORE):
-                        break
-            # 有的时候长战斗 点击后会取消战斗状态
-            self.device.detect_record = detect_record
-            # 判断网络异常
-            if self.appear(self.I_NETWORK_ABNORMAL):
-                logger.warning(f"Network abnormal")
-                raise GameStuckError
-
-            # 判断网络错误
-            if self.appear(self.I_NETWORK_ERROR):
-                logger.warning(f"Network error")
-                raise GameStuckError
+        self._burst()
+
+        # # 判断网络异常
+        # if self.appear(self.I_NETWORK_ABNORMAL):
+        #     logger.warning(f"Network abnormal")
+        #     raise GameStuckError
+        #
+        # # 判断网络错误
+        # if self.appear(self.I_NETWORK_ERROR):
+        #     logger.warning(f"Network error")
+        #     raise GameStuckError
 
         return self.device.image
-        # 返回截图
 
     def appear(self, target: RuleImage, interval: float = None, threshold: float = None):
         """