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

feat(gameui): add automatic routing

runhey 3 лет назад
Родитель
Сommit
2fe63cd9b5

+ 75 - 1
module/base/decorator.py

@@ -124,4 +124,78 @@ def has_cached_property(obj, name):
         obj:
         obj:
         name (str):
         name (str):
     """
     """
-    return name in obj.__dict__
+    return name in obj.__dict__
+
+
+
+def function_drop(rate=0.5, default=None):
+    """
+    Drop function calls to simulate random emulator stuck, for testing purpose.
+
+    Args:
+        rate (float): 0 to 1. Drop rate.
+        default: Default value to return if dropped.
+
+    Examples:
+        @function_drop(0.3)
+        def click(self, button, record_check=True):
+            pass
+
+        30% possibility:
+        INFO | Dropped: module.device.device.Device.click(REWARD_GOTO_MAIN, record_check=True)
+        70% possibility:
+        INFO | Click (1091,  628) @ REWARD_GOTO_MAIN
+    """
+    from module.logger import logger
+
+    def decorate(func):
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            if random.uniform(0, 1) > rate:
+                return func(*args, **kwargs)
+            else:
+                cls = ''
+                arguments = [str(arg) for arg in args]
+                if len(arguments):
+                    matched = re.search('<(.*?) object at', arguments[0])
+                    if matched:
+                        cls = matched.group(1) + '.'
+                        arguments.pop(0)
+                arguments += [f'{k}={v}' for k, v in kwargs.items()]
+                arguments = ', '.join(arguments)
+                logger.info(f'Dropped: {cls}{func.__name__}({arguments})')
+                return default
+
+        return wrapper
+
+    return decorate
+
+
+def run_once(f):
+    """
+    Run a function only once, no matter how many times it has been called.
+
+    Examples:
+        @run_once
+        def my_function(foo, bar):
+            return foo + bar
+
+        while 1:
+            my_function()
+
+    Examples:
+        def my_function(foo, bar):
+            return foo + bar
+
+        action = run_once(my_function)
+        while 1:
+            action()
+    """
+
+    def wrapper(*args, **kwargs):
+        if not wrapper.has_run:
+            wrapper.has_run = True
+            return f(*args, **kwargs)
+
+    wrapper.has_run = False
+    return wrapper

+ 6 - 4
tasks/GameUi/assets.py

@@ -12,9 +12,9 @@ class GameUiAssets:
 
 
 	# Image Rule Assets
 	# Image Rule Assets
 	# description 
 	# description 
