game_ui.py 8.1 KB

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