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

feat(hya): Complete basic module

huangrunheng 2 лет назад
Родитель
Сommit
672055b169

+ 34 - 0
tasks/Hyakkiyakou/assets.py

@@ -17,6 +17,18 @@ class HyakkiyakouAssets:
 	C_HSELECT_2 = RuleClick(roi_front=(531,225,202,363), roi_back=(531,225,202,363), name="hselect_2")
 	C_HSELECT_2 = RuleClick(roi_front=(531,225,202,363), roi_back=(531,225,202,363), name="hselect_2")
 	# description 
 	# description 
 	C_HSELECT_3 = RuleClick(roi_front=(875,210,219,394), roi_back=(875,210,219,394), name="hselect_3")
 	C_HSELECT_3 = RuleClick(roi_front=(875,210,219,394), roi_back=(875,210,219,394), name="hselect_3")
+	# description 
+	C_FRIEND_1 = RuleClick(roi_front=(446,227,181,67), roi_back=(446,227,181,67), name="friend_1")
+	# description 
+	C_FRIEND_2 = RuleClick(roi_front=(718,229,178,64), roi_back=(718,229,178,64), name="friend_2")
+	# description 
+	C_FRIEND_3 = RuleClick(roi_front=(445,310,183,66), roi_back=(445,310,183,66), name="friend_3")
+	# description 
+	C_FRIEND_4 = RuleClick(roi_front=(712,311,184,62), roi_back=(712,311,184,62), name="friend_4")
+	# description 
+	C_FRIEND_5 = RuleClick(roi_front=(449,394,178,61), roi_back=(449,394,178,61), name="friend_5")
+	# description 
+	C_FRIEND_6 = RuleClick(roi_front=(714,397,182,62), roi_back=(714,397,182,62), name="friend_6")
 
 
 
 
 	# Image Rule Assets
 	# Image Rule Assets
