script_task.py 12 KB

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