general_battle.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. win = self.battle_wait(config.random_click_swipt_enable)
  34. if win:
  35. return True
  36. else:
  37. return False
  38. # 点击准备按钮
  39. self.wait_until_appear(self.I_PREPARE_HIGHLIGHT)
  40. while 1:
  41. self.screenshot()
  42. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  43. continue
  44. if not self.appear(self.I_BUFF):
  45. break
  46. logger.info("Click prepare ensure button")
  47. # 照顾一下某些模拟器慢的
  48. time.sleep(0.1)
  49. # 绿标
  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: dict=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):
  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 random_click_swipt_enable:
  157. self.random_click_swipt()
  158. # 再次确认战斗结果
  159. logger.info("Reconfirm the results of the battle")
  160. while 1:
  161. self.screenshot()
  162. if win:
  163. # 点击赢了
  164. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  165. if self.appear_then_click(self.I_WIN, action=action_click, interval=0.5):
  166. continue
  167. if not self.appear(self.I_WIN):
  168. break
  169. else:
  170. # 如果失败且 点击失败后
  171. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  172. continue
  173. if not self.appear(self.I_FALSE, threshold=0.6):
  174. return False
  175. # 最后保证能点击 获得奖励
  176. self.wait_until_appear(self.I_REWARD)
  177. logger.info("Get reward")
  178. while 1:
  179. self.screenshot()
  180. # 如果出现领奖励
  181. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  182. if self.appear_then_click(self.I_REWARD, action=action_click ,interval=1.5):
  183. continue
  184. if not self.appear(self.I_REWARD):
  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