game_ui.py 10.0 KB

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