general_battle.py 29 KB

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