script_task.py 18 KB

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