script_task.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. action_click = random.choice([self.C_WIN_1, self.C_WIN_2, self.C_WIN_3])
  176. if self.appear_then_click(
  177. self.I_WIN, action=action_click, interval=0.5
  178. ):
  179. continue
  180. if not self.appear(self.I_WIN):
  181. break
  182. else:
  183. # 如果失败且 点击失败后
  184. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  185. continue
  186. if not self.appear(self.I_FALSE, threshold=0.6):
  187. return False
  188. # 最后保证能点击 获得奖励
  189. if not is_skill and not self.wait_until_appear(self.I_REWARD, wait_time=2):
  190. # 有些的战斗没有下面的奖励,所以直接返回
  191. logger.info("There is no reward, Exit battle")
  192. return win
  193. else:
  194. if self.wait_until_appear(self.I_BCMJ_SKILL_ADD_CONFIRM, wait_time=2):
  195. while 1:
  196. self.screenshot()
  197. if self.appear_then_click(self.I_BCMJ_SKILL_ADD1, interval=1):
  198. break
  199. if self.appear_then_click(self.I_BCMJ_SKILL_ADD2, interval=1):
  200. break
  201. if self.appear_then_click(self.I_BCMJ_BLESS, interval=1):
  202. break
  203. if self.appear_then_click(
  204. self.I_BCMJ_PROPERTY_ADD_CRITICAL, interval=1
  205. ):
  206. break
  207. if self.appear_then_click(
  208. self.I_BCMJ__DEFALUT_ATTRIBUTE, interval=1
  209. ):
  210. break
  211. if self.appear_then_click(self.I_BCMJ_SKILL_ADD_CONFIRM, interval=1):
  212. return win
  213. logger.info("Get reward")
  214. while 1:
  215. self.screenshot()
  216. # 如果出现领奖励
  217. action_click = random.choice(
  218. [self.C_REWARD_1, self.C_REWARD_2, self.C_REWARD_3]
  219. )
  220. if self.appear_then_click(
  221. self.I_REWARD, action=action_click, interval=1.5
  222. ) or self.appear_then_click(
  223. self.I_REWARD_GOLD, action=action_click, interval=1.5
  224. ):
  225. continue
  226. if not self.appear(self.I_REWARD) and not self.appear(self.I_REWARD_GOLD):
  227. break
  228. return win
  229. def check_art_war_card(self):
  230. self.screenshot()
  231. cu = self.O_ART_WAR_CARD.ocr(image=self.device.image)
  232. if cu[0] >= 1:
  233. logger.info("Art war card is enough")
  234. return True
  235. cu = self.O_ART_WAR_CARD_PLUS.ocr(image=self.device.image)
  236. # 转换为int
  237. if cu != "":
  238. cu = int(cu)
  239. else:
  240. cu = 0
  241. if cu >= 1:
  242. logger.info("Art war card is not enough, but plus card is enough")
  243. return True
  244. return False
  245. def home_main(self) -> bool:
  246. """
  247. 从庭院到活动的爬塔界面
  248. :return:
  249. """
  250. logger.hr("Enter HeroTest", 2)
  251. global is_update
  252. global is_skill
  253. while 1:
  254. self.screenshot()
  255. if is_update:
  256. if self.appear(self.I_BATTLE):
  257. break
  258. if is_skill:
  259. if self.appear(self.I_BCMJ_BATTLE):
  260. break
  261. if self.appear_then_click(self.I_TWO, interval=1):
  262. continue
  263. if is_update:
  264. if self.appear_then_click(self.I_GBB, interval=1):
  265. continue
  266. if is_skill:
  267. if self.appear_then_click(self.I_BCMJ, interval=1):
  268. continue
  269. def main_home(self) -> bool:
  270. """
  271. 从活动的爬塔界面到庭院
  272. :return:
  273. """
  274. logger.hr("Exit HeroTest", 2)
  275. global is_update
  276. while 1:
  277. self.screenshot()
  278. if self.appear_then_click(self.I_UI_BACK_RED, interval=2):
  279. continue
  280. if self.appear_then_click(self.I_UI_BACK_YELLOW, interval=2):
  281. continue
  282. if self.appear_then_click(self.I_BACK, interval=2):
  283. continue
  284. if self.appear_then_click(self.I_GBB_BACK, interval=2):
  285. continue
  286. self.ui_get_current_page()
  287. self.ui_goto(page_main)
  288. if is_update:
  289. # 关闭经验加成
  290. self.open_buff()
  291. self.exp_100(False)
  292. self.exp_50(False)
  293. self.close_buff()
  294. break
  295. if __name__ == "__main__":
  296. from module.config.config import Config
  297. from module.device.device import Device
  298. c = Config("oas1")
  299. d = Device(c)
  300. t = ScriptTask(c, d)
  301. t.run()