game_ui.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. from time import sleep
  6. from module.base.decorator import run_once
  7. from module.base.timer import Timer
  8. from module.atom.image import RuleImage
  9. from module.atom.ocr import RuleOcr
  10. from tasks.GameUi.assets import GameUiAssets
  11. from tasks.GameUi.page import *
  12. from tasks.base_task import BaseTask
  13. from module.logger import logger
  14. from module.exception import (GameNotRunningError, GamePageUnknownError, RequestHumanTakeover)
  15. class GameUi(BaseTask, GameUiAssets):
  16. ui_current: Page = None
  17. ui_pages = [page_main, page_summon, page_exploration, page_town,
  18. # 探索的
  19. 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,
  20. # 町中的
  21. page_duel, page_demon_encounter, page_hunt, page_draft_duel, page_hyakkisen,
  22. # 庭院里面的
  23. page_shikigami_records, page_onmyodo, page_friends, page_daily, page_mall, page_guild, page_team, page_collection
  24. ]
  25. ui_close = [GameUiAssets.I_BACK_BLUE, GameUiAssets.I_BACK_YOLLOW, GameUiAssets.I_BACK_Y, GameUiAssets.I_BACK_MALL,
  26. GameUiAssets.I_BACK_FRIENDS, GameUiAssets.I_BACK_DAILY, GameUiAssets.I_BACK_BL,
  27. GameUiAssets.I_BONDLING_GOTO_EXPLORATION, GameUiAssets.I_REALM_RAID_GOTO_EXPLORATION, GameUiAssets.I_SIX_GATES_GOTO_EXPLORATION, GameUiAssets.I_SUMMON_GOTO_MAIN
  28. ]
  29. def home_explore(self) -> bool:
  30. """
  31. 使用ocr识别到探索按钮并点击
  32. :return:
  33. """
  34. while 1:
  35. self.screenshot()
  36. if self.ocr_appear_click(self.O_HOME_EXPLORE, interval=2):
  37. continue
  38. if self.appear(self.I_BACK_BLUE, threshold=0.6):
  39. break
  40. logger.info(f'Click {self.O_HOME_EXPLORE.name}')
  41. return True
  42. def explore_home(self) -> bool:
  43. """
  44. :return:
  45. """
  46. while 1:
  47. self.screenshot()
  48. if self.appear_then_click(self.I_BACK_BLUE, threshold=0.6, interval=2):
  49. continue
  50. if self.appear(self.I_HOME_SHIKIKAMI, threshold=0.6):
  51. break
  52. logger.info(f'Click {self.I_HOME_SHIKIKAMI.name}')
  53. return True
  54. def ui_page_appear(self, page):
  55. """
  56. 判断当前页面是否为page
  57. """
  58. return self.appear(page.check_button)
  59. def ensure_button_execute(self, button):
  60. """
  61. 确保button执行
  62. """
  63. if isinstance(button, RuleImage) and self.appear(button):
  64. return True
  65. elif callable(button) and button():
  66. return True
  67. else:
  68. return False
  69. def ui_get_current_page(self, skip_first_screenshot=True) -> Page:
  70. """
  71. 获取当前页面
  72. :param skip_first_screenshot:
  73. :return:
  74. """
  75. logger.info("UI get current page")
  76. @run_once
  77. def app_check():
  78. if not self.device.app_is_running():
  79. raise GameNotRunningError("Game not running")
  80. @run_once
  81. def minicap_check():
  82. if self.config.script.device.control_method == "uiautomator2":
  83. self.device.uninstall_minicap()
  84. @run_once
  85. def rotation_check():
  86. self.device.get_orientation()
  87. timeout = Timer(10, count=20).start()
  88. while 1:
  89. if skip_first_screenshot:
  90. skip_first_screenshot = False
  91. if not hasattr(self.device, "image") or self.device.image is None:
  92. self.screenshot()
  93. else:
  94. self.screenshot()
  95. # End
  96. # 如果20S还没有到底,那么就抛出异常
  97. if timeout.reached():
  98. break
  99. # Known pages
  100. for page in self.ui_pages:
  101. if page.check_button is None:
  102. continue
  103. if self.ui_page_appear(page=page):
  104. logger.attr("UI", page.name)
  105. self.ui_current = page
  106. return page
  107. # Try to close unknown page
  108. for close in self.ui_close:
  109. if self.appear_then_click(close, interval=1):
  110. logger.info('Trying to switch to supported page')
  111. timeout = Timer(10, count=20).start()
  112. # Unknown page but able to handle
  113. # logger.info("Unknown ui page")
  114. # if self.appear_then_click(GOTO_MAIN, offset=(30, 30), interval=2) or self.ui_additional():
  115. # timeout.reset()
  116. # continue
  117. app_check()
  118. minicap_check()
  119. rotation_check()
  120. # Unknown page, need manual switching
  121. logger.warning("Unknown ui page")
  122. logger.attr("EMULATOR__SCREENSHOT_METHOD", self.config.script.device.screenshot_method)
  123. logger.attr("EMULATOR__CONTROL_METHOD", self.config.script.device.control_method)
  124. logger.warning("Starting from current page is not supported")
  125. logger.warning(f"Supported page: {[str(page) for page in self.ui_pages]}")
  126. logger.warning('Supported page: Any page with a "HOME" button on the upper-right')
  127. logger.critical("Please switch to a supported page before starting oas")
  128. raise GamePageUnknownError
  129. def ui_button_interval_reset(self, button):
  130. """
  131. Reset interval of some button to avoid mistaken clicks
  132. Args:
  133. button (Button):
  134. """
  135. pass
  136. def ui_goto(self, destination, confirm_wait=0, skip_first_screenshot=True):
  137. """
  138. Args:
  139. destination (Page):
  140. confirm_wait:
  141. skip_first_screenshot:
  142. """
  143. # Reset connection
  144. for page in self.ui_pages:
  145. page.parent = None
  146. # Create connection
  147. visited = [destination]
  148. visited = set(visited)
  149. # 广度优先搜索
  150. while 1:
  151. new = visited.copy()
  152. for page in visited:
  153. for link in self.ui_pages:
  154. if link in visited:
  155. continue
  156. if page in link.links:
  157. link.parent = page
  158. new.add(link)
  159. # 没有新的页面加入,说明已经遍历完毕
  160. if len(new) == len(visited):
  161. break
  162. visited = new
  163. logger.hr(f"UI goto {destination}")
  164. confirm_timer = Timer(confirm_wait, count=int(confirm_wait // 0.5)).start()
  165. while 1:
  166. if skip_first_screenshot:
  167. skip_first_screenshot = False
  168. else:
  169. self.screenshot()
  170. # Destination page
  171. if self.appear(destination.check_button):
  172. if confirm_timer.reached():
  173. logger.info(f'Page arrive: {destination}')
  174. break
  175. else:
  176. confirm_timer.reset()
  177. # Other pages
  178. clicked = False
  179. for page in visited:
  180. if page.parent is None or page.check_button is None:
  181. continue
  182. # 如果当前页面不出现可以检测当前按键的按钮,那可能是有一些弹窗,广告,这个时候额外处理
  183. if page.additional:
  184. if not isinstance(page.additional, list):
  185. page.additional = [page.additional]
  186. for button in page.additional:
  187. if isinstance(button, RuleImage) and self.appear_then_click(button, interval=1):
  188. sleep(0.4)
  189. logger.info(f'Page {page} additional button {button} clicked')
  190. if isinstance(button, RuleOcr) and self.ocr_appear_click(button, interval=1):
  191. sleep(0.4)
  192. logger.info(f'Page {page} additional button {button} clicked')
  193. # 获取当前页面的要点击的按钮
  194. if self.appear(page.check_button, interval=4):
  195. logger.info(f'Page switch: {page} -> {page.parent}')
  196. button = page.links[page.parent]
  197. if self.appear_then_click(button, interval=2):
  198. self.ui_button_interval_reset(button)
  199. confirm_timer.reset()
  200. clicked = True
  201. break
  202. if clicked:
  203. continue
  204. # # Additional
  205. # if self.ui_additional():
  206. # continue
  207. # Reset connection
  208. for page in self.ui_pages:
  209. page.parent = None
  210. if __name__ == '__main__':
  211. from module.config.config import Config
  212. from module.device.device import Device
  213. c = Config('oas1')
  214. d = Device(c)
  215. game = GameUi(config=c, device=d)
  216. game.ui_get_current_page(skip_first_screenshot=True)
  217. game.ui_goto(page_shikigami_records)