@@ -111,6 +123,28 @@ class HyakkiyakouAssets:
 	I_BEAN8 = RuleImage(roi_front=(132,646,14,23), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_bean8.png")
 	I_BEAN8 = RuleImage(roi_front=(132,646,14,23), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_bean8.png")
 	# description 
 	# description 
 	I_BEAN9 = RuleImage(roi_front=(133,649,15,21), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_bean9.png")
 	I_BEAN9 = RuleImage(roi_front=(133,649,15,21), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_bean9.png")
+	# description 
+	I_BEAN05 = RuleImage(roi_front=(387,640,32,35), roi_back=(371,631,86,52), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_bean05.png")
+	# description 
+	I_BEAN10 = RuleImage(roi_front=(560,640,32,35), roi_back=(531,634,72,47), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_bean10.png")
+
+
+	# Image Rule Assets
+	# description 
+	I_FRIEND_SAME_1 = RuleImage(roi_front=(375,122,106,62), roi_back=(375,122,106,62), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_friend_same_1.png")
+	# description 
+	I_FRIEND_REMOTE_1 = RuleImage(roi_front=(498,125,106,56), roi_back=(498,125,106,56), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_friend_remote_1.png")
+	# description 
+	I_FRIEND_SAME_2 = RuleImage(roi_front=(377,126,100,60), roi_back=(377,126,100,60), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_friend_same_2.png")
+	# description 
+	I_FRIEND_REMOTE_2 = RuleImage(roi_front=(504,127,100,56), roi_back=(504,127,100,56), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_friend_remote_2.png")
+
+
+	# Swipe Rule Assets
+	# description 
+	S_BEAN_05TO10 = RuleSwipe(roi_front=(390,648,21,22), roi_back=(565,646,21,23), mode="default", name="bean_05to10")
+	# description 
+	S_BEAN_10TO05 = RuleSwipe(roi_front=(565,647,24,24), roi_back=(390,647,22,22), mode="default", name="bean_10to05")
 
 
 
 
 	# Click Rule Assets
 	# Click Rule Assets

+ 36 - 0
tasks/Hyakkiyakou/hya/click.json

@@ -16,5 +16,41 @@
     "roiFront": "875,210,219,394",
     "roiFront": "875,210,219,394",
     "roiBack": "875,210,219,394",
     "roiBack": "875,210,219,394",
     "description": "description"
     "description": "description"
+  },
+  {
+    "itemName": "friend_1",
+    "roiFront": "446,227,181,67",
+    "roiBack": "446,227,181,67",
+    "description": "description"
+  },
+  {
+    "itemName": "friend_2",
+    "roiFront": "718,229,178,64",
+    "roiBack": "718,229,178,64",
+    "description": "description"
+  },
+  {
+    "itemName": "friend_3",
+    "roiFront": "445,310,183,66",
+    "roiBack": "445,310,183,66",
+    "description": "description"
+  },
+  {
+    "itemName": "friend_4",
+    "roiFront": "712,311,184,62",
+    "roiBack": "712,311,184,62",
+    "description": "description"
+  },
+  {
+    "itemName": "friend_5",
+    "roiFront": "449,394,178,61",
+    "roiBack": "449,394,178,61",
+    "description": "description"
+  },
+  {
+    "itemName": "friend_6",
+    "roiFront": "714,397,182,62",
+    "roiBack": "714,397,182,62",
+    "description": "description"
   }
   }
 ]
 ]

BIN
tasks/Hyakkiyakou/hya/hya_bean05.png


BIN
tasks/Hyakkiyakou/hya/hya_bean10.png


BIN
tasks/Hyakkiyakou/hya/hya_friend_remote_1.png


BIN
tasks/Hyakkiyakou/hya/hya_friend_remote_2.png


BIN
tasks/Hyakkiyakou/hya/hya_friend_same_1.png


BIN
tasks/Hyakkiyakou/hya/hya_friend_same_2.png


+ 18 - 0
tasks/Hyakkiyakou/hya/image4.json

@@ -88,5 +88,23 @@
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "description"
     "description": "description"
+  },
+  {
+    "itemName": "bean05",
+    "imageName": "hya_bean05.png",
+    "roiFront": "387,640,32,35",
+    "roiBack": "371,631,86,52",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "bean10",
+    "imageName": "hya_bean10.png",
+    "roiFront": "560,640,32,35",
+    "roiBack": "531,634,72,47",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
   }
   }
 ]
 ]

+ 38 - 0
tasks/Hyakkiyakou/hya/image5.json

@@ -0,0 +1,38 @@
+[
+  {
+    "itemName": "friend_same_1",
+    "imageName": "hya_friend_same_1.png",
+    "roiFront": "375,122,106,62",
+    "roiBack": "375,122,106,62",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "friend_remote_1",
+    "imageName": "hya_friend_remote_1.png",
+    "roiFront": "498,125,106,56",
+    "roiBack": "498,125,106,56",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "friend_same_2",
+    "imageName": "hya_friend_same_2.png",
+    "roiFront": "377,126,100,60",
+    "roiBack": "377,126,100,60",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "friend_remote_2",
+    "imageName": "hya_friend_remote_2.png",
+    "roiFront": "504,127,100,56",
+    "roiBack": "504,127,100,56",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  }
+]

+ 16 - 0
tasks/Hyakkiyakou/hya/swipe.json

@@ -0,0 +1,16 @@
+[
+  {
+    "itemName": "bean_05to10",
+    "roiFront": "390,648,21,22",
+    "roiBack": "565,646,21,23",
+    "mode": "default",
+    "description": "description"
+  },
+  {
+    "itemName": "bean_10to05",
+    "roiFront": "565,647,24,24",
+    "roiBack": "390,647,22,22",
+    "mode": "default",
+    "description": "description"
+  }
+]

+ 3 - 2
tasks/Hyakkiyakou/prepare/get_images.py

@@ -58,8 +58,8 @@ class GenerateImages(GameUi, HyakkiyakouAssets):
                 break
                 break
             if not self.appear(self.I_CHECK_RUN):
             if not self.appear(self.I_CHECK_RUN):
                 continue
                 continue
-            if self.appear(self.I_HFREEZE):
-                continue
+            # if self.appear(self.I_HFREEZE):
+            #     continue
             if save_img_timer.reached():
             if save_img_timer.reached():
                 save_img_timer.reset()
                 save_img_timer.reset()
                 self.save_image()
                 self.save_image()
@@ -73,6 +73,7 @@ class GenerateImages(GameUi, HyakkiyakouAssets):
         # 时间戳毫秒级别
         # 时间戳毫秒级别
         time_now1 = int(time.time() * 1000)
         time_now1 = int(time.time() * 1000)
         time_now2 = time_now1 + 1
         time_now2 = time_now1 + 1
+        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
         # cv2.imwrite(str(self.save_folder / f'{time_now1}.png'), img1)
         # cv2.imwrite(str(self.save_folder / f'{time_now1}.png'), img1)
         # cv2.imwrite(str(self.save_folder / f'{time_now2}.png'), img2)
         # cv2.imwrite(str(self.save_folder / f'{time_now2}.png'), img2)
         cv2.imwrite(str(self.save_folder / f'all{time_now1}.png'), image)
         cv2.imwrite(str(self.save_folder / f'all{time_now1}.png'), image)

+ 14 - 5
tasks/Hyakkiyakou/script_task.py

@@ -10,6 +10,9 @@ from cached_property import cached_property
 # Use cmd to install: ./toolkit/python.exe -m pip install -i https://pypi.org/simple/ oashya --trusted-host pypi.org
 # Use cmd to install: ./toolkit/python.exe -m pip install -i https://pypi.org/simple/ oashya --trusted-host pypi.org
 from oashya.tracker import Tracker
 from oashya.tracker import Tracker
 
 
+from module.exception import TaskEnd
+from tasks.GameUi.game_ui import GameUi
+from tasks.GameUi.page import page_hyakkiyakou
 from tasks.Hyakkiyakou.config import Hyakkiyakou as HyakkiyakouConfig
 from tasks.Hyakkiyakou.config import Hyakkiyakou as HyakkiyakouConfig
 from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
 from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
 from tasks.Hyakkiyakou.agent.agent import Agent
 from tasks.Hyakkiyakou.agent.agent import Agent
@@ -50,7 +53,7 @@ def test_():
     plot_save(img, resutl)
     plot_save(img, resutl)
 
 
 
 
-class ScriptTask:
+class ScriptTask(GameUi, HyaSlave):
 
 
     @cached_property
     @cached_property
     def tracker(self) -> Tracker:
     def tracker(self) -> Tracker:
@@ -61,16 +64,22 @@ class ScriptTask:
     def agent(self) -> Agent:
     def agent(self) -> Agent:
         return Agent()
         return Agent()
 
 
-    @cached_property
-    def slave(self) -> HyaSlave:
-        return HyaSlave()
-
     @cached_property
     @cached_property
     def debugger(self) -> Debugger:
     def debugger(self) -> Debugger:
         return Debugger()
         return Debugger()
 
 
 
 
     def run(self):
     def run(self):
+        self.ui_get_current_page()
+        self.ui_goto(page_hyakkiyakou)
+
+        self.one()
+
+        self.ui_click_until_disappear(self.I_UI_BACK_RED)
+        self.set_next_run(task='Hyakkiyakou', success=True, finish=False)
+        raise TaskEnd
+
+    def one(self):
         pass
         pass
 
 
 
 

+ 58 - 0
tasks/Hyakkiyakou/slave/hya_color.py

@@ -0,0 +1,58 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import cv2
+from cached_property import cached_property
+
+from tasks.base_task import BaseTask
+
+
+class HyaColor(BaseTask):
+    BUFF_STATE_POS0: tuple[int] = (190, 42, 57, 37, 46)
+    BUFF_STATE_POS1: tuple[int] = (370, 35, 58, 39, 53)
+    BUFF_STATE_POS2: tuple[int] = (905, 35, 46, 29, 37)
+    BUFF_STATE_POS3: tuple[int] = (1060, 33, 53, 33, 39)
+
+    BEAN_NUM10: tuple[int] = (550, 657, 44, 32, 43)
+    BEAN_NUM05: tuple[int] = (400, 657, 44, 32, 43)
+
+    @cached_property
+    def buff_colors(self) -> list[tuple[int]]:
+        return [self.BUFF_STATE_POS0,
+                self.BUFF_STATE_POS1,
+                self.BUFF_STATE_POS2,
+                self.BUFF_STATE_POS3]
+
+    def show_point_color(self, x: int, y: int):
+        color = self.device.image[y, x]
+        print(f"({x}, {y}) color: {color}")
+        return color
+
+    def match_color(self, package: tuple, offset: int=5) -> bool:
+        x, y, r, g, b = package
+        img_r, img_g, img_b = self.device.image[y, x]
+        if abs(img_r - r) > offset:
+            return False
+        if abs(img_g - g) > offset:
+            return False
+        if abs(img_b - b) > offset:
+            return False
+        return True
+
+
+
+if __name__ == "__main__":
+    import timeit
+    from module.config.config import Config
+    from module.device.device import Device
+
+    c = Config('oas1')
+    d = Device(c)
+    hd = HyaColor(c, d)
+    img = cv2.imread(r'E:\Project\OnmyojiAutoScript\tasks\Hyakkiyakou\temp\20240615T144058\all1718433698934.png')
+    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
+    hd.device.image = img
+
+    # print(hd.show_point_color(400, 657))
+    # print(hd.match_color(hd.BUFF_STATE_POS1))
+

+ 117 - 13
tasks/Hyakkiyakou/slave/hya_slave.py

@@ -2,14 +2,33 @@ import cv2
 
 
 from cached_property import cached_property
 from cached_property import cached_property
 from pathlib import Path
 from pathlib import Path
+from enum import Enum
 
 
 from module.logger import logger
 from module.logger import logger
 from module.atom.image import RuleImage
 from module.atom.image import RuleImage
 from tasks.Hyakkiyakou.slave.hya_device import HyaDevice
 from tasks.Hyakkiyakou.slave.hya_device import HyaDevice
+from tasks.Hyakkiyakou.slave.hya_color import HyaColor
 from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
 from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
 
 
 
 
-class HyaSlave(HyaDevice, HyakkiyakouAssets):
+class HyaBuff(int, Enum):
+    BUFF_STATE0 = -1  # None
+    BUFF_STATE2 = 0  # 式神减速
+    BUFF_STATE3 = 1  # 砸豆加速
+    BUFF_STATE5 = 2  # 式神冰冻
+    BUFF_STATE6 = 3  # 概率UP
+    BUFF_STATE7 = 4  # 好友UP
+
+    @classmethod
+    def from_index(cls, index: int):
+        for member in cls:
+            if member.value == index:
+                return member
+        raise ValueError(f"No HyaBuff member with value {index}")
+
+
+
+class HyaSlave(HyaDevice, HyaColor, HyakkiyakouAssets):
     """
     """
     主要是用来跟游戏进行交互的
     主要是用来跟游戏进行交互的
     """
     """
@@ -19,6 +38,11 @@ class HyaSlave(HyaDevice, HyakkiyakouAssets):
     UNIT0HUNDRED: list[int] = [146, 647, 18, 25]
     UNIT0HUNDRED: list[int] = [146, 647, 18, 25]
     DECADE0DECADE: list[int] = [126, 647, 18, 25]
     DECADE0DECADE: list[int] = [126, 647, 18, 25]
     UNIT0DECADE: list[int] = [140, 647, 18, 25]
     UNIT0DECADE: list[int] = [140, 647, 18, 25]
+    # buff
+    BUFF_ROI1: list[int] = [150, 1, 150, 50]
+    BUFF_ROI2: list[int] = [320, 1, 140, 50]
+    BUFF_ROI3: list[int] = [840, 1, 150, 50]
+    BUFF_ROI4: list[int] = [1100, 1, 140, 50]
 
 
     @cached_property
     @cached_property
     def res_r(self) -> list[RuleImage]:
     def res_r(self) -> list[RuleImage]:
@@ -65,6 +89,24 @@ class HyaSlave(HyaDevice, HyakkiyakouAssets):
             self.I_BEAN9,
             self.I_BEAN9,
         ]
         ]
 
 
