Răsfoiți Sursa

feat(SixRealmsGate): Done!

ryland 2 ani în urmă
părinte
comite
4ab93fa626

+ 9 - 1
tasks/SixRealms/README.md

@@ -18,4 +18,12 @@ island104 鏖战
 island105 星之屿
 
 event101 宝箱
-event
+event
+
+
+2国合
+回混范
+蜜屡战
+远宁息
+蜜馨战
+神/秘

+ 27 - 2
tasks/SixRealms/assets.py

@@ -36,6 +36,20 @@ class SixRealmsAssets:
 	C_NPC_FIRE_RIGHT = RuleClick(roi_front=(740,248,181,180), roi_back=(740,248,181,180), name="npc_fire_right")
 	# 中间的精英 
 	C_NPC_FIRE_CENTER = RuleClick(roi_front=(620,188,174,217), roi_back=(620,188,174,217), name="npc_fire_center")
+	# 默认的 
+	C_ISLAND_ENTER = RuleClick(roi_front=(0,0,100,100), roi_back=(0,0,100,100), name="island_enter")
+	# 右数第一个 
+	C_ISLAND_ENTER_1 = RuleClick(roi_front=(1012,349,136,221), roi_back=(1012,349,136,221), name="island_enter_1")
+	# description 
+	C_ISLAND_ENTER_2 = RuleClick(roi_front=(825,341,143,224), roi_back=(825,341,143,224), name="island_enter_2")
+	# description 
+	C_ISLAND_ENTER_3 = RuleClick(roi_front=(694,338,115,222), roi_back=(694,338,115,222), name="island_enter_3")
+	# description 
+	C_ISLAND_ENTER_4 = RuleClick(roi_front=(501,323,141,243), roi_back=(501,323,141,243), name="island_enter_4")
+	# description 
+	C_ISLAND_ENTER_5 = RuleClick(roi_front=(319,332,149,237), roi_back=(319,332,149,237), name="island_enter_5")
+	# description 
+	C_ISLAND_ENTER_6 = RuleClick(roi_front=(162,265,136,302), roi_back=(162,265,136,302), name="island_enter_6")
 
 
 	# Image Rule Assets
