script_task.py 19 KB

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