Explorar o código

refactor: 重构任务队列空闲处理与关闭游戏/模拟器配置

- 重新整理任务队列空闲时的处理分支与执行流程
- 调整关闭游戏/关闭模拟器相关的时间配置结构
yuyuyuyuyu hai 6 meses
pai
achega
68e6ecef9a
Modificáronse 3 ficheiros con 46 adicións e 42 borrados
  1. 5 8
      assets/i18n/zh-CN.json
  2. 35 29
      script.py
  3. 6 5
      tasks/Script/config_optimization.py

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

@@ -25,16 +25,13 @@
   "Usage": "使用说明",
   "checkin_big_god_usage_help": "自动领取网易大神APP的阴阳师福利礼包。\n\n使用前准备:\n1. 开启模拟器的Root模式\n2. 模拟器中安装并登录「网易大神」APP\n3. 在大神APP中绑定阴阳师角色\n\nFrida Server 会自动推送到模拟器并启动,无需手动操作。",
   "costume_main_14": "雪月华庭",
-  "goto_main": "前往主界面",
+  "goto_main": "前往庭院",
   "close_game": "关闭游戏",
-  "close_emulator_or_goto_main": "关闭模拟器&前往主界面",
-  "close_emulator_or_close_game": "关闭模拟器&关闭游戏",
-  "close_game_limit_time": "关闭游戏等待时间",
-  "close_game_limit_time_help": "此时间内不会关闭游戏, 防止运行间隔时间短的多个任务导致反复开关游戏",
-  "close_emulator_limit_time": "关闭模拟器等待时间",
-  "close_emulator_limit_time_help": "此时间内不会关闭模拟器, 防止运行间隔时间短的多个任务导致反复开关模拟器",
+  "close_game_wait_duration": "关闭游戏的等待时长",
+  "close_game_wait_duration_help": "距离下一个任务超过此分钟数时关闭游戏(上方选项为前往庭院时无效);设为0表示禁用",
+  "close_emulator_wait_duration": "关闭模拟器的等待时长",
+  "close_emulator_wait_duration_help": "距离下一个任务超过此分钟数时关闭模拟器;设为0表示禁用",
   "emulator_startup_lead_time": "模拟器预热时间",
   "emulator_startup_lead_time_help": "在开始运行任务之前提前启动模拟器的时间"
 }
 
-

+ 35 - 29
script.py

@@ -330,8 +330,6 @@ class Script:
         strategy_map = {
             "close_game": self._wait_close_game,
             "goto_main": self._wait_goto_main,
-            "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 func is None:
@@ -376,9 +374,23 @@ class Script:
             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:
+        close_game_wait_duration = self.config.script.optimization.close_game_wait_duration
+        close_game_wait = self._time_to_timedelta(close_game_wait_duration)
+        close_emulator_wait_duration = self.config.script.optimization.close_emulator_wait_duration
+        close_emulator_wait = self._time_to_timedelta(close_emulator_wait_duration)
+
+        if close_emulator_wait > timedelta(0) and next_run > datetime.now() + close_emulator_wait:
+            logger.info("Close emulator during wait")
+            self.device.emulator_stop()
+            self._emulator_down = True
+
+            if not self._wait_until_with_emulator_preheat(next_run):
+                return False
+
+            self.run("Restart")
+            return True
+
+        if close_game_wait <= timedelta(0):
             logger.info("Close game during wait")
             self.device.app_stop()
             self.device.release_during_wait()
@@ -387,7 +399,16 @@ class Script:
             self.run("Restart")
             return True
 
-        logger.info("Wait without closing game (close_game limit time not reached)")
+        if next_run > datetime.now() + close_game_wait:
+            logger.info("Close game during wait")
+            self.device.app_stop()
+            self.device.release_during_wait()
+            if not self.wait_until(next_run):
+                return False
+            self.run("Restart")
+            return True
+
+        logger.info("Wait without closing game (close_game wait duration not reached)")
         self.device.release_during_wait()
         if not self.wait_until(next_run):
             return False
@@ -398,29 +419,11 @@ class Script:
             logger.info("Emulator is down, skip goto_main and wait with preheat")
             return self._wait_until_with_emulator_preheat(next_run)
 
-        logger.info("Goto main page during wait")
-        self.run("GotoMain")
-        self.device.release_during_wait()
-        return self.wait_until(next_run)
-
-    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)
-
-        now = datetime.now()
-        if close_emulator_limit > timedelta(0) and next_run > now + close_emulator_limit:
+        close_emulator_wait_duration = self.config.script.optimization.close_emulator_wait_duration
+        close_emulator_wait = self._time_to_timedelta(close_emulator_wait_duration)
+        if close_emulator_wait > timedelta(0) and next_run > datetime.now() + close_emulator_wait:
             logger.info("Close emulator during wait")
-            if not self._emulator_down:
-                self.device.emulator_stop()
-            else:
-                logger.info("Emulator already closed")
-
+            self.device.emulator_stop()
             self._emulator_down = True
 
             if not self._wait_until_with_emulator_preheat(next_run):
@@ -429,7 +432,10 @@ class Script:
             self.run("Restart")
             return True
 
-        return fallback_waiter(next_run)
+        logger.info("Goto main page during wait")
+        self.run("GotoMain")
+        self.device.release_during_wait()
+        return self.wait_until(next_run)
 
     def _wait_stay_there(self, next_run: datetime) -> bool:
         if self._emulator_down:

+ 6 - 5
tasks/Script/config_optimization.py

@@ -10,8 +10,6 @@ from tasks.Component.config_base import Time
 class WhenTaskQueueEmpty(str, Enum):
     GOTO_MAIN = 'goto_main'
     CLOSE_GAME = 'close_game'
-    CLOSE_EMULATOR_OR_GOTO_MAIN = 'close_emulator_or_goto_main'
-    CLOSE_EMULATOR_OR_CLOSE_GAME = 'close_emulator_or_close_game'
 
 class ScheduleRule(str, Enum):
     FILTER = 'Filter'  # 默认的基于过滤器,(按照开发者设定的调度规则进行调度)
@@ -27,8 +25,11 @@ 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_game_limit_time_help')
-    close_emulator_limit_time: Time = Field(default=Time(minute=30),description='close_emulator_limit_time_help')
-    emulator_startup_lead_time: Time = Field(default=Time(minute=2),description='emulator_startup_lead_time_help')
+    close_game_wait_duration: Time = Field(default=Time(minute=0),
+                                          description='close_game_wait_duration_help')
+    close_emulator_wait_duration: Time = Field(default=Time(minute=0),
+                                              description='close_emulator_wait_duration_help')
+    emulator_startup_lead_time: Time = Field(default=Time(minute=2),
+                                            description='emulator_startup_lead_time_help')
     schedule_rule: ScheduleRule = Field(default=ScheduleRule.FILTER, description='schedule_rule_help')