script_task.py 11 KB

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