game_ui.py 12 KB

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