general_battle.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. 很重要 这个函数是原先写的, 优化版本在tasks/Secret/script_task下。本着不改动原先的代码的原则,所以就不改了
  130. :param random_click_swipt_enable:
  131. :return:
  132. """
  133. # 有的时候是长战斗,需要在设置stuck检测为长战斗
  134. # 但是无需取消设置,因为如果有点击或者滑动的话 handle_control_check会自行取消掉
  135. self.device.stuck_record_add('BATTLE_STATUS_S')
  136. self.device.click_record_clear()
  137. # 战斗过程 随机点击和滑动 防封
  138. logger.info("Start battle process")
  139. win: bool = False
  140. while 1:
  141. self.screenshot()
  142. # 如果出现赢 就点击, 第二个是针对封魔的图片
  143. if self.appear(self.I_WIN, threshold=0.8) or self.appear(self.I_DE_WIN):
  144. logger.info("Battle result is win")
  145. if self.appear(self.I_DE_WIN):
  146. self.ui_click_until_disappear(self.I_DE_WIN)
  147. win = True
  148. break
  149. # 如果出现失败 就点击,返回False
  150. if self.appear(self.I_FALSE, threshold=0.8):
  151. logger.info("Battle result is false")
  152. win = False
  153. break
  154. # 如果领奖励
  155. if self.appear(self.I_REWARD, threshold=0.6):
  156. win = True
  157. break
  158. # 如果领奖励出现金币
  159. if self.appear(self.I_REWARD_GOLD, threshold=0.8):
  160. win = True
  161. break
  162. # 如果开启战斗过程随机滑动
  163. if random_click_swipt_enable:
  164. self.random_click_swipt()
  165. # 再次确认战斗结果
  166. logger.info("Reconfirm the results of the battle")
  167. while 1:
  168. self.screenshot()
  169. if win:
  170. # 点击赢了
  171. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  172. if self.appear_then_click(self.I_WIN, action=action_click, interval=0.5):
  173. continue
  174. if not self.appear(self.I_WIN):
  175. break
  176. else:
  177. # 如果失败且 点击失败后
  178. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  179. continue
  180. if not self.appear(self.I_FALSE, threshold=0.6):
  181. return False
  182. # 最后保证能点击 获得奖励
  183. if not self.wait_until_appear(self.I_REWARD):
  184. # 有些的战斗没有下面的奖励,所以直接返回
  185. logger.info("There is no reward, Exit battle")
  186. return win
  187. logger.info("Get reward")
  188. while 1:
  189. self.screenshot()
  190. # 如果出现领奖励
  191. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  192. if self.appear_then_click(self.I_REWARD, action=action_click ,interval=1.5) or \
  193. self.appear_then_click(self.I_REWARD_GOLD, action=action_click ,interval=1.5):
  194. continue
  195. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  196. break
  197. return win
  198. def green_mark(self, enable: bool=False, mark_mode: GreenMarkType=GreenMarkType.GREEN_MAIN):
  199. """
  200. 绿标, 如果不使能就直接返回
  201. :param enable:
  202. :param mark_mode:
  203. :return:
  204. """
  205. if enable:
  206. logger.info("Green is enable")
  207. x, y = None, None
  208. match mark_mode:
  209. case GreenMarkType.GREEN_LEFT1:
  210. x, y = self.C_GREEN_LEFT_1.coord()
  211. logger.info("Green left 1")
  212. case GreenMarkType.GREEN_LEFT2:
  213. x, y = self.C_GREEN_LEFT_2.coord()
  214. logger.info("Green left 2")
  215. case GreenMarkType.GREEN_LEFT3:
  216. x, y = self.C_GREEN_LEFT_3.coord()
  217. logger.info("Green left 3")
  218. case GreenMarkType.GREEN_LEFT4:
  219. x, y = self.C_GREEN_LEFT_4.coord()
  220. logger.info("Green left 4")
  221. case GreenMarkType.GREEN_LEFT5:
  222. x, y = self.C_GREEN_LEFT_5.coord()
  223. logger.info("Green left 5")
  224. case GreenMarkType.GREEN_MAIN:
  225. x, y = self.C_GREEN_MAIN.coord()
  226. logger.info("Green main")
  227. # 等待那个准备的消失
  228. while 1:
  229. self.screenshot()
  230. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  231. break
  232. # 判断有无坐标的偏移
  233. self.appear_then_click(self.I_LOCAL)
  234. time.sleep(0.3)
  235. # 点击绿标
  236. self.device.click(x, y)
  237. def switch_preset_team(self, enable: bool=False, preset_group: int=1, preset_team: int=1):
  238. """
  239. 切换预设的队伍, 要求是在不锁定队伍时的情况下
  240. :param enable:
  241. :param preset_group:
  242. :param preset_team:
  243. :return:
  244. """
  245. if not enable:
  246. logger.info("Preset is disable")
  247. return None
  248. logger.info("Preset is enable")
  249. # 点击预设按钮
  250. while 1:
  251. self.screenshot()
  252. if self.appear(self.I_PRESET_ENSURE):
  253. break
  254. # 首个队伍没有满足5个式神,未出现预设按钮的情况下跳出循环
  255. if self.appear(self.I_PRESENT_LESS_THAN_5):
  256. break
  257. if self.appear_then_click(self.I_PRESET, threshold=0.8, interval=1):
  258. continue
  259. logger.info("Click preset button")
  260. # 选择预设组
  261. x, y = None, None
  262. match preset_group:
  263. case 1:
  264. x, y = self.C_PRESET_GROUP_1.coord()
  265. case 2:
  266. x, y = self.C_PRESET_GROUP_2.coord()
  267. case 3:
  268. x, y = self.C_PRESET_GROUP_3.coord()
  269. case 4:
  270. x, y = self.C_PRESET_GROUP_4.coord()
  271. case 5:
  272. x, y = self.C_PRESET_GROUP_5.coord()
  273. case 6:
  274. x, y = self.C_PRESET_GROUP_6.coord()
  275. case 7:
  276. x, y = self.C_PRESET_GROUP_7.coord()
  277. case _:
  278. x, y = self.C_PRESET_GROUP_1.coord()
  279. self.device.click(x, y)
  280. logger.info("Select preset group")
  281. # 选择预设的队伍
  282. time.sleep(0.5)
  283. match preset_team:
  284. case 1:
  285. x, y = self.C_PRESET_TEAM_1.coord()
  286. case 2:
  287. x, y = self.C_PRESET_TEAM_2.coord()
  288. case 3:
  289. x, y = self.C_PRESET_TEAM_3.coord()
  290. case 4:
  291. x, y = self.C_PRESET_TEAM_4.coord()
  292. case _:
  293. x, y = self.C_PRESET_TEAM_1.coord()
  294. self.device.click(x, y)
  295. logger.info("Select preset team")
  296. # 点击预设确认
  297. while 1:
  298. self.screenshot()
  299. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8):
  300. continue
  301. if not self.appear(self.I_PRESET_ENSURE):
  302. break
  303. logger.info("Click preset ensure")
  304. def random_click_swipt(self):
  305. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  306. rand_type = random.randint(0, 2)
  307. match rand_type:
  308. case 0:
  309. self.click(self.C_RANDOM_CLICK, interval=20)
  310. case 1:
  311. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  312. case 2:
  313. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  314. # 重新设置为长战斗
  315. self.device.stuck_record_add('BATTLE_STATUS_S')
  316. else:
  317. time.sleep(0.4) # 这样的好像不对
  318. # 判断是否在战斗中
  319. def is_in_battle(self, is_screenshot: bool = True) -> bool:
  320. """
  321. 判断是否在战斗中
  322. :return:
  323. """
  324. if is_screenshot:
  325. self.screenshot()
  326. if self.appear(self.I_FRIENDS) or \
  327. self.appear(self.I_WIN) or \
  328. self.appear(self.I_FALSE) or \
  329. self.appear(self.I_REWARD):
  330. return True
  331. else:
  332. return False
  333. def is_in_prepare(self, is_screenshot: bool = True) -> bool:
  334. """
  335. 判断是否在准备中
  336. :return:
  337. """
  338. if is_screenshot:
  339. self.screenshot()
  340. if self.appear(self.I_BUFF):
  341. return True
  342. elif self.appear(self.I_PREPARE_HIGHLIGHT):
  343. return True
  344. elif self.appear(self.I_PREPARE_DARK):
  345. return True
  346. elif self.appear(self.I_PRESET):
  347. return True
  348. else:
  349. return False
  350. def check_take_over_battle(self, is_screenshot: bool, config: GeneralBattleConfig) -> bool or None:
  351. """
  352. 中途接入战斗,并且接管
  353. :return: 赢了返回True, 输了返回False, 不是在战斗中返回None
  354. """
  355. if is_screenshot:
  356. self.screenshot()
  357. if not self.is_in_battle():
  358. return None
  359. if self.is_in_prepare(False):
  360. while 1:
  361. self.screenshot()
  362. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  363. continue
  364. if not self.appear(self.I_BUFF):
  365. break
  366. # 被接管的战斗,只有准备阶段才可以点绿标。
  367. # 因为如果是战斗中,无法保证点击的时候是否出现动画
  368. self.wait_until_disappear(self.I_BUFF)
  369. self.green_mark(config.green_enable, config.green_mark)
  370. return self.battle_wait(config.random_click_swipt_enable)
  371. def check_lock(self, enable: bool, lock_image, unlock_image):
  372. """
  373. 检测是否锁定队伍,
  374. :param enable:
  375. :param lock_image:
  376. :param unlock_image:
  377. :return:
  378. """
  379. if enable:
  380. logger.info("Lock team")
  381. while 1:
  382. self.screenshot()
  383. if self.appear(lock_image):
  384. break
  385. if self.appear_then_click(unlock_image, interval=1):
  386. continue
  387. else:
  388. logger.info("Unlock team")
  389. while 1:
  390. self.screenshot()
  391. if self.appear(unlock_image):
  392. break
  393. if self.appear_then_click(lock_image, interval=1):
  394. continue
  395. def check_buff(self, buff: BuffClass or list[BuffClass]=None):
  396. """
  397. 检测是否开启buff
  398. :param buff:
  399. :return:
  400. """
  401. if not buff:
  402. return
  403. logger.info(f'Open buff {buff}')
  404. self.ui_click(self.I_BUFF, self.I_CLOUD)
  405. if isinstance(buff, BuffClass):
  406. buff = [buff]
  407. match_method = {
  408. BuffClass.AWAKE: (self.awake, True),
  409. BuffClass.SOUL: (self.soul, True),
  410. BuffClass.GOLD_50: (self.gold_50, True),
  411. BuffClass.GOLD_100: (self.gold_100, True),
  412. BuffClass.EXP_50: (self.exp_50, True),
  413. BuffClass.EXP_100: (self.exp_100, True),
  414. BuffClass.AWAKE_CLOSE: (self.awake, False),
  415. BuffClass.SOUL_CLOSE: (self.soul, False),
  416. BuffClass.GOLD_50_CLOSE: (self.gold_50, False),
  417. BuffClass.GOLD_100_CLOSE: (self.gold_100, False),
  418. BuffClass.EXP_50_CLOSE: (self.exp_50, False),
  419. BuffClass.EXP_100_CLOSE: (self.exp_100, False),
  420. }
  421. for b in buff:
  422. func, is_open = match_method[b]
  423. func(is_open)
  424. time.sleep(0.1)
  425. logger.info(f'Open buff success')
  426. while 1:
  427. self.screenshot()
  428. if not self.appear(self.I_CLOUD):
  429. break
  430. if self.appear_then_click(self.I_BUFF, interval=1):
  431. continue
  432. if __name__ == '__main__':
  433. from module.config.config import Config
  434. from module.device.device import Device
  435. c = Config('oas1')
  436. d = Device(c)
  437. t = GeneralBattle(c, d)
  438. t.check_buff([BuffClass.EXP_50, BuffClass.GOLD_50])