script_task.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from datetime import datetime, timedelta
  5. from module.exception import TaskEnd
  6. from module.base.timer import Timer
  7. from module.logger import logger
  8. from tasks.GameUi.game_ui import GameUi, Page
  9. from tasks.GameUi.page import page_soul_zones, page_shikigami_records
  10. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  11. from tasks.Component.GeneralRoom.general_room import GeneralRoom
  12. from tasks.Component.GeneralInvite.general_invite import GeneralInvite
  13. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  14. from tasks.EternitySea.assets import EternitySeaAssets
  15. from tasks.Orochi.config import UserStatus
  16. from tasks.EternitySea.config import EternitySea
  17. from module.exception import RequestHumanTakeover
  18. from tasks.GameUi.page import page_main, page_soul_zones, page_shikigami_records
  19. from time import sleep
  20. class ScriptTask(
  21. GameUi, GeneralBattle, GeneralRoom, GeneralInvite, SwitchSoul, EternitySeaAssets
  22. ):
  23. @property
  24. def task_name(self):
  25. return "EternitySea"
  26. def _two_teams_switch_sous(self, config):
  27. if config.enable:
  28. self.ui_get_current_page()
  29. self.ui_goto(page_shikigami_records)
  30. self.run_switch_soul(config.switch_group_team)
  31. if config.enable_switch_by_name:
  32. self.ui_get_current_page()
  33. self.ui_goto(page_shikigami_records)
  34. self.run_switch_soul_by_name(config.group_name, config.team_name)
  35. def run(self) -> None:
  36. self._two_teams_switch_sous(self._task_config.switch_soul_config_1)
  37. self._two_teams_switch_sous(self._task_config.switch_soul_config_2)
  38. match self._task_config.eternity_sea_config.user_status:
  39. case UserStatus.LEADER: success = self.run_leader()
  40. case UserStatus.MEMBER: success = self.run_member()
  41. case UserStatus.ALONE: success = self.run_alone()
  42. case _: logger.error('Unknown user status')
  43. if success:
  44. self.set_next_run(self.task_name, finish=True, success=True)
  45. else:
  46. self.set_next_run(self.task_name, finish=False, success=False)
  47. raise TaskEnd(self.task_name)
  48. def run_leader(self):
  49. logger.info('Start run leader')
  50. self._navigate_to_soul_zones()
  51. self._enter_eternity_sea()
  52. layer = self._task_config.eternity_sea_config.layer
  53. self.check_layer(layer)
  54. self.check_lock(self._task_config.general_battle_config.lock_team_enable)
  55. # 创建队伍
  56. logger.info('Create team')
  57. while 1:
  58. self.screenshot()
  59. if self.appear(self.I_CHECK_TEAM):
  60. break
  61. if self.appear_then_click(self.I_FORM_TEAM, interval=1):
  62. continue
  63. # 创建房间
  64. self.create_room()
  65. self.ensure_private()
  66. self.create_ensure()
  67. # 邀请队友
  68. success = True
  69. is_first = True
  70. # 这个时候我已经进入房间了哦
  71. while 1:
  72. self.screenshot()
  73. # 无论胜利与否, 都会出现是否邀请一次队友
  74. # 区别在于,失败的话不会出现那个勾选默认邀请的框
  75. if self.check_and_invite(self._task_config.invite_config.default_invite):
  76. continue
  77. #限制
  78. if self.current_count >= self._task_config.eternity_sea_config.limit_count:
  79. logger.info("EternitySea count limit out")
  80. break
  81. if datetime.now() - self.start_time >= self._limit_time:
  82. logger.info("EternitySea time limit out")
  83. break
  84. # 如果没有进入房间那就不需要后面的邀请
  85. if not self.is_in_room():
  86. if self.is_room_dead():
  87. logger.warning('eternity_sea task failed')
  88. success = False
  89. break
  90. continue
  91. # 点击挑战
  92. if not is_first:
  93. if self.run_invite(config=self._task_config.invite_config):
  94. self.run_general_battle(config=self._task_config.general_battle_config)
  95. else:
  96. # 邀请失败,退出任务
  97. logger.warning('Invite failed and exit this eternity_sea task')
  98. success = False
  99. break
  100. # 第一次会邀请队友
  101. if is_first:
  102. if not self.run_invite(config=self._task_config.invite_config, is_first=True):
  103. logger.warning('Invite failed and exit this eternity_sea task')
  104. success = False
  105. break
  106. else:
  107. is_first = False
  108. self.run_general_battle(config=self._task_config.general_battle_config)
  109. # 战斗奖励动画可能还未结束, 先点掉残留奖励弹窗等界面稳定, 防止exit_room识别不到房间
  110. self._wait_room_stable()
  111. # 当结束或者是失败退出循环的时候只有两个UI的可能,在房间或者是在组队界面
  112. # 如果在房间就退出
  113. if self.exit_room():
  114. pass
  115. # 如果在组队界面就退出
  116. if self.exit_team():
  117. pass
  118. # 永生之海选层界面无HOME按钮且页面系统识别不了, 点返回箭头回到御魂副本选择界面
  119. self._back_to_soul_zones()
  120. self.ui_get_current_page()
  121. self.ui_goto(page_main)
  122. if not success:
  123. return False
  124. return True
  125. def _wait_room_stable(self) -> None:
  126. """
  127. 打完最后一局领取奖励后, 奖励弹窗/金币动画可能遮挡左下角导致房间识别失败, 等待界面稳定
  128. """
  129. logger.info('Wait room stable')
  130. timer = Timer(8).start()
  131. while not timer.reached():
  132. self.screenshot()
  133. if self.appear_then_click(self.I_REWARD, interval=1.5):
  134. continue
  135. if self.appear_then_click(self.I_REWARD_GOLD, interval=1.5):
  136. continue
  137. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  138. sleep(1)
  139. return
  140. def _back_to_soul_zones(self) -> None:
  141. """
  142. 点返回箭头直到回到御魂副本选择界面(page_soul_zones可识别)
  143. """
  144. logger.info('Back to soul zones')
  145. timer = Timer(15).start()
  146. while not timer.reached():
  147. self.screenshot()
  148. if self.ui_page_appear(page_soul_zones):
  149. return
  150. if self.appear_then_click(self.I_GI_SURE, interval=1):
  151. continue
  152. if self.appear_then_click(self.I_BACK_YELLOW, interval=1):
  153. continue
  154. if self.appear_then_click(self.I_BACK_YELLOW_SEA, interval=1):
  155. continue
  156. if self.appear_then_click(self.I_BACK_BOTTOM, interval=1):
  157. continue
  158. def run_member(self):
  159. logger.info('Start run member')
  160. self.ui_get_current_page()
  161. # self.ui_goto(page_soul_zones)
  162. # self.orochi_enter()
  163. # self.check_lock(self.config.orochi.general_battle_config.lock_team_enable)
  164. # 进入战斗流程
  165. self.device.stuck_record_add('BATTLE_STATUS_S')
  166. while 1:
  167. self.screenshot()
  168. #限制
  169. if self.current_count >= self._task_config.eternity_sea_config.limit_count:
  170. logger.info("EternitySea count limit out")
  171. break
  172. if datetime.now() - self.start_time >= self._limit_time:
  173. logger.info("EternitySea time limit out")
  174. break
  175. if self.check_then_accept():
  176. continue
  177. if self.is_in_room():
  178. self.device.stuck_record_clear()
  179. if self.wait_battle(wait_time=self._task_config.invite_config.wait_time):
  180. self.run_general_battle(config=self._task_config.general_battle_config)
  181. else:
  182. break
  183. # 队长秒开的时候,检测是否进入到战斗中
  184. elif self.check_take_over_battle(False, config=self._task_config.general_battle_config):
  185. continue
  186. while 1:
  187. # 有一种情况是本来要退出的,但是队长邀请了进入的战斗的加载界面
  188. if self.appear(self.I_GI_HOME) or self.appear(self.I_GI_EXPLORE):
  189. break
  190. # 如果可能在房间就退出
  191. if self.exit_room():
  192. pass
  193. # 如果还在战斗中,就退出战斗
  194. if self.exit_battle():
  195. pass
  196. self.ui_get_current_page()
  197. self.ui_goto(page_main)
  198. return True
  199. def run_alone(self) -> bool:
  200. logger.info("Start run alone")
  201. self._navigate_to_soul_zones()
  202. self._enter_eternity_sea()
  203. if self._task_config.general_battle_config.lock_team_enable == False:
  204. logger.critical(f"Only supports lock team mode")
  205. raise RequestHumanTakeover
  206. while 1:
  207. self.screenshot()
  208. if not self._is_in_eternity_sea():
  209. continue
  210. if self.current_count >= self._task_config.eternity_sea_config.limit_count:
  211. logger.info("EternitySea count limit out")
  212. break
  213. if datetime.now() - self.start_time >= self._limit_time:
  214. logger.info("EternitySea time limit out")
  215. break
  216. # 点击挑战
  217. while 1:
  218. self.screenshot()
  219. if self.appear_then_click(self.I_ETERNITY_SEA_FIRE, interval=1):
  220. pass
  221. if not self.appear(self.I_ETERNITY_SEA_FIRE):
  222. self.run_general_battle(
  223. config=self._task_config.general_battle_config
  224. )
  225. break
  226. return True
  227. def is_room_dead(self) -> bool:
  228. # 如果在探索界面或者是出现在组队界面,那就是可能房间死了
  229. sleep(0.5)
  230. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  231. sleep(0.5)
  232. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  233. return True
  234. return False
  235. def eternitysea_enter(self) -> bool:
  236. logger.info('Enter EternitySea')
  237. while True:
  238. self.screenshot()
  239. if self.appear(self.I_FORM_TEAM):
  240. return True
  241. if self.appear_then_click(self.I_ETERNITY_SEA, interval=1):
  242. continue
  243. def _is_in_eternity_sea(self) -> bool:
  244. self.screenshot()
  245. return self.appear(self.I_ETERNITY_SEA_FIRE)
  246. @property
  247. def _limit_time(self) -> timedelta:
  248. limit_time = self._task_config.eternity_sea_config.limit_time
  249. return timedelta(
  250. hours=limit_time.hour, minutes=limit_time.minute, seconds=limit_time.second
  251. )
  252. def _enter_eternity_sea(self) -> None:
  253. logger.info("Enter eternity_sea")
  254. while True:
  255. self.screenshot()
  256. if self.appear(self.I_FORM_TEAM, interval=1):
  257. return True
  258. if self.appear_then_click(self.I_ETERNITY_SEA, interval=1):
  259. continue
  260. #有可能点击到录像
  261. if self.appear_then_click(self.I_BACK_BOTTOM, interval=1):
  262. continue
  263. def _navigate_to_soul_zones(self) -> None:
  264. self.ui_get_current_page()
  265. self.ui_goto(page_soul_zones)
  266. def _navigate_to_game_page(self, destination: Page) -> None:
  267. self.ui_get_current_page()
  268. self.ui_goto(destination)
  269. @property
  270. def _task_config(self) -> EternitySea:
  271. return self.config.model.eternity_sea
  272. def check_layer(self, layer: str) -> bool:
  273. """
  274. 检查挑战的层数, 并选中挑战的层
  275. :return:
  276. """
  277. pos = self.list_find(self.L_LAYER_LIST, layer)
  278. if pos:
  279. self.device.click(x=pos[0], y=pos[1])
  280. return True
  281. def check_lock(self, lock: bool = True) -> bool:
  282. """
  283. 检查是否锁定阵容, 要求在永生之海界面
  284. :param lock:
  285. :return:
  286. """
  287. logger.info('Check lock: %s', lock)
  288. if lock:
  289. while 1:
  290. self.screenshot()
  291. if self.appear(self.I_NEWETERNITYSEA_LOCK):
  292. return True
  293. if self.appear_then_click(self.I_ETERNITYSEA_UNLOCK, interval=1):
  294. continue
  295. else:
  296. while 1:
  297. self.screenshot()
  298. if self.appear(self.I_ETERNITYSEA_UNLOCK):
  299. return True
  300. if self.appear_then_click(self.I_NEWETERNITYSEA_LOCK, interval=1):
  301. continue
  302. if __name__ == "__main__":
  303. from module.config.config import Config
  304. from module.device.device import Device
  305. c = Config("oas1")
  306. d = Device(c)
  307. t = ScriptTask(c, d)
  308. t.screenshot()