@@ -69,11 +83,15 @@ class SixRealmsAssets:
 	I_BACK_EXIT = RuleImage(roi_front=(11,18,51,47), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_back_exit.png")
 	# 可以购买的幻息 
 	I_M_STORE_ACTIVITY = RuleImage(roi_front=(1121,596,72,57), roi_back=(1107,573,100,100), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_m_store_activity.png")
+	# description 
+	I_MSTART_CONFIRM2 = RuleImage(roi_front=(1154,581,74,85), roi_back=(1079,535,196,177), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_mstart_confirm2.png")
+	# 动画完了就有这个东西 
+	I_ISLAND_TAG_FLAG = RuleImage(roi_front=(543,601,100,100), roi_back=(543,601,100,100), threshold=0.8, method="Template matching", file="./tasks/SixRealms/gate1/gate1_island_tag_flag.png")
 
 
 	# Ocr Rule Assets
 	# Ocr-description 
-	O_OCR_MAP = RuleOcr(roi=(191,286,944,197), area=(191,286,944,197), mode="Full", method="Default", keyword="", name="ocr_map")
+	O_OCR_MAP = RuleOcr(roi=(144,227,989,376), area=(144,227,989,376), mode="Full", method="Default", keyword="", name="ocr_map")
 	# Ocr-description 
 	O_ISLAND_NAME = RuleOcr(roi=(88,17,148,49), area=(88,17,148,49), mode="Single", method="Default", keyword="", name="island_name")
 	# 有多少钱 
@@ -108,6 +126,13 @@ class SixRealmsAssets:
 	I_STORE_REFRESH = RuleImage(roi_front=(545,596,46,47), roi_back=(519,568,100,100), threshold=0.8, method="Template matching", file="./tasks/SixRealms/l101/l101_store_refresh.png")
 	# 购买柔风 
 	I_STORE_SKILL_101 = RuleImage(roi_front=(876,121,47,41), roi_back=(682,100,455,571), threshold=0.8, method="Template matching", file="./tasks/SixRealms/l101/l101_store_skill_101.png")
+	# description 
+	I_STORE_EXIT = RuleImage(roi_front=(1179,586,59,56), roi_back=(1161,561,100,100), threshold=0.8, method="Template matching", file="./tasks/SixRealms/l101/l101_store_exit.png")
+
+
+	# Ocr Rule Assets
+	# 刷新次数 
+	O_STORE_REFRESH_TIME = RuleOcr(roi=(538,661,110,27), area=(538,661,110,27), mode="Single", method="Default", keyword="", name="store_refresh_time")
 
 
 	# Image Rule Assets
@@ -123,7 +148,7 @@ class SixRealmsAssets:
 
 	# Image Rule Assets
 	# description 
-	I_L103_EXIT = RuleImage(roi_front=(1161,600,69,70), roi_back=(1161,600,69,70), threshold=0.8, method="Template matching", file="./tasks/SixRealms/l103/l103_exit.png")
+	I_L103_EXIT = RuleImage(roi_front=(1171,587,69,70), roi_back=(1078,538,195,180), threshold=0.8, method="Template matching", file="./tasks/SixRealms/l103/l103_exit.png")
 
 
 	# Image Rule Assets

+ 3 - 0
tasks/SixRealms/common.py

@@ -2,9 +2,12 @@ from enum import Enum
 
 
 class MoonSeaType(Enum):
+    island100 = 100  # 未知
     island101 = 101
     island102 = 102
     island103 = 103
     island104 = 104  #
+    island105 = 105
+    island106 = 106  # boss
 
 

+ 8 - 5
tasks/SixRealms/config.py

@@ -4,18 +4,21 @@
 from pydantic import BaseModel, Field
 
 from tasks.Component.config_scheduler import Scheduler
-from tasks.Component.config_base import ConfigBase
+from tasks.Component.config_base import ConfigBase, Time
 from tasks.Component.SwitchSoul.switch_soul_config import SwitchSoulConfig
 
-# class SixRealmsGate(BaseModel):
-#     # 还没想好
-#     greed_maneki: bool = Field(default=False, description="greed_maneki_help")
+
+class SixRealmsGate(BaseModel):
+    # 限制时间
+    limit_time: Time = Field(default=Time(minute=30), description='limit_time_help')
+    # 限制次数
+    limit_count: int = Field(default=1, description='limit_count_help')
 
 
 class SixRealms(ConfigBase):
     scheduler: Scheduler = Field(default_factory=Scheduler)
     switch_soul_config: SwitchSoulConfig = Field(default_factory=SwitchSoulConfig)
-    # six_realms_gate: SixRealmsGate = Field(default_factory=SixRealmsGate)
+    six_realms_gate: SixRealmsGate = Field(default_factory=SixRealmsGate)
 
 
 

+ 42 - 0
tasks/SixRealms/gate1/click.json

@@ -16,5 +16,47 @@
     "roiFront": "620,188,174,217",
     "roiBack": "620,188,174,217",
     "description": "中间的精英"
+  },
+  {
+    "itemName": "island_enter",
+    "roiFront": "0,0,100,100",
+    "roiBack": "0,0,100,100",
+    "description": "默认的"
+  },
+  {
+    "itemName": "island_enter_1",
+    "roiFront": "1012,349,136,221",
+    "roiBack": "1012,349,136,221",
+    "description": "右数第一个"
+  },
+  {
+    "itemName": "island_enter_2",
+    "roiFront": "825,341,143,224",
+    "roiBack": "825,341,143,224",
+    "description": "description"
+  },
+  {
+    "itemName": "island_enter_3",
+    "roiFront": "694,338,115,222",
+    "roiBack": "694,338,115,222",
+    "description": "description"
+  },
+  {
+    "itemName": "island_enter_4",
+    "roiFront": "501,323,141,243",
+    "roiBack": "501,323,141,243",
+    "description": "description"
+  },
+  {
+    "itemName": "island_enter_5",
+    "roiFront": "319,332,149,237",
+    "roiBack": "319,332,149,237",
+    "description": "description"
+  },
+  {
+    "itemName": "island_enter_6",
+    "roiFront": "162,265,136,302",
+    "roiBack": "162,265,136,302",
+    "description": "description"
   }
 ]

BIN
tasks/SixRealms/gate1/gate1_island_tag_flag.png


BIN
tasks/SixRealms/gate1/gate1_mstart_confirm2.png


+ 18 - 0
tasks/SixRealms/gate1/image.json

@@ -133,5 +133,23 @@
     "method": "Template matching",
     "threshold": 0.8,
     "description": "可以购买的幻息"
+  },
+  {
+    "itemName": "mstart_confirm2",
+    "imageName": "gate1_mstart_confirm2.png",
+    "roiFront": "1154,581,74,85",
+    "roiBack": "1079,535,196,177",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "island_tag_flag",
+    "imageName": "gate1_island_tag_flag.png",
+    "roiFront": "543,601,100,100",
+    "roiBack": "543,601,100,100",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "动画完了就有这个东西"
   }
 ]

