script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. is_update = False
  14. is_skill = False
  15. def run(self) -> None:
  16. config = self.config.hero_test
  17. global is_update
  18. global is_skill
  19. if config.herotest.layer.value == "鬼兵演武":
  20. is_update = True
  21. is_skill = False
  22. if config.herotest.layer.value == "兵藏秘境":
  23. is_skill = True
  24. is_update = False
  25. self.limit_time: timedelta = config.herotest.limit_time
  26. if isinstance(self.limit_time, time):
  27. self.limit_time = timedelta(
  28. hours=self.limit_time.hour,
  29. minutes=self.limit_time.minute,
  30. seconds=self.limit_time.second,
  31. )
  32. self.limit_count = config.herotest.limit_count
  33. self.ui_get_current_page()
  34. self.ui_goto(page_main)
  35. self.home_main()
  36. # 设定是否锁定阵容
  37. if is_update:
  38. if config.general_battle.lock_team_enable:
  39. logger.info("Lock team")
  40. while 1:
  41. self.screenshot()
  42. if self.appear_then_click(self.I_UNLOCK, interval=1):
  43. continue
  44. if self.appear(self.I_LOCK):
  45. break
  46. else:
  47. logger.info("Unlock team")
  48. while 1:
  49. self.screenshot()
  50. if self.appear_then_click(self.I_LOCK, interval=1):
  51. continue
  52. if self.appear(self.I_UNLOCK):
  53. break
  54. elif is_skill:
  55. if config.general_battle.lock_team_enable:
  56. logger.info("Lock team")
  57. while 1:
  58. self.screenshot()
  59. if self.appear_then_click(self.I_BCMJ_UNLOCK, interval=1):
  60. continue
  61. if self.appear(self.I_BCMJ_LOCK):
  62. break
  63. else:
  64. logger.info("Unlock team")
  65. while 1:
  66. self.screenshot()
  67. if self.appear_then_click(self.I_BCMJ_LOCK, interval=1):
  68. continue
  69. if self.appear(self.I_BCMJ_UNLOCK):
  70. break
  71. while 1:
  72. # 1
  73. if (
  74. self.limit_time is not None
  75. and self.limit_time + self.start_time < datetime.now()
  76. ):
  77. logger.info("Time out")
  78. break
  79. if self.current_count >= self.limit_count:
  80. logger.info("Count out")
  81. break
  82. # 2
  83. if is_update:
  84. self.wait_until_appear(self.I_BATTLE)
  85. if is_skill:
  86. self.wait_until_appear(self.I_BCMJ_BATTLE)
  87. # 如果是兵藏秘境 看看是否有兵道帖
  88. if is_skill:
  89. if not self.check_art_war_card():
  90. logger.info("Art war card is enough")
  91. break
  92. # 点击战斗
  93. logger.info("Click battle")
  94. while 1:
  95. self.screenshot()
  96. if is_update:
  97. if self.appear_then_click(self.I_BATTLE, interval=2):
  98. self.device.stuck_record_clear()
  99. continue
  100. if not self.appear(self.I_BATTLE):
  101. break
  102. elif is_skill:
  103. if self.appear_then_click(self.I_BCMJ_RESET_CONFIRM, interval=1):
  104. continue
  105. if self.appear_then_click(self.I_BCMJ_BATTLE, interval=2):
  106. self.device.stuck_record_clear()
  107. continue
  108. if not self.appear(self.I_BCMJ_BATTLE):
  109. break
  110. if self.run_general_battle(config=config.general_battle):
  111. logger.info("General battle success")
  112. self.main_home()
  113. self.set_next_run(task="HeroTest", success=True)
  114. raise TaskEnd
  115. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  116. self.device.stuck_record_add("BATTLE_STATUS_S")
  117. self.device.click_record_clear()
  118. logger.info("Start battle process")
  119. win: bool = False
  120. while 1:
  121. self.screenshot()
  122. if self.appear(self.I_WIN, threshold=0.8) or self.appear(self.I_DE_WIN):
  123. logger.info("Battle result is win")
  124. if self.appear(self.I_DE_WIN):
  125. self.ui_click_until_disappear(self.I_DE_WIN)
  126. win = True
  127. break
  128. # 如果出现失败 就点击,返回False
  129. if self.appear(self.I_FALSE, threshold=0.8):
  130. logger.info("Battle result is false")
  131. win = False
  132. break
  133. # 如果领奖励
  134. if self.appear(self.I_REWARD, threshold=0.6):
  135. win = True
  136. break
  137. # 如果领奖励出现金币
  138. if self.appear(self.I_REWARD_GOLD, threshold=0.8):
  139. win = True
  140. break
  141. # 如果开启战斗过程随机滑动
  142. if random_click_swipt_enable:
  143. self.random_click_swipt()
  144. # 再次确认战斗结果
  145. logger.info("Reconfirm the results of the battle")
  146. while 1:
  147. self.screenshot()
  148. if win:
  149. # 点击赢了
  150. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  151. if self.appear_then_click(
  152. self.I_WIN, action=action_click, interval=0.5
  153. ):
  154. continue
  155. if not self.appear(self.I_WIN):
  156. break
  157. else:
  158. # 如果失败且 点击失败后
  159. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  160. continue
  161. if not self.appear(self.I_FALSE, threshold=0.6):
  162. return False
  163. # 最后保证能点击 获得奖励
  164. if not is_skill and not self.wait_until_appear(self.I_REWARD, wait_time=2):
  165. # 有些的战斗没有下面的奖励,所以直接返回
  166. logger.info("There is no reward, Exit battle")
  167. return win
  168. else:
  169. if self.wait_until_appear(self.I_BCMJ_SKILL_ADD_CONFIRM, wait_time=2):
  170. while 1:
  171. self.screenshot()
  172. if self.appear_then_click(self.I_BCMJ_SKILL_ADD1, interval=1):
  173. break
  174. if self.appear_then_click(self.I_BCMJ_SKILL_ADD2, interval=1):
  175. break
  176. if self.appear_then_click(
  177. self.I_BCMJ_PROPERTY_ADD_CRITICAL, interval=1
  178. ):
  179. break
  180. if self.appear_then_click(self.I_BCMJ_BLESS, interval=1):
  181. break
  182. if self.appear_then_click(
  183. self.I_BCMJ__DEFALUT_ATTRIBUTE, interval=1
  184. ):
  185. break
  186. if self.appear_then_click(self.I_BCMJ_SKILL_ADD_CONFIRM, interval=1):
  187. return win
  188. logger.info("Get reward")
  189. while 1:
  190. self.screenshot()
  191. # 如果出现领奖励
  192. action_click = random.choice(
  193. [self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3]
  194. )
  195. if self.appear_then_click(
  196. self.I_REWARD, action=action_click, interval=1.5
  197. ) or self.appear_then_click(
  198. self.I_REWARD_GOLD, action=action_click, interval=1.5
  199. ):
  200. continue
  201. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  202. break
  203. return win
  204. def check_art_war_card(self):
  205. self.screenshot()
  206. cu = self.O_ART_WAR_CARD.ocr(image=self.device.image)
  207. if cu[0] >= 1:
  208. logger.info("Art war card is enough")
  209. return True
  210. cu = self.O_ART_WAR_CARD_PLUS.ocr(image=self.device.image)
  211. # 转换为int
  212. if cu != "":
  213. cu = int(cu)
  214. else:
  215. cu = 0
  216. if cu >= 1:
  217. logger.info("Art war card is not enough, but plus card is enough")
  218. return True
  219. return False
  220. def home_main(self) -> bool:
  221. """
  222. 从庭院到活动的爬塔界面
  223. :return:
  224. """
  225. logger.hr("Enter HeroTest", 2)
  226. global is_update
  227. global is_skill
  228. # 启动经验加成
  229. if is_update:
  230. self.open_buff()
  231. self.exp_100(True)
  232. self.exp_50(True)
  233. self.close_buff()
  234. while 1:
  235. self.screenshot()
  236. if is_update:
  237. if self.appear(self.I_BATTLE):
  238. break
  239. if is_skill:
  240. if self.appear(self.I_BCMJ_BATTLE):
  241. break
  242. if self.appear_then_click(self.I_ONE, interval=1):
  243. continue
  244. if self.appear_then_click(self.I_TWO, interval=1):
  245. continue
  246. if is_update:
  247. if self.appear_then_click(self.I_GBB, interval=1):
  248. continue
  249. if is_skill:
  250. if self.appear_then_click(self.I_BCMJ, interval=1):
  251. continue
  252. def main_home(self) -> bool:
  253. """
  254. 从活动的爬塔界面到庭院
  255. :return:
  256. """
  257. logger.hr("Exit HeroTest", 2)
  258. global is_update
  259. while 1:
  260. self.screenshot()
  261. if self.appear(self.I_ONE):
  262. if is_update:
  263. # 关闭经验加成
  264. self.open_buff()
  265. self.exp_100(False)
  266. self.exp_50(False)
  267. self.close_buff()
  268. break
  269. if self.appear_then_click(self.I_UI_BACK_RED, interval=2):
  270. continue
  271. if self.appear_then_click(self.I_UI_BACK_YELLOW, interval=2):
  272. continue
  273. if self.appear_then_click(self.I_BACK, interval=2):
  274. continue
  275. if self.appear_then_click(self.I_GBB_BACK, interval=2):
  276. continue
  277. if __name__ == "__main__":
  278. from module.config.config import Config
  279. from module.device.device import Device
  280. c = Config("oas1")
  281. d = Device(c)
  282. t = ScriptTask(c, d)
  283. t.run()