general_battle.py 29 KB

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