+ 2 - 2
tasks/SixRealms/gate1/ocr.json

@@ -1,8 +1,8 @@
 [
   {
     "itemName": "ocr_map",
-    "roiFront": "191,286,944,197",
-    "roiBack": "191,286,944,197",
+    "roiFront": "144,227,989,376",
+    "roiBack": "144,227,989,376",
     "mode": "Full",
     "method": "Default",
     "keyword": "",

+ 9 - 0
tasks/SixRealms/l101/image.json

@@ -16,5 +16,14 @@
     "method": "Template matching",
     "threshold": 0.8,
     "description": "购买柔风"
+  },
+  {
+    "itemName": "store_exit",
+    "imageName": "l101_store_exit.png",
+    "roiFront": "1179,586,59,56",
+    "roiBack": "1161,561,100,100",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
   }
 ]

BIN
tasks/SixRealms/l101/l101_store_exit.png


+ 11 - 0
tasks/SixRealms/l101/ocr.json

@@ -0,0 +1,11 @@
+[
+  {
+    "itemName": "store_refresh_time",
+    "roiFront": "538,661,110,27",
+    "roiBack": "538,661,110,27",
+    "mode": "Single",
+    "method": "Default",
+    "keyword": "",
+    "description": "刷新次数"
+  }
+]

+ 2 - 2
tasks/SixRealms/l103/image.json

@@ -2,8 +2,8 @@
   {
     "itemName": "l103_exit",
     "imageName": "l103_exit.png",
-    "roiFront": "1161,600,69,70",
-    "roiBack": "1161,600,69,70",
+    "roiFront": "1171,587,69,70",
+    "roiBack": "1078,538,195,180",
     "method": "Template matching",
     "threshold": 0.8,
     "description": "description"

+ 79 - 30
tasks/SixRealms/moon_sea/l101.py

@@ -1,40 +1,89 @@
-
+import re
 from module.logger import logger
 from tasks.SixRealms.moon_sea.skills import MoonSeaSkills
 
 
 class MoonSeaL101(MoonSeaSkills):
+
+    def buy_skill_101(self) -> bool:
+        logger.info('Buy skill 101')
+        buy_try: int = 0
+        while 1:
+            self.screenshot()
+            if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1.5):
+                break
+            if self.appear(self.I_UI_CONFIRM):
+                break
+            if buy_try >= 4:
+                logger.warning(f'Buy skill 101 failed')
+                return False
+            if self.appear(self.I_STORE_SKILL_101, interval=3):
+                x, y = self.I_STORE_SKILL_101.front_center()
+                x -= 60
+                self.device.click(x=x, y=y, )
+                buy_try += 1
+        self.ui_click_until_disappear(self.I_UI_CONFIRM)
+        self.wait_until_appear(self.I_STORE_EXIT)
+        logger.info('Buy skill 101 done')
+        return True
+
+    def refresh_store(self):
+        # 刷新宁溪
+        logger.info('Refresh store')
+        text = self.O_STORE_REFRESH_TIME.ocr(self.device.image)
+        matches = re.search(f"剩\d+次", text)
+        if matches:
+            refresh_time = int(matches.group()[1])
+            logger.info(f'Refresh time: {refresh_time}')
+            if refresh_time <= 0:
+                logger.warning('Refresh time is 0')
+                return False
+        while 1:
+            self.screenshot()
+            if self.appear(self.I_UI_CONFIRM):
+                break
+            if self.appear_then_click(self.I_STORE_REFRESH, interval=1.5):
+                continue
+        self.ui_click_until_disappear(self.I_UI_CONFIRM, interval=2)
+        self.wait_until_appear(self.I_STORE_EXIT)
+        logger.info('Refresh store done')
+        return True
+
     def run_l101(self):
         logger.hr('Start l101')
-        self.screenshot()
-        if self.appear(self.I_STORE_SKILL_101):
-            logger.info('Start purchase skill 101')
+        logger.info('Keep buying skills until you run out of money')
+        while 1:
             self.screenshot()
