general_battle.py 11 KB

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