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

fix(): do battle logic and the ocr high memory

runhey 3 лет назад
Родитель
Сommit
99fa691dc5

+ 2 - 0
module/ocr/base_ocr.py

@@ -17,6 +17,7 @@ from module.exception import ScriptError
 from module.logger import logger
 
 
+
 class OcrMode(Enum):
     FULL = 1  # str: "Full"
     SINGLE = 2  # str: "Single"
@@ -142,6 +143,7 @@ class BaseCor:
                     text=f'[{result}]')
         return result
 
+
     def detect_and_ocr(self, image) -> list[BoxedResult]:
         """
         注意:这里使用了预处理和后处理

+ 1 - 1
module/ocr/models.py

@@ -22,7 +22,7 @@ if __name__ == "__main__":
     # 引入ocr 会导致非常巨大的内存开销
     @profile
     def test_memory():
-        for i in range(10):
+        for i in range(2):
             start_time = time.time()
             result = model.detect_and_ocr(image)
             print(result)

+ 22 - 16
tasks/Component/GeneralBattle/general_battle.py

@@ -30,26 +30,12 @@ class GeneralBattle(BaseTask, GeneralBattleAssets):
 
 
         # 如果没有锁定队伍。那么可以根据配置设定队伍
-        if not config.lock_team_enable:
+        if not config.lock_team_enable and self.is_in_prepare():
             logger.info("Lock team is not enable")
             # 如果更换队伍
             if self.current_count == 1:
                 self.switch_preset_team(config.preset_enable, config.preset_group, config.preset_team)
 
-            # 检测是否已经在战斗中
-            if self.is_in_battle():
-                while 1:
-                    self.screenshot()
-                    if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
-                        continue
-                    if not self.appear(self.I_BUFF):
-                        break
-                win = self.battle_wait(config.random_click_swipt_enable)
-                if win:
-                    return True
-                else:
-                    return False
-
             # 点击准备按钮
             self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
             while 1:
@@ -64,7 +50,9 @@ class GeneralBattle(BaseTask, GeneralBattleAssets):
             time.sleep(0.1)
 
         # 绿标
-        self.green_mark(config.green_enable, config.green_mark)
+        self.wait_until_disappear(self.I_BUFF)
+        if self.is_in_battle(False):
+            self.green_mark(config.green_enable, config.green_mark)
 
         win = self.battle_wait(config.random_click_swipt_enable)
         if win:
@@ -364,6 +352,24 @@ class GeneralBattle(BaseTask, GeneralBattleAssets):
         else:
             return False
 
+    def is_in_prepare(self, is_screenshot: bool = True) -> bool:
+        """
+        判断是否在准备中
+        :return:
+        """
+        if is_screenshot:
+            self.screenshot()
+        if self.appear(self.I_BUFF):
+            return True
+        elif self.appear(self.I_PREPARE_HIGHLIGHT):
+            return True
+        elif self.appear(self.I_PREPARE_DARK):
+            return True
+        elif self.appear(self.I_PRESET):
+            return True
+        else:
+            return False
+
 
 
 

+ 5 - 0
tasks/Orochi/assets.py

@@ -35,3 +35,8 @@ class OrochiAssets:
 					 array=["壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "悲", "神"])
 
 
+	# Ocr Rule Assets
+	# Ocr-description 
+	O_O_TEST_OCR = RuleOcr(roi=(126,136,360,491), area=(126,136,360,491), mode="Full", method="Default", keyword="", name="o_test_ocr")
+
+

+ 11 - 0
tasks/Orochi/res/ocr.json

@@ -0,0 +1,11 @@
+[
+  {
+    "itemName": "o_test_ocr",
+    "roiFront": "126,136,360,491",
+    "roiBack": "126,136,360,491",
+    "mode": "Full",
+    "method": "Default",
+    "keyword": "",
+    "description": "Ocr-description"
+  }
+]

+ 11 - 2
tasks/Orochi/script_task.py

@@ -113,7 +113,6 @@ class ScriptTask(GeneralBattle, GeneralInvite, GeneralBuff, GeneralRoom, GameUi,
 
 
 
-
     def run_leader(self):
         logger.info('Start run leader')
         self.ui_get_current_page()
@@ -326,12 +325,22 @@ class ScriptTask(GeneralBattle, GeneralInvite, GeneralBuff, GeneralRoom, GameUi,
 if __name__ == '__main__':
     from module.config.config import Config
     from module.device.device import Device
+    from memory_profiler import profile
     c = Config('oas1')
     d = Device(c)
     t = ScriptTask(c, d)
 
     # t.run()
-    t.check_layer("拾")
+
+    # t.check_layer('悲')
+
+    @profile
+    def test_memory():
+        t.screenshot()
+        print(t.ocr_appear(t.O_O_TEST_OCR))
+        print(t.L_LAYER_LIST.image_appear(t.device.image, '悲'))
+    test_memory()
+
 
 
 

+ 3 - 2
tasks/base_task.py

@@ -26,7 +26,6 @@ from tasks.GlobalGame.config_emergency import FriendInvitation, WhenNetworkAbnor
 from module.exception import GameStuckError, ScriptError
 
 
-
 class BaseTask(GlobalGameAssets):
     config: Config = None
     device: Device = None
@@ -242,7 +241,7 @@ class BaseTask(GlobalGameAssets):
                 target._match_init = True
 
             if timeout.reached():
-                logger.warning(f'wait_until_stable({target}) timeout')
+                logger.warning(f'Wait_until_stable({target}) timeout')
                 break
 
     def swipe(self, swipe: RuleSwipe, interval: float = None) -> None:
@@ -450,7 +449,9 @@ class BaseTask(GlobalGameAssets):
             self.screenshot()
 
             if self.ui_reward_appear_click():
+                self.wait_until_stable(self.I_UI_REWARD)
                 while 1:
+                    self.screenshot()
                     # 等待动画结束
                     if not self.appear(self.I_UI_REWARD):
                         logger.info('Get reward success')