script_task.py 13 KB

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