-            if not self.appear(self.I_STORE_SKILL_101):
-                logger.warning('Skill 101 purchase failed')
-            else:
-                while 1:
-                    self.screenshot()
-                    if self.in_main():
-                        break
-                    if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1.5):
-                        continue
-                    if self.appear_then_click(self.I_UI_CONFIRM, interval=1.5):
-                        continue
-                    coin = self.O_COIN_NUM.ocr()
-                    if coin < 300:
-                        logger.info('Not enough coin')
-                        break
-                    if self.appear(self.I_STORE_SKILL_101, interval=3):
-                        x, y = self.I_STORE_SKILL_101.front_center()
-                        x -= 60
-                        self.device.click(x=x, y=y,)
-            while 1:
-                self.screenshot()
-                if self.in_main():
-                    break
-                if self.appear_then_click(self.I_L103_EXIT, interval=1.5):
-                    continue
-            logger.info('Finish purchase skill 101')
+            coin = self.O_COIN_NUM.ocr(self.device.image)
+            if coin < 300:
+                logger.info('Not enough coin')
+                break
+            if self.appear(self.I_STORE_SKILL_101):
+                if not self.buy_skill_101():
+                    self.refresh_store()
+            elif not self.refresh_store():
+                break
+        logger.info('Finish purchase skill 101')
+        while 1:
+            self.screenshot()
+            if self.in_main():
+                break
+            if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
+                continue
+            if self.appear_then_click(self.I_STORE_EXIT, interval=1):
+                continue
+
+
+
+
+
+if __name__ == '__main__':
+    from module.config.config import Config
+    from module.device.device import Device
 
+    c = Config('oas1')
+    d = Device(c)
+    t = MoonSeaL101(c, d)
+    t.screenshot()
 
+    t.run_l101()

+ 3 - 2
tasks/SixRealms/moon_sea/l103.py

@@ -6,7 +6,8 @@ from tasks.SixRealms.moon_sea.skills import MoonSeaSkills
 class MoonSeaL103(MoonSeaSkills):
     def run_103(self):
         # 宝箱还是精英
-        logger.hr('Island 104')
+        logger.hr('Island 103')
+        self.wait_until_appear(self.I_ISLAND_TAG_FLAG, wait_time=2)
         is_box: bool = self.appear(self.I_L103_EXIT)
         if is_box:
             logger.info('Access to Box')
@@ -22,7 +23,7 @@ class MoonSeaL103(MoonSeaSkills):
                 if self.appear_then_click(self.I_L103_EXIT, interval=4):
                     continue
         self.battle_l103()
-        logger.info('Island 104 Finished')
+        logger.info('Island 103 Finished')
 
     def battle_l103(self):
         # 打精英

+ 127 - 82
tasks/SixRealms/moon_sea/map.py

@@ -1,99 +1,144 @@
 import cv2
+import re
 import numpy as np
 from numpy import uint8, fromfile
+from module.logger import logger
 
 from tasks.base_task import BaseTask
+from tasks.SixRealms.moon_sea.skills import MoonSeaSkills
 from tasks.SixRealms.assets import SixRealmsAssets
 from tasks.SixRealms.common import MoonSeaType
