|
|
@@ -3,8 +3,7 @@
|
|
|
# github https://github.com/runhey
|
|
|
import time
|
|
|
from time import sleep
|
|
|
-
|
|
|
-import random
|
|
|
+from collections import deque
|
|
|
from module.atom.image import RuleImage
|
|
|
from module.atom.list import RuleList
|
|
|
from module.atom.ocr import RuleOcr
|
|
|
@@ -148,15 +147,12 @@ class GameUi(BaseTask, GameUiAssets):
|
|
|
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:
|
|
|
+ if not page.check_button:
|
|
|
continue
|
|
|
if self.ui_page_appear(page=page):
|
|
|
logger.attr("UI", page.name)
|
|
|
@@ -169,17 +165,9 @@ class GameUi(BaseTask, GameUiAssets):
|
|
|
if self.appear_then_click(close, interval=1.5):
|
|
|
logger.info('Trying to switch to supported page')
|
|
|
timeout = Timer(10, count=20).start()
|
|
|
- # 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)
|
|
|
@@ -199,6 +187,27 @@ class GameUi(BaseTask, GameUiAssets):
|
|
|
"""
|
|
|
pass
|
|
|
|
|
|
+ def build_reverse_paths(self, destination: Page) -> list[tuple[Page, list[Page]]]:
|
|
|
+ """
|
|
|
+ 构建从每个页面到目标页面的最短路径(反向 BFS)
|
|
|
+ 并按路径长度从短到长排序返回。
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ list[tuple[Page, list[Page]]] -> [(start_page, [path...destinationPage]), ...]
|
|
|
+ """
|
|
|
+ paths = {destination: [destination]}
|
|
|
+ queue = deque([destination])
|
|
|
+ while queue:
|
|
|
+ cur = queue.popleft()
|
|
|
+ for page in self.ui_pages:
|
|
|
+ if page not in paths and cur in page.links:
|
|
|
+ # page -> cur
|
|
|
+ paths[page] = [page] + paths[cur]
|
|
|
+ queue.append(page)
|
|
|
+ # 转换成列表并按路径长度排序, 短到长
|
|
|
+ sorted_paths = sorted(paths.items(), key=lambda kv: len(kv[1]))
|
|
|
+ return sorted_paths
|
|
|
+
|
|
|
def ui_goto(self, destination: Page, confirm_wait=0, skip_first_screenshot=True, timeout: int = 45):
|
|
|
"""
|
|
|
Args:
|
|
|
@@ -207,97 +216,83 @@ class GameUi(BaseTask, GameUiAssets):
|
|
|
skip_first_screenshot:
|
|
|
:return: find destination page or timeout reached
|
|
|
"""
|
|
|
- # 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
|
|
|
-
|
|
|
- timeout_timer = Timer(timeout).start()
|
|
|
-
|
|
|
- def dfs(current_page: Page) -> bool:
|
|
|
- if current_page is None:
|
|
|
- return False
|
|
|
- if current_page == destination:
|
|
|
- return True
|
|
|
- if timeout_timer.reached():
|
|
|
- return False
|
|
|
- # wait current page
|
|
|
- self.ui_wait_until_appear(current_page)
|
|
|
- logger.info(f'Page switch: {current_page} -> {current_page.parent}')
|
|
|
- # current page additional
|
|
|
- if current_page.additional and isinstance(current_page.additional, list):
|
|
|
- for button in current_page.additional:
|
|
|
- if ((isinstance(button, RuleClick) and self.click(button)) or
|
|
|
- self.appear_then_click(button, interval=0.6) or
|
|
|
- (isinstance(button, RuleOcr) and self.ocr_appear_click(button, interval=2))):
|
|
|
- logger.info(f'Page {current_page} AB {button} clicked')
|
|
|
- # to next page
|
|
|
- button = current_page.links[current_page.parent]
|
|
|
- if ((isinstance(button, RuleList) and self.list_appear_click(button)) or
|
|
|
- (isinstance(button, RuleClick) and self.click(button)) or
|
|
|
- self.appear_then_click(button, interval=2) or
|
|
|
- (isinstance(button, RuleOcr) and self.ocr_appear_click(button, interval=2))):
|
|
|
- self.ui_button_interval_reset(button)
|
|
|
- confirm_timer.reset()
|
|
|
- return dfs(current_page.parent)
|
|
|
- return False
|
|
|
-
|
|
|
logger.hr(f"UI goto {destination}")
|
|
|
+ # 初始化
|
|
|
+ timeout_timer = Timer(timeout).start()
|
|
|
confirm_timer = Timer(confirm_wait, count=int(confirm_wait // 0.5)).start()
|
|
|
try_close_unknown_timer = Timer(3).start()
|
|
|
- while 1:
|
|
|
- if skip_first_screenshot:
|
|
|
- skip_first_screenshot = False
|
|
|
- else:
|
|
|
+ # 构建路径映射并排序
|
|
|
+ paths = self.build_reverse_paths(destination)
|
|
|
+
|
|
|
+ while not timeout_timer.reached():
|
|
|
+ if not skip_first_screenshot:
|
|
|
self.screenshot()
|
|
|
- # Destination page
|
|
|
+ skip_first_screenshot = False
|
|
|
+ # 已经在目标页面
|
|
|
if self.ui_page_appear(destination):
|
|
|
if confirm_timer.reached():
|
|
|
logger.info(f'Page arrive: {destination}')
|
|
|
break
|
|
|
- else:
|
|
|
confirm_timer.reset()
|
|
|
- # 超时退出
|
|
|
- if timeout_timer.reached():
|
|
|
- logger.error(f'Cannot goto page[{destination}], timeout[{timeout}s] reached')
|
|
|
- break
|
|
|
- # Try to close unknown page every 3 seconds
|
|
|
+ continue
|
|
|
+ # 尝试关闭未知页面
|
|
|
if try_close_unknown_timer.reached():
|
|
|
for close in self.ui_close:
|
|
|
if self.appear_then_click(close, interval=1.5):
|
|
|
logger.warning('Trying to switch to supported page')
|
|
|
try_close_unknown_timer.reset()
|
|
|
- # Other pages
|
|
|
- fined = False
|
|
|
- for page in visited:
|
|
|
- if page.check_button is None or not self.ui_page_appear(page):
|
|
|
+ # 遍历所有路径,优先尝试最短路径
|
|
|
+ for page, path in paths:
|
|
|
+ if not self.ui_page_appear(page):
|
|
|
continue
|
|
|
- fined = dfs(page)
|
|
|
- if fined:
|
|
|
- break
|
|
|
- if fined:
|
|
|
- continue
|
|
|
+ logger.info(f"Current page: {page}, following shortest path: {[p.name for p in path]}")
|
|
|
+ if self._execute_path(path, confirm_timer, timeout_timer):
|
|
|
+ return True
|
|
|
sleep(0.5)
|
|
|
- # Reset connection
|
|
|
- for page in self.ui_pages:
|
|
|
- page.parent = None
|
|
|
+ else:
|
|
|
+ logger.error(f'Cannot goto page[{destination}], timeout[{timeout}s] reached')
|
|
|
+ return False
|
|
|
+
|
|
|
+ def _execute_path(self, path, confirm_timer, timeout_timer):
|
|
|
+ """
|
|
|
+ 执行路径
|
|
|
+ :param path: currentPage,page1,page2,...,destinationPage
|
|
|
+ :param confirm_timer: 确认定时器
|
|
|
+ :param timeout_timer: 超时定时器
|
|
|
+ :return: currentPage==destinationPage
|
|
|
+ """
|
|
|
+ for i in range(len(path) - 1):
|
|
|
+ current_page, next_page = path[i], path[i + 1]
|
|
|
+ # 已超时则不再遍历
|
|
|
+ if timeout_timer.reached():
|
|
|
+ return False
|
|
|
+ # 等待当前页面出现
|
|
|
+ self.ui_wait_until_appear(current_page)
|
|
|
+ logger.info(f'Page switch: {current_page} -> {next_page}')
|
|
|
+ # 执行附加操作
|
|
|
+ if current_page.additional:
|
|
|
+ for button in current_page.additional:
|
|
|
+ if ((isinstance(button, RuleClick) and self.click(button)) or
|
|
|
+ self.appear_then_click(button, interval=0.6) or
|
|
|
+ (isinstance(button, RuleOcr) and self.ocr_appear_click(button, interval=2))):
|
|
|
+ logger.info(f'Page {current_page} additional {button} clicked')
|
|
|
+ # 执行页面跳转
|
|
|
+ button = current_page.links.get(next_page)
|
|
|
+ if not button:
|
|
|
+ logger.warning(f"No link from {current_page} to {next_page}")
|
|
|
+ return False
|
|
|
+ # 执行对应操作
|
|
|
+ if ((isinstance(button, RuleList) and self.list_appear_click(button)) or
|
|
|
+ (isinstance(button, RuleClick) and self.click(button)) or
|
|
|
+ self.appear_then_click(button, interval=2) or
|
|
|
+ (isinstance(button, RuleOcr) and self.ocr_appear_click(button, interval=2))):
|
|
|
+ self.ui_button_interval_reset(button)
|
|
|
+ confirm_timer.reset()
|
|
|
+ else:
|
|
|
+ logger.warning(f"Failed to click {button} on {current_page}")
|
|
|
+ return False
|
|
|
+ # 最后确认目标页面
|
|
|
+ return self.ui_page_appear(path[-1])
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------------------------
|
|
|
# 下面的这些是一些特殊的页面,需要额外处理
|