general_battle.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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().dict()
  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. if self.is_in_battle():
  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. win = self.battle_wait(config.random_click_swipt_enable)
  40. if win:
  41. return True
  42. else:
  43. return False
  44. # 点击准备按钮
  45. self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
  46. while 1:
  47. self.screenshot()
  48. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  49. continue
  50. if not self.appear(self.I_BUFF):
  51. break
  52. logger.info("Click prepare ensure button")
  53. # 照顾一下某些模拟器慢的
  54. time.sleep(0.1)
  55. # 绿标
  56. self.green_mark(config.green_enable, config.green_mark)
  57. win = self.battle_wait(config.random_click_swipt_enable)
  58. if win:
  59. return True
  60. else:
  61. return False
  62. def run_general_battle_back(self, config: dict=None) -> bool:
  63. """
  64. 进入挑战然后直接返回
  65. :param config:
  66. :return:
  67. """
  68. # 如果没有锁定队伍那么在点击准备后才退出的
  69. if not config.lock_team_enable:
  70. # 点击准备按钮
  71. self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
  72. while 1:
  73. self.screenshot()
  74. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  75. continue
  76. if not self.appear(self.I_PRESET):
  77. break
  78. logger.info(f"Click {self.I_PREPARE_HIGHLIGHT.name}")
  79. # 点击返回
  80. while 1:
  81. self.screenshot()
  82. if self.appear_then_click(self.I_EXIT, interval=1.5):
  83. continue
  84. if self.appear(self.I_EXIT_ENSURE):
  85. break
  86. logger.info(f"Click {self.I_EXIT.name}")
  87. # 点击返回确认
  88. while 1:
  89. self.screenshot()
  90. if self.appear_then_click(self.I_EXIT_ENSURE, interval=1.5):
  91. continue
  92. if self.appear(self.I_FALSE):
  93. break
  94. logger.info(f"Click {self.I_EXIT_ENSURE.name}")
  95. # 点击失败确认
  96. self.wait_until_appear(self.I_FALSE)
  97. while 1:
  98. self.screenshot()
  99. if self.appear_then_click(self.I_FALSE, interval=1.5):
  100. continue
  101. if not self.appear(self.I_FALSE):
  102. break
  103. logger.info(f"Click {self.I_FALSE.name}")
  104. return True
  105. def exit_battle(self, skip_first: bool=False) -> bool:
  106. """
  107. 在战斗的时候强制退出战斗
  108. :return:
  109. """
  110. if skip_first:
  111. self.screenshot()
  112. if not self.appear(self.I_EXIT):
  113. return False
  114. # 点击返回
  115. logger.info(f"Click {self.I_EXIT.name}")
  116. while 1:
  117. self.screenshot()
  118. if self.appear_then_click(self.I_EXIT, interval=1.5):
  119. continue
  120. if self.appear(self.I_EXIT_ENSURE):
  121. break
  122. # 点击返回确认
  123. while 1:
  124. self.screenshot()
  125. if self.appear_then_click(self.I_EXIT_ENSURE, interval=1.5):
  126. continue
  127. if self.appear_then_click(self.I_FALSE, interval=1.5):
  128. continue
  129. if not self.appear(self.I_EXIT):
  130. break
  131. return True
  132. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  133. """
  134. 等待战斗结束
  135. :param random_click_swipt_enable:
  136. :return:
  137. """
  138. # 有的时候是长战斗,需要在设置stuck检测为长战斗
  139. # 但是无需取消设置,因为如果有点击或者滑动的话 handle_control_check会自行取消掉
  140. self.device.stuck_record_add('BATTLE_STATUS_S')
  141. self.device.click_record_clear()
  142. # 战斗过程 随机点击和滑动 防封
  143. logger.info("Start battle process")
  144. win: bool = False
  145. while 1:
  146. self.screenshot()
  147. # 如果出现赢 就点击
  148. if self.appear(self.I_WIN, threshold=0.8):
  149. logger.info("Battle result is win")
  150. win = True
  151. break
  152. # 如果出现失败 就点击,返回False
  153. if self.appear(self.I_FALSE, threshold=0.8):
  154. logger.info("Battle result is false")
  155. win = False
  156. break
  157. # 如果领奖励
  158. if self.appear(self.I_REWARD, threshold=0.6):
  159. win = True
  160. break
  161. # 如果开启战斗过程随机滑动
  162. if random_click_swipt_enable:
  163. self.random_click_swipt()
  164. # 再次确认战斗结果
  165. logger.info("Reconfirm the results of the battle")
  166. while 1:
  167. self.screenshot()
  168. if win:
  169. # 点击赢了
  170. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  171. if self.appear_then_click(self.I_WIN, action=action_click, interval=0.5):
  172. continue
  173. if not self.appear(self.I_WIN):
  174. break
  175. else:
  176. # 如果失败且 点击失败后
  177. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  178. continue
  179. if not self.appear(self.I_FALSE, threshold=0.6):
  180. return False
  181. # 最后保证能点击 获得奖励
  182. self.wait_until_appear(self.I_REWARD)
  183. logger.info("Get reward")
  184. while 1:
  185. self.screenshot()
  186. # 如果出现领奖励
  187. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  188. if self.appear_then_click(self.I_REWARD, action=action_click ,interval=1.5):
  189. continue
  190. if not self.appear(self.I_REWARD):
  191. break
  192. return win
  193. def green_mark(self, enable: bool=False, mark_mode: GreenMarkType=GreenMarkType.GREEN_MAIN):
  194. """
  195. 绿标, 如果不使能就直接返回
  196. :param enable:
  197. :param mark_mode:
  198. :return:
  199. """
  200. if enable:
  201. logger.info("Green is enable")
  202. x, y = None, None
  203. match mark_mode:
  204. case GreenMarkType.GREEN_LEFT1:
  205. x, y = self.C_GREEN_LEFT_1.coord()
  206. logger.info("Green left 1")
  207. case GreenMarkType.GREEN_LEFT2:
  208. x, y = self.C_GREEN_LEFT_2.coord()
  209. logger.info("Green left 2")
  210. case GreenMarkType.GREEN_LEFT3:
  211. x, y = self.C_GREEN_LEFT_3.coord()
  212. logger.info("Green left 3")
  213. case GreenMarkType.GREEN_LEFT4:
  214. x, y = self.C_GREEN_LEFT_4.coord()
  215. logger.info("Green left 4")
  216. case GreenMarkType.GREEN_LEFT5:
  217. x, y = self.C_GREEN_LEFT_5.coord()
  218. logger.info("Green left 5")
  219. case GreenMarkType.GREEN_MAIN:
  220. x, y = self.C_GREEN_MAIN.coord()
  221. logger.info("Green main")
  222. # 等待那个准备的消失
  223. while 1:
  224. self.screenshot()
  225. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  226. break
  227. # 判断有无坐标的偏移
  228. self.appear_then_click(self.I_LOCAL)
  229. time.sleep(0.3)
  230. # 点击绿标
  231. self.device.click(x, y)
  232. def switch_preset_team(self, enable: bool=False, preset_group: int=1, preset_team: int=1):
  233. """
  234. 切换预设的队伍, 要求是在不锁定队伍时的情况下
  235. :param enable:
  236. :param preset_group:
  237. :param preset_team:
  238. :return:
  239. """
  240. if not enable:
  241. logger.info("Preset is disable")
  242. return None
  243. logger.info("Preset is enable")
  244. # 点击预设按钮
  245. while 1:
  246. self.screenshot()
  247. if self.appear(self.I_PRESET_ENSURE):
  248. break
  249. if self.appear_then_click(self.I_PRESET, threshold=0.8, interval=1):
  250. continue
  251. logger.info("Click preset button")
  252. # 选择预设组
  253. x, y = None, None
  254. match preset_group:
  255. case 1:
  256. x, y = self.C_PRESET_GROUP_1.coord()
  257. case 2:
  258. x, y = self.C_PRESET_GROUP_2.coord()
  259. case 3:
  260. x, y = self.C_PRESET_GROUP_3.coord()
  261. case 4:
  262. x, y = self.C_PRESET_GROUP_4.coord()
  263. case 5:
  264. x, y = self.C_PRESET_GROUP_5.coord()
  265. case 6:
  266. x, y = self.C_PRESET_GROUP_6.coord()
  267. case 7:
  268. x, y = self.C_PRESET_GROUP_7.coord()
  269. case _:
  270. x, y = self.C_PRESET_GROUP_1.coord()
  271. self.device.click(x, y)
  272. logger.info("Select preset group")
  273. # 选择预设的队伍
  274. time.sleep(0.5)
  275. match preset_team:
  276. case 1:
  277. x, y = self.C_PRESET_TEAM_1.coord()
  278. case 2:
  279. x, y = self.C_PRESET_TEAM_2.coord()
  280. case 3:
  281. x, y = self.C_PRESET_TEAM_3.coord()
  282. case 4:
  283. x, y = self.C_PRESET_TEAM_4.coord()
  284. case _:
  285. x, y = self.C_PRESET_TEAM_1.coord()
  286. self.device.click(x, y)
  287. logger.info("Select preset team")
  288. # 点击预设确认
  289. while 1:
  290. self.screenshot()
  291. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8):
  292. continue
  293. if not self.appear(self.I_PRESET_ENSURE):
  294. break
  295. logger.info("Click preset ensure")
  296. def random_click_swipt(self):
  297. time.sleep(0.4) # 这样的好像不对
  298. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  299. rand_type = random.randint(0, 2)
  300. match rand_type:
  301. case 0:
  302. self.click(self.C_RANDOM_CLICK, interval=20)
  303. case 1:
  304. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  305. case 2:
  306. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  307. # 重新设置为长战斗
  308. self.device.stuck_record_add('BATTLE_STATUS_S')
  309. # 判断是否在战斗中
  310. def is_in_battle(self, is_screenshot: bool = True) -> bool:
  311. """
  312. 判断是否在战斗中
  313. :return:
  314. """
  315. if is_screenshot:
  316. self.screenshot()
  317. if self.appear(self.I_FRIENDS) or \
  318. self.appear(self.I_WIN) or \
  319. self.appear(self.I_FALSE) or \
  320. self.appear(self.I_REWARD):
  321. return True
  322. else:
  323. return False