script_task.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import random
  5. from time import sleep
  6. from datetime import time, datetime, timedelta
  7. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  8. from tasks.Component.GeneralInvite.general_invite import GeneralInvite
  9. from tasks.Component.GeneralBuff.general_buff import GeneralBuff
  10. from tasks.Component.GeneralRoom.general_room import GeneralRoom
  11. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  12. from tasks.GameUi.game_ui import GameUi
  13. from tasks.GameUi.page import page_main, page_soul_zones, page_shikigami_records
  14. from tasks.Orochi.assets import OrochiAssets
  15. from tasks.Orochi.config import Orochi, UserStatus
  16. from module.logger import logger
  17. from module.exception import TaskEnd
  18. class ScriptTask(GeneralBattle, GeneralInvite, GeneralBuff, GeneralRoom, GameUi, SwitchSoul, OrochiAssets):
  19. def run(self) -> bool:
  20. # 御魂切换方式一
  21. if self.config.orochi.switch_soul.enable:
  22. self.ui_get_current_page()
  23. self.ui_goto(page_shikigami_records)
  24. self.run_switch_soul(self.config.orochi.switch_soul.switch_group_team)
  25. # 御魂切换方式二
  26. if self.config.orochi.switch_soul.enable_switch_by_name:
  27. self.ui_get_current_page()
  28. self.ui_goto(page_shikigami_records)
  29. self.run_switch_soul_by_name(self.config.orochi.switch_soul.group_name,
  30. self.config.orochi.switch_soul.team_name)
  31. limit_count = self.config.orochi.orochi_config.limit_count
  32. limit_time = self.config.orochi.orochi_config.limit_time
  33. self.current_count = 0
  34. self.limit_count: int = limit_count
  35. self.limit_time: timedelta = timedelta(hours=limit_time.hour, minutes=limit_time.minute, seconds=limit_time.second)
  36. config: Orochi = self.config.orochi
  37. if not self.is_in_battle(True):
  38. self.ui_get_current_page()
  39. self.ui_goto(page_main)
  40. if config.orochi_config.soul_buff_enable:
  41. self.open_buff()
  42. self.soul(is_open=True)
  43. self.close_buff()
  44. success = True
  45. match config.orochi_config.user_status:
  46. case UserStatus.LEADER: success = self.run_leader()
  47. case UserStatus.MEMBER: success = self.run_member()
  48. case UserStatus.ALONE: self.run_alone()
  49. case UserStatus.WILD: self.run_wild()
  50. case _: logger.error('Unknown user status')
  51. # 记得关掉
  52. if config.orochi_config.soul_buff_enable:
  53. self.open_buff()
  54. self.soul(is_open=False)
  55. self.close_buff()
  56. # 下一次运行时间
  57. if success:
  58. self.set_next_run('Orochi', finish=True, success=True)
  59. else:
  60. self.set_next_run('Orochi', finish=False, success=False)
  61. raise TaskEnd
  62. def orochi_enter(self) -> bool:
  63. logger.info('Enter orochi')
  64. while True:
  65. self.screenshot()
  66. if self.appear(self.I_FORM_TEAM):
  67. return True
  68. if self.appear_then_click(self.I_OROCHI, interval=1):
  69. continue
  70. def check_layer(self, layer: str) -> bool:
  71. """
  72. 检查挑战的层数, 并选中挑战的层
  73. :return:
  74. """
  75. pos = self.list_find(self.L_LAYER_LIST, layer)
  76. if pos:
  77. self.device.click(x=pos[0], y=pos[1])
  78. return True
  79. def check_lock(self, lock: bool = True) -> bool:
  80. """
  81. 检查是否锁定阵容, 要求在八岐大蛇界面
  82. :param lock:
  83. :return:
  84. """
  85. logger.info('Check lock: %s', lock)
  86. if lock:
  87. while 1:
  88. self.screenshot()
  89. if self.appear(self.I_OROCHI_LOCK):
  90. return True
  91. if self.appear_then_click(self.I_OROCHI_UNLOCK, interval=1):
  92. continue
  93. else:
  94. while 1:
  95. self.screenshot()
  96. if self.appear(self.I_OROCHI_UNLOCK):
  97. return True
  98. if self.appear_then_click(self.I_OROCHI_LOCK, interval=1):
  99. continue
  100. def run_leader(self):
  101. logger.info('Start run leader')
  102. self.ui_get_current_page()
  103. self.ui_goto(page_soul_zones)
  104. self.orochi_enter()
  105. layer = self.config.orochi.orochi_config.layer
  106. self.check_layer(layer)
  107. self.check_lock(self.config.orochi.general_battle_config.lock_team_enable)
  108. # 创建队伍
  109. logger.info('Create team')
  110. while 1:
  111. self.screenshot()
  112. if self.appear(self.I_OROCHI_MATCHING):
  113. break
  114. if self.appear_then_click(self.I_FORM_TEAM, interval=1):
  115. continue
  116. # 创建房间
  117. self.create_room()
  118. self.ensure_private()
  119. self.create_ensure()
  120. # 邀请队友
  121. success = True
  122. is_first = True
  123. # 这个时候我已经进入房间了哦
  124. while 1:
  125. self.screenshot()
  126. # 无论胜利与否, 都会出现是否邀请一次队友
  127. # 区别在于,失败的话不会出现那个勾选默认邀请的框
  128. if self.check_and_invite(self.config.orochi.invite_config.default_invite):
  129. continue
  130. # 检查猫咪奖励
  131. if self.appear_then_click(self.I_PET_PRESENT, action=self.C_WIN_3, interval=1):
  132. continue
  133. if self.current_count >= self.limit_count:
  134. if self.is_in_room():
  135. logger.info('Orochi count limit out')
  136. break
  137. if datetime.now() - self.start_time >= self.limit_time:
  138. if self.is_in_room():
  139. logger.info('Orochi time limit out')
  140. break
  141. # 如果没有进入房间那就不需要后面的邀请
  142. if not self.is_in_room():
  143. # 如果在探索界面或者是出现在组队界面, 那就是可能房间死了
  144. # 要结束任务
  145. sleep(0.5)
  146. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  147. sleep(0.5)
  148. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  149. logger.warning('Orochi task failed')
  150. success = False
  151. break
  152. continue
  153. # 点击挑战
  154. if not is_first:
  155. if self.run_invite(config=self.config.orochi.invite_config):
  156. self.run_general_battle(config=self.config.orochi.general_battle_config)
  157. else:
  158. # 邀请失败,退出任务
  159. logger.warning('Invite failed and exit this orochi task')
  160. success = False
  161. break
  162. # 第一次会邀请队友
  163. if is_first:
  164. if not self.run_invite(config=self.config.orochi.invite_config, is_first=True):
  165. logger.warning('Invite failed and exit this orochi task')
  166. success = False
  167. break
  168. else:
  169. is_first = False
  170. self.run_general_battle(config=self.config.orochi.general_battle_config)
  171. # 当结束或者是失败退出循环的时候只有两个UI的可能,在房间或者是在组队界面
  172. # 如果在房间就退出
  173. if self.exit_room():
  174. pass
  175. # 如果在组队界面就退出
  176. if self.exit_team():
  177. pass
  178. self.ui_get_current_page()
  179. self.ui_goto(page_main)
  180. if not success:
  181. return False
  182. return True
  183. def run_member(self):
  184. logger.info('Start run member')
  185. self.ui_get_current_page()
  186. # self.ui_goto(page_soul_zones)
  187. # self.orochi_enter()
  188. # self.check_lock(self.config.orochi.general_battle_config.lock_team_enable)
  189. # 进入战斗流程
  190. self.device.stuck_record_add('BATTLE_STATUS_S')
  191. while 1:
  192. self.screenshot()
  193. # 检查猫咪奖励
  194. if self.appear_then_click(self.I_PET_PRESENT, action=self.C_WIN_3, interval=1):
  195. continue
  196. if self.current_count >= self.limit_count:
  197. logger.info('Orochi count limit out')
  198. break
  199. if datetime.now() - self.start_time >= self.limit_time:
  200. logger.info('Orochi time limit out')
  201. break
  202. if self.check_then_accept():
  203. continue
  204. if self.is_in_room():
  205. self.device.stuck_record_clear()
  206. if self.wait_battle(wait_time=self.config.orochi.invite_config.wait_time):
  207. self.run_general_battle(config=self.config.orochi.general_battle_config)
  208. else:
  209. break
  210. # 队长秒开的时候,检测是否进入到战斗中
  211. elif self.check_take_over_battle(False, config=self.config.orochi.general_battle_config):
  212. continue
  213. while 1:
  214. # 有一种情况是本来要退出的,但是队长邀请了进入的战斗的加载界面
  215. if self.appear(self.I_GI_HOME) or self.appear(self.I_GI_EXPLORE):
  216. break
  217. # 如果可能在房间就退出
  218. if self.exit_room():
  219. pass
  220. # 如果还在战斗中,就退出战斗
  221. if self.exit_battle():
  222. pass
  223. self.ui_get_current_page()
  224. self.ui_goto(page_main)
  225. return True
  226. def run_alone(self):
  227. logger.info('Start run alone')
  228. self.ui_get_current_page()
  229. self.ui_goto(page_soul_zones)
  230. self.orochi_enter()
  231. layer = self.config.orochi.orochi_config.layer
  232. self.check_layer(layer)
  233. self.check_lock(self.config.orochi.general_battle_config.lock_team_enable)
  234. def is_in_orochi(screenshot=False) -> bool:
  235. if screenshot:
  236. self.screenshot()
  237. return self.appear(self.I_OROCHI_FIRE)
  238. while 1:
  239. self.screenshot()
  240. # 检查猫咪奖励
  241. if self.appear_then_click(self.I_PET_PRESENT, action=self.C_WIN_3, interval=1):
  242. continue
  243. if not is_in_orochi():
  244. continue
  245. if self.current_count >= self.limit_count:
  246. logger.info('Orochi count limit out')
  247. break
  248. if datetime.now() - self.start_time >= self.limit_time:
  249. logger.info('Orochi time limit out')
  250. break
  251. # 点击挑战
  252. while 1:
  253. self.screenshot()
  254. if self.appear_then_click(self.I_OROCHI_FIRE, interval=1):
  255. pass
  256. if not self.appear(self.I_OROCHI_FIRE):
  257. self.run_general_battle(config=self.config.orochi.general_battle_config)
  258. break
  259. # 回去
  260. while 1:
  261. self.screenshot()
  262. if not self.appear(self.I_FORM_TEAM):
  263. break
  264. if self.appear_then_click(self.I_BACK_BL, interval=1):
  265. continue
  266. self.ui_current = page_soul_zones
  267. self.ui_goto(page_main)
  268. def run_wild(self):
  269. logger.info('Start run wild')
  270. # 已经在战斗中不必初始化,保证已经组队开始战斗的情况下可以自动执行后续任务
  271. if not self.is_in_battle(True):
  272. self.ui_get_current_page()
  273. self.ui_goto(page_soul_zones)
  274. self.orochi_enter()
  275. layer = self.config.orochi.orochi_config.layer
  276. self.check_layer(layer)
  277. self.check_lock(self.config.orochi.general_battle_config.lock_team_enable)
  278. # 创建队伍
  279. logger.info('Create team')
  280. while 1:
  281. self.screenshot()
  282. if self.appear(self.I_OROCHI_MATCHING):
  283. break
  284. if self.appear_then_click(self.I_FORM_TEAM, interval=1):
  285. continue
  286. # 创建房间
  287. self.create_room()
  288. self.ensure_public()
  289. self.create_ensure()
  290. while 1:
  291. self.screenshot()
  292. # 无论胜利与否, 都会出现是否邀请一次队友
  293. # 区别在于,失败的话不会出现那个勾选默认邀请的框
  294. if self.check_and_invite(self.config.orochi.invite_config.default_invite):
  295. continue
  296. # 检查猫咪奖励
  297. if self.appear_then_click(self.I_PET_PRESENT, action=self.C_WIN_3, interval=1):
  298. continue
  299. if self.current_count >= self.limit_count:
  300. if self.is_in_room():
  301. logger.info('Orochi count limit out')
  302. break
  303. if datetime.now() - self.start_time >= self.limit_time:
  304. if self.is_in_room():
  305. logger.info('Orochi time limit out')
  306. break
  307. if not self.is_in_room():
  308. # 如果在探索界面或者是出现在组队界面,那就是可能房间死了,要结束任务
  309. sleep(0.5)
  310. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  311. sleep(0.5)
  312. if self.appear(self.I_MATCHING) or self.appear(self.I_CHECK_EXPLORATION):
  313. logger.warning('Orochi task failed')
  314. success = False
  315. break
  316. continue
  317. # 点击挑战
  318. logger.info('Wait for starting')
  319. while 1:
  320. self.screenshot()
  321. # 在进入战斗前必然会出现挑战界面,因此点击失败必须重复点击,防止卡在挑战界面,
  322. # 点击成功后如果网络卡顿,导致没有进入战斗,则无法进入 run_general_battle 流程,
  323. # 所以如果判断是在战斗中,则执行通用战斗流程
  324. if not (self.appear_then_click(self.I_OROCHI_WILD_FIRE, threshold=0.8)
  325. or self.is_in_battle(False)):
  326. continue
  327. self.screenshot()
  328. if not self.appear(self.I_OROCHI_WILD_FIRE, threshold=0.8):
  329. self.run_general_battle(config=self.config.orochi.general_battle_config)
  330. break
  331. # 当结束或者是失败退出循环的时候只有两个UI的可能,在房间或者是在组队界面
  332. # 如果在房间就退出
  333. if self.exit_room():
  334. pass
  335. # 如果在组队界面就退出
  336. if self.exit_team():
  337. pass
  338. self.ui_get_current_page()
  339. self.ui_goto(page_main)
  340. if not success:
  341. return False
  342. return True
  343. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  344. """
  345. 重写战斗等待
  346. :param random_click_swipt_enable:
  347. :return:
  348. """
  349. # 重写
  350. self.device.stuck_record_add('BATTLE_STATUS_S')
  351. self.device.click_record_clear()
  352. # 战斗过程 随机点击和滑动 防封
  353. logger.info("Start battle process")
  354. while 1:
  355. self.screenshot()
  356. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  357. if self.appear_then_click(self.I_WIN, action=action_click ,interval=0.8):
  358. # 赢的那个鼓
  359. continue
  360. if self.appear(self.I_GREED_GHOST):
  361. # 贪吃鬼
  362. logger.info('Win battle')
  363. self.wait_until_appear(self.I_REWARD, wait_time=1.5)
  364. while 1:
  365. self.screenshot()
  366. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  367. if self.appear_then_click(self.I_GREED_GHOST, action=action_click, interval=1.5):
  368. continue
  369. if not self.appear(self.I_GREED_GHOST):
  370. break
  371. return True
  372. if self.appear(self.I_FALSE):
  373. logger.warning('False battle')
  374. self.ui_click_until_disappear(self.I_FALSE)
  375. return False
  376. # 如果开启战斗过程随机滑动
  377. if random_click_swipt_enable:
  378. self.random_click_swipt()
  379. if __name__ == '__main__':
  380. from module.config.config import Config
  381. from module.device.device import Device
  382. from memory_profiler import profile
  383. c = Config('test')
  384. d = Device(c)
  385. t = ScriptTask(c, d)
  386. t.run()
  387. # t.check_layer('悲')
  388. from module.base.timer import timer
  389. @timer
  390. @profile
  391. def test_memory():
  392. t.screenshot()
  393. print(t.ocr_appear(t.O_O_TEST_OCR))
  394. print(t.L_LAYER_LIST.image_appear(t.device.image, '悲'))
  395. for i in range(4):
  396. test_memory()
  397. print('=====================')