general_battle.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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.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: dict=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 random_click_swipt_enable:
  152. self.random_click_swipt()
  153. # 再次确认战斗结果
  154. logger.info("Reconfirm the results of the battle")
  155. while 1:
  156. self.screenshot()
  157. if win:
  158. # 点击赢了
  159. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  160. if self.appear_then_click(self.I_WIN, action=action_click, interval=0.5):
  161. continue
  162. if not self.appear(self.I_WIN):
  163. break
  164. else:
  165. # 如果失败且 点击失败后
  166. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  167. continue
  168. if not self.appear(self.I_FALSE, threshold=0.6):
  169. return False
  170. # 最后保证能点击 获得奖励
  171. self.wait_until_appear(self.I_REWARD)
  172. logger.info("Get reward")
  173. while 1:
  174. self.screenshot()
  175. # 如果出现领奖励
  176. action_click = random.choice([self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3])
  177. if self.appear_then_click(self.I_REWARD, action=action_click ,interval=1.5):
  178. continue
  179. if not self.appear(self.I_REWARD):
  180. break
  181. return win
  182. def green_mark(self, enable: bool=False, mark_mode: GreenMarkType=GreenMarkType.GREEN_MAIN):
  183. """
  184. 绿标, 如果不使能就直接返回
  185. :param enable:
  186. :param mark_mode:
  187. :return:
  188. """
  189. if enable:
  190. logger.info("Green is enable")
  191. x, y = None, None
  192. match mark_mode:
  193. case GreenMarkType.GREEN_LEFT1:
  194. x, y = self.C_GREEN_LEFT_1.coord()
  195. logger.info("Green left 1")
  196. case GreenMarkType.GREEN_LEFT2:
  197. x, y = self.C_GREEN_LEFT_2.coord()
  198. logger.info("Green left 2")
  199. case GreenMarkType.GREEN_LEFT3:
  200. x, y = self.C_GREEN_LEFT_3.coord()
  201. logger.info("Green left 3")
  202. case GreenMarkType.GREEN_LEFT4:
  203. x, y = self.C_GREEN_LEFT_4.coord()
  204. logger.info("Green left 4")
  205. case GreenMarkType.GREEN_LEFT5:
  206. x, y = self.C_GREEN_LEFT_5.coord()
  207. logger.info("Green left 5")
  208. case GreenMarkType.GREEN_MAIN:
  209. x, y = self.C_GREEN_MAIN.coord()
  210. logger.info("Green main")
  211. # 等待那个准备的消失
  212. while 1:
  213. self.screenshot()
  214. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  215. break
  216. # 判断有无坐标的偏移
  217. self.appear_then_click(self.I_LOCAL)
  218. time.sleep(0.3)
  219. # 点击绿标
  220. self.device.click(x, y)
  221. def switch_preset_team(self, enable: bool=False, preset_group: int=1, preset_team: int=1):
  222. """
  223. 切换预设的队伍, 要求是在不锁定队伍时的情况下
  224. :param enable:
  225. :param preset_group:
  226. :param preset_team:
  227. :return:
  228. """
  229. if not enable:
  230. logger.info("Preset is disable")
  231. return None
  232. logger.info("Preset is enable")
  233. # 点击预设按钮
  234. while 1:
  235. self.screenshot()
  236. if self.appear(self.I_PRESET_ENSURE):
  237. break
  238. if self.appear_then_click(self.I_PRESET, threshold=0.8, interval=1):
  239. continue
  240. logger.info("Click preset button")
  241. # 选择预设组
  242. x, y = None, None
  243. match preset_group:
  244. case 1:
  245. x, y = self.C_PRESET_GROUP_1.coord()
  246. case 2:
  247. x, y = self.C_PRESET_GROUP_2.coord()
  248. case 3:
  249. x, y = self.C_PRESET_GROUP_3.coord()
  250. case 4:
  251. x, y = self.C_PRESET_GROUP_4.coord()
  252. case 5:
  253. x, y = self.C_PRESET_GROUP_5.coord()
  254. case 6:
  255. x, y = self.C_PRESET_GROUP_6.coord()
  256. case 7:
  257. x, y = self.C_PRESET_GROUP_7.coord()
  258. case _:
  259. x, y = self.C_PRESET_GROUP_1.coord()
  260. self.device.click(x, y)
  261. logger.info("Select preset group")
  262. # 选择预设的队伍
  263. time.sleep(0.5)
  264. match preset_team:
  265. case 1:
  266. x, y = self.C_PRESET_TEAM_1.coord()
  267. case 2:
  268. x, y = self.C_PRESET_TEAM_2.coord()
  269. case 3:
  270. x, y = self.C_PRESET_TEAM_3.coord()
  271. case 4:
  272. x, y = self.C_PRESET_TEAM_4.coord()
  273. case _:
  274. x, y = self.C_PRESET_TEAM_1.coord()
  275. self.device.click(x, y)
  276. logger.info("Select preset team")
  277. # 点击预设确认
  278. while 1:
  279. self.screenshot()
  280. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8):
  281. continue
  282. if not self.appear(self.I_PRESET_ENSURE):
  283. break
  284. logger.info("Click preset ensure")
  285. def random_click_swipt(self):
  286. time.sleep(0.4) # 这样的好像不对
  287. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  288. rand_type = random.randint(0, 2)
  289. match rand_type:
  290. case 0:
  291. self.click(self.C_RANDOM_CLICK, interval=20)
  292. case 1:
  293. self.swipe(self.S_BATTLE_RANDOM_LEFT, interval=20)
  294. case 2:
  295. self.swipe(self.S_BATTLE_RANDOM_RIGHT, interval=20)
  296. # 重新设置为长战斗
  297. self.device.stuck_record_add('BATTLE_STATUS_S')
  298. # 判断是否在战斗中
  299. def is_in_battle(self, is_screenshot: bool = True) -> bool:
  300. """
  301. 判断是否在战斗中
  302. :return:
  303. """
  304. if is_screenshot:
  305. self.screenshot()
  306. if self.appear(self.I_FRIENDS) or \
  307. self.appear(self.I_WIN) or \
  308. self.appear(self.I_FALSE) or \
  309. self.appear(self.I_REWARD):
  310. return True
  311. else:
  312. return False
  313. def is_in_prepare(self, is_screenshot: bool = True) -> bool:
  314. """
  315. 判断是否在准备中
  316. :return:
  317. """
  318. if is_screenshot:
  319. self.screenshot()
  320. if self.appear(self.I_BUFF):
  321. return True
  322. elif self.appear(self.I_PREPARE_HIGHLIGHT):
  323. return True
  324. elif self.appear(self.I_PREPARE_DARK):
  325. return True
  326. elif self.appear(self.I_PRESET):
  327. return True
  328. else:
  329. return False
  330. def check_take_over_battle(self, is_screenshot: bool, config: GeneralBattleConfig) -> bool or None:
  331. """
  332. 中途接入战斗,并且接管
  333. :return: 赢了返回True, 输了返回False, 不是在战斗中返回None
  334. """
  335. if is_screenshot:
  336. self.screenshot()
  337. if not self.is_in_battle():
  338. return None
  339. if self.is_in_prepare(False):
  340. while 1:
  341. self.screenshot()
  342. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT, interval=1.5):
  343. continue
  344. if not self.appear(self.I_BUFF):
  345. break
  346. # 被接管的战斗,只有准备阶段才可以点绿标。
  347. # 因为如果是战斗中,无法保证点击的时候是否出现动画
  348. self.wait_until_disappear(self.I_BUFF)
  349. self.green_mark(config.green_enable, config.green_mark)
  350. return self.battle_wait(config.random_click_swipt_enable)