game_ui.py 14 KB

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