+from tasks.SixRealms.oas_ocr import StoneOcr
+
+
+class MoonSeaMap(MoonSeaSkills):
+    map_ocr = StoneOcr(
+        roi=(0, 0, 1280, 720),
+        area=(0, 0, 1280, 720),
+        mode="Full",
+        method="Default",
+        keyword="",
+        name="map_ocr")
+
+    @staticmethod
+    def contains_any_char(string, chars):
+        return not set(string).isdisjoint(set(chars))
+
+    def decide(self) -> tuple:
+        """
+        这个玩意,检测不是很准
+        @return: 岛屿的类型,剩余多少回合, (x,y, w, h)
+        """
+        self.screenshot()
+        if self.appear(self.I_BOSS_FIRE):
+            # 最后的boss
+            return MoonSeaType.island106, 0, (0, 0, 0, 0)
+        isl_type = MoonSeaType.island100
+        isl_roi = (0, 0, 0, 0)
+        isl_num = 0
+        results = self.map_ocr.detect_and_ocr(image=self.device.image)
+        rx1, ry1, rw, rh = self.O_OCR_MAP.roi
+        rx2, ry2 = rx1 + rw, ry1 + rh
+        for result in results:
+            # box 是四个点坐标 左上, 右上, 右下, 左下
+            x1, y1, x2, y2 = result.box[0][0], result.box[0][1], result.box[2][0], result.box[2][1]
+            w, h = x2 - x1, y2 - y1
+            text = result.ocr_text
+
+            if self.contains_any_char(result.ocr_text, chars='回合国') and \
+                    (x1 > 1000 and y1 + h < 300):
+                # if text[1].isdigit():
+                #     isl_num = int(text[:2])
+                # elif text[0].isdigit():
+                #     isl_num = int(text[0])
+                match = re.search(r'\d{1,2}', text)
+                if match:
+                    isl_num = int(match.group())
+            if x1 < rx1 or x2 > rx2 or y1 < ry1 or y2 > ry2:
+                continue
+            if isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='宁息'):
+                isl_type = MoonSeaType.island101
+                isl_roi = x1, y1, w, h
+            elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='神秘'):
+                isl_type = MoonSeaType.island102
+                isl_roi = x1, y1, w, h
+            elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='回混范'):
+                isl_type = MoonSeaType.island103
+                isl_roi = x1, y1, w, h
+            elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='蜜馨屡战'):
+                isl_type = MoonSeaType.island104
+                isl_roi = x1, y1, w, h
+            elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='星之'):
+                isl_type = MoonSeaType.island105
+                isl_roi = x1, y1, w, h
+        logger.info('Island type: {}, Residue: {}, ROI: {}'.format(isl_type, isl_num, isl_roi))
+        return isl_type, isl_num, isl_roi
+
+    def enter_island(self, isl_type, isl_roi):
+        if isl_type == MoonSeaType.island100:
+            logger.warning('The island type was not recognized')
+            logger.warning('Pick one at random, starting from the right')
+            while 1:
+                self.screenshot()
+                if not self.in_main() and self.appear(self.I_BACK_EXIT):
+                    break
+                if self.click(self.C_ISLAND_ENTER_1, interval=2):
+                    continue
+                if self.click(self.C_ISLAND_ENTER_2, interval=2):
+                    continue
+                if self.click(self.C_ISLAND_ENTER_3, interval=2):
+                    continue
+                if self.click(self.C_ISLAND_ENTER_4, interval=2):
+                    continue
+                if self.click(self.C_ISLAND_ENTER_5, interval=2):
+                    continue
+                if self.click(self.C_ISLAND_ENTER_6, interval=2):
+                    continue
+            logger.info('Entering island randomly')
+            return
+        isl_roi = [isl_roi[0]-20, isl_roi[1]-20, isl_roi[2]+40, isl_roi[3]+40]
+        self.C_ISLAND_ENTER.roi_front = isl_roi
+        while 1:
+            self.screenshot()
+            if not self.in_main() and self.appear(self.I_BACK_EXIT):
+                break
+            if self.click(self.C_ISLAND_ENTER, interval=1):
+                continue
+        logger.info('Entering island')
+        return
+
+    def activate_store(self) -> bool:
+        """
+        最后打boss前面激活一次商店买东西
+        @return: 有钱够就是True
+        """
+        logger.info('Activating store')
+        self.screenshot()
+        if self.appear(self.I_M_STORE) and not self.appear(self.I_M_STORE_ACTIVITY):
+            logger.warning('Now you have not money to buy items')
+            logger.warning('Store is not active')
+            return False
+        while 1:
+            self.screenshot()
+            if self.appear(self.I_UI_CONFIRM):
+                self.ui_click_until_disappear(self.I_UI_CONFIRM, interval=2)
+                break
+            if self.appear_then_click(self.I_M_STORE_ACTIVITY, interval=1):
+                continue
 
 
-def binaryzate(img):
-    img0 = img[..., 0]
-    img1 = img[..., 1]
-    img2 = img[..., 2]
-
-    mask0 = cv2.inRange(img0, MoonSeaMap.COLOR_RANGE_0[0], MoonSeaMap.COLOR_RANGE_0[1])
-    result0 = cv2.bitwise_and(img0, img0, mask=mask0)
-    mask1 = cv2.inRange(img1, 200, 250)
-    result1 = cv2.bitwise_and(img1, img1, mask=mask1)
-    mask2 = cv2.inRange(img2, 180, 230)
-    result2 = cv2.bitwise_and(img2, img2, mask=mask2)
-
-    result = np.zeros(img.shape, dtype=np.uint8)
-    result[..., 0]= result0
-    result[..., 1]= result1
-    result[..., 2]= result2
-    return  result
-
-
-class MoonSeaMap(BaseTask, SixRealmsAssets):
-
-    LOWER_COLOR = np.array([220, 190, 170])
-    UPPER_COLOR = np.array([260, 250, 230])
-    COLOR_RANGE_0 = [220, 250]
-
-    def detect_current(self) -> list[tuple]:
-        # self.screenshot()
-        img = self.device.image
-        # img = 255 - img
-        # print(img[403, 384])
-        # print(img[444, 378])
-        # print(img[411, 381])
-        # binary_img = img
-        # img = cv2.inRange(img, self.LOWER_COLOR, self.UPPER_COLOR)
-
-        # cv2.imwrite('test_0.png', img[..., 0])
-        # cv2.imwrite('test_1.png', img[..., 1])
-        # cv2.imwrite('test_2.png', img[..., 2])
-
-        img = binaryzate(img)
-        # iii = np.zeros((720, 1280, 3), dtype=np.uint8)
-        # iii[..., 0] = img
-        # iii[..., 1] = img
-        # iii[..., 2] = img
-        # img = iii
-
-        cv2.imshow('Binary Image', img)
-        cv2.waitKey(0)
-        cv2.destroyAllWindows()
-
-
-        boxs = self.O_OCR_MAP.detect_and_ocr(img)
-        for box in boxs:
-            print(box)
-        return []
-
-
-def _test():
+if __name__ == '__main__':
     from module.config.config import Config
     from module.device.device import Device
