Browse Source

fix(AreaBoss): 切换鬼王前先关闭当前详情页

unknown 7 hours ago
parent
commit
76969a01ea

+ 36 - 33
tasks/AreaBoss/script_task.py

@@ -109,8 +109,8 @@ class ScriptTask(Battle, GeneralBattle, GameUi, SwitchSoul, AreaBossAssets):
         """True 表示详情页仍遮挡当前帧,包括关闭按钮处于点击冷却时。"""
         if not self.appear(self.I_AB_CLOSE_RED):
             return False
-        # 入场动画中红叉可能先出现,等挑战按钮就绪后再关闭。
-        if self.appear(self.I_FIRE):
+        # 入场动画中可能只有红叉。筛选列表盖住挑战按钮时也可以点击红叉。
+        if self.appear(self.I_FIRE) or self.appear(self.I_AB_FILTER_OPENED):
             self.appear_then_click(self.I_AB_CLOSE_RED, interval=1)
         return True
 
@@ -162,10 +162,8 @@ class ScriptTask(Battle, GeneralBattle, GameUi, SwitchSoul, AreaBossAssets):
         @rtype:
         """
         reward_floor = self.config.area_boss.boss.reward_floor
-        if fileter_open and not self.appear(self.I_AB_FILTER_OPENED):
-            self.open_filter()
         # 如果打不开鬼王详情界面,直接退出
-        if not self.open_boss_detail(battle, 3):
+        if not self.open_boss_detail(battle, select_from_filter=fileter_open):
             return False
 
         if defeated is not None:
@@ -200,15 +198,17 @@ class ScriptTask(Battle, GeneralBattle, GameUi, SwitchSoul, AreaBossAssets):
 
     def _return_from_boss_detail(self):
         timeout = Timer(20).start()
+        map_stable = Timer(1.5, count=2)
         while 1:
             self.screenshot()
             if timeout.reached():
                 raise GameStuckError("Area boss battle return timed out")
-            if self._handle_boss_detail():
+            # 结算或关闭动画可能短暂露出地图,等地图或筛选列表持续可见再选下一只。
+            if self._handle_boss_detail() or not (
+                    self.appear(self.I_FILTER) or self.appear(self.I_AB_FILTER_OPENED)):
+                map_stable.clear()
                 continue
-            # 结算和关闭动画后可能直接回地图,不固定等待红叉出现。
-            # 若地图只是过渡帧,后续 open_filter/go_back 会继续处理详情页。
-            if self.appear(self.I_FILTER) or self.appear(self.I_AB_FILTER_OPENED):
+            if map_stable.start().reached():
                 return
 
     def start_fight(self) -> bool:
@@ -219,7 +219,9 @@ class ScriptTask(Battle, GeneralBattle, GameUi, SwitchSoul, AreaBossAssets):
             if not self.appear(self.I_AB_CLOSE_RED):  # 如果这个红色的关闭不见了才可以进行继续
                 break
         strategies, options = self.loadout_from_config(self.config.area_boss.general_battle)
-        options['completion'].check_imgs = [self.I_FIRE, self.I_FILTER]
+        options['completion'].check_imgs = [
+            self.I_FIRE, self.I_FILTER, self.I_AB_CLOSE_RED, self.I_AB_FILTER_OPENED,
+        ]
         self.loadout_show((strategies, options))  # 使用的是默认的策略: strategies
         with battle_wait_strategy(**strategies), battle_wait_options(**options):
             win = self.battle_wait()
@@ -454,32 +456,33 @@ class ScriptTask(Battle, GeneralBattle, GameUi, SwitchSoul, AreaBossAssets):
                 return name
         return ''
 
-    def open_boss_detail(self, battle: RuleImage, try_num: int = 3) -> bool:
-        """
-            打开鬼王详情界面
-        @param battle:
-        @type battle:
-        @param try_num: 重试次数
-        @type try_num:
-        @return:    True        打开成功
-                    False       打开失败
-        @rtype:
-        """
+    def open_boss_detail(self, battle: RuleImage | RuleClick, try_num: int = 3, *,
+                         select_from_filter: bool = False) -> bool:
+        """从列表选择鬼王。检查悬赏时可以继续使用已打开的目标详情。"""
         try_count = 0
-        while 1:
-            self.screenshot()
-            if self.appear(self.I_AB_CLOSE_RED) and self.appear(self.I_FIRE):
-                self.screenshot()
-                if not self.appear(self.I_FIRE):
-                    continue
-                return True
-            if try_count >= try_num:
-                logger.warning(f"Cannot boss_detail, try {try_count} times")
-                return False
-            if self.click(battle, interval=3):
+        retry = Timer(3)
+        timeout = Timer(20).start()
+        while not timeout.reached():
+            if select_from_filter:
+                if try_count >= try_num:
+                    break
+                self.open_filter()
+                self.click(battle)
                 try_count += 1
+                retry.reset()
+                select_from_filter = False
+
+            self.screenshot()
+            if self.appear(self.I_AB_FILTER_OPENED) or not self.appear(self.I_AB_CLOSE_RED):
+                # 选中目标后,等列表收起和详情展开,再决定是否重试。
+                select_from_filter = retry.reached()
                 continue
-        return True
+            if self.appear(self.I_FIRE):
+                self.screenshot()
+                if self.appear(self.I_FIRE) and not self.appear(self.I_AB_FILTER_OPENED):
+                    return True
+        logger.warning(f"Cannot open boss detail after {try_count} attempts")
+        return False
 
     def is_group_ranked(self):
         """

