script_task.py 18 KB

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