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

refactor(script): 空闲等待策略统一为 map 动态分发

- 重构 _handle_wait_during_idle,统一通过 strategy_map 执行策略
- 移除 map 之后的 if/else 参数分支逻辑
- 策略方法统一只接收 next_run,配置项改为在方法内部读取
- 拆分 close_emulator_or_goto_main 与 close_emulator_or_close_game 两个明确入口
- 收敛关闭模拟器公共逻辑到 _wait_close_emulator_or,并用 fallback 回调处理后续动作
- 保留无效策略回退 stay_there 的兜底行为
- 添加翻译
yuyuyuyuyu 7 месяцев назад
Родитель
Сommit
d967bfa315
3 измененных файлов с 25 добавлено и 24 удалено
  1. 8 1
      assets/i18n/zh-CN.json
  2. 14 20
      script.py
  3. 3 3
      tasks/Script/config_optimization.py

+ 8 - 1
assets/i18n/zh-CN.json

@@ -24,7 +24,14 @@
   "checkin_config": "签到配置",
   "Usage": "使用说明",
   "checkin_big_god_usage_help": "自动领取网易大神APP的阴阳师福利礼包。\n\n使用前准备:\n1. 开启模拟器的Root模式\n2. 模拟器中安装并登录「网易大神」APP\n3. 在大神APP中绑定阴阳师角色\n\nFrida Server 会自动推送到模拟器并启动,无需手动操作。",
-  "costume_main_14": "雪月华庭"
+  "costume_main_14": "雪月华庭",
+  "goto_main": "前往主界面",
+  "close_game": "关闭游戏",
+  "close_emulator_or_goto_main": "关闭模拟器或前往主界面",
+  "close_emulator_or_close_game": "关闭模拟器或关闭游戏",
+  "close_game_limit_time": "关闭游戏等待时间",
+  "close_emulator_limit_time": "关闭模拟器等待时间",
+  "emulator_startup_lead_time": "模拟器预热时间"
 }
 
 

+ 14 - 20
script.py

@@ -327,26 +327,16 @@ class Script:
         :return: True 表示等待成功完成, False 表示等待被中断
         """
         method = self.config.script.optimization.when_task_queue_empty
-        close_game_limit_time = self.config.script.optimization.close_game_limit_time
-        close_emulator_limit_time = self.config.script.optimization.close_emulator_limit_time
         strategy_map = {
             "close_game": self._wait_close_game,
             "goto_main": self._wait_goto_main,
-            "close_emulator_or_goto_main": self._wait_close_emulator_or,
-            "close_emulator_or_close_game": self._wait_close_emulator_or,
+            "close_emulator_or_goto_main": self._wait_close_emulator_or_goto_main,
+            "close_emulator_or_close_game": self._wait_close_emulator_or_close_game,
         }
         func = strategy_map.get(method)
-        if not func:
+        if func is None:
             logger.warning(f"Invalid Optimization_WhenTaskQueueEmpty: {method}, fallback to stay_there")
             func = self._wait_stay_there
-            return func(next_run)
-
-        if method in ["close_emulator_or_goto_main", "close_emulator_or_close_game"]:
-            return func(next_run, close_game_limit_time, close_emulator_limit_time, method)
-
-        if method == "close_game":
-            return func(next_run, close_game_limit_time)
-
         return func(next_run)
 
     @staticmethod
@@ -379,11 +369,12 @@ class Script:
             return self.wait_until(next_run)
         return True
 
-    def _wait_close_game(self, next_run: datetime, close_game_limit_time=None) -> bool:
+    def _wait_close_game(self, next_run: datetime) -> bool:
         if self._emulator_down:
             logger.info("Emulator is down, skip close_game/goto_main action and wait with preheat")
             return self._wait_until_with_emulator_preheat(next_run)
 
+        close_game_limit_time = self.config.script.optimization.close_game_limit_time
         close_game_limit = self._time_to_timedelta(close_game_limit_time)
         if close_game_limit > timedelta(0) and next_run > datetime.now() + close_game_limit:
             logger.info("Close game during wait")
@@ -410,8 +401,14 @@ class Script:
         self.device.release_during_wait()
         return self.wait_until(next_run)
 
-    def _wait_close_emulator_or(self, next_run: datetime, close_game_limit_time=None,
-                                close_emulator_limit_time=None, method=None) -> bool:
+    def _wait_close_emulator_or_goto_main(self, next_run: datetime) -> bool:
+        return self._wait_close_emulator_or(next_run, self._wait_goto_main)
+
+    def _wait_close_emulator_or_close_game(self, next_run: datetime) -> bool:
+        return self._wait_close_emulator_or(next_run, self._wait_close_game)
+
+    def _wait_close_emulator_or(self, next_run: datetime, fallback_waiter: Callable[[datetime], bool]) -> bool:
+        close_emulator_limit_time = self.config.script.optimization.close_emulator_limit_time
         close_emulator_limit = self._time_to_timedelta(close_emulator_limit_time)
 
         if close_emulator_limit > timedelta(0) and next_run > datetime.now() + close_emulator_limit:
@@ -429,10 +426,7 @@ class Script:
             self.run("Restart")
             return True
 
-        if method == "close_emulator_or_goto_main":
-            return self._wait_goto_main(next_run)
-
-        return self._wait_close_game(next_run, close_game_limit_time)
+        return fallback_waiter(next_run)
 
     def _wait_stay_there(self, next_run: datetime) -> bool:
         if self._emulator_down:

+ 3 - 3
tasks/Script/config_optimization.py

@@ -27,8 +27,8 @@ class Optimization(BaseModel):
                                           description='task_hoarding_duration_help')
     when_task_queue_empty: WhenTaskQueueEmpty = Field(default=WhenTaskQueueEmpty.GOTO_MAIN,
                                                       description='when_task_queue_empty_help')
-    close_game_limit_time: Time = Field(default=Time(minute=10), description='关闭游戏等待时间')
-    close_emulator_limit_time: Time = Field(default=Time(minute=30), description='关闭模拟器等待时间')
-    emulator_startup_lead_time: Time = Field(default=Time(minute=2), description='模拟器预热时间')
+    close_game_limit_time: Time = Field(default=Time(minute=10))
+    close_emulator_limit_time: Time = Field(default=Time(minute=30))
+    emulator_startup_lead_time: Time = Field(default=Time(minute=2))
     schedule_rule: ScheduleRule = Field(default=ScheduleRule.FILTER, description='schedule_rule_help')