general_battle.py 22 KB

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