general_battle.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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) -> bool:
  64. """
  65. 进入挑战然后直接返回
  66. :param config:
  67. :return:
  68. """
  69. # 如果没有锁定队伍那么在点击准备后才退出的
  70. if not config.lock_team_enable:
  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):
  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.ocr_appear(self.O_PRESET):
  267. self.click(self.O_PRESET, interval=1)
  268. continue
  269. logger.info("Click preset button")
  270. def get_unselect_color(tmp1, tmp2, tmp3, size):
  271. # 获取未选择分组的颜色,3组之中必定存在两个颜色相似
  272. # area 参数格式是(x1,y1,x2,y2)
  273. color_1 = get_color(self.device.image,
  274. (tmp1.roi_back[0], tmp1.roi_back[1],
  275. tmp1.roi_back[0] + size[0], tmp1.roi_back[1] + size[1]))
  276. color_2 = get_color(self.device.image,
  277. (tmp2.roi_back[0], tmp2.roi_back[1],
  278. tmp2.roi_back[0] + size[0], tmp2.roi_back[1] + size[1]))
  279. color_3 = get_color(self.device.image,
  280. (tmp3.roi_back[0], tmp3.roi_back[1],
  281. tmp3.roi_back[0] + size[0], tmp3.roi_back[1] + size[1]))
  282. if color_similar(color_1, color_2):
  283. return color_1
  284. if color_similar(color_2, color_3):
  285. return color_2
  286. return color_3
  287. # 选择预设组
  288. tmp = self.__getattribute__("C_PRESET_GROUP_" + str(preset_group))
  289. if tmp is None:
  290. tmp = self.C_PRESET_GROUP_1
  291. color_size = [self.C_PRESET_GROUP_1.roi_back[2],
  292. self.C_PRESET_GROUP_1.roi_back[3]]
  293. # unselected_color = get_unselect_color(self.C_PRESET_GROUP_1, self.C_PRESET_GROUP_2, self.C_PRESET_GROUP_3, size=color_size)
  294. # 考虑到有些预设组没有预设,所以这里取一个比较固定的颜色
  295. unselected_color = (224.9, 208.3, 187.4)
  296. while True:
  297. self.screenshot()
  298. color_tmp = get_color(self.device.image,
  299. (tmp.roi_back[0], tmp.roi_back[1], tmp.roi_back[0] + color_size[0],
  300. tmp.roi_back[1] + color_size[1]))
  301. if color_similar(color_tmp, unselected_color):
  302. self.click(tmp, interval=0.2)
  303. continue
  304. break
  305. logger.info("Select preset group")
  306. # 选择预设的队伍
  307. time.sleep(0.5)
  308. tmp = self.__getattribute__("C_PRESET_TEAM_" + str(preset_team))
  309. if tmp is None:
  310. tmp = self.C_PRESET_TEAM_1
  311. color_size = [5, 5]
  312. # unselected_color = get_unselect_color(self.C_PRESET_TEAM_1, self.C_PRESET_TEAM_2, self.C_PRESET_TEAM_3, size=color_size )
  313. unselected_color = (216.8, 185.0, 146.8)
  314. while True:
  315. self.screenshot()
  316. color_tmp = get_color(self.device.image,
  317. (tmp.roi_back[0], tmp.roi_back[1], tmp.roi_back[0] + color_size[0],
  318. tmp.roi_back[1] + color_size[1]))
  319. if color_similar(color_tmp, unselected_color):
  320. self.click(tmp, interval=0.2)
  321. continue
  322. break
  323. self.click(tmp)
  324. logger.info("Select preset team")
  325. # 点击预设确认
  326. self.wait_until_appear(self.I_PRESET_ENSURE, wait_time=1)
  327. while 1:
  328. self.screenshot()
  329. if not self.appear(self.I_PRESET_ENSURE):
  330. break
  331. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8, interval=0.2):
  332. continue
  333. logger.info("Click preset ensure")
  334. def random_click_swipt(self):
  335. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  336. rand_type = random.randint(0, 2)
  337. match rand_type:
  338. case 0:
  339. self.click(self.C_RANDOM_CLICK, interval=20)
  340. case 1:
  341. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  342. case 2:
  343. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  344. # 重新设置为长战斗
  345. self.device.stuck_record_add('BATTLE_STATUS_S')
  346. else:
  347. time.sleep(0.4) # 这样的好像不对
  348. # 判断是否在战斗中
  349. def is_in_battle(self, is_screenshot: bool = True) -> bool:
  350. """
  351. 判断是否在战斗中
  352. :return:
  353. """
  354. if is_screenshot:
  355. self.screenshot()
  356. if self.appear(self.I_FRIENDS) or \
  357. self.appear(self.I_WIN) or \
  358. self.appear(self.I_FALSE) or \
  359. self.appear(self.I_REWARD):
  360. return True
  361. else:
  362. return False
  363. def is_in_prepare(self, is_screenshot: bool = True) -> bool:
  364. """
  365. 判断是否在准备中
  366. :return:
  367. """
  368. if is_screenshot:
  369. self.screenshot()
  370. if self.appear(self.I_BUFF):
  371. return True
  372. elif self.appear(self.I_PREPARE_HIGHLIGHT):
  373. return True
  374. elif self.appear(self.I_PREPARE_DARK):
  375. return True
  376. elif self.appear(self.I_PRESET):
  377. return True
  378. else:
  379. return False
  380. def check_take_over_battle(self, is_screenshot: bool, config: GeneralBattleConfig) -> bool or None:
  381. """
  382. 中途接入战斗,并且接管
  383. :return: 赢了返回True, 输了返回False, 不是在战斗中返回None
  384. """
  385. if is_screenshot:
  386. self.screenshot()
  387. if not self.is_in_battle():
  388. return None
  389. if self.is_in_prepare(False):
  390. while 1:
  391. self.screenshot()
  392. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  393. continue
  394. if not self.appear(self.I_BUFF):
  395. break
  396. # 被接管的战斗,只有准备阶段才可以点绿标。
  397. # 因为如果是战斗中,无法保证点击的时候是否出现动画
  398. self.wait_until_disappear(self.I_BUFF)
  399. self.green_mark(config.green_enable, config.green_mark)
  400. return self.battle_wait(config.random_click_swipt_enable)
  401. def check_lock(self, enable: bool, lock_image, unlock_image):
  402. """
  403. 检测是否锁定队伍,
  404. :param enable:
  405. :param lock_image:
  406. :param unlock_image:
  407. :return:
  408. """
  409. if enable:
  410. logger.info("Lock team")
  411. while 1:
  412. self.screenshot()
  413. if self.appear(lock_image):
  414. break
  415. if self.appear_then_click(unlock_image, interval=1):
  416. continue
  417. else:
  418. logger.info("Unlock team")
  419. while 1:
  420. self.screenshot()
  421. if self.appear(unlock_image):
  422. break
  423. if self.appear_then_click(lock_image, interval=1):
  424. continue
  425. def check_buff(self, buff: BuffClass or list[BuffClass] = None):
  426. """
  427. 检测是否开启buff
  428. :param buff:
  429. :return:
  430. """
  431. if not buff:
  432. return
  433. logger.info(f'Open buff {buff}')
  434. self.ui_click(self.I_BUFF, self.I_CLOUD, interval=2)
  435. if isinstance(buff, BuffClass):
  436. buff = [buff]
  437. match_method = {
  438. BuffClass.AWAKE: (self.awake, True),
  439. BuffClass.SOUL: (self.soul, True),
  440. BuffClass.GOLD_50: (self.gold_50, True),
  441. BuffClass.GOLD_100: (self.gold_100, True),
  442. BuffClass.EXP_50: (self.exp_50, True),
  443. BuffClass.EXP_100: (self.exp_100, True),
  444. BuffClass.AWAKE_CLOSE: (self.awake, False),
  445. BuffClass.SOUL_CLOSE: (self.soul, False),
  446. BuffClass.GOLD_50_CLOSE: (self.gold_50, False),
  447. BuffClass.GOLD_100_CLOSE: (self.gold_100, False),
  448. BuffClass.EXP_50_CLOSE: (self.exp_50, False),
  449. BuffClass.EXP_100_CLOSE: (self.exp_100, False),
  450. }
  451. for b in buff:
  452. func, is_open = match_method[b]
  453. func(is_open)
  454. time.sleep(0.1)
  455. logger.info(f'Open buff success')
  456. while 1:
  457. self.screenshot()
  458. if not self.appear(self.I_CLOUD):
  459. break
  460. if self.appear_then_click(self.I_BUFF, interval=1):
  461. continue
  462. if __name__ == '__main__':
  463. from module.config.config import Config
  464. from module.device.device import Device
  465. c = Config('oas1')
  466. d = Device(c)
  467. t = GeneralBattle(c, d)
  468. self = t
  469. # t.check_buff([BuffClass.EXP_50, BuffClass.GOLD_50])
  470. img = cv2.imread(r"E:\preset3.png")
  471. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  472. self.device.image = img
  473. def get_unselect_color(tmp1, tmp2, tmp3, size):
  474. # 获取未选择分组的颜色,3组之中必定存在两个颜色相似
  475. # area 参数格式是(x1,y1,x2,y2)
  476. color_1 = get_color(self.device.image,
  477. (tmp1.roi_back[0], tmp1.roi_back[1],
  478. tmp1.roi_back[0] + size[0], tmp1.roi_back[1] + size[1]))
  479. color_2 = get_color(self.device.image,
  480. (tmp2.roi_back[0], tmp2.roi_back[1],
  481. tmp2.roi_back[0] + size[0], tmp2.roi_back[1] + size[1]))
  482. color_3 = get_color(self.device.image,
  483. (tmp3.roi_back[0], tmp3.roi_back[1],
  484. tmp3.roi_back[0] + size[0], tmp3.roi_back[1] + size[1]))
  485. if color_similar(color_1, color_2):
  486. return color_1
  487. if color_similar(color_2, color_3):
  488. return color_2
  489. return color_3
  490. color_size = [self.C_PRESET_GROUP_1.roi_back[2],
  491. self.C_PRESET_GROUP_1.roi_back[3]]
  492. unselected_color = get_unselect_color(self.C_PRESET_GROUP_1, self.C_PRESET_GROUP_2, self.C_PRESET_GROUP_3,
  493. size=color_size)
  494. print("")
  495. color_size = [5, 5]
  496. unselected_color = get_unselect_color(self.C_PRESET_TEAM_1, self.C_PRESET_TEAM_2, self.C_PRESET_TEAM_3,
  497. size=color_size
  498. )
  499. print("")