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

Merge pull request #943 from eiheihi/pr/1

重写了结界寄养的filter过滤,防止进行单个key的识别导致识别到错误的好友名(如果目标名字和识别到的名字中含有相同的字,就会识别错误)
huangrunheng 1 год назад
Родитель
Сommit
a5a25dd22f
1 измененных файлов с 67 добавлено и 20 удалено
  1. 67 20
      tasks/KekkaiUtilize/script_task.py

+ 67 - 20
tasks/KekkaiUtilize/script_task.py

@@ -8,7 +8,10 @@ from datetime import timedelta, datetime
 from module.base.timer import Timer
 from module.base.timer import Timer
 from module.atom.image_grid import ImageGrid
 from module.atom.image_grid import ImageGrid
 from module.logger import logger
 from module.logger import logger
-from module.exception import TaskEnd,GameStuckError
+
+from module.exception import TaskEnd, GameStuckError
+from ppocronnx.predict_system import BoxedResult
+
 
 
 from tasks.GameUi.game_ui import GameUi
 from tasks.GameUi.game_ui import GameUi
 from tasks.Utils.config_enum import ShikigamiClass
 from tasks.Utils.config_enum import ShikigamiClass
@@ -66,11 +69,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
             )
             )
         raise TaskEnd
         raise TaskEnd
 
 
-
-
-
-
-    def check_guild_ap_or_assets(self, ap_enable: bool=True, assets_enable: bool=True) -> bool:
+    def check_guild_ap_or_assets(self, ap_enable: bool = True, assets_enable: bool = True) -> bool:
         """
         """
         在寮的主界面 检查是否有收取体力或者是收取寮资金
         在寮的主界面 检查是否有收取体力或者是收取寮资金
         如果有就顺带收取
         如果有就顺带收取
@@ -128,7 +127,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
             if self.appear_then_click(self.I_GUILD_REALM, interval=1):
             if self.appear_then_click(self.I_GUILD_REALM, interval=1):
                 continue
                 continue
 
 
-    def check_box_ap_or_exp(self, ap_enable: bool=True, exp_enable: bool=True, exp_waste: bool=True) -> bool:
+    def check_box_ap_or_exp(self, ap_enable: bool = True, exp_enable: bool = True, exp_waste: bool = True) -> bool:
         """
         """
         顺路检查盒子
         顺路检查盒子
         :param ap_enable:
         :param ap_enable:
@@ -147,7 +146,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
                     continue
                     continue
 
 
         # 先是体力盒子
         # 先是体力盒子
-        def _check_ap_box(appear: bool=False):
+        def _check_ap_box(appear: bool = False):
             if not appear:
             if not appear:
                 return False
                 return False
             # 点击盒子
             # 点击盒子
@@ -177,7 +176,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
             _exit_to_realm()
             _exit_to_realm()
 
 
         # 经验盒子
         # 经验盒子
-        def _check_exp_box(appear: bool=False):
+        def _check_exp_box(appear: bool = False):
             if not appear:
             if not appear:
                 logger.info('No exp box')
                 logger.info('No exp box')
                 return False
                 return False
@@ -391,15 +390,21 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
             :return: 成功选择返回True
             :return: 成功选择返回True
             """
             """
             logger.info(f"尝试选择好友: {friend_name}")
             logger.info(f"尝试选择好友: {friend_name}")
-            while 1:
-                self.screenshot()
-                self.O_UTILIZE_F_LIST.keyword = friend_name
-                if self.ocr_appear_click(self.O_UTILIZE_F_LIST):
-                    logger.info(f"成功选择好友: {friend_name}")
-                    return True
-                else:
-                    return False
-
+            original_filter = self.O_UTILIZE_F_LIST.filter
+            try:
+                # 替换为自定义filter
+                self.O_UTILIZE_F_LIST.filter = self.filter
+                while 1:
+                    self.screenshot()
+                    self.O_UTILIZE_F_LIST.keyword = friend_name
+                    if self.ocr_appear_click(self.O_UTILIZE_F_LIST):
+                        logger.info(f"成功选择好友: {friend_name}")
+                        return True
+                    else:
+                        return False
+            finally:
+                # 恢复原始filter方法,防止副作用
+                self.O_UTILIZE_F_LIST.filter = original_filter
 
 
         logger.hr('Start utilize')
         logger.hr('Start utilize')
         self.switch_friend_list(friend)
         self.switch_friend_list(friend)
@@ -504,6 +509,49 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
         self.set_shikigami(shikigami_order, stop_image)
         self.set_shikigami(shikigami_order, stop_image)
         return True
         return True
 
 
+    def filter(self, boxed_results: list[BoxedResult], keyword: str=None) -> list or None:
+        """
+        使用ocr获取结果后和keyword进行匹配. 返回匹配的index list
+        :param keyword: 如果不指定默认适用对象的keyword
+        :param boxed_results:
+        :return:
+        """
+        # 首先先将所有的ocr的str顺序拼接起来, 然后再进行匹配
+        logger.info(f"重写的filter方法")
+        result = None
+        strings = [boxed_result.ocr_text for boxed_result in boxed_results]
+        concatenated_string = "".join(strings)
+        if keyword is None:
+            keyword = self.keyword
+        if keyword in concatenated_string:
+            result = [index for index, word in enumerate(strings) if keyword in word]
+        else:
+            result = None
+
+        if result is not None:
+            # logger.info("Filter result: %s" % result)
+            return result
+        else:
+            return None
+
+        # 如果适用顺序拼接还是没有匹配到,那可能是竖排的,使用单个字节的keyword进行匹配
+        indices = []
+        # 对于keyword中的每一个字符,都要在strings中进行匹配
+        # 如果这个字符在strings中的某一个string中,那么就记录这个string的index
+        max_index = len(strings) - 1
+        for index, char in enumerate(keyword):
+            for i, string in enumerate(strings):
+                if char not in string:
+                    continue
+                if i <= max_index:
+                    indices.append(i)
+                    break
+        if indices:
+            # 剔除掉重复的index
+            indices = list(set(indices))
+            return indices
+        else:
+            return None
 
 
     def back_guild(self):
     def back_guild(self):
         """
         """
@@ -523,6 +571,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
             if self.appear_then_click(self.I_UI_BACK_BLUE, interval=1):
             if self.appear_then_click(self.I_UI_BACK_BLUE, interval=1):
                 continue
                 continue
 
 
+
 if __name__ == "__main__":
 if __name__ == "__main__":
     from module.config.config import Config
     from module.config.config import Config
     from module.device.device import Device
     from module.device.device import Device
@@ -535,5 +584,3 @@ if __name__ == "__main__":
     # t.screenshot()
     # t.screenshot()
     # print(t.appear(t.I_BOX_EXP, threshold=0.6))
     # print(t.appear(t.I_BOX_EXP, threshold=0.6))
     # print(t.appear(t.I_BOX_EXP_MAX, threshold=0.6))
     # print(t.appear(t.I_BOX_EXP_MAX, threshold=0.6))
-
-