general_battle.py 14 KB

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