script_task.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.config_general_battle import GeneralBattleConfig
  13. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  14. from tasks.GameUi.game_ui import GameUi
  15. from tasks.GameUi.page import page_main, page_shikigami_records
  16. from tasks.MetaDemon.config import MetaDemon, DefaultStrategy, Strategy
  17. from tasks.MetaDemon.assets import MetaDemonAssets
  18. from tasks.Restart.assets import RestartAssets
  19. from tasks.ActivityShikigami.assets import ActivityShikigamiAssets
  20. class Star56(GameUi, MetaDemonAssets):
  21. @cached_property
  22. def cached_strategies(self) -> dict[str, list[int]]:
  23. return Strategy.parse_all(self.config.meta_demon.md_strategies)
  24. @cached_property
  25. def cached_default_group_team(self) -> list[int]:
  26. return Strategy.parse_group_team(self.config.meta_demon.md_default_strategy.md_preset_group_team_default_1) \
  27. + Strategy.parse_group_team(self.config.meta_demon.md_default_strategy.md_preset_group_team_default_2)
  28. def is_star56(self):
  29. if not self.appear(self.I_STAR56_POS_1):
  30. return True
  31. return False
  32. def hp_threshold_low(self, image):
  33. """
  34. @param image:
  35. @return: 血量小于阈值返回True
  36. """
  37. return self.appear(self.I_STAR56_HP_THRESHOLD_F) and not self.appear(self.I_STAR56_HP_THRESHOLD_T)
  38. @classmethod
  39. def strategy2group_team(cls, strategies: dict[str, list[int]], detect_name: str) -> list[int] | None:
  40. """
  41. 先是默认的完全匹配,然后是1个字符不同,最后是2个字符不同
  42. @param strategies:
  43. @param detect_name:
  44. @return:
  45. """
  46. for i in range(2):
  47. for key_name in strategies:
  48. if len(key_name) != len(detect_name):
  49. continue
  50. diff_count = sum(1 for c1, c2 in zip(key_name, detect_name) if c1 != c2)
  51. if diff_count <= i < len(detect_name):
  52. return strategies[key_name]
  53. return None
  54. def get_group_team(self, detect_name: str) -> list[int]:
  55. tmp = self.strategy2group_team(self.cached_strategies, detect_name)
  56. return tmp if tmp else self.cached_default_group_team
  57. class ScriptTask(RightActivity, GeneralBattle, SwitchSoul, Star56):
  58. def run(self):
  59. if self.config.meta_demon.switch_soul.enable:
  60. self.goto_page(page_shikigami_records)
  61. self.run_switch_soul(self.config.meta_demon.switch_soul.switch_group_team)
  62. if self.config.meta_demon.switch_soul.enable_switch_by_name:
  63. self.goto_page(page_shikigami_records)
  64. self.run_switch_soul_by_name(self.config.meta_demon.switch_soul.group_name,
  65. self.config.meta_demon.switch_soul.team_name)
  66. self.enter(self.I_RIGHT_ENTER)
  67. while 1:
  68. self.screenshot()
  69. if self.appear(self.I_FIND_DEMON) or self.appear(self.I_BATTLE_DEMON):
  70. break
  71. if self.ui_reward_appear_click():
  72. continue
  73. if self.appear_then_click(self.I_UI_BACK_RED, interval=1.5):
  74. continue
  75. if self.appear_then_click(self.I_ENTER_ENSURE, interval=1):
  76. continue
  77. if self.appear_then_click(self.I_ENTER2, interval=1):
  78. continue
  79. if self.config.meta_demon.meta_demon_config.meta_crafting_card:
  80. self.crafting()
  81. boss_timer = Timer(50)
  82. boss_timer.start()
  83. first_battle_for_easy_boss = True
  84. while 1:
  85. battle_processing = False
  86. is_hard_boss = False # 五星或者六星
  87. self.screenshot()
  88. if not (self.appear(self.I_FIND_DEMON) or self.appear(self.I_BATTLE_DEMON)):
  89. continue
  90. # 先看自己有没有鬼王要打
  91. if (not self.appear(self.I_BOSS_EMPTY) and
  92. (self.appear(self.I_BOSS_CONVENE) or self.appear(self.I_BOOS_PUBLIC))):
  93. logger.info('Try to battle boss from self')
  94. self.click(self.I_BOSS_EMPTY, interval=1.5)
  95. battle_processing = True
  96. if self.is_star56():
  97. is_hard_boss = True
  98. # 看别人的是否给我共享鬼王
  99. if not battle_processing:
  100. if not self.appear(self.I_BOSS_EMPTY_1) and self.ocr_appear(self.O_BOSS_EMPTY_1):
  101. logger.info('Try to battle boss 1 from others')
  102. self.click(self.I_BOSS_EMPTY_1, interval=1.5)
  103. self.ui_click(self.I_BOSS_EMPTY_1, stop=self.I_BATTLE_DEMON, interval=1.5)
  104. battle_processing = True
  105. is_hard_boss = True
  106. elif not self.appear(self.I_BOSS_EMPTY_2):
  107. logger.info('Try to battle boss 2 from others')
  108. self.click(self.I_BOSS_EMPTY_2, interval=1.5)
  109. self.ui_click(self.I_BOSS_EMPTY_1, stop=self.I_BATTLE_DEMON, interval=1.5)
  110. battle_processing = True
  111. is_hard_boss = True
  112. # 是否喝茶 疲劳满了就不打了
  113. current_exhaustion = self.current_exhaustion()
  114. if current_exhaustion > 100:
  115. if self.config.meta_demon.meta_demon_config.auto_tea:
  116. logger.info('Exhaustion is full, buy tea')
  117. boss_timer.reset()
  118. self.buy_tea()
  119. else:
  120. logger.info('Exhaustion is full, exit')
  121. break
  122. # 超时结束
  123. if boss_timer.reached():
  124. logger.info('Time out, exit')
  125. break
  126. # 5/6星特殊处理 + 使用自定义策略组
  127. target_group_team = [None, None]
  128. if is_hard_boss:
  129. logger.info('Hard boss')
  130. if self.config.meta_demon.meta_demon_config.md_use_strategy:
  131. detect_full_name = self.O_MD_FULL_NAME.ocr_single(self.device.image)
  132. logger.info(f'detect_full_name: {detect_full_name}')
  133. if not detect_full_name:
  134. detect_full_name = ''
  135. group_team = self.get_group_team(detect_full_name)
  136. if self.hp_threshold_low(self.device.image):
  137. logger.info('Hp threshold low')
  138. target_group_team = group_team[2:4]
  139. else:
  140. logger.info('Hp threshold high')
  141. target_group_team = group_team[0:2]
  142. else:
  143. if self.config.meta_demon.meta_demon_config.md_use_strategy and first_battle_for_easy_boss:
  144. logger.info('Normal boss first battle use default group team')
  145. target_group_team = self.cached_default_group_team
  146. if battle_processing and self.appear(self.I_BATTLE_DEMON):
  147. logger.info(f'preset group team: {target_group_team}')
  148. self.battle_boss(target_group_team[0], target_group_team[1])
  149. if first_battle_for_easy_boss:
  150. first_battle_for_easy_boss = False
  151. if is_hard_boss:
  152. first_battle_for_easy_boss = True
  153. boss_timer.reset()
  154. continue
  155. # 自己召唤鬼王
  156. if current_exhaustion < 90 and self.find_demon():
  157. boss_timer.reset()
  158. continue
  159. # 退出
  160. self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MAIN, interval=1.4)
  161. logger.info('Exit MetaDemon')
  162. self.set_next_run(task="MetaDemon", success=True)
  163. raise TaskEnd
  164. def current_exhaustion(self) -> int:
  165. self.screenshot()
  166. cu, res, total = self.O_MD_EXHAUSTION.ocr(self.device.image)
  167. return int(cu)
  168. def buy_tea(self):
  169. pass
  170. def crafting(self):
  171. """
  172. 把一星合成二星
  173. @return:
  174. """
  175. logger.info('Star crafting 1 star to 2 star')
  176. self.ui_click(self.I_CRAFTING_1, self.I_CRAFTING_START, interval=1.5)
  177. timer = Timer(3).start()
  178. count = 0
  179. while 1:
  180. self.screenshot()
  181. if self.ui_reward_appear_click():
  182. timer.reset()
  183. continue
  184. if timer.reached() or count > 3:
  185. break
  186. if not self.ocr_appear(self.O_CRAFTING_CARD_1):
  187. break
  188. if self.appear(self.I_CRAFTING_EMPTY) or self.appear(self.I_CRAFTING_EMPTY_NEW):
  189. if self.appear_then_click(self.I_CRAFTING_CARD_STAR_1, interval=1):
  190. continue
  191. else:
  192. self.appear_then_click(self.I_CRAFTING_START, interval=3)
  193. count += 1
  194. timer.reset()
  195. continue
  196. logger.info('Finish star crafting')
  197. self.ui_click_until_disappear(self.I_UI_BACK_RED, interval=1)
  198. sleep(0.5)
  199. def find_demon(self) -> bool:
  200. """
  201. 召唤鬼王
  202. @return:
  203. """
  204. logger.info('Find demon')
  205. while 1:
  206. self.screenshot()
  207. if self.appear(self.I_BATTLE_DEMON) and not self.appear(self.I_BOSS_EMPTY):
  208. break
  209. if self.appear_then_click(self.I_FIND_DEMON, interval=3.5):
  210. continue
  211. self.screenshot()
  212. if self.is_star56():
  213. logger.info('Star 5 or 6 demon found')
  214. while 1:
  215. sleep(0.4)
  216. self.screenshot()
  217. detect_full_name = self.O_MD_FULL_NAME.ocr_single(self.device.image)
  218. if detect_full_name:
  219. break
  220. logger.info(f'召唤到高星鬼王: {detect_full_name}')
  221. self.config.notifier.push(content=f'召唤到高星鬼王: {detect_full_name}', title='超鬼王')
  222. return True
  223. def battle_boss(self, group: int = None, team: int = None) -> bool:
  224. logger.info('Battle boss')
  225. self.ui_click_until_disappear(self.I_BATTLE_DEMON, interval=1.8)
  226. battle_config = GeneralBattleConfig()
  227. battle_config.lock_team_enable = False
  228. if group is None and team is None:
  229. battle_config.preset_enable = False
  230. success = self.run_general_battle(battle_config)
  231. return True if success else False
  232. # 针对5/6星的挑战
  233. else:
  234. battle_config.preset_enable = True
  235. battle_config.preset_group = group
  236. battle_config.preset_team = team
  237. tmp_battle_count = self.current_count
  238. self.current_count = 0
  239. success = self.run_general_battle(battle_config)
  240. self.current_count = tmp_battle_count + 1
  241. return True if success else False
  242. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  243. # 重写
  244. self.device.stuck_record_add('BATTLE_STATUS_S')
  245. self.device.click_record_clear()
  246. logger.info("Start battle process")
  247. self.boss_mark_reset()
  248. while 1:
  249. self.screenshot()
  250. # if self.appear_then_click(self.I_WIN, interval=1):
  251. # continue
  252. if self.appear(self.I_WIN):
  253. logger.info('Win battle')
  254. self.ui_click_until_disappear(self.I_WIN)
  255. return True
  256. if self.appear(self.I_MD_BATTLE_FAILURE):
  257. logger.warning('False battle')
  258. self.ui_click_until_disappear(self.I_MD_BATTLE_FAILURE)
  259. return False
  260. if self.boss_mark():
  261. continue
  262. if __name__ == '__main__':
  263. from module.config.config import Config
  264. from module.device.device import Device
  265. c = Config('体服')
  266. d = Device(c)
  267. t = ScriptTask(c, d)
  268. t.run()