game_ui.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. from datetime import datetime
  6. from time import sleep
  7. import random
  8. from collections import deque
  9. from module.atom.gif import RuleGif
  10. from module.atom.image import RuleImage
  11. from module.atom.list import RuleList
  12. from module.atom.ocr import RuleOcr
  13. from module.base.decorator import run_once
  14. from module.base.timer import Timer
  15. from module.exception import (GameNotRunningError, GamePageUnknownError)
  16. from module.logger import logger
  17. from tasks.GameUi.assets import GameUiAssets
  18. from tasks.GameUi.page import *
  19. from tasks.Restart.assets import RestartAssets
  20. from tasks.SixRealms.assets import SixRealmsAssets
  21. from tasks.base_task import BaseTask
  22. from tasks.ActivityShikigami.assets import ActivityShikigamiAssets
  23. class GameUi(BaseTask, GameUiAssets):
  24. ui_current: Page = None
  25. ui_pages = [
  26. # 登录
  27. page_login,
  28. # 主页
  29. page_main, page_summon, page_exploration, page_town,
  30. # 探索的
  31. page_awake_zones, page_soul_zones, page_realm_raid, page_goryou_realm, page_delegation,
  32. page_secret_zones, page_area_boss, page_heian_kitan, page_six_gates, page_bondling_fairyland,
  33. page_kekkai_toppa,
  34. # 町中的
  35. page_duel, page_demon_encounter, page_hunt, page_hunt_kirin, page_draft_duel, page_hyakkisen,
  36. # 庭院里面的
  37. page_shikigami_records, page_onmyodo, page_friends, page_daily, page_mall, page_guild, page_team,
  38. page_collection, page_act_list,
  39. # 爬塔活动
  40. page_act_list_climb_act, page_climb_act, page_climb_act_2, page_climb_act_pass, page_climb_act_ap,
  41. page_climb_act_boss, page_climb_act_buff, page_climb_act_ap100,
  42. # 战斗
  43. page_battle, page_reward, page_failed
  44. ]
  45. ui_close = [GameUiAssets.I_BACK_MALL, GeneralBattleAssets.I_CONFIRM,
  46. BaseTask.I_UI_BACK_RED, BaseTask.I_UI_BACK_YELLOW,
  47. GameUiAssets.I_BACK_FRIENDS, GameUiAssets.I_BACK_DAILY,
  48. GameUiAssets.I_REALM_RAID_GOTO_EXPLORATION,
  49. GameUiAssets.I_SIX_GATES_GOTO_EXPLORATION, SixRealmsAssets.I_EXIT_SIXREALMS,
  50. ActivityShikigamiAssets.I_SKIP_BUTTON, ActivityShikigamiAssets.I_RED_EXIT, BaseTask.I_UI_BACK_BLUE,
  51. ActivityShikigamiAssets.I_RED_EXIT_2]
  52. def home_explore(self) -> bool:
  53. """
  54. 使用ocr识别到探索按钮并点击
  55. :return:
  56. """
  57. while 1:
  58. self.screenshot()
  59. if self.ocr_appear_click(self.O_HOME_EXPLORE, interval=2):
  60. continue
  61. if self.appear(self.I_BACK_BLUE, threshold=0.6):
  62. break
  63. logger.info(f'Click {self.O_HOME_EXPLORE.name}')
  64. return True
  65. def explore_home(self) -> bool:
  66. """
  67. :return:
  68. """
  69. while 1:
  70. self.screenshot()
  71. if self.appear_then_click(self.I_BACK_BLUE, threshold=0.6, interval=2):
  72. continue
  73. if self.appear(self.I_HOME_SHIKIKAMI, threshold=0.6):
  74. break
  75. logger.info(f'Click {self.I_HOME_SHIKIKAMI.name}')
  76. return True
  77. def ui_page_appear(self, page: Page, skip_first_screenshot: bool = True, interval: float = None):
  78. """
  79. 判断当前页面是否为page
  80. """
  81. self.maybe_screenshot(skip_first_screenshot)
  82. if isinstance(page.check_button, list):
  83. for button in page.check_button:
  84. if self.appear(button, interval):
  85. return True
  86. return False
  87. return self.appear(page.check_button, interval)
  88. def ui_wait_until_appear(self, page: Page, timeout: float = 5, interval: float = 0.5,
  89. skip_first_screenshot: bool = True) -> bool:
  90. """
  91. 等待页面出现
  92. :param page: 等待的页面
  93. :param timeout: 超时时间
  94. :param interval: 检查间隔时间
  95. :param skip_first_screenshot:
  96. :return: 页面出现返回True, 否则返回False
  97. """
  98. logger.info(f'Waiting for {page}')
  99. timeout_timer = Timer(timeout).start()
  100. while not timeout_timer.reached():
  101. if self.ui_page_appear(page, skip_first_screenshot, interval=interval):
  102. return True
  103. skip_first_screenshot = False
  104. return False
  105. def ensure_scroll_open(self):
  106. """
  107. 判断庭院界面卷轴是否打开
  108. """
  109. return self.appear(RestartAssets.I_LOGIN_SCROOLL_CLOSE)
  110. def ensure_button_execute(self, button):
  111. """
  112. 确保button执行
  113. """
  114. if isinstance(button, RuleImage) and self.appear(button):
  115. return True
  116. elif callable(button) and button():
  117. return True
  118. else:
  119. return False
  120. def ui_get_current_page(self, skip_first_screenshot=True) -> Page:
  121. """
  122. 获取当前页面
  123. :param skip_first_screenshot:
  124. :return:
  125. """
  126. logger.info("UI get current page")
  127. @run_once
  128. def app_check():
  129. if not self.device.app_is_running():
  130. raise GameNotRunningError("Game not running")
  131. @run_once
  132. def minicap_check():
  133. if self.config.script.device.control_method == "uiautomator2":
  134. self.device.uninstall_minicap()
  135. @run_once
  136. def rotation_check():
  137. self.device.get_orientation()
  138. timeout = Timer(10, count=20).start()
  139. while 1:
  140. self.maybe_screenshot(skip_first_screenshot)
  141. skip_first_screenshot = False
  142. # 如果20S还没有到底,那么就抛出异常
  143. if timeout.reached():
  144. break
  145. # Known pages
  146. for page in self.ui_pages:
  147. if not page.check_button:
  148. continue
  149. if self.ui_page_appear(page=page, interval=None):
  150. logger.attr("UI", page.name)
  151. self.ui_current = page
  152. return page
  153. # Try to close unknown page
  154. if self.try_close_unknown_page():
  155. timeout = Timer(10, count=20).start()
  156. # wait to ui
  157. sleep(0.3)
  158. app_check()
  159. minicap_check()
  160. rotation_check()
  161. # Unknown page, need manual switching
  162. logger.warning("Unknown ui page")
  163. logger.attr("EMULATOR__SCREENSHOT_METHOD", self.config.script.device.screenshot_method)
  164. logger.attr("EMULATOR__CONTROL_METHOD", self.config.script.device.control_method)
  165. logger.warning("Starting from current page is not supported")
  166. logger.warning(f"Supported page: {[str(page) for page in self.ui_pages]}")
  167. logger.warning('Supported page: Any page with a "HOME" button on the upper-right')
  168. logger.critical("Please switch to a supported page before starting oas")
  169. raise GamePageUnknownError
  170. def ui_button_interval_reset(self, button):
  171. """
  172. Reset interval of some button to avoid mistaken clicks
  173. Args:
  174. button (Button):
  175. """
  176. if getattr(button, 'name', None) and button.name in self.interval_timer:
  177. self.interval_timer[button.name].reset()
  178. def build_reverse_path_dict(self, destination: Page) -> dict[Page, list[Page]]:
  179. """
  180. 构建从每个页面到目标页面的最短路径(反向 BFS)
  181. Returns:
  182. dict[Page, list[Page]] -> {start_page: [page1, ...destinationPage], ...}
  183. """
  184. paths = {destination: [destination]}
  185. queue = deque([destination])
  186. while queue:
  187. cur = queue.popleft()
  188. for page in self.ui_pages:
  189. if page not in paths and cur in page.links:
  190. # page -> cur
  191. paths[page] = [page] + paths[cur]
  192. queue.append(page)
  193. return paths
  194. def build_reverse_paths(self, destination: Page) -> list[tuple[Page, list[Page]]]:
  195. """
  196. 构建从每个页面到目标页面的最短路径(反向 BFS)
  197. 路径从短到长排序
  198. Returns:
  199. [(start_page, [page1, ...destinationPage]), ...]
  200. """
  201. paths = self.build_reverse_path_dict(destination)
  202. # 转换成列表并按路径长度排序, 短到长
  203. sorted_paths = sorted(paths.items(), key=lambda kv: len(kv[1]))
  204. return sorted_paths
  205. def ui_goto(self, destination: Page, confirm_wait=0, skip_first_screenshot=True, timeout: int = 60):
  206. """
  207. Args:
  208. destination (Page):
  209. confirm_wait:
  210. skip_first_screenshot:
  211. :return: find destination page or timeout reached
  212. """
  213. logger.hr(f"UI goto {destination}")
  214. # 初始化
  215. timeout_timer = Timer(timeout).start()
  216. confirm_timer = Timer(confirm_wait, count=int(confirm_wait // 0.5)).start()
  217. close_unknown_timer = Timer(3).start()
  218. # 构建路径映射
  219. path_dict = self.build_reverse_path_dict(destination)
  220. found = False
  221. while not timeout_timer.reached():
  222. if found:
  223. confirm_timer.wait()
  224. return True
  225. confirm_timer.reset()
  226. path = path_dict.get(self.ui_current, None)
  227. # 找不到路径则重新获取页面重试
  228. if not path:
  229. self.ui_get_current_page(skip_first_screenshot)
  230. continue
  231. skip_first_screenshot = False
  232. logger.info(f"Current page: {self.ui_current}. Following shortest path:")
  233. show_paths: str = ' -> '.join([p.name for p in path])
  234. logger.info(f"{show_paths}")
  235. # 遍历路径
  236. found = self._execute_path(path, timeout_timer)
  237. if not found:
  238. if close_unknown_timer.reached_and_reset():
  239. self.try_close_unknown_page(skip_screenshot=False)
  240. else:
  241. logger.error(f'Cannot goto page[{destination}], timeout[{timeout}s] reached')
  242. return False
  243. def try_close_unknown_page(self, skip_screenshot: bool = True):
  244. """
  245. 尝试关闭未知界面
  246. :return: 执行了关闭返回True, 否则False
  247. """
  248. self.maybe_screenshot(skip_screenshot)
  249. timer = Timer(None).start()
  250. for close in self.ui_close:
  251. if self.appear_then_click(close, interval=1.5):
  252. logger.warning('Trying to switch to supported page')
  253. logger.info(f'[{timer.current():.1f}s]Click {close} on {self.ui_current} success')
  254. return True
  255. return False
  256. def _execute_path(self, path: list, timeout_timer):
  257. """
  258. 执行路径
  259. :param path: currentPage,page1,page2,...,destinationPage
  260. :param timeout_timer: 超时定时器
  261. :return: currentPage==destinationPage
  262. """
  263. for i, current_page in enumerate(path):
  264. if timeout_timer.reached():
  265. return False
  266. # 当前页不等于路径中对应页, 尝试下一页
  267. if self.ui_current != current_page:
  268. continue
  269. self.run_additional(current_page, interval=0.6, skip_first_screenshot=False)
  270. # 如果已经是最后一页,不再跳转
  271. if i == len(path) - 1:
  272. if len(path) == 1:
  273. logger.info(f'Page arrived {current_page}')
  274. break
  275. next_page = path[i + 1]
  276. logger.info(f'Page switch: {current_page} -> {next_page}')
  277. # 获取页面跳转操作
  278. button = current_page.links.get(next_page)
  279. if not button:
  280. logger.warning(f"No link from {current_page} to {next_page}")
  281. continue
  282. # 跳转页面
  283. max_wait_timer = Timer(6).start()
  284. while not max_wait_timer.reached():
  285. if timeout_timer.reached():
  286. return False
  287. if self.appear_then_operate(button, interval=0.8, skip_first_screenshot=False):
  288. break
  289. logger.warning(f"[{max_wait_timer.current():.1f}s]Failed click {button} on {current_page}, retry...")
  290. sleep(0.8)
  291. else:
  292. self.ui_get_current_page(skip_first_screenshot=False)
  293. # 当前页面不是对应路径的页面, 则尝试下一个页面
  294. if self.ui_current != current_page:
  295. continue
  296. max_wait_timer.reset()
  297. while not max_wait_timer.reached():
  298. if timeout_timer.reached():
  299. return False
  300. if self.ui_wait_until_appear(next_page, timeout=2.5, skip_first_screenshot=False):
  301. logger.info(f'[{max_wait_timer.current():.1f}s]Page arrived {next_page}')
  302. self.ui_current = next_page
  303. break
  304. else:
  305. # 重新获取当前页
  306. self.ui_get_current_page(skip_first_screenshot=False)
  307. return self.ui_current == path[-1]
  308. def run_additional(self, page: Page, interval: float = None, skip_first_screenshot: bool = True):
  309. """执行页面附加操作"""
  310. if not page.additional:
  311. return
  312. for btn in page.additional:
  313. if self.appear_then_operate(btn, interval=interval, skip_first_screenshot=skip_first_screenshot):
  314. logger.info(f'Page {page} additional {btn} clicked')
  315. skip_first_screenshot = False
  316. def appear_then_operate(self, target: RuleList | RuleImage | RuleGif | RuleOcr | RuleClick,
  317. interval: float = None, skip_first_screenshot: bool = True):
  318. """
  319. 出现对应目标执行操作(点击图像, 滑动列表至array第一个元素并点击, 点击OCR, 点击)
  320. :param target: 目标
  321. :param interval: 间隔
  322. :param skip_first_screenshot: 是否跳过首次截图
  323. :return: 是否成功操作
  324. """
  325. self.maybe_screenshot(skip_first_screenshot)
  326. operated = False
  327. if isinstance(target, RuleList):
  328. operated = self.list_appear_click(target, interval=interval)
  329. elif isinstance(target, (RuleImage, RuleGif)):
  330. operated = self.appear_then_click(target, interval=interval)
  331. elif isinstance(target, RuleOcr):
  332. operated = self.ocr_appear_click(target, interval=interval)
  333. elif isinstance(target, RuleClick):
  334. operated = self.click(target, interval=interval)
  335. return operated
  336. # ------------------------------------------------------------------------------------------------------------------
  337. # 下面的这些是一些特殊的页面,需要额外处理
  338. # ------------------------------------------------------------------------------------------------------------------
  339. def main_goto_daily(self):
  340. """
  341. 无法直接一步到花合战,需要先到主页,然后再到花合战
  342. :return:
  343. """
  344. while 1:
  345. self.screenshot()
  346. if self.appear(self.I_CHECK_DAILY):
  347. break
  348. if self.appear_then_click(self.I_MAIN_GOTO_DAILY, interval=1):
  349. continue
  350. if self.ocr_appear_click(self.O_CLICK_CLOSE_1, interval=1):
  351. continue
  352. if self.ocr_appear_click(self.O_CLICK_CLOSE_2, interval=1):
  353. continue
  354. logger.info('Page arrive: Daily')
  355. time.sleep(1)
  356. return
  357. if __name__ == '__main__':
  358. from module.config.config import Config
  359. from module.device.device import Device
  360. c = Config('oas2')
  361. d = Device(c)
  362. game = GameUi(config=c, device=d)
  363. game.ui_get_current_page()
  364. game.ui_goto(page_main)