general_battle.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.GeneralBattle.config_general_battle import GreenMarkType, GeneralBattleConfig
  8. from tasks.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. # 如果启动更换队伍
  27. if config.preset_enable:
  28. # 点击预设按钮
  29. while 1:
  30. self.screenshot()
  31. if self.appear_then_click(self.I_PRESET, threshold=0.8):
  32. continue
  33. if self.appear(self.I_PRESET_ENSURE):
  34. break
  35. # 选择预设组
  36. x, y = None, None
  37. match config.preset_group:
  38. case 1: x, y = self.C_PRESET_GROUP_1.coord()
  39. case 2: x, y = self.C_PRESET_GROUP_2.coord()
  40. case 3: x, y = self.C_PRESET_GROUP_3.coord()
  41. case 4: x, y = self.C_PRESET_GROUP_4.coord()
  42. case 5: x, y = self.C_PRESET_GROUP_5.coord()
  43. case 6: x, y = self.C_PRESET_GROUP_6.coord()
  44. case 7: x, y = self.C_PRESET_GROUP_7.coord()
  45. case _: x, y = self.C_PRESET_GROUP_1.coord()
  46. self.device.click(x, y)
  47. # 选择预设的队伍
  48. time.sleep(0.5)
  49. match config.preset_team:
  50. case 1: x, y = self.C_PRESET_TEAM_1.coord()
  51. case 2: x, y = self.C_PRESET_TEAM_2.coord()
  52. case 3: x, y = self.C_PRESET_TEAM_3.coord()
  53. case 4: x, y = self.C_PRESET_TEAM_4.coord()
  54. case _: x, y = self.C_PRESET_TEAM_1.coord()
  55. self.device.click(x, y)
  56. # 点击预设确认
  57. while 1:
  58. self.screenshot()
  59. if self.appear_then_click(self.I_PRESET_ENSURE, threshold=0.8):
  60. continue
  61. if not self.appear(self.I_PRESET_ENSURE):
  62. break
  63. if config.buff_enable:
  64. # 点击buff按钮
  65. while 1:
  66. self.screenshot()
  67. if self.appear_then_click(self.I_BUFF, interval=1.5):
  68. continue
  69. if self.appear(self.I_BUFF_AWAKEN) or self.appear(self.I_BUFF_SOUL):
  70. break
  71. # 点击准备按钮
  72. while 1:
  73. self.screenshot()
  74. if self.appear_then_click(self.I_PREPARE_HIGHLIGHT):
  75. continue
  76. if not self.appear(self.I_PREPARE_HIGHLIGHT):
  77. break
  78. # 照顾一下某些模拟器慢的
  79. time.sleep(0.2)
  80. # 绿标
  81. if config.green_enable:
  82. x, y = None, None
  83. match config.green_mark:
  84. case GreenMarkType.GREEN_LEFT1: x, y = self.C_GREEN_LEFT_1.coord()
  85. case GreenMarkType.GREEN_LEFT2: x, y = self.C_GREEN_LEFT_2.coord()
  86. case GreenMarkType.GREEN_LEFT3: x, y = self.C_GREEN_LEFT_3.coord()
  87. case GreenMarkType.GREEN_LEFT4: x, y = self.C_GREEN_LEFT_4.coord()
  88. case GreenMarkType.GREEN_LEFT5: x, y = self.C_GREEN_LEFT_5.coord()
  89. case GreenMarkType.GREEN_MAIN: x, y = self.C_GREEN_MAIN.coord()
  90. # 判断有无坐标的偏移
  91. self.appear_then_click(self.I_LOCAL)
  92. time.sleep(0.3)
  93. # 点击绿标
  94. self.device.click(x, y)
  95. # 战斗过程 随机点击和滑动 防封
  96. win: bool = False
  97. while 1:
  98. # 如果出现赢 就点击
  99. if self.appear_then_click(self.I_WIN):
  100. win = True
  101. break
  102. # 如果出现失败 就点击,返回False
  103. if self.appear_then_click(self.I_FALSE):
  104. win = False
  105. break
  106. # 如果领奖励
  107. if self.appear_then_click(self.I_REWARD):
  108. win = True
  109. break
  110. # 如果开启战斗过程随机滑动
  111. if config.random_click_swipt_enable:
  112. if 0 <= random.randint(0, 500) <= 3: # 百分之4的概率
  113. rand_type = random.randint(0, 2)
  114. x1, y1 = None, None
  115. x2, y2 = None, None
  116. match rand_type:
  117. case 0:
  118. x1, y1 = self.C_RANDOM_CLICK.coord()
  119. self.device.click(x1, y1)
  120. case 1:
  121. x1, y1, x2, y2 = self.S_BATTLE_RANDOM_LEFT.coord()
  122. self.device.swipe((x1, y1), (x2, y2))
  123. case 2:
  124. x1, y1, x2, y2 = self.S_BATTLE_RANDOM_RIGHT.coord()
  125. self.device.swipe((x1, y1), (x2, y2))
  126. # 再次确认战斗结果
  127. while 1:
  128. if win:
  129. if self.appear_then_click(self.I_WIN):
  130. continue
  131. if not self.appear(self.I_WIN):
  132. break
  133. else:
  134. if self.appear_then_click(self.I_FALSE):
  135. continue
  136. if not self.appear(self.I_FALSE):
  137. break
  138. if win:
  139. return True
  140. else:
  141. return False