general_battle.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. self.device.click_record_clear()
  129. # 战斗过程 随机点击和滑动 防封
  130. logger.info("Start battle process")
  131. win: bool = False
  132. while 1:
  133. self.screenshot()
  134. # 如果出现赢 就点击
  135. if self.appear(self.I_WIN, threshold=0.8):
  136. logger.info("Battle result is win")
  137. win = True
  138. break
  139. # 如果出现失败 就点击,返回False
  140. if self.appear(self.I_FALSE, threshold=0.8):
  141. logger.info("Battle result is false")
  142. win = False
  143. break
  144. # 如果领奖励
  145. if self.appear(self.I_REWARD, threshold=0.6):
  146. win = True
  147. break
  148. # 如果开启战斗过程随机滑动
  149. if random_click_swipt_enable:
  150. self.random_click_swipt()
  151. # 再次确认战斗结果
  152. logger.info("Reconfirm the results of the battle")
  153. while 1:
  154. self.screenshot()
  155. if win:
  156. # 点击赢了
  157. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  158. if self.appear_then_click(self.I_WIN, action=action_click, interval=0.5):
  159. continue
  160. if not self.appear(self.I_WIN):
  161. break
  162. else:
  163. # 如果失败且 点击失败后
  164. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  165. continue
  166. if not self.appear(self.I_FALSE, threshold=0.6):
  167. return False
  168. # 最后保证能点击 获得奖励
  169. self.wait_until_appear(self.I_REWARD)
  170. logger.info("Get reward")
  171. while 1:
  172. self.screenshot()
  173. # 如果出现领奖励
  174. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  175. if self.appear_then_click(self.I_REWARD, action=action_click ,interval=1.5):
  176. continue
  177. if not self.appear(self.I_REWARD):
  178. break
  179. return win
  180. def green_mark(self, enable: bool=False, mark_mode: GreenMarkType=GreenMarkType.GREEN_MAIN):
  181. """
  182. 绿标, 如果不使能就直接返回
  183. :param enable:
  184. :param mark_mode:
  185. :return:
  186. """
  187. if enable:
  188. logger.info("Green is enable")
  189. x, y = None, None
  190. match mark_mode:
  191. case GreenMarkType.GREEN_LEFT1:
  192. x, y = self.C_GREEN_LEFT_1.coord()
  193. logger.info("Green left 1")
  194. case GreenMarkType.GREEN_LEFT2:
  195. x, y = self.C_GREEN_LEFT_2.coord()
  196. logger.info("Green left 2")
  197. case GreenMarkType.GREEN_LEFT3:
  198. x, y = self.C_GREEN_LEFT_3.coord()
  199. logger.info("Green left 3")
  200. case GreenMarkType.GREEN_LEFT4:
  201. x, y = self.C_GREEN_LEFT_4.coord()
  202. logger.info("Green left 4")
  203. case GreenMarkType.GREEN_LEFT5:
  204. x, y = self.C_GREEN_LEFT_5.coord()
  205. logger.info("Green left 5")
  206. case GreenMarkType.GREEN_MAIN:
  207. x, y = self.C_GREEN_MAIN.coord()
  208. logger.info("Green main")
  209. # 等待那个准备的消失
  210. while 1:
  211. self.screenshot()
  212. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  213. break
  214. # 判断有无坐标的偏移
  215. self.appear_then_click(self.I_LOCAL)
  216. time.sleep(0.3)
  217. # 点击绿标
  218. self.device.click(x, y)
  219. def switch_preset_team(self, enable: bool=False, preset_group: int=1, preset_team: int=1):
  220. """
  221. 切换预设的队伍, 要求是在不锁定队伍时的情况下
  222. :param enable:
  223. :param preset_group:
  224. :param preset_team:
  225. :return:
  226. """
  227. if not enable:
  228. logger.info("Preset is disable")
  229. return None
  230. logger.info("Preset is enable")
  231. # 点击预设按钮
  232. while 1:
  233. self.screenshot()
  234. if self.appear_then_click(self.I_PRESET, threshold=0.8):
  235. continue
  236. if self.appear(self.I_PRESET_ENSURE):
  237. break
  238. logger.info("Click preset button")
  239. # 选择预设组
  240. x, y = None, None
  241. match preset_group:
  242. case 1:
  243. x, y = self.C_PRESET_GROUP_1.coord()
  244. case 2:
  245. x, y = self.C_PRESET_GROUP_2.coord()
  246. case 3:
  247. x, y = self.C_PRESET_GROUP_3.coord()
  248. case 4:
  249. x, y = self.C_PRESET_GROUP_4.coord()
  250. case 5:
  251. x, y = self.C_PRESET_GROUP_5.coord()
  252. case 6:
  253. x, y = self.C_PRESET_GROUP_6.coord()
  254. case 7:
  255. x, y = self.C_PRESET_GROUP_7.coord()
  256. case _:
  257. x, y = self.C_PRESET_GROUP_1.coord()
  258. self.device.click(x, y)
  259. logger.info("Select preset group")
  260. # 选择预设的队伍
  261. time.sleep(0.5)
  262. match preset_team:
  263. case 1:
  264. x, y = self.C_PRESET_TEAM_1.coord()
  265. case 2:
  266. x, y = self.C_PRESET_TEAM_2.coord()
  267. case 3:
  268. x, y = self.C_PRESET_TEAM_3.coord()
  269. case 4:
  270. x, y = self.C_PRESET_TEAM_4.coord()
  271. case _:
  272. x, y = self.C_PRESET_TEAM_1.coord()
  273. self.device.click(x, y)
  274. logger.info("Select preset team")
  275. # 点击预设确认
  276. while 1:
  277. self.screenshot()
  278. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8):
  279. continue
  280. if not self.appear(self.I_PRESET_ENSURE):
  281. break
  282. logger.info("Click preset ensure")
  283. def random_click_swipt(self):
  284. time.sleep(0.4) # 这样的好像不对
  285. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  286. rand_type = random.randint(0, 2)
  287. match rand_type:
  288. case 0:
  289. self.click(self.C_RANDOM_CLICK, interval=20)
  290. case 1:
  291. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  292. case 2:
  293. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  294. # 重新设置为长战斗
  295. self.device.stuck_record_add('BATTLE_STATUS_S')