script_task.py 12 KB

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