general_battle.py 22 KB

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