script_task.py 12 KB

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