general_battle.py 27 KB

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