script_task.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.GeneralBattle.general_battle import GeneralBattle
  7. from tasks.HeroTest.assets import HeroTestAssets
  8. from tasks.GameUi.game_ui import GameUi
  9. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  10. from module.logger import logger
  11. from module.exception import TaskEnd
  12. from tasks.HeroTest.config import Layer, HeroTest
  13. from typing import Callable
  14. import tasks.GameUi.page as pages
  15. class ScriptTask(GameUi, GeneralBattle, HeroTestAssets, SwitchSoul):
  16. conf: HeroTest
  17. page_hero_mode: pages.Page # 当前英杰对应模式界面(经验or技能)
  18. is_skill: bool = False
  19. def run(self) -> None:
  20. self.conf = self.config.hero_test
  21. self.limit_time: timedelta = self.conf.herotest.limit_time
  22. if isinstance(self.limit_time, time):
  23. self.limit_time = timedelta(
  24. hours=self.limit_time.hour,
  25. minutes=self.limit_time.minute,
  26. seconds=self.limit_time.second,
  27. )
  28. self.limit_count = self.conf.herotest.limit_count
  29. self.check_and_switch_soul()
  30. self.open_exp_buff()
  31. self.switch_hero(self.conf.herotest.layer)
  32. self.init_pages()
  33. self.ui_goto_page(self.page_hero_mode)
  34. self.check_and_lock_team()
  35. while True:
  36. if self.limit_time is not None and self.limit_time + self.start_time < datetime.now():
  37. logger.info("Time out")
  38. break
  39. if self.current_count >= self.limit_count:
  40. logger.info("Count out")
  41. break
  42. self.ui_goto_page(self.page_hero_mode)
  43. if not self.can_run(self.conf.herotest.layer):
  44. break
  45. self.enter_battle()
  46. if self.run_general_battle(config=self.conf.general_battle):
  47. logger.info("General battle success")
  48. self.close_exp_buff()
  49. self.set_next_run(task="HeroTest", success=True)
  50. raise TaskEnd
  51. def enter_battle(self):
  52. """进入战斗"""
  53. logger.info("Click battle")
  54. while True:
  55. self.screenshot()
  56. if self.is_in_battle(False):
  57. break # 在战斗中则跳出循环
  58. if self.appear_then_click(self.I_START_CHALLENGE, interval=1): # 兵藏秘境确认挑战
  59. continue
  60. if self.appear_then_click(self.I_BCMJ_RESET_CONFIRM, interval=1): # 兵藏秘境确认重置
  61. continue
  62. if self.appear_then_click(self.O_FIRE, interval=1.6): # 挑战按钮
  63. self.device.stuck_record_clear()
  64. continue
  65. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  66. self.device.stuck_record_add("BATTLE_STATUS_S")
  67. self.device.click_record_clear()
  68. logger.info("Start battle process")
  69. win: bool = False
  70. while 1:
  71. self.screenshot()
  72. if self.appear(self.I_WIN, threshold=0.8) or self.appear(self.I_DE_WIN):
  73. logger.info("Battle result is win")
  74. if self.appear(self.I_DE_WIN):
  75. self.ui_click_until_disappear(self.I_DE_WIN)
  76. win = True
  77. break
  78. # 如果出现失败 就点击,返回False
  79. if self.appear(self.I_FALSE, threshold=0.8):
  80. logger.info("Battle result is false")
  81. win = False
  82. break
  83. # 如果领奖励
  84. if self.appear(self.I_REWARD, threshold=0.6):
  85. win = True
  86. break
  87. # 如果领奖励出现金币
  88. if self.appear(self.I_REWARD_GOLD, threshold=0.8):
  89. win = True
  90. break
  91. # 如果开启战斗过程随机滑动
  92. if random_click_swipt_enable:
  93. self.random_click_swipt()
  94. # 再次确认战斗结果
  95. logger.info("Reconfirm the results of the battle")
  96. while 1:
  97. self.screenshot()
  98. if win:
  99. # 点击赢了
  100. # self.C_WIN_2 在掉落物品过多的时候可能会点击到物品,导致脚本卡死
  101. action_click = random.choice([self.C_WIN_1, self.C_WIN_3])
  102. if self.appear_then_click(
  103. self.I_WIN, action=action_click, interval=0.5
  104. ):
  105. continue
  106. if not self.appear(self.I_WIN):
  107. break
  108. else:
  109. # 如果失败且 点击失败后
  110. if self.appear_then_click(self.I_FALSE, threshold=0.6):
  111. continue
  112. if not self.appear(self.I_FALSE, threshold=0.6):
  113. return False
  114. # 最后保证能点击 获得奖励
  115. if not self.is_skill and not self.wait_until_appear(self.I_REWARD, wait_time=2):
  116. # 有些的战斗没有下面的奖励,所以直接返回
  117. logger.info("There is no reward, Exit battle")
  118. return win
  119. else:
  120. if self.wait_until_appear(self.I_BCMJ_SKILL_ADD_CONFIRM, wait_time=2):
  121. while 1:
  122. self.screenshot()
  123. if self.appear_then_click(self.I_BCMJ_SKILL_ADD1, interval=1):
  124. break
  125. if self.appear_then_click(self.I_BCMJ_SKILL_ADD2, interval=1):
  126. break
  127. if self.appear_then_click(self.I_BCMJ_BLESS, interval=1):
  128. break
  129. if self.appear_then_click(
  130. self.I_BCMJ_PROPERTY_ADD_CRITICAL, interval=1
  131. ):
  132. break
  133. if self.appear_then_click(
  134. self.I_BCMJ__DEFALUT_ATTRIBUTE, interval=1
  135. ):
  136. break
  137. if self.appear_then_click(self.I_BCMJ_SKILL_ADD_CONFIRM, interval=1):
  138. return win
  139. logger.info("Get reward")
  140. while 1:
  141. self.screenshot()
  142. # 如果出现领奖励
  143. # self.C_REWARD_2 在掉落物品过多的时候可能会点击到物品,导致脚本卡死
  144. action_click = random.choice(
  145. [self.C_REWARD_1, self.C_REWARD_3]
  146. )
  147. if self.appear_then_click(
  148. self.I_REWARD, action=action_click, interval=1.5
  149. ) or self.appear_then_click(
  150. self.I_REWARD_GOLD, action=action_click, interval=1.5
  151. ):
  152. continue
  153. if self.appear(self.O_FIRE, interval=2):
  154. break
  155. return win
  156. def switch_hero(self, layer: Layer):
  157. """切换英杰"""
  158. self.ui_goto_page(pages.page_hero_test)
  159. switch_hero_dict: dict = {
  160. Layer.YANWU: (self.I_CHECK_HERO1, self.I_SWITCH_HERO1), # 源赖光
  161. Layer.MIJING: (self.I_CHECK_HERO1, self.I_SWITCH_HERO1), # 源赖光
  162. Layer.CHUANCHENG: (self.I_CHECK_HERO2, self.I_SWITCH_HERO2), # 藤原道长
  163. Layer.MENGXU: (self.I_CHECK_HERO2, self.I_SWITCH_HERO2), # 藤原道长
  164. }
  165. check_hero_img, switch_hero_img = switch_hero_dict[layer]
  166. while True:
  167. if self.appear(check_hero_img, interval=1):
  168. break
  169. self.ui_click(self.C_SWITCH_HERO_BTN, switch_hero_img, interval=0.5)
  170. self.appear_then_click(switch_hero_img, interval=0.5)
  171. def can_run(self, layer: Layer) -> bool:
  172. """检查是否可以运行
  173. :return: True(default): can run, False: cannot run
  174. """
  175. check_func: dict[Layer, Callable] = {
  176. Layer.MIJING: self.check_art_war_card,
  177. Layer.CHUANCHENG: self.check_level_max,
  178. }
  179. if check_func.get(layer, None) is not None:
  180. return check_func[layer]()
  181. return True
  182. def check_art_war_card(self) -> bool:
  183. """兵藏秘境 看看是否有兵道帖"""
  184. self.screenshot()
  185. cu = self.O_ART_WAR_CARD.ocr(image=self.device.image)
  186. if cu[0] >= 1:
  187. logger.info("Art war card is enough")
  188. return True
  189. cu = self.O_ART_WAR_CARD_PLUS.ocr(image=self.device.image)
  190. # 转换为int
  191. if cu != "":
  192. cu = int(cu)
  193. else:
  194. cu = 0
  195. if cu >= 1:
  196. logger.info("Art war card is not enough, but plus card is enough")
  197. return True
  198. logger.warning("Art war card is not enough")
  199. return False
  200. def check_level_max(self):
  201. """检查御灵等级是否已经满级"""
  202. self.screenshot()
  203. if self.appear(self.I_HERO_EXP_MAX):
  204. logger.info('Experience is already maxed out, exit battle')
  205. return False
  206. return True
  207. def check_and_switch_soul(self):
  208. """检查并切换御魂"""
  209. if self.conf.switch_soul_config.enable:
  210. self.ui_goto_page(pages.page_shikigami_records)
  211. self.run_switch_soul(self.conf.switch_soul_config.switch_group_team)
  212. if self.conf.switch_soul_config.enable_switch_by_name:
  213. self.ui_goto_page(pages.page_shikigami_records)
  214. self.run_switch_soul_by_name(self.conf.switch_soul_config.group_name, self.conf.switch_soul_config.team_name)
  215. def open_exp_buff(self):
  216. """启用经验加成"""
  217. exp_50_buff_enable = self.conf.herotest.exp_50_buff_enable_help
  218. exp_100_buff_enable = self.conf.herotest.exp_100_buff_enable_help
  219. if exp_50_buff_enable or exp_100_buff_enable:
  220. self.ui_goto_page(pages.page_main)
  221. self.open_buff()
  222. self.exp_100(exp_100_buff_enable)
  223. self.exp_50(exp_50_buff_enable)
  224. self.close_buff()
  225. def close_exp_buff(self):
  226. """关闭经验加成"""
  227. exp_50_buff_enable = self.conf.herotest.exp_50_buff_enable_help
  228. exp_100_buff_enable = self.conf.herotest.exp_100_buff_enable_help
  229. if exp_50_buff_enable or exp_100_buff_enable:
  230. self.ui_goto_page(pages.page_main)
  231. self.open_buff()
  232. self.exp_100(False)
  233. self.exp_50(False)
  234. self.close_buff()
  235. def check_and_lock_team(self):
  236. """检查并锁定阵容"""
  237. if self.conf.general_battle.lock_team_enable:
  238. logger.info("Lock team")
  239. self.ui_click(self.I_UNLOCK, self.I_LOCK, interval=0.8)
  240. else:
  241. logger.info("Unlock team")
  242. self.ui_click(self.I_LOCK, self.I_UNLOCK, interval=0.8)
  243. def init_pages(self):
  244. """初始化页面"""
  245. match self.conf.herotest.layer:
  246. case Layer.YANWU:
  247. self.page_hero_mode = pages.Page(self.I_CHECK_HERO1_EXP)
  248. pages.page_hero_test.link(button=self.I_GBB, destination=self.page_hero_mode)
  249. case Layer.MIJING:
  250. self.page_hero_mode = pages.Page(self.I_CHECK_HERO1_SKILL)
  251. pages.page_hero_test.link(button=self.I_BCMJ, destination=self.page_hero_mode)
  252. self.is_skill = True
  253. case Layer.CHUANCHENG:
  254. self.page_hero_mode = pages.Page(self.I_CHECK_HERO2_EXP)
  255. pages.page_hero_test.link(button=self.I_ENTER_CCSL, destination=self.page_hero_mode)
  256. case Layer.MENGXU:
  257. self.page_hero_mode = pages.Page(self.I_CHECK_HERO2_SKILL)
  258. pages.page_hero_test.link(button=self.I_ENTER_MXMJ, destination=self.page_hero_mode)
  259. self.is_skill = True
  260. case _:
  261. raise ValueError(f'Unknown Layer {Layer}')
  262. self.page_hero_mode.link(button=self.I_BACK_YOLLOW, destination=pages.page_hero_test)
  263. if __name__ == "__main__":
  264. from module.config.config import Config
  265. from module.device.device import Device
  266. c = Config("oas1")
  267. d = Device(c)
  268. t = ScriptTask(c, d)
  269. t.run()