+    from module.base.utils import load_image
 
     c = Config('oas1')
     d = Device(c)
     t = MoonSeaMap(c, d)
-    import cv2
-    file_image = r'C:\Users\Ryland\Desktop\Desktop\34.png'
-    image = cv2.imdecode(fromfile(str(file_image), dtype=uint8), -1)
-    # image = cv2.imread(file_image)
-    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
-    t.device.image = image
+    t.device.image = load_image(r'C:\Users\Ryland\Desktop\Desktop\34.png')
 
-    t.detect_current()
+    match = re.search(r'\d{1,2}', '<17回合后迎战月读')
+    if match:
+        isl_num = int(match.group())
+        print(isl_num)
 
-
-def _test2():
-    file_image = r'test_1.png'
-    image = cv2.imdecode(fromfile(str(file_image), dtype=uint8), -1)
-    print(image.shape)
-    result = image
-    mask = cv2.inRange(image, 210, 250)
-    # mask = 255 * mask
-    result = cv2.bitwise_and(image, image, mask=mask)
-    # 显示结果
-    cv2.imshow('Result', result)
-    cv2.waitKey(0)
-    cv2.destroyAllWindows()
-
-
-if __name__ == '__main__':
-    _test()

+ 94 - 8
tasks/SixRealms/moon_sea/moon_sea.py

@@ -1,24 +1,108 @@
+import time
+
 from module.logger import logger
 
+from cached_property import cached_property
+from datetime import datetime, timedelta
+
 from tasks.SixRealms.moon_sea.map import MoonSeaMap
 from tasks.SixRealms.moon_sea.l101 import MoonSeaL101
 from tasks.SixRealms.moon_sea.l102 import MoonSeaL102
 from tasks.SixRealms.moon_sea.l103 import MoonSeaL103
 from tasks.SixRealms.moon_sea.l104 import MoonSeaL104
+from tasks.SixRealms.moon_sea.l105 import MoonSeaL105
 from tasks.SixRealms.common import MoonSeaType
 
 
-class MoonSea(MoonSeaMap, MoonSeaL101, MoonSeaL102, MoonSeaL103, MoonSeaL104):
-    def run_moon_sea(self):
+class MoonSea(MoonSeaMap, MoonSeaL101, MoonSeaL102, MoonSeaL103, MoonSeaL104, MoonSeaL105):
+
+    @cached_property
+    def island_func(self) -> dict:
+        return {
+            MoonSeaType.island101: self.run_l101(),
+            MoonSeaType.island102: self.run_l102(),
+            MoonSeaType.island103: self.run_103(),
+            MoonSeaType.island104: self.run_l104(),
+            MoonSeaType.island105: self.run_l105(),
+        }
+
+    @property
+    def _conf(self):
+        return self.config.model.six_realms.six_realms_gate
+
+    def _run_moon_sea(self):
         # 在六道界面
-        self.one()
+        limit_time = self._conf.limit_time
+        max_cont = self._conf.limit_count
+        max_time: timedelta = timedelta(
+            hours=limit_time.hour,
+            minutes=limit_time.minute,
+            seconds=limit_time.second
+        )
+        cnt = 0
+        while 1:
+            if cnt >= max_cont:
+                logger.info('Run out of count, exit')
+                break
+            if datetime.now() - self.start_time >= max_time:
+                logger.info('Run out of time, exit')
+                break
+            self.one()
+            cnt += 1
+        logger.info('Exit Moon Sea')
+
 
     def one(self):
         self._start()
