general_battle.py 22 KB

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