game_ui.py 10 KB

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