+        while 1:
+            self.screenshot()
+            if not self.in_main():
+                continue
+            isl_type, isl_num, isl_roi = self.decide()
+            # 如果前一个,召唤一次宁息
+            if isl_num == 1 and isl_type != MoonSeaType.island106:
+                self.activate_store()
+                isl_type, isl_num, isl_roi = self.decide()
+            # 如果是boss
+            if isl_type == MoonSeaType.island106:
+                self.boss_team_lock()
+                self.boss_battle()
+                break
+
+            self.enter_island(isl_type=isl_type, isl_roi=isl_roi)
+            isl_type = self.island_name()
+            match isl_type:
+                case MoonSeaType.island101: self.run_l101()
+                case MoonSeaType.island102: self.run_l102()
+                case MoonSeaType.island103: self.run_103()
+                case MoonSeaType.island104: self.run_l104()
+                case MoonSeaType.island105: self.run_l105()
+            # 不知道怎么处理过场动画
+            # TODO
+            time.sleep(1)
+            continue
+
+    def _continue(self):
+        logger.warning('Moon Sea Continue')
+        while 1:
+            self.screenshot()
+            if self.in_main():
+                break
+            if self.appear_then_click(self.I_MCONINUE, interval=1):
+                continue
 
     def _start(self):
         logger.hr('Moon Sea', 1)
-        self.ui_click(self.I_MENTER, stop=self.I_MSTART)
+        while 1:
+            self.screenshot()
+            if self.appear(self.I_MSTART):
+                break
+            if self.appear_then_click(self.I_MENTER, interval=1):
+                continue
+            if self.appear(self.I_MCONINUE):
+                # 继续上一把的
+                self._continue()
+                return
         logger.info("Ensure select ShouZu")
         while 1:
             self.screenshot()
@@ -45,6 +129,8 @@ class MoonSea(MoonSeaMap, MoonSeaL101, MoonSeaL102, MoonSeaL103, MoonSeaL104):
                 continue
             if self.appear_then_click(self.I_MSTART_CONFIRM, interval=3):
                 continue
+            if self.appear_then_click(self.I_MSTART_CONFIRM2, interval=3):
+                continue
             if self.appear_then_click(self.I_MCONINUE, interval=3):
                 continue
         logger.info("Start Roguelike")
@@ -61,10 +147,11 @@ class MoonSea(MoonSeaMap, MoonSeaL101, MoonSeaL102, MoonSeaL103, MoonSeaL104):
         while 1:
             self.screenshot()
             text = self.O_ISLAND_NAME.ocr(self.device.image)
-            # 混沌 战
+            if '星' in text:
+                return MoonSeaType.island105
             if '战' in text:
                 return MoonSeaType.island104
-            if '混沌' in text:
+            if '混' in text:
                 return MoonSeaType.island103
             if '神秘' in text:
                 return MoonSeaType.island102
@@ -115,5 +202,4 @@ if __name__ == '__main__':
     t = MoonSea(c, d)
     t.screenshot()
 
-    # t.run_moon_sea()
-    t.boss_team_lock()
+    t.one()

+ 10 - 7
tasks/SixRealms/moon_sea/skills.py

@@ -1,3 +1,5 @@
+import time
+
 from cached_property import cached_property
 
 from module.logger import logger
@@ -78,15 +80,16 @@ class MoonSeaSkills(BaseTask, SixRealmsAssets):
 
         while 1:
             self.screenshot()
+            if self.in_main():
+                break
+
             if self.appear(self.I_SKILL_REFRESH):
                 select = self._select_skill()
                 if self.appear_then_click(self.selects_button[select]):
+                    time.sleep(1)
+                    # TODO 两次选的间隔太短就直接跳过了,或者说动画没有显示中间的就跳过了
                     continue
-            if self.appear(self.I_COIN):
-                while 1:
-                    self.screenshot()
-                    if self.in_main():
-                        break
-                    if self.appear_then_click(self.I_COIN, action=self.C_UI_REWARD, interval=0.8):
-                        continue
+            if self.appear_then_click(self.I_COIN, action=self.C_UI_REWARD, interval=1.5):
+                continue
+
 

+ 91 - 0
tasks/SixRealms/oas_ocr.py