+    @cached_property
+    def buff_state_rois(self) -> list[list[int]]:
+        return [
+            self.BUFF_ROI1,
+            self.BUFF_ROI2,
+            self.BUFF_ROI3,
+            self.BUFF_ROI4
+        ]
+
+    @cached_property
+    def buff_state_images(self) -> list[RuleImage]:
+        return [
+            self.I_HYA_STATE_BUFF02,  # 式神减速
+            self.I_HYA_STATE_BUFF03,  # 砸豆加速
+            self.I_HYA_STATE_BUFF05,  # 冰冻
+            self.I_HYA_STATE_BUFF06,  # 概率up
+            self.I_HYA_STATE_BUFF07,  # 好友UP
+        ]
 
 
     def predict_res(self, current: int) -> int:
     def predict_res(self, current: int) -> int:
         for i in range(current, current-5, -1):
         for i in range(current, current-5, -1):
@@ -101,13 +143,11 @@ class HyaSlave(HyaDevice, HyakkiyakouAssets):
                 decade_img = self.bean[decade]
                 decade_img = self.bean[decade]
                 decade_img.roi_back = self.DECADE0DECADE
                 decade_img.roi_back = self.DECADE0DECADE
                 if not self.appear(decade_img):
                 if not self.appear(decade_img):
