general_battle.py 17 KB

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