瀏覽代碼

fix(GeneralBattle): retry prepare click after battle_before timeout

When the start-battle click (GB_PREPARE_HIGHLIGHT) is intercepted by the
closing preset-panel animation, the battle never starts and battle_before
returns False after its 5s timeout. run_general_battle then entered
battle_wait's infinite loop waiting for a result screen that never
appears - the script looks stuck until the user clicks start manually.

Fix: after battle_before times out, re-check the screen for up to 3s:
- still on the prepare screen -> click start battle once and wait for
  the battle to begin (click only once, then just poll - avoid repeated
  clicks during UI transition)
- already in real battle -> continue normally
- neither (abnormal state) -> log a warning and return False so callers
  handle it as a failed battle instead of waiting forever
m123711 2 月之前
父節點
當前提交
65a6b73739
共有 1 個文件被更改,包括 36 次插入 和 1 次删除
  1. 36 1
      tasks/Component/GeneralBattle/general_battle.py

+ 36 - 1
tasks/Component/GeneralBattle/general_battle.py

@@ -38,7 +38,14 @@ class GeneralBattle(BattleWait, GeneralBuff):
         self.current_count += 1
         logger.info(f"Current count: {self.current_count}")
         # 战前设置
-        self.battle_before(buff, config)
+        if not self.battle_before(buff, config):
+            # battle_before 超时未进入战斗: 常见原因是点击"开始战斗"时被预设面板的关闭动画拦截,
+            # 点击未生效。此时画面已经稳定(面板动画已结束), 再识别一次:
+            # 若仍在战斗准备界面则再点击一次开始战斗, 若已进入战斗则直接继续。
+            if not self.retry_prepare_click():
+                logger.warning('battle_before failed: not in real battle within timeout, '
+                               'skip this battle and return False')
+                return False
         # 绿标
         if self.is_in_battle(False):
             self.green_mark(config.green_enable, config.green_mark)
@@ -78,6 +85,34 @@ class GeneralBattle(BattleWait, GeneralBuff):
             sleep(random.uniform(0.4, 0.8))
         return False
 
+    def retry_prepare_click(self, timeout: float = 3) -> bool:
+        """
+        battle_before 超时后的补偿: 再次识别画面并尝试开始战斗。
+        点击"开始战斗"可能被预设面板关闭动画拦截而未生效, 超时返回后画面已稳定,
+        此时若仍在战斗准备界面则再点击一次开始战斗; 若已进入战斗则直接成功。
+        :return: True: 超时内确认已进入真实战斗
+                 False: 超时仍未进入战斗(界面异常, 应由上层按战斗失败处理)
+        """
+        timer = Timer(timeout).start()
+        clicked = False  # 只补点一次, 避免在界面切换/点击延迟期间重复触发开始战斗
+        while not timer.reached():
+            self.screenshot()
+            if self.is_in_real_battle(False):  # 已经进入战斗
+                return True
+            if self.is_in_prepare(False):  # 仍在准备界面
+                if not clicked:
+                    clicked = True  # 只补点一次: 无论本次点击是否成功, 之后仅轮询
+                    if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=0.8):
+                        continue
+                    sleep(0.3)
+                    continue
+                # 已补点过一次, 只轮询等待进入战斗, 不再重复点击
+                sleep(0.3)
+                continue
+            # 未知界面, 等待画面变化
+            sleep(random.uniform(0.4, 0.8))
+        return False
+
     def run_general_battle_back(self, config: GeneralBattleConfig = None, exit_four: bool = False) -> bool:
         """
         进入挑战然后直接返回