Prechádzať zdrojové kódy

feat(GeneralBattle): Add battle count and state reset helpers, restore climb counting

add Battle.battle_count_reached to compare per-task count against task limit
add Battle.battle_state_reset to clear per_task/per_battle state on task switch
restore ActivityShikigami count_map increment on battle success and reset battle state when climbing type switches
migrate battle_wait strategy usage docs into battle_wait_strategy docstring and prune outdated examples
neptine 2 mesiacov pred
rodič
commit
d54042b538

+ 8 - 4
tasks/ActivityShikigami/script_task.py

@@ -137,8 +137,8 @@ class StateMachine(BaseTask):
         if self.run_idx >= len(self.conf.general_climb.run_sequence_v):
             logger.info('All climbing activities have been completed')
             return False
-        # 切换爬塔类型了, 恢复所有状态
-        self.current_count = 0
+        # 切换爬塔类型, count_map 按类型独立统计无需重置;但战斗框架状态需要清掉上一类型的残留
+        self.battle_state_reset()
         logger.hr(f'Climb switch to {self.climb_type}', 2)
         return True
 
@@ -295,13 +295,17 @@ class ScriptTask(StateMachine, GameUi, Battle, BaseActivity, SwitchSoul, Activit
                 logger.info(f'Try click fire, remain times[{max_times - click_times}]')
                 continue
         # 运行战斗
-        # self.run_general_battle(config=self.get_general_battle_conf())
         strategies, options = self.loadout_from_config(self.get_general_battle_conf())
         strategies['success'] = 'activity'
         self.loadout_show((strategies, options))
         self.state_show()
         with battle_wait_strategy(**strategies), battle_wait_options(**options):
-            return self.battle_wait()
+            win = self.battle_wait()
+        # 对齐历史语义: 战斗成功结束后记为一次
+        if win:
+            self.count_map[self.climb_type] += 1
+            logger.info(f'Count {self.climb_type}: {self.count_map[self.climb_type]}')
+        return win
 
     # @battle_wait_strategy(success='activity')
     # def battle_wait(self, *args, **kwargs):

+ 21 - 0
tasks/Component/GeneralBattle/battle.py

@@ -7,6 +7,7 @@ from module.atom.ocr import RuleOcr
 from tasks.Component.GeneralBattle.battle_wait import (
     BattleWait, battle_wait_strategy, battle_wait_options,
     OptionPrepareDefault, OptionPresetDefault, OptionGreenDefault, OptionRandomclickDefault,
+    runtime,
 )
 from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig, GreenMarkType
 
@@ -55,6 +56,26 @@ class Battle(BattleWait):
         with battle_wait_strategy(), battle_wait_options():
             return self.battle_wait()
 
+    @classmethod
+    def battle_count_reached(cls, limit: int) -> bool:
+        """
+        判断当前任务累计战斗次数是否已达到上限。
+        对比的是新流程的任务级计数 per_task.count(每完成一场结算 +1)。
+
+        Args:
+            limit: 上限对比值,如 FallenSun 的 limit_count。
+
+        Returns:
+            达到或超过上限返回 True,否则 False。
+        """
+        count = runtime.pub_ctx.per_task.count if runtime.pub_ctx else 0
+        return count >= limit
+
+    @classmethod
+    def battle_state_reset(cls):
+        runtime.reset_per_task()
+        runtime.reset_per_battle()
+
     def battle_setup(self):
         pass
 

+ 19 - 32
tasks/Component/GeneralBattle/battle_wait.py

@@ -634,6 +634,21 @@ class BattleWaitPlan:
         return self
 
 class battle_wait_strategy:
+    """
+    用法(入参为 'event_strategy' 字符串或 event=strategy 的 kwargs):
+    1) 装饰器 = 永久覆盖(任务级):
+         @battle_wait_strategy(success='activity')
+         def battle_wait(self, *args, **kwargs):
+             return self.battle_wait_with_strategy(*args, **kwargs)
+    2) with = 临时覆盖(本次调用, 退出还原):
+         with battle_wait_strategy(success='activity'):
+             task.battle_wait()
+    3) 调用时动态传参(临时更新, 不覆盖, 兼容旧接口):
+         task.battle_wait(random_click_swipt_enable=True)
+
+    新增事件需实现对应 _bw_<event>_<strategy> 的 hook;自定义顺序用 sequence 参数,
+    未指定时新增事件插到 failure 与 idle 之间。
+    """
     battle_wait_plan: BattleWaitPlan = None
 
     def __init__(self, *arg, **kwargs):
@@ -1097,6 +1112,7 @@ class BattleWait(BaseTask, GeneralBattleAssets):
             raise
         if options.check_imgs is None:
             pub.per_task.count += 1
+            # self.current_count += 1  # 兼容旧的计数
             return HookSignal.DONE
         # 如果需要进一步确认
         if not isinstance(options.check_imgs, list):
@@ -1104,6 +1120,7 @@ class BattleWait(BaseTask, GeneralBattleAssets):
         appears = [self.appear(check) for check in options.check_imgs]
         if any(appears):
             pub.per_task.count += 1
+            # self.current_count += 1  # 兼容旧的计数
             return HookSignal.DONE
         # check_imgs 全没出现 → 兜底点击回退界面:进入 completion 满 8s 才首次点击,之后每 8s 一次
         if state.fallback_timer is None:
@@ -1554,38 +1571,8 @@ class BattleWait(BaseTask, GeneralBattleAssets):
         |   public      | cross | per_task              | per_battle   |
         |   private     | cross | per_task              | per_battle   |
 
-        ----------------------------------------------------------------------------------------------------------------
-        三种自定义策略方法:
-        1. 使用装饰器battle_wait_strategy, 将会覆盖掉类变量
-            @battle_wait_strategy( 'reserve_default', 'idle_default', failure='default')
-            def battle_wait(self, *args, **kwargs):
-                return self.battle_wait_with_strategy(*args, **kwargs)
-        2. 使用 with 上下文 (!在1基础上), 将会临时覆盖掉原先的类变量,退出后恢复
-            with battle_wait_strategy('reserve_default'):
-                test_battle_wait.battle_wait()
-        3. 调用时动态传参 (!在1基础上), 不覆盖,就临时更新策略
-            obj.battle_wait(random_click_swipt_enable=1)  # 详细参数看 battle_wait_strategy.__call__()
-
-        自定义hook就是字符串拼起来:  battle_wait_strategy的入参可以有 ‘event_strategy’ 或者 'event=strategy'
-        可以添加任意 event 以及其对应的 strategy。比如 ‘yyy_default’ 'abcd_edf'
-        但是必须要实现对应的hook 上面的比如 _bw_yyy_default() 以及 _bw_abcd_edf()
-        hook 可以自定义顺序,比如 battle_wait_strategy(sequence='completion > interrupt > success > failure > idle')
-        如果没有指定sequence, 新增的event会按照传参时候从左到右排序,左边高优先级,新增的会插入到 failure 和 idle 之间
-
-        ----------------------------------------------------------------------------------------------------------------
-        如果希望每一个hook带上参数:
-        1. 在 battle_wait_strategy 定义了一组默认的 options
-        2. 可以在装饰器定义 @battle_wait_strategy(options = options),这里将会覆盖掉原先的 battle_wait_strategy.options
-        3. 上下文带上  with battle_wait_strategy(...).with_options(options),同样也是临时覆盖掉 battle_wait_strategy.options
-        4.
-        options: dict[str: dict] = {
-            "setup": {...}
-            ...
-        }
-        ----------------------------------------------------------------------------------------------------------------
-        跨战斗,考虑把状态挂到方法上,而不是挂到类对象上。】
-        我突然感觉 一个类里面装了 策略和参数,这样不好,考虑拆分成两个装饰器
-
+        三种自定义策略方法见 battle_wait_strategy 的 docstring。
+        自定义options方法见 battle_wait_options 的 docstring
         """
         battle_wait_plan = kwargs.get('battle_wait_plan')
         if battle_wait_plan is None: