general_battle.py 16 KB

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