script_task.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 GameStuckError, 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 (not self.appear(self.I_BOSS_EMPTY) and
  97. (self.appear(self.I_BOSS_CONVENE) or self.appear(self.I_BOOS_PUBLIC))):
  98. logger.info('Try to battle boss from self')
  99. self.click(self.I_BOSS_EMPTY, interval=1.5)
  100. battle_processing = True
  101. if self.is_star56():
  102. is_hard_boss = True
  103. # 看别人的是否给我共享鬼王
  104. if not battle_processing and self.config.meta_demon.meta_demon_config.auto_shared_boss:
  105. if not self.appear(self.I_BOSS_EMPTY_1) and self.ocr_appear(self.O_BOSS_EMPTY_1):
  106. logger.info('Try to battle boss 1 from others')
  107. self.click(self.I_BOSS_EMPTY_1, interval=1.5)
  108. self.ui_click(self.I_BOSS_EMPTY_1, stop=self.I_BATTLE_DEMON, interval=1.5)
  109. battle_processing = True
  110. is_hard_boss = True
  111. elif not self.appear(self.I_BOSS_EMPTY_2):
  112. logger.info('Try to battle boss 2 from others')
  113. self.click(self.I_BOSS_EMPTY_2, interval=1.5)
  114. self.ui_click(self.I_BOSS_EMPTY_1, stop=self.I_BATTLE_DEMON, interval=1.5)
  115. battle_processing = True
  116. is_hard_boss = True
  117. # 是否喝茶 疲劳满了就不打了
  118. current_exhaustion = self.current_exhaustion()
  119. if current_exhaustion > 100:
  120. if self.config.meta_demon.meta_demon_config.auto_tea:
  121. logger.info('Exhaustion is full, buy tea')
  122. boss_timer.reset()
  123. self.buy_tea()
  124. else:
  125. logger.info('Exhaustion is full, exit')
  126. break
  127. # 超时结束
  128. if boss_timer.reached():
  129. logger.info('Time out, exit')
  130. break
  131. # 5/6星特殊处理 + 使用自定义策略组
  132. target_group_team = [None, None]
  133. if is_hard_boss:
  134. logger.info('Hard boss')
  135. if self.config.meta_demon.meta_demon_config.md_use_strategy:
  136. detect_full_name = self.O_MD_FULL_NAME.ocr_single(self.device.image)
  137. logger.info(f'detect_full_name: {detect_full_name}')
  138. if not detect_full_name:
  139. detect_full_name = ''
  140. group_team = self.get_group_team(detect_full_name)
  141. if self.hp_threshold_low(self.device.image):
  142. logger.info('Hp threshold low')
  143. target_group_team = group_team[2:4]
  144. else:
  145. logger.info('Hp threshold high')
  146. target_group_team = group_team[0:2]
  147. else:
  148. if self.config.meta_demon.meta_demon_config.md_use_strategy and first_battle_for_easy_boss:
  149. logger.info('Normal boss first battle use default group team')
  150. target_group_team = self.cached_default_group_team
  151. if battle_processing and self.appear(self.I_BATTLE_DEMON):
  152. logger.info(f'preset group team: {target_group_team}')
  153. self.battle_boss(target_group_team[0], target_group_team[1])
  154. if first_battle_for_easy_boss:
  155. first_battle_for_easy_boss = False
  156. if is_hard_boss:
  157. first_battle_for_easy_boss = True
  158. boss_timer.reset()
  159. continue
  160. # 自己召唤鬼王
  161. if current_exhaustion < 90 and self.find_demon():
  162. boss_timer.reset()
  163. continue
  164. # 退出
  165. self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MAIN, interval=1.4)
  166. logger.info('Exit MetaDemon')
  167. self.set_next_run(task="MetaDemon", success=True)
  168. raise TaskEnd
  169. def current_exhaustion(self) -> int:
  170. self.screenshot()
  171. cu, res, total = self.O_MD_EXHAUSTION.ocr(self.device.image)
  172. return int(cu)
  173. def buy_tea(self):
  174. pass
  175. def crafting(self):
  176. """
  177. 把一星合成二星
  178. @return:
  179. """
  180. logger.info('Star crafting 1 star to 2 star')
  181. self.ui_click(self.I_CRAFTING_1, self.I_CRAFTING_START, interval=1.5)
  182. timer = Timer(3).start()
  183. count = 0
  184. while 1:
  185. self.screenshot()
  186. if self.ui_reward_appear_click():
  187. timer.reset()
  188. continue
  189. if timer.reached() or count > 3:
  190. break
  191. if not self.ocr_appear(self.O_CRAFTING_CARD_1):
  192. break
  193. if self.appear(self.I_CRAFTING_EMPTY) or self.appear(self.I_CRAFTING_EMPTY_NEW):
  194. if self.appear_then_click(self.I_CRAFTING_CARD_STAR_1, interval=1):
  195. continue
  196. else:
  197. self.appear_then_click(self.I_CRAFTING_START, interval=3)
  198. count += 1
  199. timer.reset()
  200. continue
  201. logger.info('Finish star crafting')
  202. self.ui_click_until_disappear(self.I_UI_BACK_RED, interval=1)
  203. sleep(0.5)
  204. def find_demon(self) -> bool:
  205. """
  206. 召唤鬼王
  207. @return:
  208. """
  209. logger.info('Find demon')
  210. while 1:
  211. self.screenshot()
  212. if self.appear(self.I_BATTLE_DEMON) and not self.appear(self.I_BOSS_EMPTY):
  213. break
  214. if self.appear_then_click(self.I_FIND_DEMON, interval=3.5):
  215. continue
  216. self.screenshot()
  217. if self.is_star56():
  218. logger.info('Star 5 or 6 demon found')
  219. while 1:
  220. sleep(0.4)
  221. self.screenshot()
  222. detect_full_name = self.O_MD_FULL_NAME.ocr_single(self.device.image)
  223. if detect_full_name:
  224. break
  225. logger.info(f'召唤到高星鬼王: {detect_full_name}')
  226. self.config.notifier.push(content=f'召唤到高星鬼王: {detect_full_name}', title='超鬼王')
  227. return True
  228. def battle_boss(self, group: int = None, team: int = None) -> bool:
  229. """
  230. 战斗流程不再自己写 while 循环,全部交给 battle_wait 的策略化钩子:
  231. prepare/preset 准备阶段与预设队伍(5/6 星按推荐阵容切队)
  232. success/failure 超鬼王自带的结算界面(I_MD_WIN / I_MD_BATTLE_FAILURE)
  233. idle 战斗中给鬼王上标记
  234. @param group: 预设组,与 team 同时为 None 时不切预设队伍
  235. @param team: 预设队伍,与 group 同时为 None 时不切预设队伍
  236. @return: True 战斗胜利,False 战斗失败
  237. """
  238. logger.info('Battle boss')
  239. if not self.ui_click(self.I_TEAM_LOCK, self.I_TEAM_UNLOCK, interval=1.5, timeout=5):
  240. raise GameStuckError("MetaDemon team unlock timed out")
  241. self.ui_click_until_disappear(self.I_BATTLE_DEMON, interval=1.8)
  242. # 任务级战斗计数:本场是第几场(和旧日志 Current count 对齐)
  243. count = runtime.pub_ctx.per_task.count if runtime.pub_ctx else 0
  244. logger.info(f'Current count: {count + 1}')
  245. battle_config = GeneralBattleConfig()
  246. battle_config.lock_team_enable = False
  247. # 针对5/6星的挑战
  248. if group is not None and team is not None:
  249. battle_config.preset_enable = True
  250. battle_config.preset_group = group
  251. battle_config.preset_team = team
  252. # 每只5/6星鬼王的推荐阵容不同,而预设队伍默认是按任务只切一次的,
  253. # 这里只复位 preset 的任务级状态,per_task.count 保持累加
  254. PerTaskPreset.reset_preset_state(self)
  255. strategies, options = self.loadout_from_config(battle_config)
  256. strategies['success'] = 'metademon'
  257. strategies['failure'] = 'metademon'
  258. strategies['idle'] = 'metademon'
  259. options['completion'].check_imgs = [self.I_CRAFTING_1, self.I_FIND_DEMON, self.I_BATTLE_DEMON]
  260. self.loadout_show((strategies, options))
  261. self.boss_mark_reset()
  262. with battle_wait_strategy(**strategies), battle_wait_options(**options):
  263. win = self.battle_wait()
  264. logger.info(f'Battle boss win: {win}, current count: {runtime.pub_ctx.per_task.count}')
  265. return win
  266. # ------------------------------------------------------------------------------------------------------------------
  267. # battle_wait 钩子:超鬼王自己的结算界面与战中行为
  268. # ------------------------------------------------------------------------------------------------------------------
  269. def _bw_success_metademon(self, pub: PublicContext, pri: PrivateContext) -> HookSignal:
  270. if not self.appear(self.I_MD_WIN):
  271. return HookSignal.CONTINUE
  272. logger.info('Win battle')
  273. while 1:
  274. self.screenshot()
  275. if not self.appear(self.I_MD_WIN):
  276. break
  277. self.click(self.I_MD_WIN)
  278. sleep(0.5)
  279. pub.per_battle.success = BattleResult.SUCCESS
  280. return runtime.hook_enabled_update(
  281. enable=('completion',),
  282. disable=('success', 'failure'),
  283. )
  284. def _bw_failure_metademon(self, pub: PublicContext, pri: PrivateContext) -> HookSignal:
  285. if not self.appear(self.I_MD_BATTLE_FAILURE):
  286. return HookSignal.CONTINUE
  287. logger.warning('False battle')
  288. self.ui_click_until_disappear(self.I_MD_BATTLE_FAILURE)
  289. pub.per_battle.success = BattleResult.FAILURE
  290. return runtime.hook_enabled_update(
  291. enable=('completion',),
  292. disable=('success', 'failure'),
  293. )
  294. def _bw_idle_metademon(self, pub: PublicContext, pri: PrivateContext) -> HookSignal:
  295. if self.is_in_real_battle(False):
  296. self.boss_mark()
  297. return HookSignal.CONTINUE
  298. if __name__ == '__main__':
  299. from module.config.config import Config
  300. from module.device.device import Device
  301. c = Config('oas1')
  302. d = Device(c)
  303. t = ScriptTask(c, d)
  304. t.run()