general_battle.py 21 KB

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