script_task.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from datetime import datetime, timedelta, time
  5. import random # type: ignore
  6. from tasks.Component.BaseActivity.base_activity import BaseActivity
  7. from tasks.HeroTest.assets import HeroTestAssets
  8. from tasks.GameUi.page import page_main
  9. from tasks.GameUi.game_ui import GameUi
  10. from module.logger import logger
  11. from module.exception import TaskEnd
  12. class ScriptTask(GameUi, BaseActivity, HeroTestAssets):
  13. def run(self) -> None:
  14. config = self.config.hero_test
  15. self.limit_time: timedelta = config.herotest.limit_time
  16. if isinstance(self.limit_time, time):
  17. self.limit_time = timedelta(
  18. hours=self.limit_time.hour,
  19. minutes=self.limit_time.minute,
  20. seconds=self.limit_time.second,
  21. )
  22. self.limit_count = config.herotest.limit_count
  23. self.ui_get_current_page()
  24. self.ui_goto(page_main)
  25. self.home_main()
  26. # # 2024-04-04 ---------------------start
  27. # config.herotest.ap_mode = ApMode.AP_GAME
  28. # # 2024-04-04 ---------------------end
  29. # 设定是否锁定阵容
  30. if config.general_battle.lock_team_enable:
  31. logger.info("Lock team")
  32. while 1:
  33. self.screenshot()
  34. if self.appear_then_click(self.I_UNLOCK, interval=1):
  35. continue
  36. if self.appear(self.I_LOCK):
  37. break
  38. else:
  39. logger.info("Unlock team")
  40. while 1:
  41. self.screenshot()
  42. if self.appear_then_click(self.I_LOCK, interval=1):
  43. continue
  44. if self.appear(self.I_UNLOCK):
  45. break
  46. # 流程应该是 在页面处:
  47. # 1. 判定计数是否超了,时间是否超了
  48. # 2. 如果是消耗活动体力,判定活动体力是否足够 如果是消耗一般的体力,判定一般体力是否足够
  49. # 3. 如果开启买体力,就买体力
  50. # 4. 如果开启了切换到游戏体力,就切换
  51. while 1:
  52. # 1
  53. if (
  54. self.limit_time is not None
  55. and self.limit_time + self.start_time < datetime.now()
  56. ):
  57. logger.info("Time out")
  58. break
  59. if self.current_count >= self.limit_count:
  60. logger.info("Count out")
  61. break
  62. # 2
  63. self.wait_until_appear(self.I_BATTLE)
  64. # 点击战斗
  65. logger.info("Click battle")
  66. while 1:
  67. self.screenshot()
  68. if self.appear_then_click(self.I_BATTLE, interval=2):
  69. self.device.stuck_record_clear()
  70. continue
  71. if not self.appear(self.I_BATTLE):
  72. break
  73. if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1):
  74. continue
  75. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  76. continue
  77. if self.run_general_battle(config=config.general_battle):
  78. logger.info("General battle success")
  79. self.main_home()
  80. self.set_next_run(task="HeroTest", success=True)
  81. raise TaskEnd
  82. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  83. self.device.stuck_record_add("BATTLE_STATUS_S")
  84. self.device.click_record_clear()
  85. logger.info("Start battle process")
  86. win: bool = False
  87. while 1:
  88. self.screenshot()
  89. if self.appear(self.I_WIN, threshold=0.8) or self.appear(self.I_DE_WIN):
  90. logger.info("Battle result is win")
  91. if self.appear(self.I_DE_WIN):
  92. self.ui_click_until_disappear(self.I_DE_WIN)
  93. win = True
  94. break
  95. # 如果出现失败 就点击,返回False
  96. if self.appear(self.I_FALSE, threshold=0.8):
  97. logger.info("Battle result is false")
  98. win = False
  99. break
  100. # 如果领奖励
  101. if self.appear(self.I_REWARD, threshold=0.6):
  102. win = True
  103. break
  104. # 如果领奖励出现金币
  105. if self.appear(self.I_REWARD_GOLD, threshold=0.8):
  106. win = True
  107. break
  108. # 如果开启战斗过程随机滑动
  109. if random_click_swipt_enable:
  110. self.random_click_swipt()
  111. # 再次确认战斗结果
  112. logger.info("Reconfirm the results of the battle")
  113. while 1:
  114. self.screenshot()
  115. if win:
  116. # 点击赢了
  117. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  118. if self.appear_then_click(
  119. self.I_WIN, action=action_click, interval=0.5
  120. ):
  121. continue
  122. if not self.appear(self.I_WIN):
  123. break
  124. else:
  125. # 如果失败且 点击失败后
  126. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  127. continue
  128. if not self.appear(self.I_FALSE, threshold=0.6):
  129. return False
  130. # 最后保证能点击 获得奖励
  131. if not self.wait_until_appear(self.I_REWARD, wait_time=2):
  132. # 有些的战斗没有下面的奖励,所以直接返回
  133. logger.info("There is no reward, Exit battle")
  134. return win
  135. logger.info("Get reward")
  136. while 1:
  137. self.screenshot()
  138. # 如果出现领奖励
  139. action_click = random.choice(
  140. [self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3]
  141. )
  142. if self.appear_then_click(
  143. self.I_REWARD, action=action_click, interval=1.5
  144. ) or self.appear_then_click(
  145. self.I_REWARD_GOLD, action=action_click, interval=1.5
  146. ):
  147. continue
  148. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  149. break
  150. return win
  151. def home_main(self) -> bool:
  152. """
  153. 从庭院到活动的爬塔界面
  154. :return:
  155. """
  156. # 启动经验加成
  157. self.open_buff()
  158. self.exp_100(True)
  159. self.exp_50(True)
  160. self.close_buff()
  161. logger.hr("Enter HeroTest", 2)
  162. while 1:
  163. self.screenshot()
  164. if self.appear(self.I_BATTLE):
  165. logger.info("发现了战斗按钮,进入活动界面成功")
  166. break
  167. # 2024-04-04 --------------start
  168. if self.appear_then_click(self.I_ONE, interval=1):
  169. continue
  170. # 2024-04-04 --------------end
  171. if self.appear_then_click(self.I_TWO, interval=1):
  172. continue
  173. if self.appear_then_click(self.I_GBB, interval=1):
  174. continue
  175. def main_home(self) -> bool:
  176. """
  177. 从活动的爬塔界面到庭院
  178. :return:
  179. """
  180. logger.hr("Exit HeroTest", 2)
  181. while 1:
  182. self.screenshot()
  183. if self.appear(self.I_ONE):
  184. # 关闭经验加成
  185. self.open_buff()
  186. self.exp_100(False)
  187. self.exp_50(False)
  188. self.close_buff()
  189. break
  190. if self.appear_then_click(self.I_UI_BACK_RED, interval=2):
  191. continue
  192. if self.appear_then_click(self.I_UI_BACK_YELLOW, interval=2):
  193. continue
  194. if self.appear_then_click(self.I_BACK, interval=2):
  195. continue
  196. if self.appear_then_click(self.I_GBB_BACK, interval=2):
  197. continue
  198. if __name__ == "__main__":
  199. from module.config.config import Config
  200. from module.device.device import Device
  201. c = Config("oas1")
  202. d = Device(c)
  203. t = ScriptTask(c, d)
  204. t.run()