general_battle.py 16 KB

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