script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. logger.info("升级八华斩")
  174. break
  175. if self.appear_then_click(self.I_BCMJ_SKILL_ADD2, interval=1):
  176. logger.info("升级无畏")
  177. break
  178. if self.appear_then_click(
  179. self.I_BCMJ_PROPERTY_ADD_CRITICAL, interval=1
  180. ):
  181. logger.info("升级暴击伤害")
  182. break
  183. if self.appear_then_click(self.I_BCMJ_BLESS, interval=1):
  184. logger.info("默认升级祝福")
  185. break
  186. if self.appear_then_click(
  187. self.I_BCMJ__DEFALUT_ATTRIBUTE, interval=1
  188. ):
  189. logger.info("默认升级属性")
  190. break
  191. if self.appear_then_click(self.I_BCMJ_SKILL_ADD_CONFIRM, interval=1):
  192. return win
  193. logger.info("Get reward")
  194. while 1:
  195. self.screenshot()
  196. # 如果出现领奖励
  197. action_click = random.choice(
  198. [self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3]
  199. )
  200. if self.appear_then_click(
  201. self.I_REWARD, action=action_click, interval=1.5
  202. ) or self.appear_then_click(
  203. self.I_REWARD_GOLD, action=action_click, interval=1.5
  204. ):
  205. continue
  206. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  207. break
  208. return win
  209. def check_art_war_card(self):
  210. self.screenshot()
  211. cu = self.O_ART_WAR_CARD.ocr(image=self.device.image)
  212. if cu[0] >= 1:
  213. logger.info("Art war card is enough")
  214. return True
  215. cu = self.O_ART_WAR_CARD_PLUS.ocr(image=self.device.image)
  216. # 转换为int
  217. if cu != "":
  218. cu = int(cu)
  219. else:
  220. cu = 0
  221. if cu >= 1:
  222. logger.info("Art war card is not enough, but plus card is enough")
  223. return True
  224. return False
  225. def home_main(self) -> bool:
  226. """
  227. 从庭院到活动的爬塔界面
  228. :return:
  229. """
  230. logger.hr("Enter HeroTest", 2)
  231. global is_update
  232. global is_skill
  233. # 启动经验加成
  234. if is_update:
  235. self.open_buff()
  236. self.exp_100(True)
  237. self.exp_50(True)
  238. self.close_buff()
  239. while 1:
  240. self.screenshot()
  241. if is_update:
  242. if self.appear(self.I_BATTLE):
  243. break
  244. if is_skill:
  245. if self.appear(self.I_BCMJ_BATTLE):
  246. break
  247. if self.appear_then_click(self.I_ONE, interval=1):
  248. continue
  249. if self.appear_then_click(self.I_TWO, interval=1):
  250. continue
  251. if is_update:
  252. if self.appear_then_click(self.I_GBB, interval=1):
  253. continue
  254. if is_skill:
  255. if self.appear_then_click(self.I_BCMJ, interval=1):
  256. continue
  257. def main_home(self) -> bool:
  258. """
  259. 从活动的爬塔界面到庭院
  260. :return:
  261. """
  262. logger.hr("Exit HeroTest", 2)
  263. global is_update
  264. while 1:
  265. self.screenshot()
  266. if self.appear(self.I_ONE):
  267. if is_update:
  268. # 关闭经验加成
  269. self.open_buff()
  270. self.exp_100(False)
  271. self.exp_50(False)
  272. self.close_buff()
  273. break
  274. if self.appear_then_click(self.I_UI_BACK_RED, interval=2):
  275. continue
  276. if self.appear_then_click(self.I_UI_BACK_YELLOW, interval=2):
  277. continue
  278. if self.appear_then_click(self.I_BACK, interval=2):
  279. continue
  280. if self.appear_then_click(self.I_GBB_BACK, interval=2):
  281. continue
  282. if __name__ == "__main__":
  283. from module.config.config import Config
  284. from module.device.device import Device
  285. c = Config("oas1")
  286. d = Device(c)
  287. t = ScriptTask(c, d)
  288. t.run()