Jelajahi Sumber

fix: image list find add sleep, page path log pattern, ws runtime error

Azure 1 tahun lalu
induk
melakukan
32770f0c15

+ 12 - 7
module/server/script_websocket.py

@@ -19,27 +19,32 @@ class ScriptWSManager:
         try:
             # 给前端发送最后一次关闭信号
             await ws.close()
-        except RuntimeError:
-            pass
+        except RuntimeError as e:
+            print(e)
 
     async def broadcast(self, message: str):
         # 广播消息
         for connection in self.active_connections:
-            await connection.send_text(message)
+            try:
+                await connection.send_text(message)
+            except RuntimeError:
+                await self.disconnect(connection)
 
     async def broadcast_state(self, data: dict):
         # 广播自身的状态
         for connection in self.active_connections:
-            await connection.send_json(data)
+            try:
+                await connection.send_json(data)
+            except RuntimeError:
+                await self.disconnect(connection)
 
     async def broadcast_log(self, log: str):
         # 广播日志
         for connection in self.active_connections:
             try:
                 await connection.send_text(log)
-            except RuntimeError as e:
-                print(e)
-                self.active_connections.remove(connection)
+            except RuntimeError:
+                await self.disconnect(connection)
 
 
 

+ 1 - 1
tasks/ActivityShikigami/script_task.py

@@ -449,7 +449,7 @@ class ScriptTask(GameUi, BaseActivity, SwitchSoul, ActivityShikigamiAssets):
         self.ui_goto(game.page_main)
 
     def goto_act(self):
-        self.ui_goto(self.page_map[self.climb_type], timeout=20)
+        self.ui_goto(self.page_map[self.climb_type], timeout=25)
 
     def random_reward_click(self, exclude_click: list = None, click_now: bool = True) -> RuleClick:
         """

+ 3 - 2
tasks/GameUi/game_ui.py

@@ -246,7 +246,7 @@ class GameUi(BaseTask, GameUiAssets):
                 if not self.ui_page_appear(page):
                     continue
                 logger.info(f"Current page: {page}. Following shortest path:")
-                show_paths: str = ''.join([f"{p.name} -> " for p in path])
+                show_paths: str = ' -> '.join([p.name for p in path])
                 logger.info(f" {show_paths}")
                 if self._execute_path(path, confirm_timer, timeout_timer):
                     return True
@@ -278,7 +278,8 @@ class GameUi(BaseTask, GameUiAssets):
                             self.appear_then_click(button, interval=0.6) or
                             (isinstance(button, RuleOcr) and self.ocr_appear_click(button, interval=2))):
                         logger.info(f'Page {current_page} additional {button} clicked')
-                sleep(0.3)
+                        # 每次点击之间添加随机延迟, 等待响应
+                        sleep(random.uniform(0.2, 0.6))
             # 执行页面跳转
             button = current_page.links.get(next_page)
             if not button:

+ 23 - 23
tasks/base_task.py

@@ -4,6 +4,7 @@
 
 from time import sleep
 
+import random
 from datetime import datetime, timedelta
 from module.atom.animate import RuleAnimate
 from module.atom.click import RuleClick
@@ -468,31 +469,30 @@ class BaseTask(GlobalGameAssets, CostumeBase):
         :param name:
         :return:
         """
-        if target.is_image:
-            while True:
-                self.screenshot()
+        swipe_down = False
+        swipe_distance_ratio = None
+        result = None
+        if not target:
+            return False
+        while True:
+            self.screenshot()
+            if target.is_image:
                 result = target.image_appear(self.device.image, name=name)
-                if result is not None:
-                    return result
-                x1, y1, x2, y2 = target.swipe_pos()
-                self.device.swipe(p1=(x1, y1), p2=(x2, y2))
-
-        elif target.is_ocr:
-            while True:
-                self.screenshot()
+                swipe_down = True
+            elif target.is_ocr:
                 result = target.ocr_appear(self.device.image, name=name)
-                if isinstance(result, tuple):
-                    return result
-
-                after = True
-                if isinstance(result, int) and result > 0:
-                    after = True
-                elif isinstance(result, int) and result < 0:
-                    after = False
-
-                x1, y1, x2, y2 = target.swipe_pos(number=1, after=after)
-                self.device.swipe(p1=(x1, y1), p2=(x2, y2))
-                sleep(1)  # 等待滑动完成, 还没想好如何优化
+                swipe_down = isinstance(result, int) and result > 0
+                swipe_distance_ratio = 1
+            if not result:
+                return False
+            if isinstance(result, tuple):
+                return result
+            if swipe_distance_ratio:
+                x1, y1, x2, y2 = target.swipe_pos(number=swipe_distance_ratio, after=swipe_down)
+            else:
+                x1, y1, x2, y2 = target.swipe_pos(after=swipe_down)
+            self.device.swipe(p1=(x1, y1), p2=(x2, y2))
+            sleep(random.uniform(0.8, 1.3))  # 等待滑动完成, 待优化
 
     def list_appear_click(self, target: RuleList) -> bool:
         appear = self.list_find(target, name=target.array[0])