game_ui.py 11 KB

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