-	I_CHECK_MAIN = RuleImage(roi_front=(355,47,48,43), roi_back=(355,47,48,43), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_main.png")
+	I_CHECK_MAIN = RuleImage(roi_front=(54,228,38,37), roi_back=(30,205,1033,96), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_main.png")
 	# description 
 	# description 
-	I_MAIN_GOTO_EXPLORATION = RuleImage(roi_front=(475,104,45,66), roi_back=(475,104,45,66), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_main_goto_exploration.png")
+	I_MAIN_GOTO_EXPLORATION = RuleImage(roi_front=(493,116,45,75), roi_back=(243,100,933,211), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_main_goto_exploration.png")
 	# description 
 	# description 
 	I_CHECK_EXPLORATION = RuleImage(roi_front=(1130,120,61,55), roi_back=(1130,120,61,55), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_exploration.png")
 	I_CHECK_EXPLORATION = RuleImage(roi_front=(1130,120,61,55), roi_back=(1130,120,61,55), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_exploration.png")
 	# description 
 	# description 
@@ -85,15 +85,17 @@ class GameUiAssets:
 	# description 
 	# description 
 	I_CHECK_DUEL = RuleImage(roi_front=(1045,610,50,53), roi_back=(1018,579,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_duel.png")
 	I_CHECK_DUEL = RuleImage(roi_front=(1045,610,50,53), roi_back=(1018,579,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_duel.png")
 	# description 
 	# description 
-	I_CHECK_DEMON_ENCOUNTER = RuleImage(roi_front=(0,0,100,100), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_demon_encounter.png")
+	I_CHECK_DEMON_ENCOUNTER = RuleImage(roi_front=(26,658,42,43), roi_back=(2,619,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_demon_encounter.png")
 	# description 
 	# description 
-	I_CHECK_HUNT = RuleImage(roi_front=(0,0,100,100), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_hunt.png")
+	I_CHECK_HUNT = RuleImage(roi_front=(1071,605,81,85), roi_back=(1026,558,152,156), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_hunt.png")
 	# description 
 	# description 
 	I_CHECK_HYAKKISEN = RuleImage(roi_front=(1014,607,53,55), roi_back=(986,587,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_hyakkisen.png")
 	I_CHECK_HYAKKISEN = RuleImage(roi_front=(1014,607,53,55), roi_back=(986,587,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_hyakkisen.png")
 	# description 
 	# description 
 	I_CHECK_DRAFT_DUEL = RuleImage(roi_front=(1051,612,56,58), roi_back=(1029,594,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_draft_duel.png")
 	I_CHECK_DRAFT_DUEL = RuleImage(roi_front=(1051,612,56,58), roi_back=(1029,594,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_check_draft_duel.png")
 	# description 
 	# description 
 	I_BACK_YOLLOW = RuleImage(roi_front=(24,16,48,55), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_back_yollow.png")
 	I_BACK_YOLLOW = RuleImage(roi_front=(24,16,48,55), roi_back=(0,0,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_back_yollow.png")
+	# description 
+	I_DEMON_ENCOUNTER_GOTO_TOWN = RuleImage(roi_front=(28,20,56,52), roi_back=(3,4,100,100), threshold=0.8, method="Template matching", file="./tasks/GameUi/page/page_demon_encounter_goto_town.png")
 
 
 
 
 	# Image Rule Assets
 	# Image Rule Assets

+ 195 - 0
tasks/GameUi/game_ui.py

@@ -3,12 +3,28 @@
 # github https://github.com/runhey
 # github https://github.com/runhey
 import time
 import time
 
 
+from module.base.decorator import run_once
+from module.base.timer import Timer
+from module.atom.image import RuleImage
+from module.atom.ocr import RuleOcr
+
 from tasks.GameUi.assets import GameUiAssets
 from tasks.GameUi.assets import GameUiAssets
+from tasks.GameUi.page import *
 from tasks.base_task import BaseTask
 from tasks.base_task import BaseTask
 from module.logger import logger
 from module.logger import logger
+from module.exception import (GameNotRunningError, GamePageUnknownError, RequestHumanTakeover)
 
 
 
 
 class GameUi(BaseTask, GameUiAssets):
 class GameUi(BaseTask, GameUiAssets):
+    ui_current: Page = None
+    ui_pages = [page_main, page_summon, page_exploration, page_town,
+                # 探索的
+                page_awake_zones, page_soul_zones, page_realm_raid, page_goryou_realm, page_delegation, page_secret_zones, page_area_boss, page_heian_kitan, page_six_gates, page_bondling_fairyland,
+                # 町中的
+                page_duel, page_demon_encounter, page_hunt, page_draft_duel, page_hyakkisen,
+                # 庭院里面的
+                page_shikigami_records, page_onmyodo, page_friends, page_daily, page_mall, page_guild, page_team, page_collection
+                ]
 
 
     def home_explore(self) -> bool:
     def home_explore(self) -> bool:
         """
         """
@@ -38,5 +54,184 @@ class GameUi(BaseTask, GameUiAssets):
         logger.info(f'Click {self.I_HOME_SHIKIKAMI.name}')
         logger.info(f'Click {self.I_HOME_SHIKIKAMI.name}')
         return True
         return True
 
 
+    def ui_page_appear(self, page):
+        """
+        判断当前页面是否为page
+        """
+        return self.appear(page.check_button)
+
+    def ensure_button_execute(self, button):
+        """
+        确保button执行
+        """
+        if isinstance(button, RuleImage) and self.appear(button):
+            return True
+        elif callable(button) and button():
+            return True
+        else:
+            return False
+
+    def ui_get_current_page(self, skip_first_screenshot=True) -> Page:
+        """
+        获取当前页面
+        :param skip_first_screenshot:
+        :return:
+        """
+        logger.info("UI get current page")
+
+        @run_once
+        def app_check():
+            if not self.device.app_is_running():
+                raise GameNotRunningError("Game not running")
+
+        @run_once
+        def minicap_check():
+            if self.config.script.device.control_method == "uiautomator2":
+                self.device.uninstall_minicap()
+
+        @run_once
+        def rotation_check():
+            self.device.get_orientation()
+
+        timeout = Timer(10, count=20).start()
+        while 1:
+            if skip_first_screenshot:
+                skip_first_screenshot = False
+                if not hasattr(self.device, "image") or self.device.image is None:
+                    self.screenshot()
+            else:
+                self.screenshot()
+
+            # End
+            # 如果20S还没有到底,那么就抛出异常
+            if timeout.reached():
+                break
+
+            # Known pages
+            for page in self.ui_pages:
+                if page.check_button is None:
+                    continue
+                if self.ui_page_appear(page=page):
+                    logger.attr("UI", page.name)
+                    self.ui_current = page
+                    return page
+
+            # Unknown page but able to handle
+            # logger.info("Unknown ui page")
+            # if self.appear_then_click(GOTO_MAIN, offset=(30, 30), interval=2) or self.ui_additional():
+            #     timeout.reset()
+            #     continue
+
+            app_check()
+            minicap_check()
+            rotation_check()
+
+        # Unknown page, need manual switching
+        logger.warning("Unknown ui page")
+        logger.attr("EMULATOR__SCREENSHOT_METHOD", self.config.script.device.screenshot_method)
+        logger.attr("EMULATOR__CONTROL_METHOD", self.config.script.device.control_method)
+        logger.warning("Starting from current page is not supported")
+        logger.warning(f"Supported page: {[str(page) for page in self.ui_pages]}")
+        logger.warning('Supported page: Any page with a "HOME" button on the upper-right')
+        logger.critical("Please switch to a supported page before starting oas")
+        raise GamePageUnknownError
+
+
+    def ui_button_interval_reset(self, button):
+        """
+        Reset interval of some button to avoid mistaken clicks
+
+        Args:
+            button (Button):
+        """
+        pass
+
+    def ui_goto(self, destination, confirm_wait=0, skip_first_screenshot=True):
+        """
+        Args:
+            destination (Page):
+            confirm_wait:
+            skip_first_screenshot:
+        """
+        # Reset connection
+        for page in self.ui_pages:
+            page.parent = None
+
+        # Create connection
+        visited = [destination]
+        visited = set(visited)
+        # 广度优先搜索
+        while 1:
+            new = visited.copy()
+            for page in visited:
+                for link in self.ui_pages:
+                    if link in visited:
+                        continue
+                    if page in link.links:
+                        link.parent = page
+                        new.add(link)
+            # 没有新的页面加入,说明已经遍历完毕
+            if len(new) == len(visited):
+                break
+            visited = new
+
+
+        logger.hr(f"UI goto {destination}")
+        for page in visited:
+            logger.info(f"visited: {page}")
+        confirm_timer = Timer(confirm_wait, count=int(confirm_wait // 0.5)).start()
+        while 1:
+            if skip_first_screenshot:
+                skip_first_screenshot = False
+            else:
+                self.screenshot()
+
+            # Destination page
+            if self.appear(destination.check_button):
+                if confirm_timer.reached():
+                    logger.info(f'Page arrive: {destination}')
+                    break
+            else:
+                confirm_timer.reset()
+
+            # Other pages
+            clicked = False
+            for page in visited:
+                if page.parent is None or page.check_button is None:
+                    continue
+                # 获取当前页面的要点击的按钮
+                if self.appear(page.check_button, interval=4):
+                    logger.info(f'Page switch: {page} -> {page.parent}')
+                    button = page.links[page.parent]
+                    if self.appear_then_click(button, interval=2):
+                        self.ui_button_interval_reset(button)
+                        confirm_timer.reset()
+                        clicked = True
+                        break
+                # 如果当前页面不出现可以检测当前按键的按钮,那可能是有一些弹窗,广告,这个时候额外处理
+                elif page.additional:
+                    for button in page.additional:
+                        if isinstance(button, RuleImage) and self.appear_then_click(button, interval=1):
+                            logger.info(f'Page {page} additional button {button} clicked')
+                        if isinstance(button, RuleOcr) and self.ocr_appear_click(button, interval=1):
+                            logger.info(f'Page {page} additional button {button} clicked')
+            if clicked:
+                continue
+
+            # # Additional
+            # if self.ui_additional():
+            #     continue
+
+        # Reset connection
+        for page in self.ui_pages:
+            page.parent = None
 
 
 
 
+if __name__ == '__main__':
+    from module.config.config import Config
+    from module.device.device import Device
+    c = Config('oas1')
+    d = Device(c)
+    game = GameUi(config=c, device=d)
+    game.ui_get_current_page(skip_first_screenshot=True)
+    game.ui_goto(page_delegation)

+ 6 - 4
tasks/GameUi/page.py

@@ -6,9 +6,11 @@ from tasks.GameUi.assets import GameUiAssets as G
 class Page:
 class Page:
     parent = None
     parent = None
 
 
+
     def __init__(self, check_button):
     def __init__(self, check_button):
         self.check_button = check_button
         self.check_button = check_button
         self.links = {}
         self.links = {}
+        self.additional: list = None  # 附加按钮或者是ocr检测按钮
         (filename, line_number, function_name, text) = traceback.extract_stack()[-2]
         (filename, line_number, function_name, text) = traceback.extract_stack()[-2]
         self.name = text[:text.find('=')].strip()
         self.name = text[:text.find('=')].strip()
 
 
@@ -41,8 +43,8 @@ page_town.link(button=G.I_TOWN_GOTO_MAIN, destination=page_main)
 page_main.link(button=G.I_MAIN_GOTO_TOWN, destination=page_town)
 page_main.link(button=G.I_MAIN_GOTO_TOWN, destination=page_town)
 
 
 # Unknown
 # Unknown
-page_unknown = Page(None)
-page_unknown.link(button=G.I_CHECK_MAIN, destination=page_main)
+# page_unknown = Page(None)
+# page_unknown.link(button=G.I_CHECK_MAIN, destination=page_main)
 
 
 # ************************************* 探索部分 *****************************************#
 # ************************************* 探索部分 *****************************************#
 # 觉醒 awake zones
 # 觉醒 awake zones
@@ -94,11 +96,11 @@ page_duel.link(button=G.I_BACK_YOLLOW, destination=page_town)
 page_town.link(button=G.I_TOWN_GOTO_DUEL, destination=page_duel)
 page_town.link(button=G.I_TOWN_GOTO_DUEL, destination=page_duel)
 # 封魔之时 demon_encounter
 # 封魔之时 demon_encounter
 page_demon_encounter = Page(G.I_CHECK_DEMON_ENCOUNTER)
 page_demon_encounter = Page(G.I_CHECK_DEMON_ENCOUNTER)
-page_demon_encounter.link(button=G.I_BACK_YOLLOW, destination=page_town)
+page_demon_encounter.link(button=G.I_DEMON_ENCOUNTER_GOTO_TOWN, destination=page_town)
 page_town.link(button=G.I_TOWN_GOTO_DEMON_ENCOUNTER, destination=page_demon_encounter)
 page_town.link(button=G.I_TOWN_GOTO_DEMON_ENCOUNTER, destination=page_demon_encounter)
 # 狩猎战 hunt
 # 狩猎战 hunt
 page_hunt = Page(G.I_CHECK_HUNT)
 page_hunt = Page(G.I_CHECK_HUNT)
-page_hunt.link(button=G.I_BACK_YOLLOW, destination=page_town)
+page_hunt.link(button=G.I_BACK_BL, destination=page_town)
 page_town.link(button=G.I_TOWN_GOTO_HUNT, destination=page_hunt)
 page_town.link(button=G.I_TOWN_GOTO_HUNT, destination=page_hunt)
 # 协同斗技 draft_duel
 # 协同斗技 draft_duel
 page_draft_duel = Page(G.I_CHECK_DRAFT_DUEL)
 page_draft_duel = Page(G.I_CHECK_DRAFT_DUEL)

BIN
tasks/GameUi/page/page_check_demon_encounter.png


BIN
tasks/GameUi/page/page_check_exploration.png


BIN
tasks/GameUi/page/page_check_hunt.png


BIN
tasks/GameUi/page/page_check_main.png


BIN
tasks/GameUi/page/page_demon_encounter_goto_town.png


+ 4 - 4
tasks/GameUi/page/page_img.json

@@ -2,8 +2,8 @@
   {
   {
     "itemName": "check_main",
     "itemName": "check_main",
     "imageName": "page_check_main.png",
     "imageName": "page_check_main.png",
-    "roiFront": "355,47,48,43",
-    "roiBack": "355,47,48,43",
+    "roiFront": "54,228,38,37",
+    "roiBack": "30,205,1033,96",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "description"
     "description": "description"
@@ -11,8 +11,8 @@
   {
   {
     "itemName": "main_goto_exploration",
     "itemName": "main_goto_exploration",
     "imageName": "page_main_goto_exploration.png",
     "imageName": "page_main_goto_exploration.png",
-    "roiFront": "475,104,45,66",
-    "roiBack": "475,104,45,66",
+    "roiFront": "493,116,45,75",
+    "roiBack": "243,100,933,211",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "description"
     "description": "description"

+ 13 - 4
tasks/GameUi/page/page_img_2.json

@@ -83,8 +83,8 @@
   {
   {
     "itemName": "check_demon_encounter",
     "itemName": "check_demon_encounter",
     "imageName": "page_check_demon_encounter.png",
     "imageName": "page_check_demon_encounter.png",
-    "roiFront": "0,0,100,100",
-    "roiBack": "0,0,100,100",
+    "roiFront": "26,658,42,43",
+    "roiBack": "2,619,100,100",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "description"
     "description": "description"
@@ -92,8 +92,8 @@
   {
   {
     "itemName": "check_hunt",
     "itemName": "check_hunt",
     "imageName": "page_check_hunt.png",
     "imageName": "page_check_hunt.png",
-    "roiFront": "0,0,100,100",
-    "roiBack": "0,0,100,100",
+    "roiFront": "1071,605,81,85",
+    "roiBack": "1026,558,152,156",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "description"
     "description": "description"
@@ -124,5 +124,14 @@
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "description"
     "description": "description"
+  },
+  {
+    "itemName": "demon_encounter_goto_town",
+    "imageName": "page_demon_encounter_goto_town.png",
+    "roiFront": "28,20,56,52",
+    "roiBack": "3,4,100,100",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
   }
   }
 ]
 ]

BIN
tasks/GameUi/page/page_main_goto_exploration.png


BIN
tasks/GameUi/page/page_town_goto_demon_encounter.png