general_battle.py 24 KB

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