script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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:
  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.ui_get_current_page()
  61. self.ui_goto(page_shikigami_records)
  62. self.run_switch_soul(self.config.meta_demon.switch_soul.switch_group_team)
  63. if self.config.meta_demon.switch_soul.enable_switch_by_name:
  64. self.ui_get_current_page()
  65. self.ui_goto(page_shikigami_records)
  66. self.run_switch_soul_by_name(self.config.meta_demon.switch_soul.group_name,
  67. self.config.meta_demon.switch_soul.team_name)
  68. self.enter(self.I_RIGHT_ENTER)
  69. while 1:
  70. self.screenshot()
  71. if self.appear(self.I_FIND_DEMON) or self.appear(self.I_BATTLE_DEMON):
  72. break
  73. if self.ui_reward_appear_click():
  74. continue
  75. if self.appear_then_click(self.I_UI_BACK_RED, interval=1.5):
  76. continue
  77. if self.appear_then_click(self.I_ENTER_ENSURE, interval=1):
  78. continue
  79. if self.appear_then_click(self.I_ENTER2, interval=1):
  80. continue
  81. if self.config.meta_demon.meta_demon_config.meta_crafting_card:
  82. self.crafting()
  83. boss_timer = Timer(180)
  84. boss_timer.start()
  85. first_battle = True
  86. while 1:
  87. battle_processing = False
  88. is_hard_boss = False # 五星或者六星
  89. self.screenshot()
  90. if not (self.appear(self.I_FIND_DEMON) or self.appear(self.I_BATTLE_DEMON)):
  91. continue
  92. # 先看自己有没有鬼王要打
  93. if not self.appear(self.I_BOSS_EMPTY):
  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):
  101. logger.info('Try to battle boss 1 from others')
  102. self.click(self.I_BOSS_EMPTY_1, interval=1.5)
  103. battle_processing = True
  104. elif not self.appear(self.I_BOSS_EMPTY_2):
  105. logger.info('Try to battle boss 2 from others')
  106. self.click(self.I_BOSS_EMPTY_2, interval=1.5)
  107. battle_processing = True
  108. # 是否喝茶 疲劳满了就不打了
  109. current_exhaustion = self.current_exhaustion()
  110. if current_exhaustion > 99:
  111. if self.config.meta_demon.meta_demon_config.auto_tea:
  112. logger.info('Exhaustion is full, buy tea')
  113. boss_timer.reset()
  114. self.buy_tea()
  115. else:
  116. logger.info('Exhaustion is full, exit')
  117. break
  118. # 超时结束
  119. if boss_timer.reached():
  120. logger.info('Time out, exit')
  121. break
  122. # 5/6星特殊处理 + 使用自定义策略组
  123. target_group_team = [None, None]
  124. if is_hard_boss:
  125. logger.info('Hard boss')
  126. if self.config.meta_demon.meta_demon_config.md_use_strategy:
  127. detect_full_name = self.O_MD_FULL_NAME.ocr_single(self.device.image)
  128. logger.info(f'detect_full_name: {detect_full_name}')
  129. if not detect_full_name:
  130. detect_full_name = ''
  131. group_team = self.get_group_team(detect_full_name)
  132. if self.hp_threshold_low(self.device.image):
  133. logger.info('Hp threshold low')
  134. target_group_team = group_team[2:4]
  135. else:
  136. logger.info('Hp threshold high')
  137. target_group_team = group_team[0:2]
  138. else:
  139. if self.config.meta_demon.meta_demon_config.md_use_strategy and first_battle:
  140. logger.info('Normal boss first battle use default group team')
  141. target_group_team = self.cached_default_group_team
  142. if battle_processing and self.appear(self.I_BATTLE_DEMON):
  143. logger.info(f'preset group team: {target_group_team}')
  144. self.battle_boss(target_group_team[0], target_group_team[1])
  145. if first_battle:
  146. first_battle = False
  147. boss_timer.reset()
  148. continue
  149. # 自己召唤鬼王
  150. if self.find_demon():
  151. boss_timer.reset()
  152. continue
  153. # 退出
  154. self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MAIN, interval=1.4)
  155. logger.info('Exit MetaDemon')
  156. self.set_next_run(task="MetaDemon", success=True)
  157. raise TaskEnd
  158. def current_exhaustion(self) -> int:
  159. self.screenshot()
  160. cu, res, total = self.O_MD_EXHAUSTION.ocr(self.device.image)
  161. return int(cu)
  162. def buy_tea(self):
  163. pass
  164. def crafting(self):
  165. """
  166. 把一星合成二星
  167. @return:
  168. """
  169. logger.info('Star crafting 1 star to 2 star')
  170. self.ui_click(self.I_CRAFTING_1, self.I_CRAFTING_START, interval=1.5)
  171. timer = Timer(3).start()
  172. count = 0
  173. while 1:
  174. self.screenshot()
  175. if self.ui_reward_appear_click():
  176. timer.reset()
  177. continue
  178. if timer.reached() or count > 3:
  179. break
  180. if not self.ocr_appear(self.O_CRAFTING_CARD_1):
  181. break
  182. if self.appear(self.I_CRAFTING_EMPTY) or self.appear(self.I_CRAFTING_EMPTY_NEW):
  183. if self.appear_then_click(self.I_CRAFTING_CARD_STAR_1, interval=1):
  184. continue
  185. else:
  186. self.appear_then_click(self.I_CRAFTING_START, interval=3)
  187. count += 1
  188. timer.reset()
  189. continue
  190. logger.info('Finish star crafting')
  191. self.ui_click_until_disappear(self.I_UI_BACK_RED, interval=1)
  192. def find_demon(self) -> bool:
  193. """
  194. 召唤鬼王
  195. @return:
  196. """
  197. logger.info('Find demon')
  198. while 1:
  199. self.screenshot()
  200. if self.appear(self.I_BATTLE_DEMON) and not self.appear(self.I_BOSS_EMPTY):
  201. break
  202. if self.appear_then_click(self.I_FIND_DEMON, interval=3.5):
  203. continue
  204. self.screenshot()
  205. if self.is_star56():
  206. logger.info('Star 5 or 6 demon found')
  207. detect_full_name = self.O_MD_FULL_NAME.ocr_single(self.device.image)
  208. logger.info(f'召唤到高星鬼王: {detect_full_name}')
  209. self.config.notifier.push(content=f'召唤到高星鬼王: {detect_full_name}', title='超鬼王')
  210. return True
  211. def battle_boss(self, group: int = None, team: int = None) -> bool:
  212. logger.info('Battle boss')
  213. self.ui_click_until_disappear(self.I_BATTLE_DEMON, interval=1.8)
  214. battle_config = GeneralBattleConfig()
  215. battle_config.lock_team_enable = False
  216. if group is None and team is None:
  217. battle_config.preset_enable = False
  218. success = self.run_general_battle(battle_config)
  219. return True if success else False
  220. # 针对5/6星的挑战
  221. else:
  222. battle_config.preset_enable = True
  223. battle_config.preset_group = group
  224. battle_config.preset_team = team
  225. tmp_battle_count = self.current_count
  226. self.current_count = 0
  227. success = self.run_general_battle(battle_config)
  228. self.current_count = tmp_battle_count + 1
  229. return True if success else False
  230. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  231. # 重写
  232. self.device.stuck_record_add('BATTLE_STATUS_S')
  233. self.device.click_record_clear()
  234. logger.info("Start battle process")
  235. while 1:
  236. self.screenshot()
  237. # if self.appear_then_click(self.I_WIN, interval=1):
  238. # continue
  239. if self.appear(self.I_WIN):
  240. logger.info('Win battle')
  241. self.ui_click_until_disappear(self.I_WIN)
  242. return True
  243. if self.appear(self.I_MD_BATTLE_FAILURE):
  244. logger.warning('False battle')
  245. self.ui_click_until_disappear(self.I_MD_BATTLE_FAILURE)
  246. return False
  247. if __name__ == '__main__':
  248. from module.config.config import Config
  249. from module.device.device import Device
  250. c = Config('oas1')
  251. d = Device(c)
  252. t = ScriptTask(c, d)
  253. t.run()