general_battle.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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)# or
  201. # self.appear_then_click(self.I_REWARD_STATISTICS, action=action_click, interval=1.5) or
  202. # self.appear_then_click(self.I_REWARD_PURPLE_SNAKE_SKIN, action=action_click, interval=1.5) or
  203. # self.appear_then_click(self.I_REWARD_GOLD_SNAKE_SKIN, action=action_click, interval=1.5) or
  204. # self.appear_then_click(self.I_REWARD_EXP_SOUL_4, action=action_click, interval=1.5) or
  205. # self.appear_then_click(self.I_REWARD_SOUL_5, action=action_click, interval=1.5) or
  206. # self.appear_then_click(self.I_REWARD_SOUL_6, action=action_click, interval=1.5)
  207. ):
  208. continue
  209. if (not self.appear(self.I_REWARD) and
  210. not self.appear(self.I_REWARD_GOLD)# and
  211. # not self.appear(self.I_REWARD_STATISTICS) and
  212. # not self.appear(self.I_REWARD_PURPLE_SNAKE_SKIN) and
  213. # not self.appear(self.I_REWARD_GOLD_SNAKE_SKIN) and
  214. # not self.appear(self.I_REWARD_EXP_SOUL_4) and
  215. # not self.appear(self.I_REWARD_SOUL_5) and
  216. # not self.appear(self.I_REWARD_SOUL_6)
  217. ):
  218. break
  219. return win
  220. def green_mark(self, enable: bool = False, mark_mode: GreenMarkType = GreenMarkType.GREEN_MAIN):
  221. """
  222. 绿标, 如果不使能就直接返回
  223. :param enable:
  224. :param mark_mode:
  225. :return:
  226. """
  227. if enable:
  228. logger.info("Green is enable")
  229. x, y = None, None
  230. match mark_mode:
  231. case GreenMarkType.GREEN_LEFT1:
  232. x, y = self.C_GREEN_LEFT_1.coord()
  233. logger.info("Green left 1")
  234. case GreenMarkType.GREEN_LEFT2:
  235. x, y = self.C_GREEN_LEFT_2.coord()
  236. logger.info("Green left 2")
  237. case GreenMarkType.GREEN_LEFT3:
  238. x, y = self.C_GREEN_LEFT_3.coord()
  239. logger.info("Green left 3")
  240. case GreenMarkType.GREEN_LEFT4:
  241. x, y = self.C_GREEN_LEFT_4.coord()
  242. logger.info("Green left 4")
  243. case GreenMarkType.GREEN_LEFT5:
  244. x, y = self.C_GREEN_LEFT_5.coord()
  245. logger.info("Green left 5")
  246. case GreenMarkType.GREEN_MAIN:
  247. x, y = self.C_GREEN_MAIN.coord()
  248. logger.info("Green main")
  249. # 等待那个准备的消失
  250. while 1:
  251. self.screenshot()
  252. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  253. break
  254. # 判断有无坐标的偏移
  255. self.appear_then_click(self.I_LOCAL)
  256. time.sleep(0.3)
  257. # 点击绿标
  258. self.device.click(x, y)
  259. def switch_preset_team(self, enable: bool = False, preset_group: int = 1, preset_team: int = 1):
  260. """
  261. 切换预设的队伍, 要求是在不锁定队伍时的情况下
  262. :param enable:
  263. :param preset_group:
  264. :param preset_team:
  265. :return:
  266. """
  267. if not enable:
  268. logger.info("Preset is disable")
  269. return None
  270. logger.info("Preset is enable")
  271. # 点击预设按钮
  272. while 1:
  273. self.screenshot()
  274. if self.appear(self.I_PRESET_ENSURE):
  275. break
  276. # 首个队伍没有满足5个式神,未出现预设按钮的情况下跳出循环
  277. if self.appear(self.I_PRESENT_LESS_THAN_5):
  278. break
  279. if self.appear_then_click(self.I_PRESET, threshold=0.8, interval=1):
  280. continue
  281. if self.appear_then_click(self.I_PRESET_WIT_NUMBER, threshold=0.8, interval=1):
  282. continue
  283. if self.ocr_appear(self.O_PRESET):
  284. self.click(self.O_PRESET, interval=1)
  285. continue
  286. logger.info("Click preset button")
  287. def get_unselect_color(tmp1, tmp2, tmp3, size):
  288. # 获取未选择分组的颜色,3组之中必定存在两个颜色相似
  289. # area 参数格式是(x1,y1,x2,y2)
  290. color_1 = get_color(self.device.image,
  291. (tmp1.roi_back[0], tmp1.roi_back[1],
  292. tmp1.roi_back[0] + size[0], tmp1.roi_back[1] + size[1]))
  293. color_2 = get_color(self.device.image,
  294. (tmp2.roi_back[0], tmp2.roi_back[1],
  295. tmp2.roi_back[0] + size[0], tmp2.roi_back[1] + size[1]))
  296. color_3 = get_color(self.device.image,
  297. (tmp3.roi_back[0], tmp3.roi_back[1],
  298. tmp3.roi_back[0] + size[0], tmp3.roi_back[1] + size[1]))
  299. if color_similar(color_1, color_2):
  300. return color_1
  301. if color_similar(color_2, color_3):
  302. return color_2
  303. return color_3
  304. # 选择预设组
  305. tmp = self.__getattribute__("C_PRESET_GROUP_" + str(preset_group))
  306. if tmp is None:
  307. tmp = self.C_PRESET_GROUP_1
  308. color_size = [self.C_PRESET_GROUP_1.roi_back[2],
  309. self.C_PRESET_GROUP_1.roi_back[3]]
  310. # unselected_color = get_unselect_color(self.C_PRESET_GROUP_1, self.C_PRESET_GROUP_2, self.C_PRESET_GROUP_3, size=color_size)
  311. # 考虑到有些预设组没有预设,所以这里取一个比较固定的颜色
  312. unselected_color = (224.9, 208.3, 187.4)
  313. while True:
  314. self.screenshot()
  315. color_tmp = get_color(self.device.image,
  316. (tmp.roi_back[0], tmp.roi_back[1], tmp.roi_back[0] + color_size[0],
  317. tmp.roi_back[1] + color_size[1]))
  318. if color_similar(color_tmp, unselected_color):
  319. self.click(tmp, interval=0.2)
  320. continue
  321. break
  322. logger.info("Select preset group")
  323. # 选择预设的队伍
  324. time.sleep(0.5)
  325. tmp = self.__getattribute__("C_PRESET_TEAM_" + str(preset_team))
  326. if tmp is None:
  327. tmp = self.C_PRESET_TEAM_1
  328. color_size = [5, 5]
  329. # unselected_color = get_unselect_color(self.C_PRESET_TEAM_1, self.C_PRESET_TEAM_2, self.C_PRESET_TEAM_3, size=color_size )
  330. unselected_color = (216.8, 185.0, 146.8)
  331. while True:
  332. self.screenshot()
  333. color_tmp = get_color(self.device.image,
  334. (tmp.roi_back[0], tmp.roi_back[1], tmp.roi_back[0] + color_size[0],
  335. tmp.roi_back[1] + color_size[1]))
  336. if color_similar(color_tmp, unselected_color):
  337. self.click(tmp, interval=0.2)
  338. continue
  339. break
  340. self.click(tmp)
  341. logger.info("Select preset team")
  342. # 点击预设确认
  343. self.wait_until_appear(self.I_PRESET_ENSURE, wait_time=1)
  344. while 1:
  345. self.screenshot()
  346. if not self.appear(self.I_PRESET_ENSURE):
  347. break
  348. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8, interval=0.2):
  349. continue
  350. logger.info("Click preset ensure")
  351. def random_click_swipt(self):
  352. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  353. rand_type = random.randint(0, 2)
  354. match rand_type:
  355. case 0:
  356. self.click(self.C_RANDOM_CLICK, interval=20)
  357. case 1:
  358. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  359. case 2:
  360. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  361. # 重新设置为长战斗
  362. self.device.stuck_record_add('BATTLE_STATUS_S')
  363. else:
  364. time.sleep(0.4) # 这样的好像不对
  365. # 判断是否在战斗中
  366. def is_in_battle(self, is_screenshot: bool = True) -> bool:
  367. """
  368. 判断是否在战斗中
  369. :return:
  370. """
  371. if is_screenshot:
  372. self.screenshot()
  373. if self.appear(self.I_FRIENDS) or \
  374. self.appear(self.I_WIN) or \
  375. self.appear(self.I_FALSE) or \
  376. self.appear(self.I_REWARD):
  377. return True
  378. else:
  379. return False
  380. def is_in_prepare(self, is_screenshot: bool = True) -> bool:
  381. """
  382. 判断是否在准备中
  383. :return:
  384. """
  385. if is_screenshot:
  386. self.screenshot()
  387. if self.appear(self.I_BUFF):
  388. return True
  389. elif self.appear(self.I_PREPARE_HIGHLIGHT):
  390. return True
  391. elif self.appear(self.I_PREPARE_DARK):
  392. return True
  393. elif self.appear(self.I_PRESET) or self.appear(self.I_PRESET_WIT_NUMBER):
  394. return True
  395. else:
  396. return False
  397. def check_take_over_battle(self, is_screenshot: bool, config: GeneralBattleConfig) -> bool or None:
  398. """
  399. 中途接入战斗,并且接管
  400. :return: 赢了返回True, 输了返回False, 不是在战斗中返回None
  401. """
  402. if is_screenshot:
  403. self.screenshot()
  404. if not self.is_in_battle():
  405. return None
  406. if self.is_in_prepare(False):
  407. while 1:
  408. self.screenshot()
  409. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  410. continue
  411. if not self.appear(self.I_BUFF):
  412. break
  413. # 被接管的战斗,只有准备阶段才可以点绿标。
  414. # 因为如果是战斗中,无法保证点击的时候是否出现动画
  415. self.wait_until_disappear(self.I_BUFF)
  416. self.green_mark(config.green_enable, config.green_mark)
  417. return self.battle_wait(config.random_click_swipt_enable)
  418. def check_lock(self, enable: bool, lock_image, unlock_image):
  419. """
  420. 检测是否锁定队伍,
  421. :param enable:
  422. :param lock_image:
  423. :param unlock_image:
  424. :return:
  425. """
  426. if enable:
  427. logger.info("Lock team")
  428. while 1:
  429. self.screenshot()
  430. if self.appear(lock_image):
  431. break
  432. if self.appear_then_click(unlock_image, interval=1):
  433. continue
  434. else:
  435. logger.info("Unlock team")
  436. while 1:
  437. self.screenshot()
  438. if self.appear(unlock_image):
  439. break
  440. if self.appear_then_click(lock_image, interval=1):
  441. continue
  442. def check_buff(self, buff: BuffClass or list[BuffClass] = None):
  443. """
  444. 检测是否开启buff
  445. :param buff:
  446. :return:
  447. """
  448. if not buff:
  449. return
  450. logger.info(f'Open buff {buff}')
  451. self.ui_click(self.I_BUFF, self.I_CLOUD, interval=2)
  452. if isinstance(buff, BuffClass):
  453. buff = [buff]
  454. match_method = {
  455. BuffClass.AWAKE: (self.awake, True),
  456. BuffClass.SOUL: (self.soul, True),
  457. BuffClass.GOLD_50: (self.gold_50, True),
  458. BuffClass.GOLD_100: (self.gold_100, True),
  459. BuffClass.EXP_50: (self.exp_50, True),
  460. BuffClass.EXP_100: (self.exp_100, True),
  461. BuffClass.AWAKE_CLOSE: (self.awake, False),
  462. BuffClass.SOUL_CLOSE: (self.soul, False),
  463. BuffClass.GOLD_50_CLOSE: (self.gold_50, False),
  464. BuffClass.GOLD_100_CLOSE: (self.gold_100, False),
  465. BuffClass.EXP_50_CLOSE: (self.exp_50, False),
  466. BuffClass.EXP_100_CLOSE: (self.exp_100, False),
  467. }
  468. for b in buff:
  469. func, is_open = match_method[b]
  470. func(is_open)
  471. time.sleep(0.1)
  472. logger.info(f'Open buff success')
  473. while 1:
  474. self.screenshot()
  475. if not self.appear(self.I_CLOUD):
  476. break
  477. if self.appear_then_click(self.I_BUFF, interval=1):
  478. continue
  479. if __name__ == '__main__':
  480. from module.config.config import Config
  481. from module.device.device import Device
  482. c = Config('oas1')
  483. d = Device(c)
  484. t = GeneralBattle(c, d)
  485. self = t
  486. # t.check_buff([BuffClass.EXP_50, BuffClass.GOLD_50])
  487. img = cv2.imread(r"E:\preset3.png")
  488. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  489. self.device.image = img
  490. def get_unselect_color(tmp1, tmp2, tmp3, size):
  491. # 获取未选择分组的颜色,3组之中必定存在两个颜色相似
  492. # area 参数格式是(x1,y1,x2,y2)
  493. color_1 = get_color(self.device.image,
  494. (tmp1.roi_back[0], tmp1.roi_back[1],
  495. tmp1.roi_back[0] + size[0], tmp1.roi_back[1] + size[1]))
  496. color_2 = get_color(self.device.image,
  497. (tmp2.roi_back[0], tmp2.roi_back[1],
  498. tmp2.roi_back[0] + size[0], tmp2.roi_back[1] + size[1]))
  499. color_3 = get_color(self.device.image,
  500. (tmp3.roi_back[0], tmp3.roi_back[1],
  501. tmp3.roi_back[0] + size[0], tmp3.roi_back[1] + size[1]))
  502. if color_similar(color_1, color_2):
  503. return color_1
  504. if color_similar(color_2, color_3):
  505. return color_2
  506. return color_3
  507. color_size = [self.C_PRESET_GROUP_1.roi_back[2],
  508. self.C_PRESET_GROUP_1.roi_back[3]]
  509. unselected_color = get_unselect_color(self.C_PRESET_GROUP_1, self.C_PRESET_GROUP_2, self.C_PRESET_GROUP_3,
  510. size=color_size)
  511. print("")
  512. color_size = [5, 5]
  513. unselected_color = get_unselect_color(self.C_PRESET_TEAM_1, self.C_PRESET_TEAM_2, self.C_PRESET_TEAM_3,
  514. size=color_size
  515. )
  516. print("")