game_ui.py 11 KB

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