game_ui.py 11 KB

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