script_task.py 18 KB

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