-                    print(f'没有匹配十位{decade}')
                     continue
                     continue
                 unit = bean % 10
                 unit = bean % 10
                 unit_img = self.bean[unit]
                 unit_img = self.bean[unit]
                 unit_img.roi_back = self.UNIT0DECADE
                 unit_img.roi_back = self.UNIT0DECADE
                 if self.appear(unit_img):
                 if self.appear(unit_img):
-                    print(f'匹配到{bean}')
                     return bean
                     return bean
                 for i in range(10):
                 for i in range(10):
                     unit_img = self.bean[i]
                     unit_img = self.bean[i]
@@ -117,6 +157,57 @@ class HyaSlave(HyaDevice, HyakkiyakouAssets):
         logger.warning(f'Cannot predict bean, current: {current}')
         logger.warning(f'Cannot predict bean, current: {current}')
         return current
         return current
 
 
+    def predict_buff_state(self, pos: int, current: HyaBuff = None) -> HyaBuff:
+        color = self.buff_colors[pos]
+        if self.match_color(color):
+            return HyaBuff.BUFF_STATE0
+        roi = self.buff_state_rois[pos]
+        if current is not None and current != HyaBuff.BUFF_STATE0:
+            current_image = self.buff_state_images[current]
+            current_image.roi_back = roi
+            if self.appear(current_image):
+                return current
+        for i, img in enumerate(self.buff_state_images):
+            img.roi_back = roi
+            if self.appear(img):
+                # int to HyaBuff
+                return HyaBuff.from_index(i)
+        return HyaBuff.BUFF_STATE0
+
+    def recognize_bean_05(self) -> bool:
+        return self.appear(self.I_BEAN05)
+
+    def recognize_bean_10(self) -> bool:
+        return self.appear(self.I_BEAN10)
+
+    def bean_05to10(self):
+        self.swipe(self.S_BEAN_05TO10)
+
+    def bean_10to05(self):
+        self.swipe(self.S_BEAN_10TO05)
+
+    # main process
+    # ------------------------------------------------------------------------------------------------------------------
+
+    def invite_friend(self, same: bool = True):
+        logger.hr('Invite friend', 2)
+        while 1:
+            self.screenshot()
+            if self.appear(self.I_FRIEND_SAME_1) or self.appear(self.I_FRIEND_SAME_2):
+                break
+            if self.appear_then_click(self.I_HINVITE, interval=4):
+                continue
+        if not same:
+            logger.info('Invite different server friend')
+            self.ui_click(self.I_FRIEND_REMOTE_1, self.I_FRIEND_REMOTE_2)
+        while 1:
+            self.screenshot()
+            if not self.appear(self.I_HINVITE):
+                break
+            if self.click(self.C_FRIEND_1, interval=2):
+                continue
+        logger.info('Invite friend done')
+
 
 
 def covert_rgb():
 def covert_rgb():
     images_folders: Path = Path(r'E:\Project\OnmyojiAutoScript\tasks\Hyakkiyakou\temp\20240614T214216')
     images_folders: Path = Path(r'E:\Project\OnmyojiAutoScript\tasks\Hyakkiyakou\temp\20240614T214216')
