general_battle.py 9.1 KB

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