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