game_ui.py 8.4 KB

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