@@ -0,0 +1,91 @@
+import functools
+
+import cv2
+import numpy as np
+
+
+from module.base.utils import color_similarity_2d, load_image
+# from module.ocr.ocr import Ocr
+from module.ocr.base_ocr import BaseCor
+
+
+
+
+def apply_mask(image, mask):
+    image16 = image.astype(np.uint16)
+    mask16 = mask.astype(np.uint16)
+    mask16 = cv2.merge([mask16, mask16, mask16])
+    image16 = cv2.multiply(image16, mask16)
+    # cv2.multiply(image16, mask16, dst=image16)
+    image16 = cv2.convertScaleAbs(image16, alpha=1 / 255)
+    # cv2.convertScaleAbs(image16, alpha=1 / 255, dst=image16)
+    # Image.fromarray(image16.astype(np.uint8)).show()
+    return image16.astype(np.uint8)
+
+
+class VerticalText(BaseCor):
+    @staticmethod
+    def rotate_image(image):
+        height, width = image.shape[0:2]
+        if height * 1.0 / width >= 1.5:
+            image = np.rot90(image)
+            return image
+        return image
+
+    def detect_and_ocr(self, *args, **kwargs):
+        # Try hard to lower TextSystem.box_thresh
+        backup = self.model.text_detector.box_thresh
+        # Patch text_recognizer
+        text_recognizer = self.model.text_recognizer
+        # Lower drop_score
+        lower_score = functools.partial(self.model.detect_and_ocr, drop_score=0.1)
+        detect_and_ocr = self.model.detect_and_ocr
+
+        def vertical_text_recognizer(img_crop_list):
+            img_crop_list = [VerticalText.rotate_image(i) for i in img_crop_list]
+            result = text_recognizer(img_crop_list)
+            return result
+
+        self.model.text_detector.box_thresh = 0.2
+        self.model.text_recognizer = vertical_text_recognizer
+        self.model.detect_and_ocr = lower_score
+
+        try:
+            result = super().detect_and_ocr(*args, **kwargs)
+        finally:
+            self.model.text_detector.box_thresh = backup
+            self.model.text_recognizer = text_recognizer
+            self.model.detect_and_ocr = detect_and_ocr
+        return result
+
+
+class StoneOcr(VerticalText):
+    def pre_process(self, image):
+        yuv = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
+        _, u, _ = cv2.split(yuv)
+        cv2.subtract(128, u, dst=u)
+        cv2.multiply(u, 8, dst=u)
+
+        color = color_similarity_2d(image, color=(234, 213, 181))
+        hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
+        _, _, v = cv2.split(hsv)
+        image = apply_mask(image, u)
+        image = apply_mask(image, color)
+        image = apply_mask(image, v)
+
+        cv2.convertScaleAbs(image, alpha=3, dst=image)
+        cv2.subtract((255, 255, 255, 0), image, dst=image)
+
+        # from PIL import Image
+        # Image.fromarray(image.astype(np.uint8)).show()
+        return image
+
+
+if __name__ == '__main__':
+    from tasks.SixRealms.assets import SixRealmsAssets
+    file = r'C:\Users\Ryland\Desktop\Desktop\20.png'
+    image = load_image(file)
+    ocr = StoneOcr(roi=(0,0,1280,720), area=(0,0,1280,720), mode="Full", method="Default", keyword="", name="ocr_map")
+    results = ocr.detect_and_ocr(image)
+    for r in results:
+        print(r.box, r.ocr_text)

+ 6 - 2
tasks/SixRealms/script_task.py

@@ -20,10 +20,11 @@ from tasks.Sougenbi.config import SougenbiConfig, SougenbiClass
 from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
 from tasks.GameUi.game_ui import GameUi
 from tasks.GameUi.page import page_main, page_six_gates
+from tasks.SixRealms.moon_sea.moon_sea import MoonSea
 from module.logger import logger
 
 
-class ScriptTask(GameUi, SwitchSoul):
+class ScriptTask(GameUi, SwitchSoul, MoonSea):
 
     @property
     def _config(self):
@@ -44,11 +45,14 @@ class ScriptTask(GameUi, SwitchSoul):
         self.ui_get_current_page()
         self.ui_goto(page_six_gates)
 
+        self.run_moon_sea()
+
         self.set_next_run('SixRealms', success=True, finish=True)
         raise TaskEnd
 
     def run_moon_sea(self):
-        pass
+        self._run_moon_sea()
+        self.ui_click(self.I_BACK_EXIT, self.I_CHECK_SIX_GATES)