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

fix(GeneralBattle): prioritize the exit confirmation dialog

Refresh and crop the new confirmation button, synchronize its JSON and generated recognition rule, and handle the dialog before clicking the background exit icon. Add regression coverage for retreat and forced-exit flows.
unknown 1 месяц назад
Родитель
Сommit
05579206b2

+ 1 - 1
tasks/Component/GeneralBattle/assets.py

@@ -110,7 +110,7 @@ class GeneralBattleAssets:
 	# 左上角的退出 
 	# 左上角的退出 
 	I_EXIT = RuleImage(roi_front=(14,12,43,41), roi_back=(14,12,43,41), threshold=0.8, method="Template matching", file="./tasks/Component/GeneralBattle/gb/gb_exit.png")
 	I_EXIT = RuleImage(roi_front=(14,12,43,41), roi_back=(14,12,43,41), threshold=0.8, method="Template matching", file="./tasks/Component/GeneralBattle/gb/gb_exit.png")
 	# 退出确认 
 	# 退出确认 
-	I_EXIT_ENSURE = RuleImage(roi_front=(674,388,135,63), roi_back=(674,388,135,63), threshold=0.8, method="Template matching", file="./tasks/Component/GeneralBattle/gb/gb_exit_ensure.png")
+	I_EXIT_ENSURE = RuleImage(roi_front=(718,424,69,30), roi_back=(667,398,179,66), threshold=0.8, method="Template matching", file="./tasks/Component/GeneralBattle/gb/gb_exit_ensure.png")
 	# 左上角好友图标 
 	# 左上角好友图标 
 	I_FRIENDS = RuleImage(roi_front=(89,14,36,36), roi_back=(89,14,36,36), threshold=0.8, method="Template matching", file="./tasks/Component/GeneralBattle/gb/gb_friends.png")
 	I_FRIENDS = RuleImage(roi_front=(89,14,36,36), roi_back=(89,14,36,36), threshold=0.8, method="Template matching", file="./tasks/Component/GeneralBattle/gb/gb_friends.png")
 	# 结算时左下角统计图标 
 	# 结算时左下角统计图标 

BIN
tasks/Component/GeneralBattle/gb/gb_exit_ensure.png


+ 2 - 2
tasks/Component/GeneralBattle/gb/image.json