@@ -134,7 +225,6 @@ def test_predict_res():
     from module.config.config import Config
     from module.config.config import Config
     from module.device.device import Device
     from module.device.device import Device
 
 
-
     c = Config('oas1')
     c = Config('oas1')
     d = Device(c)
     d = Device(c)
     hd = HyaSlave(c, d)
     hd = HyaSlave(c, d)
@@ -150,7 +240,6 @@ def test_predict_res():
     # total time is 36.2ms on my computer /cpu:AMD Ryzen 5 3550H with Radeon Vega Mobile
     # total time is 36.2ms on my computer /cpu:AMD Ryzen 5 3550H with Radeon Vega Mobile
     # 0.362ms per predict_res
     # 0.362ms per predict_res
 
 
-
 def test_predict_bean():
 def test_predict_bean():
     import timeit
     import timeit
     from module.config.config import Config
     from module.config.config import Config
@@ -169,15 +258,30 @@ def test_predict_bean():
     print(f"执行总的时间: {execution_time * 1000} ms")
     print(f"执行总的时间: {execution_time * 1000} ms")
     # total time is 17.9ms on my computer in 100 times
     # total time is 17.9ms on my computer in 100 times
 
 
+def test_predict_buff():
+    import timeit
+    from module.config.config import Config
+    from module.device.device import Device
+
+    c = Config('oas1')
+    d = Device(c)
+    hd = HyaSlave(c, d)
+    img = cv2.imread(r'E:\Project\OnmyojiAutoScript\tasks\Hyakkiyakou\temp\save14\all1718372600237.png')
+    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
+    hd.device.image = img
+    print(hd.predict_buff_state(1))
+
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    # from module.config.config import Config
-    # from module.device.device import Device
-    #
-    # c = Config('oas1')
-    # d = Device(c)
-    # hd = HyaSlave(c, d)
-    # covert_rgb()
+    from module.config.config import Config
+    from module.device.device import Device
+
+    c = Config('oas1')
+    d = Device(c)
+    hd = HyaSlave(c, d)
+    hd.invite_friend(False)
+
     # test_predict_res()
     # test_predict_res()
-    test_predict_bean()
+    # test_predict_bean()
+    # test_predict_buff()