script_task.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from cached_property import cached_property
  5. from datetime import datetime
  6. from time import sleep
  7. from module.base.timer import Timer
  8. from module.logger import logger
  9. from module.exception import TaskEnd
  10. from tasks.Component.RightActivity.right_activity import RightActivity
  11. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  12. from tasks.Component.GeneralBattle.battle import Battle
  13. from tasks.Component.GeneralBattle.battle_wait import (
  14. BattleResult, HookSignal, PerTaskPreset, PrivateContext, PublicContext,
  15. battle_wait_strategy, battle_wait_options, runtime,
  16. )
  17. from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig
  18. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  19. from tasks.GameUi.game_ui import GameUi
  20. from tasks.GameUi.page import page_main, page_shikigami_records
  21. from tasks.MetaDemon.config import MetaDemon, DefaultStrategy, Strategy
  22. from tasks.MetaDemon.assets import MetaDemonAssets
  23. from tasks.Restart.assets import RestartAssets
  24. from tasks.ActivityShikigami.assets import ActivityShikigamiAssets
  25. class Star56(GameUi, MetaDemonAssets):
  26. @cached_property
  27. def cached_strategies(self) -> dict[str, list[int]]:
  28. return Strategy.parse_all(self.config.meta_demon.md_strategies)
  29. @cached_property
  30. def cached_default_group_team(self) -> list[int]:
  31. return Strategy.parse_group_team(self.config.meta_demon.md_default_strategy.md_preset_group_team_default_1) \
  32. + Strategy.parse_group_team(self.config.meta_demon.md_default_strategy.md_preset_group_team_default_2)
  33. def is_star56(self):
  34. if not self.appear(self.I_STAR56_POS_1):
  35. return True
  36. return False
  37. def hp_threshold_low(self, image):
  38. """
  39. @param image:
  40. @return: 血量小于阈值返回True
  41. """
  42. return self.appear(self.I_STAR56_HP_THRESHOLD_F) and not self.appear(self.I_STAR56_HP_THRESHOLD_T)
  43. @classmethod
  44. def strategy2group_team(cls, strategies: dict[str, list[int]], detect_name: str) -> list[int] | None:
  45. """
  46. 先是默认的完全匹配,然后是1个字符不同,最后是2个字符不同
  47. @param strategies:
  48. @param detect_name:
  49. @return:
  50. """
  51. for i in range(2):
  52. for key_name in strategies:
  53. if len(key_name) != len(detect_name):
  54. continue
  55. diff_count = sum(1 for c1, c2 in zip(key_name, detect_name) if c1 != c2)
  56. if diff_count <= i < len(detect_name):
  57. return strategies[key_name]
  58. return None
  59. def get_group_team(self, detect_name: str) -> list[int]:
  60. tmp = self.strategy2group_team(self.cached_strategies, detect_name)
  61. return tmp if tmp else self.cached_default_group_team
  62. class ScriptTask(RightActivity, Battle, GeneralBattle, SwitchSoul, Star56):
  63. def run(self):
  64. if self.config.meta_demon.switch_soul.enable:
  65. self.goto_page(page_shikigami_records)
  66. self.run_switch_soul(self.config.meta_demon.switch_soul.switch_group_team)
  67. if self.config.meta_demon.switch_soul.enable_switch_by_name:
  68. self.goto_page(page_shikigami_records)
  69. self.run_switch_soul_by_name(self.config.meta_demon.switch_soul.group_name,
  70. self.config.meta_demon.switch_soul.team_name)
  71. self.enter(self.I_RIGHT_ENTER)
  72. while 1:
  73. self.screenshot()
  74. if self.appear(self.I_FIND_DEMON) or self.appear(self.I_BATTLE_DEMON):
  75. break
  76. if self.ui_reward_appear_click():
  77. continue
  78. if self.appear_then_click(self.I_UI_BACK_RED, interval=1.5):
  79. continue
  80. if self.appear_then_click(self.I_ENTER_ENSURE, interval=1):
  81. continue
  82. if self.appear_then_click(self.I_ENTER2, interval=1):
  83. continue
  84. if self.config.meta_demon.meta_demon_config.meta_crafting_card:
  85. self.crafting()
  86. boss_timer = Timer(50)
  87. boss_timer.start()
  88. first_battle_for_easy_boss = True
  89. while 1:
  90. battle_processing = False
  91. is_hard_boss = False # 五星或者六星
  92. self.screenshot()
  93. if not (self.appear(self.I_FIND_DEMON) or self.appear(self.I_BATTLE_DEMON)):
  94. continue
  95. # 疲劳达到阈值时优先召唤鬼王(开关控制,判定先于原有逻辑)
  96. if self.config.meta_demon.meta_demon_config.md_fatigue_summon_first:
  97. fatigue = self.current_exhaustion()
  98. if 61 <= fatigue <= 100 and self.appear(self.I_BOSS_EMPTY):
  99. logger.info('Fatigue >= 61, summon demon first')
  100. if self.find_demon():
  101. boss_timer.reset()
  102. continue
  103. # 先看自己有没有鬼王要打
  104. if (not self.appear(self.I_BOSS_EMPTY) and
  105. (self.appear(self.I_BOSS_CONVENE) or self.appear(self.I_BOOS_PUBLIC))):
  106. logger.info('Try to battle boss from self')
  107. self.click(self.I_BOSS_EMPTY, interval=1.5)
  108. battle_processing = True
  109. if self.is_star56():
  110. is_hard_boss = True
  111. # 看别人的是否给我共享鬼王
  112. if not battle_processing and self.config.meta_demon.meta_demon_config.auto_shared_boss:
  113. if not self.appear(self.I_BOSS_EMPTY_1) and self.ocr_appear(self.O_BOSS_EMPTY_1):
  114. logger.info('Try to battle boss 1 from others')
  115. self.click(self.I_BOSS_EMPTY_1, interval=1.5)
  116. self.ui_click(self.I_BOSS_EMPTY_1, stop=self.I_BATTLE_DEMON, interval=1.5)
  117. battle_processing = True
  118. is_hard_boss = True
  119. elif not self.appear(self.I_BOSS_EMPTY_2):
  120. logger.info('Try to battle boss 2 from others')
  121. self.click(self.I_BOSS_EMPTY_2, interval=1.5)
  122. self.ui_click(self.I_BOSS_EMPTY_1, stop=self.I_BATTLE_DEMON, interval=1.5)
  123. battle_processing = True
  124. is_hard_boss = True
  125. # 是否喝茶 疲劳满了就不打了
  126. current_exhaustion = self.current_exhaustion()
  127. if current_exhaustion > 100:
  128. if self.config.meta_demon.meta_demon_config.auto_tea:
  129. logger.info('Exhaustion is full, buy tea')
  130. boss_timer.reset()
  131. self.buy_tea()
  132. else:
  133. logger.info('Exhaustion is full, exit')
  134. break
  135. # 超时结束
  136. if boss_timer.reached():
  137. logger.info('Time out, exit')
  138. break
  139. # 5/6星特殊处理 + 使用自定义策略组
  140. target_group_team = [None, None]
  141. if is_hard_boss:
  142. logger.info('Hard boss')
  143. if self.config.meta_demon.meta_demon_config.md_use_strategy:
  144. detect_full_name = self.O_MD_FULL_NAME.ocr_single(self.device.image)
  145. logger.info(f'detect_full_name: {detect_full_name}')
  146. if not detect_full_name:
  147. detect_full_name = ''
  148. group_team = self.get_group_team(detect_full_name)
  149. if self.hp_threshold_low(self.device.image):
  150. logger.info('Hp threshold low')
  151. target_group_team = group_team[2:4]
  152. else:
  153. logger.info('Hp threshold high')
  154. target_group_team = group_team[0:2]
  155. else:
  156. if self.config.meta_demon.meta_demon_config.md_use_strategy and first_battle_for_easy_boss:
  157. logger.info('Normal boss first battle use default group team')
  158. target_group_team = self.cached_default_group_team
  159. if battle_processing and self.appear(self.I_BATTLE_DEMON):
  160. logger.info(f'preset group team: {target_group_team}')
  161. self.battle_boss(target_group_team[0], target_group_team[1])
  162. if first_battle_for_easy_boss:
  163. first_battle_for_easy_boss = False
  164. if is_hard_boss:
  165. first_battle_for_easy_boss = True
  166. boss_timer.reset()
  167. continue
  168. # 自己召唤鬼王
  169. if current_exhaustion < 90 and self.find_demon():
  170. boss_timer.reset()
  171. continue
  172. # 退出
  173. self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MAIN, interval=1.4)
  174. logger.info('Exit MetaDemon')
  175. self.set_next_run(task="MetaDemon", success=True)
  176. raise TaskEnd
  177. def current_exhaustion(self) -> int:
  178. self.screenshot()
  179. cu, res, total = self.O_MD_EXHAUSTION.ocr(self.device.image)
  180. return int(cu)
  181. def buy_tea(self):
  182. pass
  183. def crafting(self):
  184. """
  185. 把一星合成二星
  186. @return:
  187. """
  188. logger.info('Star crafting 1 star to 2 star')
  189. self.ui_click(self.I_CRAFTING_1, self.I_CRAFTING_START, interval=1.5)
  190. timer = Timer(3).start()
  191. count = 0
  192. while 1:
  193. self.screenshot()
  194. if self.ui_reward_appear_click():
  195. timer.reset()
  196. continue
  197. if timer.reached() or count > 3:
  198. break
  199. if not self.ocr_appear(self.O_CRAFTING_CARD_1):
  200. break
  201. if self.appear(self.I_CRAFTING_EMPTY) or self.appear(self.I_CRAFTING_EMPTY_NEW):
  202. if self.appear_then_click(self.I_CRAFTING_CARD_STAR_1, interval=1):
  203. continue
  204. else:
  205. self.appear_then_click(self.I_CRAFTING_START, interval=3)
  206. count += 1
  207. timer.reset()
  208. continue
  209. logger.info('Finish star crafting')
  210. self.ui_click_until_disappear(self.I_UI_BACK_RED, interval=1)
  211. sleep(0.5)
  212. def find_demon(self) -> bool:
  213. """
  214. 召唤鬼王
  215. @return:
  216. """
  217. logger.info('Find demon')
  218. while 1:
  219. self.screenshot()
  220. if self.appear(self.I_BATTLE_DEMON) and not self.appear(self.I_BOSS_EMPTY):
  221. break
  222. if self.appear_then_click(self.I_FIND_DEMON, interval=3.5):
  223. continue
  224. self.screenshot()
  225. if self.is_star56():
  226. logger.info('Star 5 or 6 demon found')
  227. while 1:
  228. sleep(0.4)
  229. self.screenshot()
  230. detect_full_name = self.O_MD_FULL_NAME.ocr_single(self.device.image)
  231. if detect_full_name:
  232. break
  233. logger.info(f'召唤到高星鬼王: {detect_full_name}')
  234. self.config.notifier.push(content=f'召唤到高星鬼王: {detect_full_name}', title='超鬼王')
  235. return True
  236. def battle_boss(self, group: int = None, team: int = None) -> bool:
  237. """
  238. 战斗流程不再自己写 while 循环,全部交给 battle_wait 的策略化钩子:
  239. prepare/preset 准备阶段与预设队伍(5/6 星按推荐阵容切队)
  240. success/failure 超鬼王自带的结算界面(I_MD_WIN / I_MD_BATTLE_FAILURE)
  241. idle 战斗中给鬼王上标记
  242. @param group: 预设组,与 team 同时为 None 时不切预设队伍
  243. @param team: 预设队伍,与 group 同时为 None 时不切预设队伍
  244. @return: True 战斗胜利,False 战斗失败
  245. """
  246. logger.info('Battle boss')
  247. self.ui_click(self.I_TEAM_LOCK, self.I_TEAM_UNLOCK, interval=1.5, timeout=5)
  248. self.ui_click_until_disappear(self.I_BATTLE_DEMON, interval=1.8)
  249. # 任务级战斗计数:本场是第几场(和旧日志 Current count 对齐)
  250. count = runtime.pub_ctx.per_task.count if runtime.pub_ctx else 0
  251. logger.info(f'Current count: {count + 1}')
  252. battle_config = GeneralBattleConfig()
  253. battle_config.lock_team_enable = False
  254. # 针对5/6星的挑战
  255. if group is not None and team is not None:
  256. battle_config.preset_enable = True
  257. battle_config.preset_group = group
  258. battle_config.preset_team = team
  259. # 每只5/6星鬼王的推荐阵容不同,而预设队伍默认是按任务只切一次的,
  260. # 这里只复位 preset 的任务级状态,per_task.count 保持累加
  261. PerTaskPreset.reset_preset_state(self)
  262. strategies, options = self.loadout_from_config(battle_config)
  263. strategies['success'] = 'metademon'
  264. strategies['failure'] = 'metademon'
  265. strategies['idle'] = 'metademon'
  266. options['completion'].check_imgs = [self.I_CRAFTING_1, self.I_FIND_DEMON, self.I_BATTLE_DEMON]
  267. self.loadout_show((strategies, options))
  268. self.boss_mark_reset()
  269. with battle_wait_strategy(**strategies), battle_wait_options(**options):
  270. win = self.battle_wait()
  271. logger.info(f'Battle boss win: {win}, current count: {runtime.pub_ctx.per_task.count}')
  272. return win
  273. # ------------------------------------------------------------------------------------------------------------------
  274. # battle_wait 钩子:超鬼王自己的结算界面与战中行为
  275. # ------------------------------------------------------------------------------------------------------------------
  276. def _bw_success_metademon(self, pub: PublicContext, pri: PrivateContext) -> HookSignal:
  277. if not self.appear(self.I_MD_WIN):
  278. return HookSignal.CONTINUE
  279. logger.info('Win battle')
  280. while 1:
  281. self.screenshot()
  282. if not self.appear(self.I_MD_WIN):
  283. break
  284. self.click(self.I_MD_WIN)
  285. sleep(0.5)
  286. pub.per_battle.success = BattleResult.SUCCESS
  287. return runtime.hook_enabled_update(
  288. enable=('completion',),
  289. disable=('success', 'failure'),
  290. )
  291. def _bw_failure_metademon(self, pub: PublicContext, pri: PrivateContext) -> HookSignal:
  292. if not self.appear(self.I_MD_BATTLE_FAILURE):
  293. return HookSignal.CONTINUE
  294. logger.warning('False battle')
  295. self.ui_click_until_disappear(self.I_MD_BATTLE_FAILURE)
  296. pub.per_battle.success = BattleResult.FAILURE
  297. return runtime.hook_enabled_update(
  298. enable=('completion',),
  299. disable=('success', 'failure'),
  300. )
  301. def _bw_idle_metademon(self, pub: PublicContext, pri: PrivateContext) -> HookSignal:
  302. if self.is_in_real_battle(False):
  303. self.boss_mark()
  304. return HookSignal.CONTINUE
  305. if __name__ == '__main__':
  306. from module.config.config import Config
  307. from module.device.device import Device
  308. c = Config('oas1')
  309. d = Device(c)
  310. t = ScriptTask(c, d)
  311. t.run()