script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.logger import logger
  7. from tasks.GameUi.game_ui import GameUi, Page
  8. from tasks.GameUi.page import page_soul_zones, page_shikigami_records
  9. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  10. from tasks.Component.GeneralRoom.general_room import GeneralRoom
  11. from tasks.Component.GeneralInvite.general_invite import GeneralInvite
  12. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  13. from tasks.EternitySea.assets import EternitySeaAssets
  14. from tasks.Orochi.config import UserStatus
  15. from tasks.EternitySea.config import EternitySea
  16. from module.exception import RequestHumanTakeover
  17. from tasks.GameUi.page import page_main, page_soul_zones, page_shikigami_records
  18. from time import sleep
  19. class ScriptTask(
  20. GameUi, GeneralBattle, GeneralRoom, GeneralInvite, SwitchSoul, EternitySeaAssets
  21. ):
  22. @property
  23. def task_name(self):
  24. return "EternitySea"
  25. def _two_teams_switch_sous(self, config):
  26. if config.enable:
  27. self.ui_get_current_page()
  28. self.ui_goto(page_shikigami_records)
  29. self.run_switch_soul(config.switch_group_team)
  30. if config.enable_switch_by_name:
  31. self.ui_get_current_page()
  32. self.ui_goto(page_shikigami_records)
  33. self.run_switch_soul_by_name(config.group_name, config.team_name)
  34. def run(self) -> None:
  35. self._two_teams_switch_sous(self._task_config.switch_soul_config_1)
  36. self._two_teams_switch_sous(self._task_config.switch_soul_config_2)
  37. match self._task_config.eternity_sea_config.user_status:
  38. case UserStatus.LEADER: success = self.run_leader()
  39. case UserStatus.MEMBER: success = self.run_member()
  40. case UserStatus.ALONE: success = self.run_alone()
  41. case _: logger.error('Unknown user status')
  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._navigate_to_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. # 当结束或者是失败退出循环的时候只有两个UI的可能,在房间或者是在组队界面
  109. # 如果在房间就退出
  110. if self.exit_room():
  111. pass
  112. # 如果在组队界面就退出
  113. if self.exit_team():
  114. pass
  115. self.ui_get_current_page()
  116. self.ui_goto(page_main)
  117. if not success:
  118. return False
  119. return True
  120. def run_member(self):
  121. logger.info('Start run member')
  122. self.ui_get_current_page()
  123. # self.ui_goto(page_soul_zones)
  124. # self.orochi_enter()
  125. # self.check_lock(self.config.orochi.general_battle_config.lock_team_enable)
  126. # 进入战斗流程
  127. self.device.stuck_record_add('BATTLE_STATUS_S')
  128. while 1:
  129. self.screenshot()
  130. #限制
  131. if self.current_count >= self._task_config.eternity_sea_config.limit_count:
  132. logger.info("EternitySea count limit out")
  133. break
  134. if datetime.now() - self.start_time >= self._limit_time:
  135. logger.info("EternitySea time limit out")
  136. break
  137. if self.check_then_accept():
  138. continue
  139. if self.is_in_room():
  140. self.device.stuck_record_clear()
  141. if self.wait_battle(wait_time=self._task_config.invite_config.wait_time):
  142. self.run_general_battle(config=self._task_config.general_battle_config)
  143. else:
  144. break
  145. # 队长秒开的时候,检测是否进入到战斗中
  146. elif self.check_take_over_battle(False, config=self._task_config.general_battle_config):
  147. continue
  148. while 1:
  149. # 有一种情况是本来要退出的,但是队长邀请了进入的战斗的加载界面
  150. if self.appear(self.I_GI_HOME) or self.appear(self.I_GI_EXPLORE):
  151. break
  152. # 如果可能在房间就退出
  153. if self.exit_room():
  154. pass
  155. # 如果还在战斗中,就退出战斗
  156. if self.exit_battle():
  157. pass
  158. self.ui_get_current_page()
  159. self.ui_goto(page_main)
  160. return True
  161. def run_alone(self) -> bool:
  162. logger.info("Start run alone")
  163. self._navigate_to_soul_zones()
  164. self._enter_eternity_sea()
  165. if self._task_config.general_battle_config.lock_team_enable == False:
  166. logger.critical(f"Only supports lock team mode")
  167. raise RequestHumanTakeover
  168. while 1:
  169. self.screenshot()
  170. if not self._is_in_eternity_sea():
  171. continue
  172. if self.current_count >= self._task_config.eternity_sea_config.limit_count:
  173. logger.info("EternitySea count limit out")
  174. break
  175. if datetime.now() - self.start_time >= self._limit_time:
  176. logger.info("EternitySea time limit out")
  177. break
  178. # 点击挑战
  179. while 1:
  180. self.screenshot()
  181. if self.appear_then_click(self.I_ETERNITY_SEA_FIRE, interval=1):
  182. pass
  183. if not self.appear(self.I_ETERNITY_SEA_FIRE):
  184. self.run_general_battle(
  185. config=self._task_config.general_battle_config
  186. )
  187. break
  188. return True
  189. def is_room_dead(self) -> bool:
  190. # 如果在探索界面或者是出现在组队界面,那就是可能房间死了
  191. sleep(0.5)
  192. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  193. sleep(0.5)
  194. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  195. return True
  196. return False
  197. def eternitysea_enter(self) -> bool:
  198. logger.info('Enter EternitySea')
  199. while True:
  200. self.screenshot()
  201. if self.appear(self.I_FORM_TEAM):
  202. return True
  203. if self.appear_then_click(self.I_ETERNITY_SEA, interval=1):
  204. continue
  205. def _is_in_eternity_sea(self) -> bool:
  206. self.screenshot()
  207. return self.appear(self.I_ETERNITY_SEA_FIRE)
  208. @property
  209. def _limit_time(self) -> timedelta:
  210. limit_time = self._task_config.eternity_sea_config.limit_time
  211. return timedelta(
  212. hours=limit_time.hour, minutes=limit_time.minute, seconds=limit_time.second
  213. )
  214. def _enter_eternity_sea(self) -> None:
  215. logger.info("Enter eternity_sea")
  216. while True:
  217. self.screenshot()
  218. if self.appear(self.I_FORM_TEAM, interval=1):
  219. return True
  220. if self.appear_then_click(self.I_ETERNITY_SEA, interval=1):
  221. continue
  222. #有可能点击到录像
  223. if self.appear_then_click(self.I_BACK_BOTTOM, interval=1):
  224. continue
  225. def _navigate_to_soul_zones(self) -> None:
  226. self.ui_get_current_page()
  227. self.ui_goto(page_soul_zones)
  228. def _navigate_to_game_page(self, destination: Page) -> None:
  229. self.ui_get_current_page()
  230. self.ui_goto(destination)
  231. @property
  232. def _task_config(self) -> EternitySea:
  233. return self.config.model.eternity_sea
  234. def check_layer(self, layer: str) -> bool:
  235. """
  236. 检查挑战的层数, 并选中挑战的层
  237. :return:
  238. """
  239. pos = self.list_find(self.L_LAYER_LIST, layer)
  240. if pos:
  241. self.device.click(x=pos[0], y=pos[1])
  242. return True
  243. def check_lock(self, lock: bool = True) -> bool:
  244. """
  245. 检查是否锁定阵容, 要求在永生之海界面
  246. :param lock:
  247. :return:
  248. """
  249. logger.info('Check lock: %s', lock)
  250. if lock:
  251. while 1:
  252. self.screenshot()
  253. if self.appear(self.I_NEWETERNITYSEA_LOCK):
  254. return True
  255. if self.appear_then_click(self.I_ETERNITYSEA_UNLOCK, interval=1):
  256. continue
  257. else:
  258. while 1:
  259. self.screenshot()
  260. if self.appear(self.I_ETERNITYSEA_UNLOCK):
  261. return True
  262. if self.appear_then_click(self.I_NEWETERNITYSEA_LOCK, interval=1):
  263. continue
  264. if __name__ == "__main__":
  265. from module.config.config import Config
  266. from module.device.device import Device
  267. c = Config("oas1")
  268. d = Device(c)
  269. t = ScriptTask(c, d)
  270. t.screenshot()