script_task.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 module.atom.image import RuleImage
  7. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  8. from tasks.HeroTest.assets import HeroTestAssets
  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. from tasks.HeroTest.config import Layer, HeroTest, SkillMode
  14. from typing import Callable
  15. import tasks.GameUi.page as pages
  16. class ScriptTask(GameUi, GeneralBattle, HeroTestAssets, SwitchSoul):
  17. conf: HeroTest
  18. page_hero_mode: pages.Page # 当前英杰对应模式界面(经验or技能)
  19. success: bool = True
  20. def run(self) -> None:
  21. self.conf = self.config.hero_test
  22. self.limit_time: timedelta = self.conf.herotest.limit_time
  23. if isinstance(self.limit_time, time):
  24. self.limit_time = timedelta(
  25. hours=self.limit_time.hour,
  26. minutes=self.limit_time.minute,
  27. seconds=self.limit_time.second,
  28. )
  29. self.limit_count = self.conf.herotest.limit_count
  30. self.check_and_switch_soul()
  31. self.open_exp_buff()
  32. self.switch_hero(self.conf.herotest.layer)
  33. self.init_pages()
  34. self.ui_goto_page(self.page_hero_mode)
  35. self.check_and_lock_team()
  36. while True:
  37. if self.limit_time is not None and self.limit_time + self.start_time < datetime.now():
  38. logger.info("Time out")
  39. break
  40. if self.current_count >= self.limit_count:
  41. logger.info("Count out")
  42. break
  43. self.ui_goto_page(self.page_hero_mode)
  44. if not self.can_run(self.conf.herotest.layer):
  45. break
  46. entered = self.enter_battle()
  47. if not entered:
  48. break
  49. if self.run_general_battle(config=self.conf.general_battle):
  50. logger.info("General battle success")
  51. self.close_exp_buff()
  52. self.set_next_run(task="HeroTest", success=self.success)
  53. raise TaskEnd
  54. def enter_battle(self) -> bool:
  55. """进入战斗
  56. :return: True:进入成功 False:进入失败
  57. """
  58. logger.info("Click battle")
  59. click_cnt, max_click = 0, random.randint(3, 4)
  60. while True:
  61. self.screenshot()
  62. if self.is_in_battle(False):
  63. return True # 成功进入战斗
  64. if click_cnt >= max_click: # 异常情况,怎么都无法进入
  65. break
  66. if self.appear_then_click(self.I_START_CHALLENGE, interval=1): # 兵藏秘境确认挑战
  67. continue
  68. if self.appear_then_click(self.I_BCMJ_RESET_CONFIRM, interval=1): # 兵藏秘境确认重置
  69. continue
  70. if self.appear(self.I_REAL_MONEY, interval=1): # 这里因为门票不够而不是其他异常所以success默认还是true
  71. logger.warning('Ticket is not enough')
  72. return False
  73. if self.appear_then_click(self.O_FIRE, interval=1.2): # 挑战按钮
  74. self.device.stuck_record_clear()
  75. click_cnt += 1
  76. continue
  77. logger.error('Battle cannot enter, maybe recognize failed')
  78. self.success = False # 进入失败且不知道发生了什么情况
  79. return False
  80. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  81. self.device.stuck_record_add("BATTLE_STATUS_S")
  82. self.device.click_record_clear()
  83. logger.info("Start battle process")
  84. win = None
  85. # 处理不同模式下的结算界面
  86. mode_wait_dict: dict[Layer, Callable] = {
  87. Layer.MIJING: self.hero1_skill_wait,
  88. Layer.MENGXU: self.hero2_skill_wait,
  89. }
  90. while True:
  91. self.screenshot()
  92. if win is not None and self.appear(self.O_FIRE, interval=1.5):
  93. break
  94. if mode_wait_dict.get(self.conf.herotest.layer, None) is not None and \
  95. mode_wait_dict[self.conf.herotest.layer]():
  96. win = True
  97. continue
  98. if self.appear(self.I_WIN, interval=1.2) or \
  99. self.appear(self.I_DE_WIN, interval=1.2) or \
  100. self.appear(self.I_REWARD, interval=1.2):
  101. win = True
  102. self.click(pages.random_click(ltrb=(False, True, True, False)))
  103. continue
  104. if self.appear(self.I_FALSE, interval=1.5):
  105. win = False
  106. self.click(pages.random_click(ltrb=(False, True, True, False)))
  107. continue
  108. if win is None and random_click_swipt_enable:
  109. self.random_click_swipt()
  110. logger.info(f'Battle win = {win}')
  111. return win
  112. def hero1_skill_wait(self):
  113. if self.wait_until_appear(self.I_BCMJ_SKILL_ADD_CONFIRM, wait_time=2):
  114. while 1:
  115. self.screenshot()
  116. if self.appear_then_click(self.I_BCMJ_SKILL_ADD1, interval=1):
  117. break
  118. if self.appear_then_click(self.I_BCMJ_SKILL_ADD2, interval=1):
  119. break
  120. if self.appear_then_click(self.I_BCMJ_BLESS, interval=1):
  121. break
  122. if self.appear_then_click(
  123. self.I_BCMJ_PROPERTY_ADD_CRITICAL, interval=1
  124. ):
  125. break
  126. if self.appear_then_click(
  127. self.I_BCMJ__DEFALUT_ATTRIBUTE, interval=1
  128. ):
  129. break
  130. if self.appear_then_click(self.I_BCMJ_SKILL_ADD_CONFIRM, interval=1):
  131. return True
  132. return False
  133. def hero2_skill_wait(self):
  134. if not self.appear(self.I_BCMJ_SKILL_ADD_CONFIRM):
  135. return False
  136. # pve技能列表, 按优先级顺序
  137. pve_skill = [
  138. self.I_HERO2_SKILL1, # 同调祝福
  139. self.I_HERO2_SKILL2, # 韵迟祝福
  140. self.I_HERO2_SKILL3, # 弥天祝福
  141. self.I_HERO2_SKILL4, # 叠辉祝福
  142. self.I_HERO2_SKILL5, # 敛神祝福
  143. self.I_HERO2_SKILL6, # 速度祝福
  144. ]
  145. # TODO: PVP
  146. pvp_skill = []
  147. target_skill_dict: dict[SkillMode, list] = {
  148. SkillMode.PVE: pve_skill,
  149. SkillMode.PVP: pvp_skill,
  150. }
  151. target_skills = target_skill_dict[self.conf.herotest.skill_mode]
  152. while True:
  153. self.screenshot()
  154. if any(self.appear_then_click(ts, interval=1) for ts in target_skills):
  155. break
  156. self.ui_click_until_disappear(self.I_BCMJ_SKILL_ADD_CONFIRM, interval=1.5)
  157. return True
  158. def switch_hero(self, layer: Layer):
  159. """切换英杰"""
  160. self.ui_goto_page(pages.page_hero_test)
  161. switch_hero_dict: dict = {
  162. Layer.YANWU: (self.I_CHECK_HERO1, self.I_SWITCH_HERO1), # 源赖光
  163. Layer.MIJING: (self.I_CHECK_HERO1, self.I_SWITCH_HERO1), # 源赖光
  164. Layer.CHUANCHENG: (self.I_CHECK_HERO2, self.I_SWITCH_HERO2), # 藤原道长
  165. Layer.MENGXU: (self.I_CHECK_HERO2, self.I_SWITCH_HERO2), # 藤原道长
  166. }
  167. check_hero_img, switch_hero_img = switch_hero_dict[layer]
  168. while True:
  169. self.screenshot()
  170. if self.appear_then_click(switch_hero_img, interval=1):
  171. continue
  172. appeared = self.appear(check_hero_img, interval=1)
  173. if not appeared and self.appear(self.I_CHECK_HERO_TEST):
  174. self.click(self.C_SWITCH_HERO_BTN, interval=1.5)
  175. continue
  176. if appeared:
  177. break
  178. def can_run(self, layer: Layer) -> bool:
  179. """检查是否可以运行
  180. :return: True(default): can run, False: cannot run
  181. """
  182. check_func: dict[Layer, Callable] = {
  183. Layer.MIJING: self.check_art_war_card,
  184. Layer.CHUANCHENG: self.check_level_max,
  185. Layer.MENGXU: self.check_art_war_card,
  186. }
  187. if check_func.get(layer, None) is not None:
  188. return check_func[layer]()
  189. return True
  190. def check_art_war_card(self) -> bool:
  191. """兵藏秘境/梦虚秘境 看看门票是否足够"""
  192. self.screenshot()
  193. cu = self.O_ART_WAR_CARD.ocr(image=self.device.image)
  194. if cu[0] >= 1:
  195. logger.info("Art war card is enough")
  196. return True
  197. cu = self.O_ART_WAR_CARD_PLUS.ocr(image=self.device.image)
  198. cu = 0 if cu == '' else int(cu)
  199. if cu >= 1:
  200. logger.info("Art war card is not enough, but plus card is enough")
  201. return True
  202. logger.warning("Art war card is not enough")
  203. return False
  204. def check_level_max(self):
  205. """检查御灵等级是否已经满级"""
  206. self.screenshot()
  207. if self.appear(self.I_HERO_EXP_MAX):
  208. logger.info('Experience is already maxed out, exit battle')
  209. return False
  210. return True
  211. def check_and_switch_soul(self):
  212. """检查并切换御魂"""
  213. if self.conf.switch_soul_config.enable:
  214. self.ui_goto_page(pages.page_shikigami_records)
  215. self.run_switch_soul(self.conf.switch_soul_config.switch_group_team)
  216. if self.conf.switch_soul_config.enable_switch_by_name:
  217. self.ui_goto_page(pages.page_shikigami_records)
  218. self.run_switch_soul_by_name(self.conf.switch_soul_config.group_name, self.conf.switch_soul_config.team_name)
  219. def open_exp_buff(self):
  220. """启用经验加成"""
  221. exp_50_buff_enable = self.conf.herotest.exp_50_buff_enable_help
  222. exp_100_buff_enable = self.conf.herotest.exp_100_buff_enable_help
  223. if exp_50_buff_enable or exp_100_buff_enable:
  224. self.ui_goto_page(pages.page_main)
  225. self.open_buff()
  226. self.exp_100(exp_100_buff_enable)
  227. self.exp_50(exp_50_buff_enable)
  228. self.close_buff()
  229. def close_exp_buff(self):
  230. """关闭经验加成"""
  231. exp_50_buff_enable = self.conf.herotest.exp_50_buff_enable_help
  232. exp_100_buff_enable = self.conf.herotest.exp_100_buff_enable_help
  233. if exp_50_buff_enable or exp_100_buff_enable:
  234. self.ui_goto_page(pages.page_main)
  235. self.open_buff()
  236. self.exp_100(False)
  237. self.exp_50(False)
  238. self.close_buff()
  239. def check_and_lock_team(self):
  240. """检查并锁定阵容"""
  241. lock_img = self.I_LOCK
  242. unlock_img = self.I_UNLOCK
  243. match self.conf.herotest.layer:
  244. case Layer.YANWU | Layer.CHUANCHENG:
  245. pass
  246. case Layer.MIJING | Layer.MENGXU:
  247. lock_img = self.I_BCMJ_LOCK
  248. unlock_img = self.I_BCMJ_UNLOCK
  249. case _:
  250. raise ValueError(f'Unknown layer on lock: {self.conf.herotest.layer}')
  251. if self.conf.general_battle.lock_team_enable:
  252. logger.info("Lock team")
  253. self.ui_click(unlock_img, lock_img, interval=0.8)
  254. else:
  255. logger.info("Unlock team")
  256. self.ui_click(lock_img, unlock_img, interval=0.8)
  257. def init_pages(self):
  258. """初始化页面"""
  259. match self.conf.herotest.layer:
  260. case Layer.YANWU:
  261. self.page_hero_mode = pages.Page(self.I_CHECK_HERO1_EXP)
  262. pages.page_hero_test.link(button=self.I_GBB, destination=self.page_hero_mode)
  263. case Layer.MIJING:
  264. self.page_hero_mode = pages.Page(self.I_CHECK_HERO1_SKILL)
  265. pages.page_hero_test.link(button=self.I_BCMJ, destination=self.page_hero_mode)
  266. case Layer.CHUANCHENG:
  267. self.page_hero_mode = pages.Page(self.I_CHECK_HERO2_EXP)
  268. pages.page_hero_test.link(button=self.I_ENTER_CCSL, destination=self.page_hero_mode)
  269. case Layer.MENGXU:
  270. self.page_hero_mode = pages.Page(self.I_CHECK_HERO2_SKILL)
  271. pages.page_hero_test.link(button=self.I_ENTER_MXMJ, destination=self.page_hero_mode)
  272. case _:
  273. raise ValueError(f'Unknown Layer {Layer}')
  274. self.page_hero_mode.link(button=self.I_BACK_YOLLOW, destination=pages.page_hero_test)
  275. if __name__ == "__main__":
  276. from module.config.config import Config
  277. from module.device.device import Device
  278. c = Config("oas1")
  279. d = Device(c)
  280. t = ScriptTask(c, d)
  281. t.check_and_lock_team()