script_task.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from enum import Enum, auto
  5. from time import sleep
  6. from datetime import datetime, timedelta
  7. import cv2
  8. import numpy as np
  9. import random
  10. from typing import Any
  11. from cached_property import cached_property
  12. from module.atom.image import RuleImage
  13. from module.atom.click import RuleClick
  14. from module.atom.ocr import RuleOcr
  15. from module.base.protect import random_sleep
  16. from module.base.timer import Timer
  17. from module.exception import TaskEnd
  18. from module.logger import logger
  19. from tasks.base_task import BaseTask
  20. from tasks.ActivityShikigami.assets import ActivityShikigamiAssets
  21. from tasks.ActivityShikigami.config import SwitchSoulConfig, GeneralBattleConfig, ActivityShikigami
  22. from tasks.Component.BaseActivity.base_activity import BaseActivity
  23. from tasks.Component.BaseActivity.config_activity import GeneralClimb
  24. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  25. from tasks.GameUi.game_ui import GameUi
  26. import tasks.Component.GeneralBattle.config_general_battle
  27. import tasks.ActivityShikigami.page as game
  28. def _prepare_image_for_ocr(image: np.ndarray, asset: RuleOcr) -> np.ndarray:
  29. image_copy = image.copy()
  30. x, y, w, h = asset.roi
  31. roi_to_process = image_copy[y:y + h, x:x + w]
  32. if len(roi_to_process.shape) == 3:
  33. gray_image = cv2.cvtColor(roi_to_process, cv2.COLOR_BGR2GRAY)
  34. else:
  35. gray_image = roi_to_process
  36. # 自适应二值化
  37. _, binary_norm = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  38. _, binary_inv = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
  39. if cv2.countNonZero(binary_norm) < cv2.countNonZero(binary_inv):
  40. binary_correct = binary_norm
  41. else:
  42. binary_correct = binary_inv
  43. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 1))
  44. dilated_image = cv2.dilate(binary_correct, kernel, iterations=1)
  45. # 找轮廓
  46. contours, _ = cv2.findContours(dilated_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  47. processed_roi_content = None
  48. if contours:
  49. all_points = np.concatenate(contours, axis=0)
  50. bx, by, bw, bh = cv2.boundingRect(all_points)
  51. processed_roi_content = binary_correct[by:by + bh, bx:bx + bw]
  52. centered_roi = np.full((h, w), 255, dtype=np.uint8) # 255代表白色
  53. if processed_roi_content is not None:
  54. content_h, content_w = processed_roi_content.shape
  55. if content_h <= h and content_w <= w:
  56. # 计算居中粘贴的位置,放到中间
  57. start_y = (h - content_h) // 2
  58. start_x = (w - content_w) // 2
  59. paste_area = centered_roi[start_y:start_y + content_h, start_x:start_x + content_w]
  60. paste_area[processed_roi_content == 255] = 0
  61. else:
  62. logger.warning(f"Content for asset '{asset.name}' is larger than ROI. Skipping centering.")
  63. # 内容过大,直接使用原始二值图的反转作为结果
  64. centered_roi = cv2.bitwise_not(binary_correct)
  65. else:
  66. logger.warning(f"No content found in ROI for asset: {asset.name}. ROI will be blank.")
  67. processed_roi_bgr = cv2.cvtColor(centered_roi, cv2.COLOR_GRAY2BGR)
  68. image_copy[y:y + h, x:x + w] = processed_roi_bgr
  69. return image_copy
  70. class LimitTimeOut(Exception):
  71. pass
  72. class LimitCountOut(Exception):
  73. pass
  74. class StateMachine(BaseTask):
  75. run_idx: int = 0 # 当前爬塔类型
  76. _count_map = None
  77. @cached_property
  78. def conf(self) -> GeneralClimb:
  79. return self.config.model.activity_shikigami
  80. @property
  81. def climb_type(self) -> str:
  82. if self.run_idx >= len(self.conf.general_climb.run_sequence_v):
  83. return self.conf.general_climb.run_sequence_v[-1]
  84. return self.conf.general_climb.run_sequence_v[self.run_idx]
  85. @property
  86. def count_map(self) -> dict[str, int]:
  87. """
  88. :return: key: climb type, value: run count
  89. """
  90. if not getattr(self, "_count_map", None):
  91. self._count_map = {climb_type: 0 for climb_type in self.conf.general_climb.run_sequence_v}
  92. return self._count_map
  93. # ----------------------------------------------------
  94. def put_status(self):
  95. """
  96. 更新全局状态
  97. """
  98. def get_count(self) -> int:
  99. return self.count_map[self.climb_type]
  100. def get_limit(self) -> int:
  101. limit = getattr(self.conf.general_climb, f'{self.climb_type}_limit', 0)
  102. return 0 if not limit else limit
  103. # 超过运行时间
  104. if self.limit_time is not None and datetime.now() - self.start_time >= self.limit_time:
  105. logger.info(f"Climb type {self.climb_type} time out")
  106. raise LimitTimeOut
  107. # 次数达到限制
  108. if get_count(self) >= get_limit(self):
  109. logger.info(f"Climb type {self.climb_type} count limit reached")
  110. raise LimitCountOut
  111. def switch_next(self):
  112. """
  113. 切换下一种爬塔类型
  114. :return: True 切换成功 or False
  115. """
  116. self.run_idx += 1
  117. if self.run_idx >= len(self.conf.general_climb.run_sequence_v):
  118. logger.info('All climbing activities have been completed')
  119. return False
  120. # 切换爬塔类型了, 恢复所有状态
  121. self.current_count = 0
  122. logger.hr(f'Climb switch to {self.climb_type}', 2)
  123. return True
  124. class ScriptTask(StateMachine, GameUi, BaseActivity, SwitchSoul, ActivityShikigamiAssets):
  125. def run(self) -> None:
  126. self.limit_time: timedelta = self.conf.general_climb.limit_time_v
  127. #
  128. for climb_type in self.conf.general_climb.run_sequence_v:
  129. # 进入到活动的主页面,不是具体的战斗页面
  130. self.ui_get_current_page()
  131. self.ui_goto(game.page_climb_act)
  132. try:
  133. method_func = getattr(self, f'_run_{climb_type}')
  134. method_func()
  135. except LimitCountOut as e:
  136. self.ui_click(self.I_UI_BACK_YELLOW, stop=self.I_TO_BATTLE_MAIN, interval=1)
  137. except LimitTimeOut as e:
  138. break
  139. finally:
  140. # 切换下一个爬塔类型
  141. self.switch_next()
  142. # 返回庭院
  143. logger.hr("Exit Shikigami", 2)
  144. self.ui_get_current_page(False)
  145. self.ui_goto(game.page_main)
  146. if self.conf.general_climb.active_souls_clean:
  147. self.set_next_run(task='SoulsTidy', success=False, finish=False, target=datetime.now())
  148. self.set_next_run(task="ActivityShikigami", success=True)
  149. raise TaskEnd
  150. def _run_pass(self):
  151. logger.hr(f'Start run climb type PASS', 1)
  152. self.ui_click(self.I_TO_BATTLE_MAIN, stop=self.I_CHECK_BATTLE_MAIN, interval=1)
  153. self.switch_soul(self.I_BATTLE_MAIN_TO_RECORDS, self.I_CHECK_BATTLE_MAIN)
  154. ocr_limit_timer = Timer(1).start()
  155. while 1:
  156. self.screenshot()
  157. self.put_status()
  158. ##
  159. if self.appear_then_click(self.I_PASS_1, interval=4):
  160. continue
  161. if self.appear_then_click(self.I_PASS_1, interval=4):
  162. continue
  163. if not ocr_limit_timer.reached():
  164. continue
  165. ocr_limit_timer.reset()
  166. if not self.ocr_appear(self.O_FIRE):
  167. continue
  168. # --------------------------------------------------------------
  169. self.lock_team(self.conf.general_battle)
  170. if not self.check_tickets_enough():
  171. logger.warning(f'No tickets left, wait for next time')
  172. break
  173. # 点击战斗前随机休息
  174. if self.conf.general_climb.random_sleep:
  175. random_sleep(probability=0.2)
  176. if self.start_battle():
  177. continue
  178. self.ui_click(self.I_UI_BACK_YELLOW, stop=self.I_TO_BATTLE_MAIN, interval=1)
  179. def _run_ap(self):
  180. logger.hr(f'Start run climb type AP')
  181. self.ui_click(self.I_TO_BATTLE_MAIN, stop=self.I_CHECK_BATTLE_MAIN, interval=1)
  182. self.switch_soul(self.I_BATTLE_MAIN_TO_RECORDS, self.I_CHECK_BATTLE_MAIN)
  183. ocr_limit_timer = Timer(1).start()
  184. while 1:
  185. self.screenshot()
  186. self.put_status()
  187. # --------------------------------------------------------------
  188. if self.appear_then_click(self.I_CHECK_BATTLE_MAIN, interval=4):
  189. continue
  190. if not ocr_limit_timer.reached():
  191. continue
  192. ocr_limit_timer.reset()
  193. if not self.ocr_appear(self.O_FIRE):
  194. continue
  195. # --------------------------------------------------------------
  196. self.lock_team(self.conf.general_battle)
  197. if not self.check_tickets_enough():
  198. logger.warning(f'No tickets left, wait for next time')
  199. break
  200. # 点击战斗前随机休息
  201. if self.conf.general_climb.random_sleep:
  202. random_sleep(probability=0.2)
  203. if self.start_battle():
  204. continue
  205. self.ui_click(self.I_UI_BACK_YELLOW, stop=self.I_TO_BATTLE_MAIN, interval=1)
  206. def _run_boss(self):
  207. pass
  208. def start_battle(self):
  209. click_times, max_times = 0, random.randint(2, 4)
  210. while 1:
  211. self.screenshot()
  212. if self.is_in_battle(False):
  213. break
  214. if click_times >= max_times:
  215. logger.warning(f'Climb {self.climb_type} cannot enter, maybe already end, try next')
  216. return
  217. if ((self.appear_then_click(self.I_C_CONFIRM1, interval=0.6) or
  218. self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1) or
  219. self.appear_then_click(self.I_UI_CONFIRM, interval=1)) or
  220. self.appear_then_click(self.I_N_CONFIRM, interval=1)):
  221. continue
  222. if self.ocr_appear_click(self.O_FIRE, interval=2):
  223. click_times += 1
  224. logger.info(f'Try click fire, remain times[{max_times - click_times}]')
  225. continue
  226. # 运行战斗
  227. self.run_general_battle(config=self.get_general_battle_conf())
  228. def battle_wait(self, random_click_swipt_enable: bool) -> bool:
  229. # 通用战斗结束判断
  230. self.device.stuck_record_add("BATTLE_STATUS_S")
  231. self.device.click_record_clear()
  232. logger.info(f"Start {self.climb_type} battle process")
  233. self.count_map[self.climb_type] = self.current_count
  234. for btn in (self.C_RANDOM_LEFT, self.C_RANDOM_RIGHT, self.C_RANDOM_TOP, self.C_RANDOM_BOTTOM):
  235. btn.name = "BATTLE_RANDOM"
  236. ok_cnt, max_retry = 0, 5
  237. single_start_time = datetime.now()
  238. while 1:
  239. sleep(random.uniform(0.5, 1.5))
  240. self.screenshot()
  241. # 达到最大重试次数则直接交给上层处理
  242. if ok_cnt > max_retry:
  243. break
  244. # 识别到挑战说明已经退出战斗
  245. if ok_cnt > 0 and self.ocr_appear(self.O_FIRE):
  246. return True
  247. # 战斗失败
  248. if self.appear(self.I_FALSE):
  249. logger.warning("Battle failed")
  250. self.ui_click_until_smt_disappear(self.random_reward_click(click_now=False), self.I_FALSE, interval=1.5)
  251. return False
  252. # 战斗成功
  253. if self.appear_then_click(self.I_WIN, interval=2):
  254. continue
  255. if self.appear(self.I_REWARD):
  256. logger.info('Win battle')
  257. self.ui_click_until_disappear(self.I_REWARD, interval=1.5)
  258. return True
  259. # 已经不在战斗中了, 且奖励也识别过了, 则随机点击
  260. # if ok_cnt > 0 and not self.is_in_battle(False):
  261. # self.random_reward_click(exclude_click=[self.C_RANDOM_BOTTOM])
  262. # ok_cnt += 1
  263. # continue
  264. # # 单局到时间自动退出战斗
  265. # if ok_cnt == 0 and datetime.now() - single_start_time > self.conf.general_climb.get_single_limit_time(
  266. # self.climb_type, timedelta(days=1)):
  267. # logger.attr(self.climb_type, 'Time limit arrived, close current battle')
  268. # self.exit_battle(skip_first=True)
  269. # ok_cnt += 1
  270. # continue
  271. # 战斗中随机滑动
  272. if ok_cnt == 0 and random_click_swipt_enable:
  273. self.random_click_swipt()
  274. return True
  275. # def exit_battle(self, skip_first: bool = False) -> bool:
  276. # """
  277. # 在战斗的时候强制退出战斗
  278. # :param skip_first: 是否跳过第一次截屏
  279. # :return: 退出战斗成功True or 失败False
  280. # """
  281. # timeout_timer = Timer(6).start()
  282. # exit_clicked = False
  283. # while not timeout_timer.reached():
  284. # self.maybe_screenshot(skip_first)
  285. # skip_first = False
  286. # # 不在战斗界面, 认为退出成功
  287. # if not self.is_in_battle(False):
  288. # return True
  289. # if exit_clicked:
  290. # self.appear_then_click(self.I_EXIT_ENSURE)
  291. # if not exit_clicked and self.appear_then_click(self.I_EXIT):
  292. # exit_clicked = True
  293. # sleep(random.uniform(0.5, 1.5))
  294. # # 还在战斗界面且超时则退出失败
  295. # return False
  296. def switch_soul(self, enter_button: RuleImage, cur_img: RuleImage):
  297. conf = self.conf.switch_soul_config
  298. conf.validate_switch_soul()
  299. enable_switch = getattr(conf, f"enable_switch_{self.climb_type}", False)
  300. enable_by_name = getattr(conf, f"enable_switch_{self.climb_type}_by_name", False)
  301. if not enable_switch and not enable_by_name:
  302. return
  303. self.ui_click(enter_button, stop=self.I_CHECK_RECORDS, interval=1)
  304. if enable_by_name:
  305. group, team = getattr(conf, f"{self.climb_type}_group_team_name").split(",")
  306. self.run_switch_soul_by_name(group, team)
  307. elif enable_switch:
  308. group_team = getattr(conf, f"{self.climb_type}_group_team")
  309. self.run_switch_soul(group_team)
  310. self.ui_click(self.I_UI_BACK_YELLOW, stop=cur_img, interval=1)
  311. def lock_team(self, battle_conf: GeneralBattleConfig):
  312. """
  313. 根据配置判断当前爬塔类型是否锁定阵容, 并执行锁定或解锁
  314. """
  315. enable_preset = getattr(battle_conf, f"enable_{self.climb_type}_preset", False)
  316. if not enable_preset:
  317. logger.info(f'Lock {self.climb_type} team')
  318. self.ui_click(self.I_UNLOCK, stop=self.I_LOCK, interval=1)
  319. return
  320. logger.info(f'Unlock {self.climb_type} team')
  321. self.ui_click(self.I_LOCK, stop=self.I_UNLOCK, interval=1)
  322. def check_tickets_enough(self) -> bool:
  323. """
  324. 判断当前爬塔门票是否足够
  325. :return: True 可以运行 or False
  326. """
  327. logger.hr(f'Check {self.climb_type} tickets')
  328. if not self.wait_until_appear(self.O_FIRE, wait_time=3):
  329. logger.warning(f'Detect fire fail, try reidentify')
  330. return False
  331. self.screenshot()
  332. remain_times = 0
  333. if self.climb_type == 'pass':
  334. remain_times = self.O_REMAIN_AP_ACTIVITY2.ocr_digit(
  335. _prepare_image_for_ocr(self.device.image, asset=self.O_REMAIN_AP_ACTIVITY2))
  336. if self.climb_type == 'ap':
  337. remain_times = self.O_REMAIN_AP.ocr_digit(
  338. _prepare_image_for_ocr(self.device.image, asset=self.O_REMAIN_AP))
  339. if self.climb_type == 'boss':
  340. _, remain_times, _ = self.O_REMAIN_BOSS.ocr_digit_counter(self.device.image)
  341. if self.climb_type == 'ap100':
  342. remain_times = self.O_REMAIN_AP100.ocr_digit(
  343. _prepare_image_for_ocr(self.device.image, asset=self.O_REMAIN_AP100))
  344. return remain_times > 0
  345. def get_general_battle_conf(self) -> tasks.Component.GeneralBattle.config_general_battle.GeneralBattleConfig:
  346. from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig as gbc
  347. self.conf.validate_switch_preset()
  348. enable_preset = getattr(self.conf.general_battle, f'enable_{self.climb_type}_preset', False)
  349. group, team = getattr(self.conf.switch_soul_config, f'{self.climb_type}_group_team').split(',')
  350. return gbc(lock_team_enable=not enable_preset,
  351. preset_enable=enable_preset,
  352. preset_group=group if enable_preset else 1,
  353. preset_team=team if enable_preset else 1,
  354. green_enable=getattr(self.conf.general_battle, f'enable_{self.climb_type}_green', False),
  355. green_mark=getattr(self.conf.general_battle, f'{self.climb_type}_green_mark'),
  356. random_click_swipt_enable=getattr(self.conf.general_battle, f'enable_{self.climb_type}_anti_detect',
  357. False), )
  358. def random_reward_click(self, exclude_click: list = None, click_now: bool = True) -> RuleClick:
  359. """
  360. 随机点击
  361. :param exclude_click: 排除的点击位置
  362. :param click_now: 是否立即点击
  363. :return: 随机的点击位置
  364. """
  365. options = [self.C_RANDOM_LEFT, self.C_RANDOM_RIGHT, self.C_RANDOM_TOP, self.C_RANDOM_BOTTOM]
  366. if exclude_click:
  367. options = [option for option in options if option not in exclude_click]
  368. target = random.choice(options)
  369. if click_now:
  370. self.click(target, interval=1.8)
  371. return target
  372. if __name__ == '__main__':
  373. from module.config.config import Config
  374. from module.device.device import Device
  375. c = Config('oas1')
  376. d = Device(c)
  377. t = ScriptTask(c, d)
  378. t.run()