general_battle.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 time
  5. import random
  6. from time import sleep
  7. from tasks.base_task import BaseTask
  8. from tasks.Component.GeneralBattle.config_general_battle import GreenMarkType, GeneralBattleConfig
  9. from tasks.Component.GeneralBattle.assets import GeneralBattleAssets
  10. from tasks.Component.GeneralBuff.config_buff import BuffClass
  11. from tasks.Component.GeneralBuff.general_buff import GeneralBuff
  12. from module.logger import logger
  13. class GeneralBattle(GeneralBuff, GeneralBattleAssets):
  14. """
  15. 使用这个通用的战斗必须要求这个任务的config有config_general_battle
  16. """
  17. def run_general_battle(self, config: GeneralBattleConfig = None, buff: BuffClass or list[BuffClass] = None) -> bool:
  18. """
  19. 运行脚本
  20. :return:
  21. """
  22. # 本人选择的策略是只要进来了就算一次,不管是不是打完了
  23. logger.hr("General battle start", 2)
  24. self.current_count += 1
  25. logger.info(f"Current count: {self.current_count}")
  26. if config is None:
  27. config = GeneralBattleConfig()
  28. # 如果没有锁定队伍。那么可以根据配置设定队伍
  29. if not config.lock_team_enable:
  30. logger.info("Lock team is not enable")
  31. # 如果更换队伍
  32. if self.current_count == 1:
  33. self.switch_preset_team(config.preset_enable, config.preset_group, config.preset_team)
  34. # 打开buff
  35. self.check_buff(buff)
  36. # 点击准备按钮
  37. self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
  38. self.wait_until_appear(self.I_BUFF)
  39. occur_prepare_button = False
  40. while 1:
  41. self.screenshot()
  42. if not self.appear(self.I_BUFF):
  43. break
  44. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  45. occur_prepare_button = True
  46. continue
  47. # if occur_prepare_button and self.ocr_appear_click(self.O_BATTLE_PREPARE, interval=2):
  48. # continue
  49. logger.info("Click prepare ensure button")
  50. # 照顾一下某些模拟器慢的
  51. time.sleep(0.1)
  52. # 绿标
  53. self.wait_until_disappear(self.I_BUFF)
  54. if self.is_in_battle(False):
  55. self.green_mark(config.green_enable, config.green_mark)
  56. win = self.battle_wait(config.random_click_swipt_enable)
  57. if win:
  58. return True
  59. else:
  60. return False
  61. def run_general_battle_back(self, config: GeneralBattleConfig = None) -> bool:
  62. """
  63. 进入挑战然后直接返回
  64. :param config:
  65. :return:
  66. """
  67. # 如果没有锁定队伍那么在点击准备后才退出的
  68. if not config.lock_team_enable:
  69. # 点击准备按钮
  70. self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
  71. while 1:
  72. self.screenshot()
  73. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  74. continue
  75. if not self.appear(self.I_PRESET):
  76. break
  77. logger.info(f"Click {self.I_PREPARE_HIGHLIGHT.name}")
  78. # 点击返回
  79. while 1:
  80. self.screenshot()
  81. if self.appear_then_click(self.I_EXIT, interval=1.5):
  82. continue
  83. if self.appear(self.I_EXIT_ENSURE):
  84. break
  85. logger.info(f"Click {self.I_EXIT.name}")
  86. # 点击返回确认
  87. while 1:
  88. self.screenshot()
  89. if self.appear_then_click(self.I_EXIT_ENSURE, interval=1.5):
  90. continue
  91. if self.appear(self.I_FALSE):
  92. break
  93. logger.info(f"Click {self.I_EXIT_ENSURE.name}")
  94. # 点击失败确认
  95. self.wait_until_appear(self.I_FALSE)
  96. while 1:
  97. self.screenshot()
  98. if self.appear_then_click(self.I_FALSE, interval=1.5):
  99. continue
  100. if not self.appear(self.I_FALSE):
  101. break
  102. logger.info(f"Click {self.I_FALSE.name}")
  103. return True
  104. def exit_battle(self, skip_first: bool = False) -> bool:
  105. """
  106. 在战斗的时候强制退出战斗
  107. :return:
  108. """
  109. if skip_first:
  110. self.screenshot()
  111. if not self.appear(self.I_EXIT):
  112. return False
  113. # 点击返回
  114. logger.info(f"Click {self.I_EXIT.name}")
  115. while 1:
  116. self.screenshot()
  117. if self.appear_then_click(self.I_EXIT, interval=1.5):
  118. continue
  119. if self.appear(self.I_EXIT_ENSURE):
  120. break
  121. # 点击返回确认
  122. while 1:
  123. self.screenshot()
  124. if self.appear_then_click(self.I_EXIT_ENSURE, interval=1.5):
  125. continue
  126. if self.appear_then_click(self.I_FALSE, interval=1.5):
  127. continue
  128. if not self.appear(self.I_EXIT):
  129. break
  130. return True
  131. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  132. """
  133. 等待战斗结束 !!!
  134. 很重要 这个函数是原先写的, 优化版本在tasks/Secret/script_task下。本着不改动原先的代码的原则,所以就不改了
  135. :param random_click_swipt_enable:
  136. :return:
  137. """
  138. # 有的时候是长战斗,需要在设置stuck检测为长战斗
  139. # 但是无需取消设置,因为如果有点击或者滑动的话 handle_control_check会自行取消掉
  140. self.device.stuck_record_add('BATTLE_STATUS_S')
  141. self.device.click_record_clear()
  142. # 战斗过程 随机点击和滑动 防封
  143. logger.info("Start battle process")
  144. win: bool = False
  145. while 1:
  146. self.screenshot()
  147. # 如果出现赢 就点击, 第二个是针对封魔的图片
  148. if self.appear(self.I_WIN, threshold=0.8) or self.appear(self.I_DE_WIN):
  149. logger.info("Battle result is win")
  150. if self.appear(self.I_DE_WIN):
  151. self.ui_click_until_disappear(self.I_DE_WIN)
  152. win = True
  153. break
  154. # 如果出现失败 就点击,返回False
  155. if self.appear(self.I_FALSE, threshold=0.8):
  156. logger.info("Battle result is false")
  157. win = False
  158. break
  159. # 如果领奖励
  160. if self.appear(self.I_REWARD, threshold=0.6):
  161. win = True
  162. break
  163. # 如果领奖励出现金币
  164. if self.appear(self.I_REWARD_GOLD, threshold=0.8):
  165. win = True
  166. break
  167. # 如果开启战斗过程随机滑动
  168. if random_click_swipt_enable:
  169. self.random_click_swipt()
  170. # 再次确认战斗结果
  171. logger.info("Reconfirm the results of the battle")
  172. while 1:
  173. self.screenshot()
  174. if win:
  175. # 点击赢了
  176. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  177. if self.appear_then_click(self.I_WIN, action=action_click, interval=0.5):
  178. continue
  179. if not self.appear(self.I_WIN):
  180. break
  181. else:
  182. # 如果失败且 点击失败后
  183. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  184. continue
  185. if not self.appear(self.I_FALSE, threshold=0.6):
  186. return False
  187. # 最后保证能点击 获得奖励
  188. if not self.wait_until_appear(self.I_REWARD):
  189. # 有些的战斗没有下面的奖励,所以直接返回
  190. logger.info("There is no reward, Exit battle")
  191. return win
  192. logger.info("Get reward")
  193. while 1:
  194. self.screenshot()
  195. # 如果出现领奖励
  196. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  197. if self.appear_then_click(self.I_REWARD, action=action_click, interval=1.5) or \
  198. self.appear_then_click(self.I_REWARD_GOLD, action=action_click, interval=1.5):
  199. continue
  200. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  201. break
  202. return win
  203. def green_mark(self, enable: bool = False, mark_mode: GreenMarkType = GreenMarkType.GREEN_MAIN):
  204. """
  205. 绿标, 如果不使能就直接返回
  206. :param enable:
  207. :param mark_mode:
  208. :return:
  209. """
  210. if enable:
  211. logger.info("Green is enable")
  212. x, y = None, None
  213. match mark_mode:
  214. case GreenMarkType.GREEN_LEFT1:
  215. x, y = self.C_GREEN_LEFT_1.coord()
  216. logger.info("Green left 1")
  217. case GreenMarkType.GREEN_LEFT2:
  218. x, y = self.C_GREEN_LEFT_2.coord()
  219. logger.info("Green left 2")
  220. case GreenMarkType.GREEN_LEFT3:
  221. x, y = self.C_GREEN_LEFT_3.coord()
  222. logger.info("Green left 3")
  223. case GreenMarkType.GREEN_LEFT4:
  224. x, y = self.C_GREEN_LEFT_4.coord()
  225. logger.info("Green left 4")
  226. case GreenMarkType.GREEN_LEFT5:
  227. x, y = self.C_GREEN_LEFT_5.coord()
  228. logger.info("Green left 5")
  229. case GreenMarkType.GREEN_MAIN:
  230. x, y = self.C_GREEN_MAIN.coord()
  231. logger.info("Green main")
  232. # 等待那个准备的消失
  233. while 1:
  234. self.screenshot()
  235. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  236. break
  237. # 判断有无坐标的偏移
  238. self.appear_then_click(self.I_LOCAL)
  239. time.sleep(0.3)
  240. # 点击绿标
  241. self.device.click(x, y)
  242. def switch_preset_team(self, enable: bool = False, preset_group: int = 1, preset_team: int = 1):
  243. """
  244. 切换预设的队伍, 要求是在不锁定队伍时的情况下
  245. :param enable:
  246. :param preset_group:
  247. :param preset_team:
  248. :return:
  249. """
  250. if not enable:
  251. logger.info("Preset is disable")
  252. return None
  253. logger.info("Preset is enable")
  254. # 点击预设按钮
  255. while 1:
  256. self.screenshot()
  257. if self.appear(self.I_PRESET_ENSURE):
  258. break
  259. # 首个队伍没有满足5个式神,未出现预设按钮的情况下跳出循环
  260. if self.appear(self.I_PRESENT_LESS_THAN_5):
  261. break
  262. if self.appear_then_click(self.I_PRESET, threshold=0.8, interval=1):
  263. continue
  264. logger.info("Click preset button")
  265. # 选择预设组
  266. x, y = None, None
  267. match preset_group:
  268. case 1:
  269. x, y = self.C_PRESET_GROUP_1.coord()
  270. case 2:
  271. x, y = self.C_PRESET_GROUP_2.coord()
  272. case 3:
  273. x, y = self.C_PRESET_GROUP_3.coord()
  274. case 4:
  275. x, y = self.C_PRESET_GROUP_4.coord()
  276. case 5:
  277. x, y = self.C_PRESET_GROUP_5.coord()
  278. case 6:
  279. x, y = self.C_PRESET_GROUP_6.coord()
  280. case 7:
  281. x, y = self.C_PRESET_GROUP_7.coord()
  282. case _:
  283. x, y = self.C_PRESET_GROUP_1.coord()
  284. count = 0
  285. while count < 3:
  286. self.device.click(x, y)
  287. sleep(0.02)
  288. count += 1
  289. logger.info("Select preset group")
  290. # 选择预设的队伍
  291. time.sleep(0.5)
  292. match preset_team:
  293. case 1:
  294. x, y = self.C_PRESET_TEAM_1.coord()
  295. case 2:
  296. x, y = self.C_PRESET_TEAM_2.coord()
  297. case 3:
  298. x, y = self.C_PRESET_TEAM_3.coord()
  299. case 4:
  300. x, y = self.C_PRESET_TEAM_4.coord()
  301. case _:
  302. x, y = self.C_PRESET_TEAM_1.coord()
  303. count = 0
  304. while count < 3:
  305. self.device.click(x, y)
  306. sleep(0.02)
  307. count += 1
  308. logger.info("Select preset team")
  309. # 点击预设确认
  310. self.wait_until_appear(self.I_PRESET_ENSURE, wait_time=1)
  311. while 1:
  312. self.screenshot()
  313. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8):
  314. continue
  315. if not self.appear(self.I_PRESET_ENSURE):
  316. break
  317. logger.info("Click preset ensure")
  318. def random_click_swipt(self):
  319. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  320. rand_type = random.randint(0, 2)
  321. match rand_type:
  322. case 0:
  323. self.click(self.C_RANDOM_CLICK, interval=20)
  324. case 1:
  325. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  326. case 2:
  327. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  328. # 重新设置为长战斗
  329. self.device.stuck_record_add('BATTLE_STATUS_S')
  330. else:
  331. time.sleep(0.4) # 这样的好像不对
  332. # 判断是否在战斗中
  333. def is_in_battle(self, is_screenshot: bool = True) -> bool:
  334. """
  335. 判断是否在战斗中
  336. :return:
  337. """
  338. if is_screenshot:
  339. self.screenshot()
  340. if self.appear(self.I_FRIENDS) or \
  341. self.appear(self.I_WIN) or \
  342. self.appear(self.I_FALSE) or \
  343. self.appear(self.I_REWARD):
  344. return True
  345. else:
  346. return False
  347. def is_in_prepare(self, is_screenshot: bool = True) -> bool:
  348. """
  349. 判断是否在准备中
  350. :return:
  351. """
  352. if is_screenshot:
  353. self.screenshot()
  354. if self.appear(self.I_BUFF):
  355. return True
  356. elif self.appear(self.I_PREPARE_HIGHLIGHT):
  357. return True
  358. elif self.appear(self.I_PREPARE_DARK):
  359. return True
  360. elif self.appear(self.I_PRESET):
  361. return True
  362. else:
  363. return False
  364. def check_take_over_battle(self, is_screenshot: bool, config: GeneralBattleConfig) -> bool or None:
  365. """
  366. 中途接入战斗,并且接管
  367. :return: 赢了返回True, 输了返回False, 不是在战斗中返回None
  368. """
  369. if is_screenshot:
  370. self.screenshot()
  371. if not self.is_in_battle():
  372. return None
  373. if self.is_in_prepare(False):
  374. while 1:
  375. self.screenshot()
  376. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  377. continue
  378. if not self.appear(self.I_BUFF):
  379. break
  380. # 被接管的战斗,只有准备阶段才可以点绿标。
  381. # 因为如果是战斗中,无法保证点击的时候是否出现动画
  382. self.wait_until_disappear(self.I_BUFF)
  383. self.green_mark(config.green_enable, config.green_mark)
  384. return self.battle_wait(config.random_click_swipt_enable)
  385. def check_lock(self, enable: bool, lock_image, unlock_image):
  386. """
  387. 检测是否锁定队伍,
  388. :param enable:
  389. :param lock_image:
  390. :param unlock_image:
  391. :return:
  392. """
  393. if enable:
  394. logger.info("Lock team")
  395. while 1:
  396. self.screenshot()
  397. if self.appear(lock_image):
  398. break
  399. if self.appear_then_click(unlock_image, interval=1):
  400. continue
  401. else:
  402. logger.info("Unlock team")
  403. while 1:
  404. self.screenshot()
  405. if self.appear(unlock_image):
  406. break
  407. if self.appear_then_click(lock_image, interval=1):
  408. continue
  409. def check_buff(self, buff: BuffClass or list[BuffClass] = None):
  410. """
  411. 检测是否开启buff
  412. :param buff:
  413. :return:
  414. """
  415. if not buff:
  416. return
  417. logger.info(f'Open buff {buff}')
  418. self.ui_click(self.I_BUFF, self.I_CLOUD, interval=2)
  419. if isinstance(buff, BuffClass):
  420. buff = [buff]
  421. match_method = {
  422. BuffClass.AWAKE: (self.awake, True),
  423. BuffClass.SOUL: (self.soul, True),
  424. BuffClass.GOLD_50: (self.gold_50, True),
  425. BuffClass.GOLD_100: (self.gold_100, True),
  426. BuffClass.EXP_50: (self.exp_50, True),
  427. BuffClass.EXP_100: (self.exp_100, True),
  428. BuffClass.AWAKE_CLOSE: (self.awake, False),
  429. BuffClass.SOUL_CLOSE: (self.soul, False),
  430. BuffClass.GOLD_50_CLOSE: (self.gold_50, False),
  431. BuffClass.GOLD_100_CLOSE: (self.gold_100, False),
  432. BuffClass.EXP_50_CLOSE: (self.exp_50, False),
  433. BuffClass.EXP_100_CLOSE: (self.exp_100, False),
  434. }
  435. for b in buff:
  436. func, is_open = match_method[b]
  437. func(is_open)
  438. time.sleep(0.1)
  439. logger.info(f'Open buff success')
  440. while 1:
  441. self.screenshot()
  442. if not self.appear(self.I_CLOUD):
  443. break
  444. if self.appear_then_click(self.I_BUFF, interval=1):
  445. continue
  446. if __name__ == '__main__':
  447. from module.config.config import Config
  448. from module.device.device import Device
  449. c = Config('oas1')
  450. d = Device(c)
  451. t = GeneralBattle(c, d)
  452. t.check_buff([BuffClass.EXP_50, BuffClass.GOLD_50])