+ 13 - 5
tests/tasks/AreaBoss/test_area_battle.py

@@ -2,6 +2,7 @@ from types import SimpleNamespace
 
 import pytest
 
+from module.base import timer
 from tasks.AreaBoss.script_task import ScriptTask
 from tasks.AreaBoss.config_boss import AreaBossFloor
 from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig
@@ -24,7 +25,9 @@ def test_start_fight_applies_configured_strategies_and_return_markers(win):
         assert plan is not None
         assert all(getattr(plan, key) == value for key, value in strategies.items())
         active = battle_wait_options._context.get()
-        assert active['completion'].check_imgs == [task.I_FIRE, task.I_FILTER]
+        assert active['completion'].check_imgs == [
+            task.I_FIRE, task.I_FILTER, task.I_AB_CLOSE_RED, task.I_AB_FILTER_OPENED,
+        ]
         seen.append(True)
         return win
     task.battle_wait = battle
@@ -36,7 +39,7 @@ def test_start_fight_applies_configured_strategies_and_return_markers(win):
 
 @pytest.mark.parametrize('returned_to_filter', [True, False])
 @pytest.mark.parametrize('win', [True, False])
-def test_boss_fight_accepts_either_supported_return_screen(returned_to_filter, win):
+def test_boss_fight_accepts_either_supported_return_screen(monkeypatch, returned_to_filter, win):
     task = object.__new__(ScriptTask)
     task.config = SimpleNamespace(area_boss=SimpleNamespace(boss=SimpleNamespace(reward_floor=AreaBossFloor.DEFAULT)))
     screen = ['map' if returned_to_filter else 'detail']
