general_battle.py 16 KB

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