script_task.py 12 KB

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