script_task.py 11 KB

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