game_ui.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. import importlib
  6. from pathlib import Path
  7. from datetime import datetime
  8. from time import sleep
  9. import random
  10. from collections import deque
  11. from module.atom.click import RuleClick
  12. from module.atom.gif import RuleGif
  13. from module.atom.image import RuleImage
  14. from module.atom.list import RuleList
  15. from module.atom.ocr import RuleOcr
  16. from module.base.decorator import run_once
  17. from module.base.timer import Timer
  18. from module.exception import (GameNotRunningError, GamePageUnknownError)
  19. from module.logger import logger
  20. from tasks.Component.GeneralBattle.assets import GeneralBattleAssets
  21. from tasks.GameUi.assets import GameUiAssets
  22. from tasks.GameUi.page import Page, PageRegistry, page_main, random_click
  23. from tasks.Restart.assets import RestartAssets
  24. from tasks.SixRealms.assets import SixRealmsAssets
  25. from tasks.base_task import BaseTask
  26. from tasks.ActivityShikigami.assets import ActivityShikigamiAssets
  27. class GameUi(BaseTask, GameUiAssets):
  28. ui_current: Page = None
  29. ui_close = [GameUiAssets.I_BACK_MALL, GeneralBattleAssets.I_CONFIRM,
  30. BaseTask.I_UI_BACK_RED, BaseTask.I_UI_BACK_YELLOW,
  31. GameUiAssets.I_BACK_FRIENDS, GameUiAssets.I_BACK_DAILY,
  32. GameUiAssets.I_REALM_RAID_GOTO_EXPLORATION,
  33. GameUiAssets.I_SIX_GATES_GOTO_EXPLORATION, SixRealmsAssets.I_EXIT_SIXREALMS,
  34. ActivityShikigamiAssets.I_SKIP_BUTTON, ActivityShikigamiAssets.I_RED_EXIT, BaseTask.I_UI_BACK_BLUE]
  35. def __init__(self, config, device):
  36. super().__init__(config, device)
  37. # 初始化时动态导入所有 page 模块
  38. self._import_all_pages()
  39. @staticmethod
  40. def _import_all_pages():
  41. """动态加载 tasks/**/page.py"""
  42. base_dir = Path(__file__).resolve().parent.parent # tasks 目录
  43. for task_dir in base_dir.iterdir():
  44. if not task_dir.is_dir():
  45. continue
  46. page_file = task_dir / "page.py"
  47. if not page_file.exists():
  48. continue
  49. module_name = f"tasks.{task_dir.name}.page"
  50. spec = importlib.util.spec_from_file_location(module_name, page_file)
  51. if spec and spec.loader:
  52. module = importlib.util.module_from_spec(spec)
  53. spec.loader.exec_module(module)
  54. @property
  55. def ui_pages(self) -> list[Page]:
  56. return PageRegistry.all()
  57. def ui_page_appear(self, page: Page, skip_first_screenshot: bool = True, interval: float = None):
  58. """
  59. 判断当前页面是否为page
  60. """
  61. self.maybe_screenshot(skip_first_screenshot)
  62. if isinstance(page.check_button, list):
  63. for button in page.check_button:
  64. if self.appear(button, interval):
  65. return True
  66. return False
  67. return self.appear(page.check_button, interval)
  68. def ui_wait_until_appear(self, page: Page, timeout: float = 5, interval: float = 0.5,
  69. skip_first_screenshot: bool = True) -> bool:
  70. """
  71. 等待页面出现
  72. :param page: 等待的页面
  73. :param timeout: 超时时间
  74. :param interval: 检查间隔时间
  75. :param skip_first_screenshot:
  76. :return: 页面出现返回True, 否则返回False
  77. """
  78. logger.info(f'Waiting for {page}')
  79. timeout_timer = Timer(timeout).start()
  80. while not timeout_timer.reached():
  81. if self.ui_page_appear(page, skip_first_screenshot, interval=interval):
  82. return True
  83. skip_first_screenshot = False
  84. return False
  85. def ui_get_current_page(self, skip_first_screenshot=True) -> Page:
  86. """
  87. 获取当前页面
  88. :param skip_first_screenshot:
  89. :return:
  90. """
  91. logger.info("UI get current page")
  92. @run_once
  93. def app_check():
  94. if not self.device.app_is_running():
  95. raise GameNotRunningError("Game not running")
  96. @run_once
  97. def minicap_check():
  98. if self.config.script.device.control_method == "uiautomator2":
  99. self.device.uninstall_minicap()
  100. @run_once
  101. def rotation_check():
  102. self.device.get_orientation()
  103. timeout = Timer(10, count=20).start()
  104. while 1:
  105. self.maybe_screenshot(skip_first_screenshot)
  106. skip_first_screenshot = False
  107. # 如果10S还没有到底,那么就抛出异常
  108. if timeout.reached():
  109. break
  110. # Known pages
  111. for page in self.ui_pages:
  112. if not page.check_button:
  113. continue
  114. if self.ui_page_appear(page=page, interval=None):
  115. logger.attr("UI", page.name)
  116. self.ui_current = page
  117. return page
  118. # Try to close unknown page
  119. if self.try_close_unknown_page():
  120. timeout = Timer(10, count=20).start()
  121. else:
  122. # entirely unknown page, click safe random area
  123. self.click(random_click(), interval=4)
  124. # wait to ui
  125. sleep(0.3)
  126. app_check()
  127. minicap_check()
  128. rotation_check()
  129. # Unknown page, need manual switching
  130. logger.warning("Unknown ui page")
  131. logger.attr("EMULATOR__SCREENSHOT_METHOD", self.config.script.device.screenshot_method)
  132. logger.attr("EMULATOR__CONTROL_METHOD", self.config.script.device.control_method)
  133. logger.warning("Starting from current page is not supported")
  134. logger.warning(f"Supported page: {[str(page) for page in self.ui_pages]}")
  135. logger.warning('Supported page: Any page with a "HOME" button on the upper-right')
  136. logger.critical("Please switch to a supported page before starting oas")
  137. raise GamePageUnknownError
  138. def ui_button_interval_reset(self, button):
  139. """
  140. Reset interval of some button to avoid mistaken clicks
  141. Args:
  142. button (Button):
  143. """
  144. if getattr(button, 'name', None) and button.name in self.interval_timer:
  145. self.interval_timer[button.name].reset()
  146. def build_reverse_path_dict(self, destination: Page) -> dict[Page, list[Page]]:
  147. """
  148. 构建从每个页面到目标页面的最短路径(反向 BFS)
  149. Returns:
  150. dict[Page, list[Page]] -> {start_page: [page1, ...destinationPage], ...}
  151. """
  152. paths = {destination: [destination]}
  153. queue = deque([destination])
  154. while queue:
  155. cur = queue.popleft()
  156. for page in self.ui_pages:
  157. if page not in paths and cur in page.links:
  158. # page -> cur
  159. paths[page] = [page] + paths[cur]
  160. queue.append(page)
  161. return paths
  162. def build_reverse_paths(self, destination: Page) -> list[tuple[Page, list[Page]]]:
  163. """
  164. 构建从每个页面到目标页面的最短路径(反向 BFS)
  165. 路径从短到长排序
  166. Returns:
  167. [(start_page, [page1, ...destinationPage]), ...]
  168. """
  169. paths = self.build_reverse_path_dict(destination)
  170. # 转换成列表并按路径长度排序, 短到长
  171. sorted_paths = sorted(paths.items(), key=lambda kv: len(kv[1]))
  172. return sorted_paths
  173. def ui_goto_page(self, dest_page: Page, confirm_wait=0, skip_first_screenshot=True, timeout: int = 60) -> bool:
  174. """前往指定page, 自动调用获取当前页面方法, 其他参数同ui_goto
  175. """
  176. self.ui_get_current_page()
  177. return self.ui_goto(dest_page, confirm_wait, skip_first_screenshot, timeout)
  178. def ui_goto(self, destination: Page, confirm_wait=0, skip_first_screenshot=True, timeout: int = 60) -> bool:
  179. """
  180. Args:
  181. destination (Page):
  182. confirm_wait:
  183. skip_first_screenshot:
  184. :return: find destination page or timeout reached
  185. """
  186. logger.hr(f"UI goto {destination}")
  187. # 初始化
  188. timeout_timer = Timer(timeout).start()
  189. confirm_timer = Timer(confirm_wait, count=int(confirm_wait // 0.5)).start()
  190. close_unknown_timer = Timer(3).start()
  191. # 构建路径映射
  192. path_dict = self.build_reverse_path_dict(destination)
  193. found = False
  194. while not timeout_timer.reached():
  195. if found:
  196. confirm_timer.wait()
  197. return True
  198. confirm_timer.reset()
  199. path = path_dict.get(self.ui_current, None)
  200. # 找不到路径则重新获取页面重试
  201. if not path:
  202. self.ui_get_current_page(skip_first_screenshot)
  203. continue
  204. skip_first_screenshot = False
  205. logger.info(f"Current page: {self.ui_current}. Following shortest path:")
  206. show_paths: str = ' -> '.join([p.name for p in path])
  207. logger.info(f"{show_paths}")
  208. # 遍历路径
  209. found = self._execute_path(path, timeout_timer)
  210. if not found:
  211. if close_unknown_timer.reached_and_reset():
  212. self.try_close_unknown_page(skip_screenshot=False)
  213. self.ui_current = None
  214. else:
  215. logger.error(f'Cannot goto page[{destination}], timeout[{timeout}s] reached')
  216. return False
  217. def try_close_unknown_page(self, skip_screenshot: bool = True):
  218. """
  219. 尝试关闭未知界面
  220. :return: 执行了关闭返回True, 否则False
  221. """
  222. self.maybe_screenshot(skip_screenshot)
  223. timer = Timer(None).start()
  224. for close in self.ui_close:
  225. if self.appear_then_click(close, interval=1.5):
  226. logger.warning('Trying to switch to supported page')
  227. logger.info(f'[{timer.current():.1f}s]Click {close} on {self.ui_current} success')
  228. return True
  229. return False
  230. def _execute_path(self, path: list, timeout_timer):
  231. """
  232. 执行路径
  233. :param path: currentPage,page1,page2,...,destinationPage
  234. :param timeout_timer: 超时定时器
  235. :return: currentPage==destinationPage
  236. """
  237. for i, current_page in enumerate(path):
  238. if timeout_timer.reached():
  239. return False
  240. # 当前页不等于路径中对应页, 尝试下一页
  241. if self.ui_current != current_page:
  242. continue
  243. self.run_additional(current_page, interval=0.6, skip_first_screenshot=False)
  244. # 如果已经是最后一页,不再跳转
  245. if i == len(path) - 1:
  246. if len(path) == 1:
  247. logger.info(f'Page arrived {current_page}')
  248. break
  249. next_page = path[i + 1]
  250. logger.info(f'Page switch: {current_page} -> {next_page}')
  251. # 获取页面跳转操作
  252. button = current_page.links.get(next_page)
  253. if not button:
  254. logger.warning(f"No link from {current_page} to {next_page}")
  255. continue
  256. # 跳转页面
  257. max_wait_timer = Timer(6).start()
  258. logger.info(f'Wait appear and operate {button} on {current_page}')
  259. while not max_wait_timer.reached():
  260. if timeout_timer.reached():
  261. return False
  262. if isinstance(button, list):
  263. exec_operates = [self.appear_then_operate(btn, interval=0.8, skip_first_screenshot=False)
  264. for btn in button]
  265. if exec_operates[0]: # 只要第一个成功就跳出
  266. break
  267. if self.appear_then_operate(button, interval=0.8, skip_first_screenshot=False):
  268. break
  269. else:
  270. logger.warning(f'Failed recognize {button} on {current_page}')
  271. self.ui_get_current_page(skip_first_screenshot=False)
  272. # 当前页面不是对应路径的页面, 则尝试下一个页面
  273. if self.ui_current != current_page:
  274. continue
  275. max_wait_timer.reset()
  276. while not max_wait_timer.reached():
  277. if timeout_timer.reached():
  278. return False
  279. if self.ui_wait_until_appear(next_page, timeout=2.5, skip_first_screenshot=False):
  280. logger.info(f'[{max_wait_timer.current():.1f}s]Page arrived {next_page}')
  281. self.ui_current = next_page
  282. break
  283. else:
  284. # 重新获取当前页
  285. self.ui_get_current_page(skip_first_screenshot=False)
  286. return self.ui_current == path[-1]
  287. def run_additional(self, page: Page, interval: float = None, skip_first_screenshot: bool = True):
  288. """执行页面附加操作"""
  289. if not page.additional:
  290. return
  291. for btn in page.additional:
  292. # 支持反向条件操作: [条件元素, 操作元素, True]。不出现条件图片时点击另一个区域。
  293. if isinstance(btn, list) and len(btn) == 3 and btn[2] is True:
  294. condition, action, invert = btn
  295. self.maybe_screenshot(skip_first_screenshot)
  296. if not self.appear(condition):
  297. if self.appear_then_operate(action, interval=interval, skip_first_screenshot=False):
  298. logger.info(f'Page {page} additional invert conditional not {condition} -> {action} executed')
  299. skip_first_screenshot = False
  300. # 支持复合条件操作: [条件元素, 操作元素]。出现条件图片时点击另一个区域。
  301. elif isinstance(btn, list) and len(btn) == 2:
  302. condition, action = btn
  303. self.maybe_screenshot(skip_first_screenshot)
  304. if self.appear(condition):
  305. if self.appear_then_operate(action, interval=interval, skip_first_screenshot=False):
  306. logger.info(f'Page {page} additional conditional {condition} -> {action} executed')
  307. skip_first_screenshot = False
  308. elif self.appear_then_operate(btn, interval=interval, skip_first_screenshot=skip_first_screenshot):
  309. logger.info(f'Page {page} additional {btn} clicked')
  310. skip_first_screenshot = False
  311. def appear_then_operate(self, target: RuleList | RuleImage | RuleGif | RuleOcr | RuleClick,
  312. interval: float = None, skip_first_screenshot: bool = True):
  313. """
  314. 出现对应目标执行操作(点击图像, 滑动列表至array第一个元素并点击, 点击OCR, 点击)
  315. :param target: 目标
  316. :param interval: 间隔
  317. :param skip_first_screenshot: 是否跳过首次截图
  318. :return: 是否成功操作
  319. """
  320. self.maybe_screenshot(skip_first_screenshot)
  321. operated = False
  322. if isinstance(target, RuleList):
  323. operated = self.list_appear_click(target, interval=interval)
  324. elif isinstance(target, (RuleImage, RuleGif)):
  325. operated = self.appear_then_click(target, interval=interval)
  326. elif isinstance(target, RuleOcr):
  327. operated = self.ocr_appear_click(target, interval=interval)
  328. elif isinstance(target, RuleClick):
  329. operated = self.click(target, interval=interval)
  330. return operated
  331. if __name__ == '__main__':
  332. from module.config.config import Config
  333. from module.device.device import Device
  334. from tasks.GameUi.page import page_guild
  335. c = Config('oas1')
  336. d = Device(c)
  337. game = GameUi(config=c, device=d)
  338. for i in range(10):
  339. game.ui_get_current_page()
  340. game.ui_goto(page_guild)
  341. game.ui_get_current_page()
  342. game.ui_goto(page_main)