Prechádzať zdrojové kódy

feat(WantedQuests): Use exploration to complete quests

ryland 2 rokov pred
rodič
commit
c9f77c5990

+ 2 - 2
module/atom/animate.py

@@ -15,8 +15,8 @@ class RuleAnimate(RuleImage):
                  name: str = None):
         if isinstance(rule, RuleImage):
             roi_front = rule.roi_front
-            roi_back = Path(rule.file).stem.upper()
-            self._name = rule.file
+            roi_back = rule.roi_back
+            self._name = Path(rule.file).stem.upper()
             threshold = threshold
         elif isinstance(rule, RuleClick) or isinstance(rule, RuleLongClick):
             roi_front = rule.roi_front

+ 2 - 1
tasks/Exploration/base.py

@@ -37,7 +37,7 @@ class Scene(Enum):
 
 
 
-class BaseExploration(GeneralBattle, GeneralRoom, GeneralInvite, ReplaceShikigami, GameUi, SwitchSoul, ExplorationAssets):
+class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, ReplaceShikigami, SwitchSoul, ExplorationAssets):
     minions_cnt = 0
 
     @cached_property
@@ -341,6 +341,7 @@ class BaseExploration(GeneralBattle, GeneralRoom, GeneralInvite, ReplaceShikigam
 
     def fire(self, button) -> bool:
         self.ui_click_until_disappear(button, interval=3)
+        self.screenshot()
         if (self.appear(self.I_E_SETTINGS_BUTTON) or
                 self.appear(self.I_E_AUTO_ROTATE_ON) or
                 self.appear(self.I_E_AUTO_ROTATE_OFF)):

+ 1 - 0
tasks/Exploration/version.py

@@ -56,6 +56,7 @@ class HighlightGif(RuleGif):
     def pre_process(self, image):
         return highlight(image)
 
+
 class HighLight(BaseTask, ExplorationAssets):
 
     @cached_property

+ 105 - 0
tasks/WantedQuests/explore.py

@@ -0,0 +1,105 @@
+from cached_property import cached_property
+
+from module.atom.image import RuleImage
+from module.atom.animate import RuleAnimate
+from module.base.timer import Timer
+from module.logger import logger
+
+from tasks.Exploration.base import BaseExploration, Scene
+from tasks.Exploration.version import HighLight
+
+
+class ExploreWantedBoss(Exception):
+    # 出现一种情况,要求的怪是仅仅最后的Boss,其他小怪不是
+    pass
+
+
+class WQExplore(BaseExploration, HighLight):
+    _cnt_exploration: int = 0
+
+    @cached_property
+    def _match_end(self):
+        return RuleAnimate(self.I_SWIPE_END)
+
+    def explore(self, goto: RuleImage, num: int):
+        logger.info(f'Start exploring with number: {num}')
+        explore_init = False
+        explore_only_boss: bool = True
+        _cnt_exploration = 0
+        search_fail_cnt = 0
+        while 1:
+            self.screenshot()
+            if self.appear(self.I_UI_BACK_RED) and self.appear(self.I_E_EXPLORATION_CLICK):
+                break
+            if self.appear_then_click(goto, interval=2):
+                continue
+
+        while 1:
+            scene = self.get_current_scene(reuse_screenshot=False)
+            # 进入探索
+            if scene == Scene.ENTRANCE:
+                if _cnt_exploration >= num:
+                    logger.info('Execution exploration end')
+                    self.ui_click_until_disappear(self.I_UI_BACK_RED)
+                    if explore_only_boss:
+                        raise ExploreWantedBoss
+                    break
+                self.ui_click(self.I_E_EXPLORATION_CLICK, stop=self.I_E_SETTINGS_BUTTON)
+                continue
+            # 探索大世界
+            elif scene == Scene.WORLD:
+                self.wait_until_stable(
+                    self.I_CHECK_EXPLORATION,
+                    timer=Timer(limit=0.5, count=2),
+                    timeout=Timer(2, count=10)
+                )
+                curr_scene = self.get_current_scene(reuse_screenshot=False)
+                if curr_scene == Scene.WORLD and _cnt_exploration >= num:
+                    logger.info('All Done')
+                    break
+                continue
+            # 探索里面
+            elif scene == Scene.MAIN:
+                if not explore_init:
+                    self.ui_click(self.I_E_AUTO_ROTATE_OFF, stop=self.I_E_AUTO_ROTATE_ON)
+                    explore_init = True
+                    continue
+                # 小纸人
+                if self.appear(self.I_BATTLE_REWARD):
+                    if self.ui_get_reward(self.I_BATTLE_REWARD):
+                        continue
+                # boss
+                if self.appear(self.I_BOSS_BATTLE_BUTTON):
+                    if self.fire(self.I_BOSS_BATTLE_BUTTON):
+                        logger.info(f'Boss battle, minions cnt {self.minions_cnt}')
+                        _cnt_exploration += 1
+                        explore_only_boss = False
+                    continue
+                # 小怪
+                if self.appear(self.TEMPLATE_GIF) and self.fire(self.TEMPLATE_GIF):
+                    explore_only_boss = False
+                    logger.info(f'Fight, minions cnt {self.minions_cnt}')
+                # 向后拉,寻找怪
+                if search_fail_cnt >= 4:
+                    search_fail_cnt = 0
+                    if self._match_end.stable(self.device.image):
+                        _cnt_exploration += 1
+                        self.quit_explore()
+                        continue
+                    if self.swipe(self.S_SWIPE_BACKGROUND_RIGHT, interval=3):
+                        continue
+                else:
+                    search_fail_cnt += 1
+            elif scene == Scene.UNKNOWN:
+                continue
+
+
+if __name__ == '__main__':
+    from module.config.config import Config
+    from module.device.device import Device
+    from tasks.WantedQuests.assets import WantedQuestsAssets
+
+    config = Config('oas1')
+    device = Device(config)
+    t = WQExplore(config, device)
+    t.explore(goto=WantedQuestsAssets.I_GOTO_1, num=2)

+ 20 - 16
tasks/WantedQuests/script_task.py

@@ -22,12 +22,14 @@ from tasks.Secret.script_task import ScriptTask as SecretScriptTask
 from tasks.WantedQuests.config import WantedQuestsConfig, CooperationType, CooperationSelectMask, \
     CooperationSelectMaskDescription
 from tasks.WantedQuests.assets import WantedQuestsAssets
+from tasks.WantedQuests.explore import WQExplore, ExploreWantedBoss
 from tasks.Component.Costume.config import MainType
 from typing import List
 from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
 
 
-class ScriptTask(SecretScriptTask, GeneralInvite, WantedQuestsAssets, SwitchSoul):
+class ScriptTask(WQExplore, SecretScriptTask, WantedQuestsAssets):
+    want_strategy_excluding: list[list] = []  # 不需要执行的
 
     def run(self):
         con = self.config.model.wanted_quests
@@ -68,7 +70,7 @@ class ScriptTask(SecretScriptTask, GeneralInvite, WantedQuestsAssets, SwitchSoul
                     logger.warning('Current number of wanted quests is greater than total number')
                     cu = cu % 10
                 if cu < total and re != 0:
-                    self.execute_mission(self.O_WQ_TEXT_1, total, number_challenge)
+                    self.execute_mission(self.O_WQ_TEXT_1, min(total, 20), number_challenge)
 
             if self.ocr_appear(self.O_WQ_TEXT_2, interval=1):
                 cu, re, total = self.O_WQ_NUM_2.ocr(self.device.image)
@@ -79,7 +81,7 @@ class ScriptTask(SecretScriptTask, GeneralInvite, WantedQuestsAssets, SwitchSoul
                     logger.warning('Current number of wanted quests is greater than total number')
                     cu = cu % 10
                 if cu < total and re != 0:
-                    self.execute_mission(self.O_WQ_TEXT_2, total, number_challenge)
+                    self.execute_mission(self.O_WQ_TEXT_2, min(total, 20), number_challenge)
                 continue
 
             if self.appear(self.I_WQ_CHECK_TASK):
@@ -190,7 +192,7 @@ class ScriptTask(SecretScriptTask, GeneralInvite, WantedQuestsAssets, SwitchSoul
             result[1] = wq_destination
             result[2] = wq_number
             if type_wq == '探索':
-                result[0] = -1
+                result[0] = 0
             elif type_wq == '挑战':
                 result[0] = 1 if num_challenge >= 10 else -1
             elif type_wq == '秘闻':
@@ -216,6 +218,7 @@ class ScriptTask(SecretScriptTask, GeneralInvite, WantedQuestsAssets, SwitchSoul
             info_wq = extract_info(i)
             if info_wq:
                 info_wq_list.append(info_wq)
+        info_wq_list = [item for item in info_wq_list if item not in self.want_strategy_excluding]
         if not info_wq_list:
             logger.warning('No wanted quests can be challenged')
             return False
@@ -223,18 +226,19 @@ class ScriptTask(SecretScriptTask, GeneralInvite, WantedQuestsAssets, SwitchSoul
         info_wq_list.sort(key=lambda x: x[0])
         best_type, destination, once_number, goto_button = info_wq_list[0]
         do_number = 1 if once_number >= num_want else num_want // once_number + (1 if num_want % once_number > 0 else 0)
-        match best_type:
-            case 0:
-                self.explore(goto_button, do_number)
-            case 1:
-                self.challenge(goto_button, do_number)
-            case 2:
-                self.secret(goto_button, do_number)
-            case _:
-                logger.warning('No wanted quests can be challenged')
-
-    def explore(self, goto, num):
-        self.challenge(goto, num)
+        try:
+            match best_type:
+                case 0:
+                    self.explore(goto_button, do_number)
+                case 1:
+                    self.challenge(goto_button, do_number)
+                case 2:
+                    self.secret(goto_button, do_number)
+                case _:
+                    logger.warning('No wanted quests can be challenged')
+        except ExploreWantedBoss:
+            logger.warning('The extreme case. The quest only needs to challenge one final boss, so skip it')
+            self.want_strategy_excluding.append(info_wq_list[0])
 
     def challenge(self, goto, num):
         self.ui_click(goto, self.I_WQC_FIRE)