@@ -155,8 +155,8 @@
   {
   {
     "itemName": "exit_ensure",
     "itemName": "exit_ensure",
     "imageName": "gb_exit_ensure.png",
     "imageName": "gb_exit_ensure.png",
-    "roiFront": "674,388,135,63",
-    "roiBack": "674,388,135,63",
+    "roiFront": "718,424,69,30",
+    "roiBack": "667,398,179,66",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "退出确认"
     "description": "退出确认"

+ 5 - 4
tasks/Component/GeneralBattle/general_battle.py

@@ -105,10 +105,11 @@ class GeneralBattle(BattleWait, GeneralBuff):
         # 点击返回
         # 点击返回
         while 1:
         while 1:
             self.screenshot()
             self.screenshot()
-            if self.appear_then_click(self.I_EXIT, interval=1.5):
-                continue
+            # 弹窗后方的返回图标仍能匹配,先处理确认框,避免反复点击背景。
             if self.appear(self.I_EXIT_ENSURE):
             if self.appear(self.I_EXIT_ENSURE):
                 break
                 break
+            if self.appear_then_click(self.I_EXIT, interval=1.5):
+                continue
         logger.info(f"Click {self.I_EXIT.name}")
         logger.info(f"Click {self.I_EXIT.name}")
 
 
         # 点击返回确认
         # 点击返回确认
@@ -147,10 +148,10 @@ class GeneralBattle(BattleWait, GeneralBuff):
         logger.info(f"Click {self.I_EXIT.name}")
         logger.info(f"Click {self.I_EXIT.name}")
         while 1:
         while 1:
             self.screenshot()
             self.screenshot()
-            if self.appear_then_click(self.I_EXIT, interval=1.5):
-                continue
             if self.appear(self.I_EXIT_ENSURE):
             if self.appear(self.I_EXIT_ENSURE):
                 break
                 break
+            if self.appear_then_click(self.I_EXIT, interval=1.5):
+                continue
 
 
         # 点击返回确认
         # 点击返回确认
         while 1:
         while 1:

+ 99 - 0
tests/tasks/Component/GeneralBattle/test_battle_exit.py

@@ -0,0 +1,99 @@
+import pytest
+
+from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig
+from tasks.Component.GeneralBattle.general_battle import GeneralBattle
+
+
+class BattleExitReplay(GeneralBattle):
+    def __init__(self, *, screen='battle', background_exit_visible=True,
+                 ignored_confirms=0):
+        self.screen = screen
+        self.background_exit_visible = background_exit_visible
+        self.ignored_confirms = ignored_confirms
+        self.frames = 0
+        self.clicks = []
+
+    def screenshot(self):
+        self.frames += 1
+        assert self.frames < 15, 'Kept clicking behind the exit confirmation dialog'
+
+    def appear(self, asset, **kwargs):
+        if asset is self.I_EXIT:
+            return self.screen == 'battle' or (
+                self.screen == 'confirm' and self.background_exit_visible
+            )
+        if asset is self.I_EXIT_ENSURE:
+            return self.screen == 'confirm'
+        if asset is self.I_FALSE:
+            return self.screen == 'defeat'
+        return False
+
+    def appear_then_click(self, asset, **kwargs):
+        if not self.appear(asset):
+            return False
+        self.clicks.append(asset.name)
+        if asset is self.I_EXIT:
+            self.screen = 'confirm'
+        elif asset is self.I_EXIT_ENSURE:
+            if self.ignored_confirms:
+                self.ignored_confirms -= 1
+            else:
+                self.screen = 'defeat'
+        elif asset is self.I_FALSE:
+            self.screen = 'finished'
+        return True
+
+    def wait_until_appear(self, asset, **kwargs):
+        assert self.appear(asset)
+        return True
+
+    def run_exit(self, method):
+        if method == 'run_general_battle_back':
+            return self.run_general_battle_back(
+                GeneralBattleConfig(lock_team_enable=True), exit_four=True,
+            )
+        return self.exit_battle()
+
+
+@pytest.mark.parametrize('method', ['run_general_battle_back', 'exit_battle'])
+@pytest.mark.parametrize('background_exit_visible', [True, False])
+def test_exit_confirms_dialog_before_clicking_the_background(method, background_exit_visible):
+    task = BattleExitReplay(background_exit_visible=background_exit_visible)
+
+    assert task.run_exit(method) is True
+    assert task.screen == 'finished'
+    assert task.clicks == [task.I_EXIT.name, task.I_EXIT_ENSURE.name, task.I_FALSE.name]
+
+
+@pytest.mark.parametrize('method', ['run_general_battle_back', 'exit_battle'])
+def test_already_open_exit_dialog_is_confirmed_without_reopening_it(method):
+    task = BattleExitReplay(screen='confirm')
+
+    assert task.run_exit(method) is True
+    assert task.screen == 'finished'
+    assert task.clicks == [task.I_EXIT_ENSURE.name, task.I_FALSE.name]
+
+
+def test_exit_battle_ignores_confirmation_without_a_battle_anchor():
+    task = BattleExitReplay(screen='confirm', background_exit_visible=False)
+
+    assert task.exit_battle() is False
+    assert task.clicks == []
+
+
+@pytest.mark.parametrize('method', ['run_general_battle_back', 'exit_battle'])
+def test_ignored_confirmation_click_retries_the_dialog_not_the_background(method):
+    task = BattleExitReplay(ignored_confirms=1)
+
+    assert task.run_exit(method) is True
+    assert task.screen == 'finished'
+    assert task.clicks == [
+        task.I_EXIT.name, task.I_EXIT_ENSURE.name, task.I_EXIT_ENSURE.name, task.I_FALSE.name,
+    ]
+
+
+def test_exit_battle_does_nothing_outside_battle():
+    task = BattleExitReplay(screen='finished')
+
+    assert task.exit_battle() is False
+    assert task.clicks == []