@@ -45,10 +48,15 @@ def test_boss_fight_accepts_either_supported_return_screen(returned_to_filter, w
             return asset is task.I_FILTER
         return asset is task.I_AB_CLOSE_RED or asset is task.I_FIRE
     task.appear = appear
-    task.open_boss_detail = lambda *args: True
+    task.open_boss_detail = lambda *args, **kwargs: True
     task.is_group_ranked = lambda: False
     task.start_fight = lambda: win
-    task.screenshot = lambda: None
+    now = 100.0
+    def screenshot():
+        nonlocal now
+        now += 0.25
+    task.screenshot = screenshot
+    monkeypatch.setattr(timer, 'time', SimpleNamespace(time=lambda: now))
     calls = []
     def close(asset, **kwargs):
         assert asset is task.I_AB_CLOSE_RED and screen[0] == 'detail'
@@ -68,7 +76,7 @@ def test_boss_fight_accepts_either_supported_return_screen(returned_to_filter, w
 def test_reward_run_only_counts_new_wins(name, win, should_fight):
     task = object.__new__(ScriptTask)
     task.config = SimpleNamespace(area_boss=SimpleNamespace(boss=SimpleNamespace(reward_floor=AreaBossFloor.DEFAULT)))
-    task.open_boss_detail = lambda *args: True
+    task.open_boss_detail = lambda *args, **kwargs: True
     task.read_boss_name = lambda: name
     actions = []
     def start_fight():

+ 64 - 11
tests/tasks/AreaBoss/test_area_navigation.py

@@ -1,6 +1,7 @@
 """Replay battle return transitions without a running game or screenshot fixtures."""
 from copy import deepcopy
 from types import SimpleNamespace
+from unittest.mock import patch
 
 import pytest
 
@@ -28,7 +29,6 @@ class AreaNavigationReplay(ScriptTask):
         self.config = SimpleNamespace(area_boss=SimpleNamespace(
             boss=SimpleNamespace(reward_floor=AreaBossFloor.DEFAULT)))
         self.device = SimpleNamespace(image=frozenset(), click=self.record_click)
-        self.open_boss_detail = lambda *args: True
         self.start_fight = lambda: win
         for name in (
             'I_AB_CLOSE_RED', 'I_FIRE', 'I_FILTER', 'I_AB_FILTER_OPENED',
@@ -39,6 +39,10 @@ class AreaNavigationReplay(ScriptTask):
             setattr(self, name, asset)
         monkeypatch.setattr(timer, 'time', SimpleNamespace(time=lambda: self.now))
 
+    def finish_first_battle(self):
+        with patch.object(self, 'open_boss_detail', return_value=True):
+            return self.boss_fight(self.I_BATTLE_1, fileter_open=False)
+
     def screenshot(self):
         self.frames += 1
         assert self.frames <= 200, 'Navigation did not finish or respect its timeout'
@@ -52,6 +56,7 @@ class AreaNavigationReplay(ScriptTask):
             'map': {'I_FILTER', 'I_UI_BACK_YELLOW'},
             'detail': {'I_AB_CLOSE_RED', 'I_FIRE'},
             'detail_loading': {'I_AB_CLOSE_RED'},
+            'detail_with_filter': {'I_AB_CLOSE_RED', 'I_AB_FILTER_OPENED'},
             'filter': {'I_AB_FILTER_OPENED'},
             'main': {'I_CHECK_MAIN'},
             'unknown': set(),
@@ -63,9 +68,12 @@ class AreaNavigationReplay(ScriptTask):
     def record_click(self, x, y, control_name):
         self.clicks.append((control_name, self.now))
         if control_name == self.I_AB_CLOSE_RED.name:
-            assert self.screen == 'detail'
+            assert self.screen in ('detail', 'detail_with_filter')
             if self.ignored_closes:
                 self.ignored_closes -= 1
+            elif self.screen == 'detail_with_filter':
+                # The first tap outside the list can dismiss just the list.
+                self.screen = 'detail'
             elif self.close_delay:
                 self.screen = 'unknown'
                 self.transitions[self.frames + self.close_delay] = 'map'
@@ -79,6 +87,11 @@ class AreaNavigationReplay(ScriptTask):
             assert self.screen == 'map', 'Clicked back behind the boss detail'
             if not self.rebound:
                 self.screen = 'main'
+        elif control_name == self.I_BATTLE_2.name:
+            assert self.screen == 'filter', 'Selected a boss while the previous detail was open'
+            # The list can still be folding away as the selected detail appears.
+            self.screen = 'detail_with_filter'
+            self.transitions[self.frames + 3] = 'detail'
         else:
             pytest.fail(f'Unexpected click: {control_name}')
 
@@ -97,18 +110,58 @@ def test_battle_return_recovers_detail_reappearing_after_map(
     task = AreaNavigationReplay(monkeypatch, rebound=True, win=win,
                                 ignored_closes=3, background_visible=True)
 
-    assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is win
+    assert task.finish_first_battle() is win
+    assert task.clicked_names == [task.I_AB_CLOSE_RED.name] * 4
     getattr(task, navigate)()
 
     assert task.screen == destination
     button = getattr(task, navigation_asset).name
-    # Stay over the detail long enough for the background button's cooldown
-    # to expire, so a failed close attempt cannot fall through to that button.
-    assert task.clicked_names == [button] + [task.I_AB_CLOSE_RED.name] * 4 + [button]
+    assert task.clicked_names == [task.I_AB_CLOSE_RED.name] * 4 + [button]
     close_times = [at for name, at in task.clicks if name == task.I_AB_CLOSE_RED.name]
     assert all(later - earlier >= 1 for earlier, later in zip(close_times, close_times[1:]))
 
 
+def test_battle_return_closes_detail_under_filter_before_selecting_second_boss(monkeypatch):
+    task = AreaNavigationReplay(monkeypatch, screen='map', transitions={3: 'detail_with_filter'})
+    close = task.I_AB_CLOSE_RED.name
+
+    assert task.finish_first_battle()
+    assert task.screen == 'map'
+    assert task.clicked_names == [close, close]
+
+    assert task.boss_fight(task.I_BATTLE_2)
+    assert task.clicked_names == [close, close, task.I_FILTER.name, task.I_BATTLE_2.name, close]
+
+
+def test_second_boss_selection_recovers_filter_already_over_detail(monkeypatch):
+    task = AreaNavigationReplay(monkeypatch, screen='detail_with_filter')
+    task.screenshot()
+
+    assert task.boss_fight(task.I_BATTLE_2)
+
+    close = task.I_AB_CLOSE_RED.name
+    assert task.clicked_names == [close, close, task.I_FILTER.name, task.I_BATTLE_2.name, close]
+
+
+def test_reward_boss_fights_already_selected_detail(monkeypatch):
+    task = AreaNavigationReplay(monkeypatch, screen='detail')
+
+    assert task.boss_fight(task.C_AB_BOSS_REWARD_PHOTO_1, fileter_open=False)
+    assert task.clicked_names == [task.I_AB_CLOSE_RED.name]
+
+
+def test_unavailable_boss_retries_only_on_clear_list(monkeypatch):
+    task = AreaNavigationReplay(monkeypatch, screen='filter')
+    def ignore_selection(x, y, control_name):
+        assert task.screen == 'filter'
+        task.clicks.append((control_name, task.now))
+    task.device.click = ignore_selection
+
+    assert not task.boss_fight(task.I_BATTLE_2)
+    assert task.clicked_names == [task.I_BATTLE_2.name] * 3
+    assert all(later - earlier >= 3 for (_, earlier), (_, later) in zip(task.clicks, task.clicks[1:]))
+
+
 @pytest.mark.parametrize('navigate, destination', [
     ('open_filter', 'filter'), ('go_back', 'main'),
 ])
@@ -117,7 +170,7 @@ def test_battle_return_still_accepts_direct_map_and_normal_detail(
         monkeypatch, navigate, destination, screen):
     task = AreaNavigationReplay(monkeypatch, screen=screen)
 
-    assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is True
+    assert task.finish_first_battle() is True
     getattr(task, navigate)()
 
     assert task.screen == destination
@@ -155,7 +208,7 @@ def test_battle_return_waits_through_unknown_frames(monkeypatch, screen, win):
     task = AreaNavigationReplay(monkeypatch, screen='unknown', win=win,
                                 transitions={3: screen})
 
-    assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is win
+    assert task.finish_first_battle() is win
 
     assert task.screen == screen
     assert task.clicks == []
@@ -164,7 +217,7 @@ def test_battle_return_waits_through_unknown_frames(monkeypatch, screen, win):
 def test_battle_return_waits_for_map_after_detail_close_animation(monkeypatch):
     task = AreaNavigationReplay(monkeypatch, screen='detail', close_delay=3)
 
-    assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is True
+    assert task.finish_first_battle() is True
 
     assert task.screen == 'map'
     assert task.clicked_names == [task.I_AB_CLOSE_RED.name]
@@ -175,7 +228,7 @@ def test_battle_return_times_out_without_a_confirmed_page(monkeypatch, screen):
     task = AreaNavigationReplay(monkeypatch, screen=screen, ignored_closes=100)
 
     with pytest.raises(GameStuckError):
-        task.boss_fight(task.I_BATTLE_1, fileter_open=False)
+        task.finish_first_battle()
 
     assert task.now <= 125
     assert task.screen == screen
@@ -191,7 +244,7 @@ def test_detail_loading_blocks_background_controls_until_ready(
                                 transitions={5: 'detail'}, background_visible=True)
 
     if navigate == 'boss_fight':
-        assert task.boss_fight(task.I_BATTLE_1, fileter_open=False) is True
+        assert task.finish_first_battle() is True
     else:
         getattr(task, navigate)()