general_battle.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 battle_wait_v2(self, random_click_swipt_enable: bool) -> bool:
  233. """
  234. 第二版战斗等待,参考 Orochi 和 Secret 的优化版本。
  235. :return: 胜利返回 True,失败返回 False
  236. """
  237. # 统一点击名称,防止 GameTooManyClickError 误报
  238. self.C_REWARD_1.name, self.C_REWARD_2.name, self.C_REWARD_3.name = 'C_REWARD', 'C_REWARD', 'C_REWARD'
  239. self.device.stuck_record_add('BATTLE_STATUS_S')
  240. self.device.click_record_clear()
  241. logger.info("Start battle process")
  242. while 1:
  243. self.screenshot()
  244. # 出现赢的鼓,点击直到消失
  245. if self.appear_then_click(self.I_WIN, interval=0.8):
  246. continue
  247. # 逢魔胜利图
  248. if self.appear(self.I_DE_WIN):
  249. self.ui_click_until_disappear(self.I_DE_WIN)
  250. continue
  251. if self.appear(self.I_FALSE, threshold=0.8):
  252. logger.warning('False battle')
  253. self.ui_click_until_disappear(self.I_FALSE)
  254. return False
  255. appear_ghost, appear_reward, appear_gold = (
  256. self.appear(self.I_GREED_GHOST),
  257. self.appear(self.I_REWARD),
  258. self.appear(self.I_REWARD_GOLD)
  259. )
  260. if appear_ghost or appear_reward or appear_gold:
  261. logger.info('Win battle')
  262. timer = Timer(20).start()
  263. while 1:
  264. self.screenshot()
  265. _appear_ghost, _appear_reward, _appear_gold = (
  266. self.appear(self.I_GREED_GHOST, threshold=0.6),
  267. self.appear(self.I_REWARD),
  268. self.appear(self.I_REWARD_GOLD)
  269. )
  270. # logger.info(f'_appear_ghost: {_appear_ghost} _appear_reward: {_appear_reward} _appear_gold: {_appear_gold}')
  271. if any([_appear_ghost, _appear_reward, _appear_gold]):
  272. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  273. self.click(action_click, interval=1.5)
  274. else:
  275. logger.info('Battle done')
  276. return True
  277. if self._hook_special_reward():
  278. continue
  279. if timer.reached_and_reset():
  280. logger.warning('battle ')
  281. break
  282. # 随机滑动
  283. if random_click_swipt_enable:
  284. self.random_click_swipt()
  285. return False
  286. def _hook_special_reward(self) -> bool:
  287. """
  288. For overwrite https://github.com/runhey/OnmyojiAutoScript/issues/1580
  289. """
  290. return False
  291. def green_mark(self, enable: bool = False, mark_mode: GreenMarkType = GreenMarkType.GREEN_MAIN):
  292. """
  293. 绿标, 如果不使能就直接返回
  294. :param enable:
  295. :param mark_mode:
  296. :return:
  297. """
  298. if enable:
  299. logger.info("Green is enable")
  300. x, y = None, None
  301. match mark_mode:
  302. case GreenMarkType.GREEN_LEFT1:
  303. x, y = self.C_GREEN_LEFT_1.coord()
  304. logger.info("Green left 1")
  305. case GreenMarkType.GREEN_LEFT2:
  306. x, y = self.C_GREEN_LEFT_2.coord()
  307. logger.info("Green left 2")
  308. case GreenMarkType.GREEN_LEFT3:
  309. x, y = self.C_GREEN_LEFT_3.coord()
  310. logger.info("Green left 3")
  311. case GreenMarkType.GREEN_LEFT4:
  312. x, y = self.C_GREEN_LEFT_4.coord()
  313. logger.info("Green left 4")
  314. case GreenMarkType.GREEN_LEFT5:
  315. x, y = self.C_GREEN_LEFT_5.coord()
  316. logger.info("Green left 5")
  317. case GreenMarkType.GREEN_MAIN:
  318. x, y = self.C_GREEN_MAIN.coord()
  319. logger.info("Green main")
  320. # 等待那个准备的消失
  321. while 1:
  322. self.screenshot()
  323. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  324. break
  325. # 判断有无坐标的偏移
  326. self.appear_then_click(self.I_LOCAL)
  327. time.sleep(0.3)
  328. # 点击绿标
  329. self.device.click(x, y)
  330. def switch_preset_team(self, enable: bool = False, preset_group: int = 1, preset_team: int = 1):
  331. """
  332. 切换预设的队伍, 要求是在不锁定队伍时的情况下
  333. :param enable:
  334. :param preset_group:
  335. :param preset_team:
  336. :return:
  337. """
  338. if not enable:
  339. logger.info("Preset is disable")
  340. return None
  341. logger.info("Preset is enable")
  342. # 点击预设按钮
  343. while 1:
  344. self.screenshot()
  345. if self.appear(self.I_PRESET_ENSURE):
  346. break
  347. # 首个队伍没有满足5个式神,未出现预设按钮的情况下跳出循环
  348. if self.appear(self.I_PRESENT_LESS_THAN_5):
  349. break
  350. if self.appear_then_click(self.I_PRESET, threshold=0.8, interval=1):
  351. continue
  352. if self.appear_then_click(self.I_PRESET_WIT_NUMBER, threshold=0.8, interval=1):
  353. continue
  354. if self.ocr_appear(self.O_PRESET):
  355. self.click(self.O_PRESET, interval=1)
  356. continue
  357. if self.ocr_appear(self.O_PRESET_FULL):
  358. self.click(self.O_PRESET_FULL, interval=1)
  359. continue
  360. logger.info("Click preset button")
  361. def get_unselect_color(tmp1, tmp2, tmp3, size):
  362. # 获取未选择分组的颜色,3组之中必定存在两个颜色相似
  363. # area 参数格式是(x1,y1,x2,y2)
  364. color_1 = get_color(self.device.image,
  365. (tmp1.roi_back[0], tmp1.roi_back[1],
  366. tmp1.roi_back[0] + size[0], tmp1.roi_back[1] + size[1]))
  367. color_2 = get_color(self.device.image,
  368. (tmp2.roi_back[0], tmp2.roi_back[1],
  369. tmp2.roi_back[0] + size[0], tmp2.roi_back[1] + size[1]))
  370. color_3 = get_color(self.device.image,
  371. (tmp3.roi_back[0], tmp3.roi_back[1],
  372. tmp3.roi_back[0] + size[0], tmp3.roi_back[1] + size[1]))
  373. if color_similar(color_1, color_2):
  374. return color_1
  375. if color_similar(color_2, color_3):
  376. return color_2
  377. return color_3
  378. # 选择预设组
  379. tmp = self.__getattribute__("C_PRESET_GROUP_" + str(preset_group))
  380. if tmp is None:
  381. tmp = self.C_PRESET_GROUP_1
  382. color_size = [self.C_PRESET_GROUP_1.roi_back[2],
  383. self.C_PRESET_GROUP_1.roi_back[3]]
  384. # unselected_color = get_unselect_color(self.C_PRESET_GROUP_1, self.C_PRESET_GROUP_2, self.C_PRESET_GROUP_3, size=color_size)
  385. # 考虑到有些预设组没有预设,所以这里取一个比较固定的颜色
  386. unselected_color = (224.9, 208.3, 187.4)
  387. while True:
  388. self.screenshot()
  389. color_tmp = get_color(self.device.image,
  390. (tmp.roi_back[0], tmp.roi_back[1], tmp.roi_back[0] + color_size[0],
  391. tmp.roi_back[1] + color_size[1]))
  392. if color_similar(color_tmp, unselected_color):
  393. self.click(tmp, interval=0.2)
  394. continue
  395. break
  396. logger.info("Select preset group")
  397. # 选择预设的队伍
  398. time.sleep(0.5)
  399. tmp = self.__getattribute__("C_PRESET_TEAM_" + str(preset_team))
  400. if tmp is None:
  401. tmp = self.C_PRESET_TEAM_1
  402. color_size = [5, 5]
  403. # unselected_color = get_unselect_color(self.C_PRESET_TEAM_1, self.C_PRESET_TEAM_2, self.C_PRESET_TEAM_3, size=color_size )
  404. unselected_color = (216.8, 185.0, 146.8)
  405. while True:
  406. self.screenshot()
  407. color_tmp = get_color(self.device.image,
  408. (tmp.roi_back[0], tmp.roi_back[1], tmp.roi_back[0] + color_size[0],
  409. tmp.roi_back[1] + color_size[1]))
  410. if color_similar(color_tmp, unselected_color):
  411. self.click(tmp, interval=0.2)
  412. continue
  413. break
  414. self.click(tmp)
  415. logger.info("Select preset team")
  416. # 点击预设确认
  417. self.wait_until_appear(self.I_PRESET_ENSURE, wait_time=1)
  418. click_timer = Timer(10).start()
  419. while 1:
  420. self.screenshot()
  421. if click_timer.reached():
  422. logger.warning("Switch preset failure")
  423. if not self.appear(self.I_PRESET_ENSURE):
  424. break
  425. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8, interval=1):
  426. continue
  427. logger.info("Click preset ensure")
  428. return None
  429. def random_click_swipt(self):
  430. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  431. rand_type = random.randint(0, 2)
  432. match rand_type:
  433. case 0:
  434. self.click(self.C_RANDOM_CLICK, interval=20)
  435. case 1:
  436. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  437. case 2:
  438. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  439. # 重新设置为长战斗
  440. # self.device.stuck_record_add('BATTLE_STATUS_S')
  441. else:
  442. time.sleep(0.4) # 这样的好像不对
  443. # 判断是否在战斗中
  444. def is_in_battle(self, is_screenshot: bool = True) -> bool:
  445. """
  446. 判断是否在战斗中
  447. tip: 因为有friends判别, 所以即使在准备界面也会识别在战斗中
  448. :return:
  449. """
  450. if is_screenshot:
  451. self.screenshot()
  452. if self.appear(self.I_BATTLE_INFO) or \
  453. self.appear(self.I_FRIENDS) or \
  454. self.appear(self.I_WIN) or \
  455. self.appear(self.I_FALSE) or \
  456. self.appear(self.I_REWARD):
  457. return True
  458. else:
  459. return False
  460. def is_in_real_battle(self, is_screenshot: bool = True):
  461. """
  462. 判断是否在真正的战斗中(不是战斗准备界面也不是战斗结束界面)
  463. :param is_screenshot:
  464. :return:
  465. """
  466. if is_screenshot:
  467. self.screenshot()
  468. return self.appear(self.I_BATTLE_INFO)
  469. def is_in_prepare(self, is_screenshot: bool = True) -> bool:
  470. """
  471. 判断是否在准备中
  472. :return:
  473. """
  474. if is_screenshot:
  475. self.screenshot()
  476. if self.appear(self.I_BUFF):
  477. return True
  478. elif self.appear(self.I_PREPARE_HIGHLIGHT):
  479. return True
  480. elif self.appear(self.I_PREPARE_DARK):
  481. return True
  482. elif self.appear(self.I_PRESET) or self.appear(self.I_PRESET_WIT_NUMBER):
  483. return True
  484. else:
  485. return False
  486. def check_take_over_battle(self, is_screenshot: bool, config: GeneralBattleConfig) -> bool or None:
  487. """
  488. 中途接入战斗,并且接管
  489. :return: 赢了返回True, 输了返回False, 不是在战斗中返回None
  490. """
  491. if is_screenshot:
  492. self.screenshot()
  493. if not self.is_in_battle():
  494. return None
  495. return self.run_general_battle(config=config)
  496. def check_lock(self, enable: bool, lock_image, unlock_image):
  497. """
  498. 检测是否锁定队伍,
  499. :param enable:
  500. :param lock_image:
  501. :param unlock_image:
  502. :return:
  503. """
  504. if enable:
  505. logger.info("Lock team")
  506. while 1:
  507. self.screenshot()
  508. if self.appear(lock_image):
  509. break
  510. if self.appear_then_click(unlock_image, interval=1):
  511. continue
  512. else:
  513. logger.info("Unlock team")
  514. while 1:
  515. self.screenshot()
  516. if self.appear(unlock_image):
  517. break
  518. if self.appear_then_click(lock_image, interval=1):
  519. continue
  520. def check_and_open_buff(self, buff: BuffClass or list[BuffClass] = None):
  521. """
  522. 检测是否开启buff
  523. :param buff:
  524. :return:
  525. """
  526. if not buff:
  527. return
  528. logger.info(f'Open buff {buff}')
  529. self.ui_click(self.I_BUFF, self.I_CLOUD, interval=2)
  530. if isinstance(buff, BuffClass):
  531. buff = [buff]
  532. match_method = {
  533. BuffClass.AWAKE: (self.awake, True),
  534. BuffClass.SOUL: (self.soul, True),
  535. BuffClass.GOLD_50: (self.gold_50, True),
  536. BuffClass.GOLD_100: (self.gold_100, True),
  537. BuffClass.EXP_50: (self.exp_50, True),
  538. BuffClass.EXP_100: (self.exp_100, True),
  539. BuffClass.AWAKE_CLOSE: (self.awake, False),
  540. BuffClass.SOUL_CLOSE: (self.soul, False),
  541. BuffClass.GOLD_50_CLOSE: (self.gold_50, False),
  542. BuffClass.GOLD_100_CLOSE: (self.gold_100, False),
  543. BuffClass.EXP_50_CLOSE: (self.exp_50, False),
  544. BuffClass.EXP_100_CLOSE: (self.exp_100, False),
  545. }
  546. for b in buff:
  547. func, is_open = match_method[b]
  548. func(is_open)
  549. time.sleep(0.1)
  550. logger.info(f'Open buff success')
  551. while 1:
  552. self.screenshot()
  553. if not self.appear(self.I_CLOUD):
  554. break
  555. if self.appear_then_click(self.I_BUFF, interval=1):
  556. continue
  557. def boss_mark(self, enable=True) -> bool:
  558. if not enable or self._boss_mark_flag:
  559. return False
  560. if self.ocr_appear(self.O_BOSS_MARK):
  561. self.screenshot()
  562. if self.ocr_appear(self.O_BOSS_MARK):
  563. self._boss_mark_flag = True
  564. logger.info('Boss marked')
  565. self.device.stuck_record_add('BATTLE_STATUS_S')
  566. return True
  567. if self.device.click_record.count(str(self.O_BOSS_MARK)) >= 3:
  568. self._boss_mark_flag = True
  569. logger.info('Boss mark skipped due to maybe no boss')
  570. self.device.stuck_record_add('BATTLE_STATUS_S')
  571. return False
  572. if self.click(self.O_BOSS_MARK, interval=1.8):
  573. return False
  574. return False
  575. def boss_mark_reset(self):
  576. self._boss_mark_flag = False
  577. if __name__ == '__main__':
  578. from module.config.config import Config
  579. from module.device.device import Device
  580. c = Config('oas1')
  581. d = Device(c)
  582. t = GeneralBattle(c, d)
  583. self = t
  584. # t.check_buff([BuffClass.EXP_50, BuffClass.GOLD_50])
  585. img = cv2.imread(r"E:\preset3.png")
  586. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  587. self.device.image = img
  588. def get_unselect_color(tmp1, tmp2, tmp3, size):
  589. # 获取未选择分组的颜色,3组之中必定存在两个颜色相似
  590. # area 参数格式是(x1,y1,x2,y2)
  591. color_1 = get_color(self.device.image,
  592. (tmp1.roi_back[0], tmp1.roi_back[1],
  593. tmp1.roi_back[0] + size[0], tmp1.roi_back[1] + size[1]))
  594. color_2 = get_color(self.device.image,
  595. (tmp2.roi_back[0], tmp2.roi_back[1],
  596. tmp2.roi_back[0] + size[0], tmp2.roi_back[1] + size[1]))
  597. color_3 = get_color(self.device.image,
  598. (tmp3.roi_back[0], tmp3.roi_back[1],
  599. tmp3.roi_back[0] + size[0], tmp3.roi_back[1] + size[1]))
  600. if color_similar(color_1, color_2):
  601. return color_1
  602. if color_similar(color_2, color_3):
  603. return color_2
  604. return color_3
  605. color_size = [self.C_PRESET_GROUP_1.roi_back[2],
  606. self.C_PRESET_GROUP_1.roi_back[3]]
  607. unselected_color = get_unselect_color(self.C_PRESET_GROUP_1, self.C_PRESET_GROUP_2, self.C_PRESET_GROUP_3,
  608. size=color_size)
  609. print("")
  610. color_size = [5, 5]
  611. unselected_color = get_unselect_color(self.C_PRESET_TEAM_1, self.C_PRESET_TEAM_2, self.C_PRESET_TEAM_3,
  612. size=color_size
  613. )
  614. print("")