script_task.py 19 KB

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