general_battle.py 11 KB

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