script_task.py 12 KB

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