script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_BATTLE, interval=2):
  104. self.device.stuck_record_clear()
  105. continue
  106. if not self.appear(self.I_BCMJ_BATTLE):
  107. break
  108. if self.run_general_battle(config=config.general_battle):
  109. logger.info("General battle success")
  110. self.main_home()
  111. self.set_next_run(task="HeroTest", success=True)
  112. raise TaskEnd
  113. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  114. self.device.stuck_record_add("BATTLE_STATUS_S")
  115. self.device.click_record_clear()
  116. logger.info("Start battle process")
  117. win: bool = False
  118. while 1:
  119. self.screenshot()
  120. if self.appear(self.I_WIN, threshold=0.8) or self.appear(self.I_DE_WIN):
  121. logger.info("Battle result is win")
  122. if self.appear(self.I_DE_WIN):
  123. self.ui_click_until_disappear(self.I_DE_WIN)
  124. win = True
  125. break
  126. # 如果出现失败 就点击,返回False
  127. if self.appear(self.I_FALSE, threshold=0.8):
  128. logger.info("Battle result is false")
  129. win = False
  130. break
  131. # 如果领奖励
  132. if self.appear(self.I_REWARD, threshold=0.6):
  133. win = True
  134. break
  135. # 如果领奖励出现金币
  136. if self.appear(self.I_REWARD_GOLD, threshold=0.8):
  137. win = True
  138. break
  139. # 如果开启战斗过程随机滑动
  140. if random_click_swipt_enable:
  141. self.random_click_swipt()
  142. # 再次确认战斗结果
  143. logger.info("Reconfirm the results of the battle")
  144. while 1:
  145. self.screenshot()
  146. if win:
  147. # 点击赢了
  148. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  149. if self.appear_then_click(
  150. self.I_WIN, action=action_click, interval=0.5
  151. ):
  152. continue
  153. if not self.appear(self.I_WIN):
  154. break
  155. else:
  156. # 如果失败且 点击失败后
  157. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  158. continue
  159. if not self.appear(self.I_FALSE, threshold=0.6):
  160. return False
  161. # 最后保证能点击 获得奖励
  162. if not self.wait_until_appear(self.I_REWARD, wait_time=2) and not is_skill:
  163. # 有些的战斗没有下面的奖励,所以直接返回
  164. logger.info("There is no reward, Exit battle")
  165. return win
  166. else:
  167. if self.wait_until_appear(self.I_BCMJ_SKILL_ADD_CONFIRM, wait_time=2):
  168. while 1:
  169. self.screenshot()
  170. if self.appear_then_click(self.I_BCMJ_SKILL_ADD1, interval=1):
  171. logger.info("升级八华斩")
  172. break
  173. if self.appear_then_click(self.I_BCMJ_SKILL_ADD2, interval=1):
  174. logger.info("升级无畏")
  175. break
  176. if self.appear_then_click(
  177. self.I_BCMJ_PROPERTY_ADD_CRITICAL, interval=1
  178. ):
  179. logger.info("升级暴击伤害")
  180. break
  181. if self.appear_then_click(self.I_BCMJ_BLESS, interval=1):
  182. logger.info("默认升级祝福")
  183. break
  184. if self.appear_then_click(
  185. self.I_BCMJ__DEFALUT_ATTRIBUTE, interval=1
  186. ):
  187. logger.info("默认升级属性")
  188. break
  189. if self.appear_then_click(self.I_BCMJ_SKILL_ADD_CONFIRM, interval=1):
  190. return win
  191. logger.info("Get reward")
  192. while 1:
  193. self.screenshot()
  194. # 如果出现领奖励
  195. action_click = random.choice(
  196. [self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3]
  197. )
  198. if self.appear_then_click(
  199. self.I_REWARD, action=action_click, interval=1.5
  200. ) or self.appear_then_click(
  201. self.I_REWARD_GOLD, action=action_click, interval=1.5
  202. ):
  203. continue
  204. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  205. break
  206. return win
  207. def check_art_war_card(self):
  208. self.screenshot()
  209. cu, res, total = self.O_ART_WAR_CARD.ocr(image=self.device.image)
  210. if cu >= 1:
  211. logger.info("Art war card is enough")
  212. return True
  213. cu, res, total = self.O_ART_WAR_CARD_PLUS.ocr(image=self.device.image)
  214. if cu >= 1:
  215. logger.info("Art war card is not enough, but plus card is enough")
  216. return True
  217. return False
  218. def home_main(self) -> bool:
  219. """
  220. 从庭院到活动的爬塔界面
  221. :return:
  222. """
  223. logger.hr("Enter HeroTest", 2)
  224. global is_update
  225. global is_skill
  226. # 启动经验加成
  227. if is_update:
  228. self.open_buff()
  229. self.exp_100(True)
  230. self.exp_50(True)
  231. self.close_buff()
  232. while 1:
  233. self.screenshot()
  234. if is_update:
  235. if self.appear(self.I_BATTLE):
  236. break
  237. if is_skill:
  238. if self.appear(self.I_BCMJ_BATTLE):
  239. break
  240. if self.appear_then_click(self.I_ONE, interval=1):
  241. continue
  242. if self.appear_then_click(self.I_TWO, interval=1):
  243. continue
  244. if is_update:
  245. if self.appear_then_click(self.I_GBB, interval=1):
  246. continue
  247. if is_skill:
  248. if self.appear_then_click(self.I_BCMJ, interval=1):
  249. continue
  250. def main_home(self) -> bool:
  251. """
  252. 从活动的爬塔界面到庭院
  253. :return:
  254. """
  255. logger.hr("Exit HeroTest", 2)
  256. global is_update
  257. while 1:
  258. self.screenshot()
  259. if self.appear(self.I_ONE):
  260. if is_update:
  261. # 关闭经验加成
  262. self.open_buff()
  263. self.exp_100(False)
  264. self.exp_50(False)
  265. self.close_buff()
  266. break
  267. if self.appear_then_click(self.I_UI_BACK_RED, interval=2):
  268. continue
  269. if self.appear_then_click(self.I_UI_BACK_YELLOW, interval=2):
  270. continue
  271. if self.appear_then_click(self.I_BACK, interval=2):
  272. continue
  273. if self.appear_then_click(self.I_GBB_BACK, interval=2):
  274. continue
  275. if __name__ == "__main__":
  276. from module.config.config import Config
  277. from module.device.device import Device
  278. c = Config("oas1")
  279. d = Device(c)
  280. t = ScriptTask(c, d)
  281. t.run()