general_battle.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 tasks.base_task import BaseTask
  7. from tasks.Component.GeneralBattle.config_general_battle import GreenMarkType, GeneralBattleConfig
  8. from tasks.Component.GeneralBattle.assets import GeneralBattleAssets
  9. from tasks.Component.GeneralBuff.config_buff import BuffClass
  10. from tasks.Component.GeneralBuff.general_buff import GeneralBuff
  11. from module.logger import logger
  12. class GeneralBattle(GeneralBuff, GeneralBattleAssets):
  13. """
  14. 使用这个通用的战斗必须要求这个任务的config有config_general_battle
  15. """
  16. def run_general_battle(self, config: dict=None, buff: BuffClass or list[BuffClass]=None) -> bool:
  17. """
  18. 运行脚本
  19. :return:
  20. """
  21. # 本人选择的策略是只要进来了就算一次,不管是不是打完了
  22. logger.hr("General battle start", 2)
  23. self.current_count += 1
  24. logger.info(f"Current count: {self.current_count}")
  25. if config is None:
  26. config = GeneralBattleConfig()
  27. # 如果没有锁定队伍。那么可以根据配置设定队伍
  28. if not config.lock_team_enable:
  29. logger.info("Lock team is not enable")
  30. # 如果更换队伍
  31. if self.current_count == 1:
  32. self.switch_preset_team(config.preset_enable, config.preset_group, config.preset_team)
  33. # 打开buff
  34. self.check_buff(buff)
  35. # 点击准备按钮
  36. self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
  37. self.wait_until_appear(self.I_BUFF)
  38. while 1:
  39. self.screenshot()
  40. if not self.appear(self.I_BUFF):
  41. break
  42. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  43. continue
  44. logger.info("Click prepare ensure button")
  45. # 照顾一下某些模拟器慢的
  46. time.sleep(0.1)
  47. # 绿标
  48. self.wait_until_disappear(self.I_BUFF)
  49. if self.is_in_battle(False):
  50. self.green_mark(config.green_enable, config.green_mark)
  51. win = self.battle_wait(config.random_click_swipt_enable)
  52. if win:
  53. return True
  54. else:
  55. return False
  56. def run_general_battle_back(self, config: GeneralBattleConfig=None) -> bool:
  57. """
  58. 进入挑战然后直接返回
  59. :param config:
  60. :return:
  61. """
  62. # 如果没有锁定队伍那么在点击准备后才退出的
  63. if not config.lock_team_enable:
  64. # 点击准备按钮
  65. self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
  66. while 1:
  67. self.screenshot()
  68. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  69. continue
  70. if not self.appear(self.I_PRESET):
  71. break
  72. logger.info(f"Click {self.I_PREPARE_HIGHLIGHT.name}")
  73. # 点击返回
  74. while 1:
  75. self.screenshot()
  76. if self.appear_then_click(self.I_EXIT, interval=1.5):
  77. continue
  78. if self.appear(self.I_EXIT_ENSURE):
  79. break
  80. logger.info(f"Click {self.I_EXIT.name}")
  81. # 点击返回确认
  82. while 1:
  83. self.screenshot()
  84. if self.appear_then_click(self.I_EXIT_ENSURE, interval=1.5):
  85. continue
  86. if self.appear(self.I_FALSE):
  87. break
  88. logger.info(f"Click {self.I_EXIT_ENSURE.name}")
  89. # 点击失败确认
  90. self.wait_until_appear(self.I_FALSE)
  91. while 1:
  92. self.screenshot()
  93. if self.appear_then_click(self.I_FALSE, interval=1.5):
  94. continue
  95. if not self.appear(self.I_FALSE):
  96. break
  97. logger.info(f"Click {self.I_FALSE.name}")
  98. return True
  99. def exit_battle(self, skip_first: bool=False) -> bool:
  100. """
  101. 在战斗的时候强制退出战斗
  102. :return:
  103. """
  104. if skip_first:
  105. self.screenshot()
  106. if not self.appear(self.I_EXIT):
  107. return False
  108. # 点击返回
  109. logger.info(f"Click {self.I_EXIT.name}")
  110. while 1:
  111. self.screenshot()
  112. if self.appear_then_click(self.I_EXIT, interval=1.5):
  113. continue
  114. if self.appear(self.I_EXIT_ENSURE):
  115. break
  116. # 点击返回确认
  117. while 1:
  118. self.screenshot()
  119. if self.appear_then_click(self.I_EXIT_ENSURE, interval=1.5):
  120. continue
  121. if self.appear_then_click(self.I_FALSE, interval=1.5):
  122. continue
  123. if not self.appear(self.I_EXIT):
  124. break
  125. return True
  126. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  127. """
  128. 等待战斗结束
  129. :param random_click_swipt_enable:
  130. :return:
  131. """
  132. # 有的时候是长战斗,需要在设置stuck检测为长战斗
  133. # 但是无需取消设置,因为如果有点击或者滑动的话 handle_control_check会自行取消掉
  134. self.device.stuck_record_add('BATTLE_STATUS_S')
  135. self.device.click_record_clear()
  136. # 战斗过程 随机点击和滑动 防封
  137. logger.info("Start battle process")
  138. win: bool = False
  139. while 1:
  140. self.screenshot()
  141. # 如果出现赢 就点击, 第二个是针对封魔的图片
  142. if self.appear(self.I_WIN, threshold=0.8) or self.appear(self.I_DE_WIN):
  143. logger.info("Battle result is win")
  144. if self.appear(self.I_DE_WIN):
  145. self.ui_click_until_disappear(self.I_DE_WIN)
  146. win = True
  147. break
  148. # 如果出现失败 就点击,返回False
  149. if self.appear(self.I_FALSE, threshold=0.8):
  150. logger.info("Battle result is false")
  151. win = False
  152. break
  153. # 如果领奖励
  154. if self.appear(self.I_REWARD, threshold=0.6):
  155. win = True
  156. break
  157. # 如果领奖励出现金币
  158. if self.appear(self.I_REWARD_GOLD, threshold=0.8):
  159. win = True
  160. break
  161. # 如果开启战斗过程随机滑动
  162. if random_click_swipt_enable:
  163. self.random_click_swipt()
  164. # 再次确认战斗结果
  165. logger.info("Reconfirm the results of the battle")
  166. while 1:
  167. self.screenshot()
  168. if win:
  169. # 点击赢了
  170. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  171. if self.appear_then_click(self.I_WIN, action=action_click, interval=0.5):
  172. continue
  173. if not self.appear(self.I_WIN):
  174. break
  175. else:
  176. # 如果失败且 点击失败后
  177. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  178. continue
  179. if not self.appear(self.I_FALSE, threshold=0.6):
  180. return False
  181. # 最后保证能点击 获得奖励
  182. if not self.wait_until_appear(self.I_REWARD):
  183. # 有些的战斗没有下面的奖励,所以直接返回
  184. logger.info("There is no reward, Exit battle")
  185. return win
  186. logger.info("Get reward")
  187. while 1:
  188. self.screenshot()
  189. # 如果出现领奖励
  190. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  191. if self.appear_then_click(self.I_REWARD, action=action_click ,interval=1.5) or \
  192. self.appear_then_click(self.I_REWARD_GOLD, action=action_click ,interval=1.5):
  193. continue
  194. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  195. break
  196. return win
  197. def green_mark(self, enable: bool=False, mark_mode: GreenMarkType=GreenMarkType.GREEN_MAIN):
  198. """
  199. 绿标, 如果不使能就直接返回
  200. :param enable:
  201. :param mark_mode:
  202. :return:
  203. """
  204. if enable:
  205. logger.info("Green is enable")
  206. x, y = None, None
  207. match mark_mode:
  208. case GreenMarkType.GREEN_LEFT1:
  209. x, y = self.C_GREEN_LEFT_1.coord()
  210. logger.info("Green left 1")
  211. case GreenMarkType.GREEN_LEFT2:
  212. x, y = self.C_GREEN_LEFT_2.coord()
  213. logger.info("Green left 2")
  214. case GreenMarkType.GREEN_LEFT3:
  215. x, y = self.C_GREEN_LEFT_3.coord()
  216. logger.info("Green left 3")
  217. case GreenMarkType.GREEN_LEFT4:
  218. x, y = self.C_GREEN_LEFT_4.coord()
  219. logger.info("Green left 4")
  220. case GreenMarkType.GREEN_LEFT5:
  221. x, y = self.C_GREEN_LEFT_5.coord()
  222. logger.info("Green left 5")
  223. case GreenMarkType.GREEN_MAIN:
  224. x, y = self.C_GREEN_MAIN.coord()
  225. logger.info("Green main")
  226. # 等待那个准备的消失
  227. while 1:
  228. self.screenshot()
  229. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  230. break
  231. # 判断有无坐标的偏移
  232. self.appear_then_click(self.I_LOCAL)
  233. time.sleep(0.3)
  234. # 点击绿标
  235. self.device.click(x, y)
  236. def switch_preset_team(self, enable: bool=False, preset_group: int=1, preset_team: int=1):
  237. """
  238. 切换预设的队伍, 要求是在不锁定队伍时的情况下
  239. :param enable:
  240. :param preset_group:
  241. :param preset_team:
  242. :return:
  243. """
  244. if not enable:
  245. logger.info("Preset is disable")
  246. return None
  247. logger.info("Preset is enable")
  248. # 点击预设按钮
  249. while 1:
  250. self.screenshot()
  251. if self.appear(self.I_PRESET_ENSURE):
  252. break
  253. # 首个队伍没有满足5个式神,未出现预设按钮的情况下跳出循环
  254. if self.appear(self.I_PRESENT_LESS_THAN_5):
  255. break
  256. if self.appear_then_click(self.I_PRESET, threshold=0.8, interval=1):
  257. continue
  258. logger.info("Click preset button")
  259. # 选择预设组
  260. x, y = None, None
  261. match preset_group:
  262. case 1:
  263. x, y = self.C_PRESET_GROUP_1.coord()
  264. case 2:
  265. x, y = self.C_PRESET_GROUP_2.coord()
  266. case 3:
  267. x, y = self.C_PRESET_GROUP_3.coord()
  268. case 4:
  269. x, y = self.C_PRESET_GROUP_4.coord()
  270. case 5:
  271. x, y = self.C_PRESET_GROUP_5.coord()
  272. case 6:
  273. x, y = self.C_PRESET_GROUP_6.coord()
  274. case 7:
  275. x, y = self.C_PRESET_GROUP_7.coord()
  276. case _:
  277. x, y = self.C_PRESET_GROUP_1.coord()
  278. self.device.click(x, y)
  279. logger.info("Select preset group")
  280. # 选择预设的队伍
  281. time.sleep(0.5)
  282. match preset_team:
  283. case 1:
  284. x, y = self.C_PRESET_TEAM_1.coord()
  285. case 2:
  286. x, y = self.C_PRESET_TEAM_2.coord()
  287. case 3:
  288. x, y = self.C_PRESET_TEAM_3.coord()
  289. case 4:
  290. x, y = self.C_PRESET_TEAM_4.coord()
  291. case _:
  292. x, y = self.C_PRESET_TEAM_1.coord()
  293. self.device.click(x, y)
  294. logger.info("Select preset team")
  295. # 点击预设确认
  296. while 1:
  297. self.screenshot()
  298. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8):
  299. continue
  300. if not self.appear(self.I_PRESET_ENSURE):
  301. break
  302. logger.info("Click preset ensure")
  303. def random_click_swipt(self):
  304. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  305. rand_type = random.randint(0, 2)
  306. match rand_type:
  307. case 0:
  308. self.click(self.C_RANDOM_CLICK, interval=20)
  309. case 1:
  310. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  311. case 2:
  312. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  313. # 重新设置为长战斗
  314. self.device.stuck_record_add('BATTLE_STATUS_S')
  315. time.sleep(0.4) # 这样的好像不对
  316. # 判断是否在战斗中
  317. def is_in_battle(self, is_screenshot: bool = True) -> bool:
  318. """
  319. 判断是否在战斗中
  320. :return:
  321. """
  322. if is_screenshot:
  323. self.screenshot()
  324. if self.appear(self.I_FRIENDS) or \
  325. self.appear(self.I_WIN) or \
  326. self.appear(self.I_FALSE) or \
  327. self.appear(self.I_REWARD):
  328. return True
  329. else:
  330. return False
  331. def is_in_prepare(self, is_screenshot: bool = True) -> bool:
  332. """
  333. 判断是否在准备中
  334. :return:
  335. """
  336. if is_screenshot:
  337. self.screenshot()
  338. if self.appear(self.I_BUFF):
  339. return True
  340. elif self.appear(self.I_PREPARE_HIGHLIGHT):
  341. return True
  342. elif self.appear(self.I_PREPARE_DARK):
  343. return True
  344. elif self.appear(self.I_PRESET):
  345. return True
  346. else:
  347. return False
  348. def check_take_over_battle(self, is_screenshot: bool, config: GeneralBattleConfig) -> bool or None:
  349. """
  350. 中途接入战斗,并且接管
  351. :return: 赢了返回True, 输了返回False, 不是在战斗中返回None
  352. """
  353. if is_screenshot:
  354. self.screenshot()
  355. if not self.is_in_battle():
  356. return None
  357. if self.is_in_prepare(False):
  358. while 1:
  359. self.screenshot()
  360. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  361. continue
  362. if not self.appear(self.I_BUFF):
  363. break
  364. # 被接管的战斗,只有准备阶段才可以点绿标。
  365. # 因为如果是战斗中,无法保证点击的时候是否出现动画
  366. self.wait_until_disappear(self.I_BUFF)
  367. self.green_mark(config.green_enable, config.green_mark)
  368. return self.battle_wait(config.random_click_swipt_enable)
  369. def check_lock(self, enable: bool, lock_image, unlock_image):
  370. """
  371. 检测是否锁定队伍,
  372. :param enable:
  373. :param lock_image:
  374. :param unlock_image:
  375. :return:
  376. """
  377. if enable:
  378. logger.info("Lock team")
  379. while 1:
  380. self.screenshot()
  381. if self.appear(lock_image):
  382. break
  383. if self.appear_then_click(unlock_image, interval=1):
  384. continue
  385. else:
  386. logger.info("Unlock team")
  387. while 1:
  388. self.screenshot()
  389. if self.appear(unlock_image):
  390. break
  391. if self.appear_then_click(lock_image, interval=1):
  392. continue
  393. def check_buff(self, buff: BuffClass or list[BuffClass]=None):
  394. """
  395. 检测是否开启buff
  396. :param buff:
  397. :return:
  398. """
  399. if not buff:
  400. return
  401. logger.info(f'Open buff {buff}')
  402. self.ui_click(self.I_BUFF, self.I_CLOUD)
  403. if isinstance(buff, BuffClass):
  404. buff = [buff]
  405. match_method = {
  406. BuffClass.AWAKE: (self.awake, True),
  407. BuffClass.SOUL: (self.soul, True),
  408. BuffClass.GOLD_50: (self.gold_50, True),
  409. BuffClass.GOLD_100: (self.gold_100, True),
  410. BuffClass.EXP_50: (self.exp_50, True),
  411. BuffClass.EXP_100: (self.exp_100, True),
  412. BuffClass.AWAKE_CLOSE: (self.awake, False),
  413. BuffClass.SOUL_CLOSE: (self.soul, False),
  414. BuffClass.GOLD_50_CLOSE: (self.gold_50, False),
  415. BuffClass.GOLD_100_CLOSE: (self.gold_100, False),
  416. BuffClass.EXP_50_CLOSE: (self.exp_50, False),
  417. BuffClass.EXP_100_CLOSE: (self.exp_100, False),
  418. }
  419. for b in buff:
  420. func, is_open = match_method[b]
  421. func(is_open)
  422. time.sleep(0.1)
  423. logger.info(f'Open buff success')
  424. while 1:
  425. self.screenshot()
  426. if not self.appear(self.I_CLOUD):
  427. break
  428. if self.appear_then_click(self.I_BUFF, interval=1):
  429. continue
  430. if __name__ == '__main__':
  431. from module.config.config import Config
  432. from module.device.device import Device
  433. c = Config('oas1')
  434. d = Device(c)
  435. t = GeneralBattle(c, d)
  436. t.check_buff([BuffClass.EXP_50, BuffClass.GOLD_50])