general_battle.py 16 KB

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