script_task.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import numpy as np
  5. from time import sleep
  6. from cached_property import cached_property
  7. from datetime import datetime, timedelta
  8. from tasks.base_task import BaseTask
  9. from tasks.GameUi.game_ui import GameUi
  10. from tasks.BondlingFairyland.config import (BondlingFairyland, BondlingMode,
  11. BondlingClass,
  12. BondlingSwitchSoul, BondlingConfig)
  13. from tasks.BondlingFairyland.assets import BondlingFairylandAssets
  14. from tasks.BondlingFairyland.battle import BondlingBattle
  15. from tasks.BondlingFairyland.config_battle import BattleConfig
  16. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul, switch_parser
  17. from tasks.Component.GeneralBattle.config_general_battle import GeneralBattleConfig
  18. from module.atom.image import RuleImage
  19. from module.logger import logger
  20. class ScriptTask(GameUi, BondlingBattle, SwitchSoul, BondlingFairylandAssets):
  21. ball_pos_list = [None, None, None, None, None] # 用于记录每一个位置的球是否出现
  22. first_catch = True # 用于记录是否是第一次捕捉
  23. def run(self):
  24. pass
  25. def run_stone(self, bondling_stone_enable: bool, bondling_stone_class: BondlingClass):
  26. """
  27. 使用结契石 进行召唤 契灵
  28. :param bondling_stone_class:
  29. :return:
  30. (0) 不开启使用结契石,(探查界面)返回False
  31. (1) 五个球满了,(探查界面)返回True
  32. (2) 没有结契石了,(探查界面)返回False
  33. """
  34. if not bondling_stone_enable:
  35. return False
  36. while 1:
  37. self.screenshot()
  38. # 检查是不是在探查界面,
  39. if not self.in_search_ui(screenshot=False):
  40. continue
  41. # 如果没有石头了
  42. cu, res, total = self.O_B_STONE_NUMBER.ocr(self.device.image)
  43. if cu == 0 and cu + res == total:
  44. logger.warning(f'No bondling stone')
  45. return False
  46. # 如果五个球满了
  47. click_count = 0
  48. while 1:
  49. self.screenshot()
  50. if click_count >= 2:
  51. logger.info(f'Ball is full')
  52. return True
  53. if not self.appear(self.I_STONE_ENTER):
  54. # 这时进入到了召唤的界面
  55. self.wait_until_appear(self.I_STONE_SURE)
  56. action_click = None
  57. match bondling_stone_class:
  58. case BondlingClass.LITTLE_KURO: action_click = self.C_LEFT_1
  59. case BondlingClass.SNOWBALL: action_click = self.C_LEFT_2
  60. case BondlingClass.AZURE_BASAN: action_click = self.C_LEFT_3
  61. case BondlingClass.TOMB_GUARD: action_click = self.C_LEFT_4
  62. sleep(0.5)
  63. self.click(action_click)
  64. # 点击召唤
  65. while 1:
  66. self.screenshot()
  67. if not self.appear(self.I_STONE_SURE):
  68. break
  69. if self.appear_then_click(self.I_STONE_SURE, interval=1):
  70. continue
  71. break
  72. if self.appear_then_click(self.I_STONE_ENTER, interval=1):
  73. click_count += 1
  74. continue
  75. def run_search(self, bondling_config: BondlingConfig):
  76. """
  77. 运行探查
  78. :return:
  79. (1) 超出战斗的次数的了,(探查页面)返回False
  80. (2) 超过时间限制了,(探查页面)返回False
  81. (3) 打满五只球了,(探查界面)返回True
  82. """
  83. while 1:
  84. # 检查是不是在探查界面,
  85. if not self.in_search_ui(screenshot=True):
  86. continue
  87. # 检查是否有挑战次数
  88. if self.current_count >= bondling_config.limit_count:
  89. logger.warning(f'No challenge count, exit')
  90. return False
  91. # 检查是否到了限制时间
  92. if datetime.now() - self.start_time >= self.limit_time:
  93. logger.warning(f'No time, exit')
  94. return False
  95. if self.click_search():
  96. self.run_general_battle(self.general_battle_config)
  97. else:
  98. logger.warning(f'Full five ball')
  99. return True
  100. def run_catch(self, bondling_config: BondlingConfig,
  101. bondling_switch_soul: BondlingSwitchSoul,
  102. battle_config: BattleConfig):
  103. """
  104. 执行捕捉的(确保进入了结契界面)
  105. :return:
  106. (1) 盘子没了,返回False (退出页面是结契界面)
  107. (2) 时间到了,返回False (退出页面是结契界面)
  108. (3) 挑战次数到了,返回False (退出页面是结契界面)
  109. (4) 捕获成功,返回True (退出页面是结契界面)
  110. """
  111. self.lock_team()
  112. if self.first_catch:
  113. self.first_catch = False
  114. self.capture_setting(bondling_config.bondling_mode)
  115. target_plate = None
  116. match bondling_config.bondling_mode:
  117. case BondlingMode.MODE2: target_plate = self.O_B_LOW_NUMBER
  118. case BondlingMode.MODE3: target_plate = self.O_B_MEDIUM_NUMBER
  119. def check_plate_number(target_plate):
  120. self.screenshot()
  121. cu, res, total = target_plate.ocr(self.device.image)
  122. if cu == 0 and cu + res == total:
  123. logger.warning(f'No plate number, exit')
  124. return False
  125. return True
  126. if not check_plate_number(target_plate):
  127. return False
  128. def switch_soul(bondling_class: BondlingClass):
  129. # 按照结契的来切换御魂
  130. group_team: str = None
  131. match bondling_class:
  132. case BondlingClass.AZURE_BASAN: group_team = bondling_switch_soul.azure_basan_switch
  133. case BondlingClass.SNOWBALL: group_team = bondling_switch_soul.snowball_switch
  134. case BondlingClass.LITTLE_KURO: group_team = bondling_switch_soul.little_kuro_switch
  135. case BondlingClass.TOMB_GUARD: group_team = bondling_switch_soul.tomb_guard_switch
  136. group_team = switch_parser(group_team)
  137. if group_team == [-1, -1]:
  138. logger.info(f'{bondling_class.name } switch soul is not set, skip')
  139. return False
  140. self.enter_shikigami_records()
  141. self.run_switch_soul(tuple(group_team))
  142. # 返回的时候可能会多点一次导致 返回到探查界面
  143. self.exit_shikigami_records()
  144. bondling_class = self.get_bondling_class()
  145. first_switch_soul = True
  146. if bondling_switch_soul.auto_switch_soul:
  147. first_switch_soul = False
  148. switch_soul(bondling_class)
  149. # 开始执行循环
  150. while 1:
  151. self.screenshot()
  152. # 如果不在结契界面,就等待
  153. if not self.in_catch_ui():
  154. continue
  155. # 检查是否有盘子
  156. if not check_plate_number(target_plate):
  157. logger.warning(f'No plate number, exit')
  158. return False
  159. # 检查是否有挑战次数
  160. if self.current_count >= bondling_config.limit_count:
  161. logger.warning(f'No challenge count, exit')
  162. return False
  163. # 检查是否到了限制时间
  164. if datetime.now() - self.start_time >= self.limit_time:
  165. logger.warning(f'No time, exit')
  166. return False
  167. # ok 就进行挑战
  168. self.click_fire()
  169. if self.run_battle(battle_config):
  170. return True
  171. @cached_property
  172. def balls_target(self) -> list[RuleImage]:
  173. """
  174. 获取所有的球的图片
  175. :return:
  176. """
  177. result = [self.I_BF_LOCAL_1_AZURE_BASAN,
  178. self.I_BF_LOCAL_2_SNOWBALL,
  179. self.I_BF_LOCAL_3_LITTLE_KURO,
  180. self.I_BF_LOCAL_5_TOMB_GUARD]
  181. return result
  182. @cached_property
  183. def balls_roi(self) -> list[tuple]:
  184. """
  185. 获取球的五个位置是识别区域
  186. :return:
  187. """
  188. result = [self.I_BF_LOCAL_1_AZURE_BASAN.roi_back,
  189. self.I_BF_LOCAL_2_SNOWBALL.roi_back,
  190. self.I_BF_LOCAL_3_LITTLE_KURO.roi_back,
  191. self.I_BF_LOCAL_4_NONE.roi_back,
  192. self.I_BF_LOCAL_5_TOMB_GUARD.roi_back]
  193. return result
  194. def roi_appear_ball(self, roi: tuple, image: np.ndarray) -> tuple | None:
  195. """
  196. 判断某个固定的区域是否出现了球
  197. :param roi:
  198. :param image:
  199. :return: 如果出现了,那就返回可以点击的区域,如果没有出现,那就返回None
  200. """
  201. for ball in self.balls_target:
  202. ball.roi_back = list(roi)
  203. if ball.match(image, threshold=0.8):
  204. logger.info(f'Find ball {ball.name}')
  205. logger.info(f'Ball roi {ball.roi_front}')
  206. return ball.roi_front
  207. return None
  208. def ball_number(self) -> int:
  209. """
  210. !!! 弃用,原因是发现进行探查的时候会出现: 你的阴阳师会挡住球的位置,导致识别不到球
  211. 获取当前球的数量, 探查阶段优化使用的,但是在你抓完契灵后记得清空
  212. :return:
  213. """
  214. self.screenshot()
  215. count = 0
  216. for index, ball_pos in enumerate(self.ball_pos_list):
  217. if ball_pos is not None:
  218. count += 1
  219. continue
  220. appear_roi = self.roi_appear_ball(self.balls_roi[index], self.device.image)
  221. if appear_roi is not None:
  222. count += 1
  223. self.ball_pos_list[index] = appear_roi
  224. return count
  225. def use_stone(self, bondling_class: BondlingClass) -> bool:
  226. """
  227. 使用契灵石
  228. :param bondling_class:
  229. :return: 如果使用成功,那就返回True 如果已经有五个了返回False
  230. """
  231. return False
  232. def ball_click(self, index: int) -> bool:
  233. """
  234. 点击球, 进去结契战斗的界面
  235. :param index:
  236. :return:
  237. """
  238. def get_click_target(ind: int):
  239. if ind > 5 or ind < 1:
  240. raise ValueError('index must be 1-5')
  241. match = {
  242. 1: self.C_STONE_1,
  243. 2: self.C_STONE_2,
  244. 3: self.C_STONE_3,
  245. 4: self.C_STONE_4,
  246. 5: self.C_STONE_5,
  247. }
  248. return match[ind]
  249. click_target = get_click_target(index)
  250. click_count = 0
  251. while 1:
  252. self.screenshot()
  253. if self.appear(self.I_CLICK_CAPTION):
  254. return True
  255. if click_count >= 4:
  256. return False
  257. # 点击
  258. self.click(click_target, interval=1)
  259. click_count += 1
  260. def capture_setting(self, mode: BondlingMode) -> None:
  261. """
  262. 第一次进入任务的时候触发这一项方法:为了确保用户的选择默认是正确的
  263. 在结契的跳转页面,进入结契设置,按照模式来勾选
  264. :param mode:
  265. :return:
  266. """
  267. if mode == BondlingMode.MODE1:
  268. return None
  269. logger.info(f'Capture setting mode: {mode}')
  270. while 1:
  271. self.screenshot()
  272. if self.appear(self.I_CAPTION_ENSURE):
  273. break
  274. if self.appear_then_click(self.I_CLICK_CAPTION, interval=1):
  275. continue
  276. while 1:
  277. self.screenshot()
  278. if self.appear(self.I_C_AUTO_TRUE):
  279. break
  280. if self.appear_then_click(self.I_C_AUTO_FALSE, interval=1):
  281. continue
  282. target_true = None
  283. target_false = None
  284. target_first = None
  285. if mode == BondlingMode.MODE3:
  286. target_true = self.I_C_MIDUM_TRUE
  287. target_false = self.I_C_MIDUM_FALSE
  288. target_first = self.I_C_FIRST_ENABLE
  289. elif mode == BondlingMode.MODE2:
  290. target_true = self.I_C_LOW_TRUE
  291. target_false = self.I_C_LOW_FALSE
  292. target_first = self.I_C_FIRST_DISABLE
  293. while 1:
  294. self.screenshot()
  295. if self.appear(target_true):
  296. break
  297. if self.appear_then_click(target_false, interval=1):
  298. continue
  299. while 1:
  300. self.screenshot()
  301. if not self.appear(target_first):
  302. break
  303. if self.appear_then_click(target_first, interval=1):
  304. continue
  305. # 点击确定退出
  306. while 1:
  307. self.screenshot()
  308. if not self.appear(self.I_CAPTION_ENSURE):
  309. break
  310. if self.appear_then_click(self.I_CAPTION_ENSURE, interval=1):
  311. continue
  312. def enter_shikigami_records(self) -> None:
  313. """
  314. 进入式神录(在探查或者是结契的界面)
  315. :return:
  316. """
  317. while 1:
  318. self.screenshot()
  319. if self.appear(self.I_CHECK_RECORDS):
  320. break
  321. if self.appear_then_click(self.I_BF_RECORDS, interval=1):
  322. continue
  323. if self.appear_then_click(self.I_BALL_RECORDS, interval=1):
  324. continue
  325. def lock_team(self):
  326. """
  327. 锁定队伍(在探查或者是结契的界面)
  328. :return:
  329. """
  330. while 1:
  331. self.screenshot()
  332. if self.appear(self.I_BALL_LOCK):
  333. break
  334. if self.appear(self.I_BF_LOCK):
  335. break
  336. if self.appear_then_click(self.I_BALL_UNLOCK, interval=1):
  337. continue
  338. if self.appear_then_click(self.I_BF_UNLOCK, interval=1):
  339. continue
  340. def get_bondling_class(self) -> BondlingClass or None:
  341. """
  342. 获取契灵的种类 (这个不应该一开始就调用因为这些会有出场动画)
  343. :return:
  344. """
  345. self.screenshot()
  346. if self.appear(self.I_TOMB_GUARD):
  347. return BondlingClass.TOMB_GUARD
  348. elif self.appear(self.I_SNOWBALL):
  349. return BondlingClass.SNOWBALL
  350. elif self.appear(self.I_LITTLE_KURO):
  351. return BondlingClass.LITTLE_KURO
  352. elif self.appear(self.I_AZURE_BASAN, threshold=0.7):
  353. return BondlingClass.AZURE_BASAN
  354. return None
  355. @cached_property
  356. def limit_time(self) -> timedelta:
  357. if not self.config.bondling_fairyland.bondling_config.limit_time:
  358. return timedelta(minutes=20)
  359. limit_time = self.config.bondling_fairyland.bondling_config.limit_time
  360. return timedelta(hours=limit_time.hour, minutes=limit_time.minute, seconds=limit_time.second)
  361. def click_fire(self):
  362. """
  363. 点击 挑战, 主要是结契时的挑战
  364. """
  365. while 1:
  366. self.screenshot()
  367. if not self.appear(self.I_BALL_FIRE):
  368. break
  369. if self.appear_then_click(self.I_BALL_FIRE, interval=1):
  370. continue
  371. def in_catch_ui(self, screenshot=False) -> bool:
  372. """
  373. 判断是否在结契的总界面
  374. :return:
  375. """
  376. if screenshot:
  377. self.screenshot()
  378. return self.appear(self.I_BALL_FIRE)
  379. def in_search_ui(self, screenshot=False) -> bool:
  380. """
  381. 判断是否在探查的总界面
  382. :return:
  383. """
  384. if screenshot:
  385. self.screenshot()
  386. return self.appear(self.I_BF_STORE)
  387. def click_search(self) -> bool:
  388. """
  389. 点击探查
  390. :return: 如果五个球满了 就返回False。如果进入战斗不出现点击按钮那就是返回True
  391. """
  392. count = 0
  393. while 1:
  394. self.screenshot()
  395. if count >= 3:
  396. return False
  397. if not self.appear(self.I_BF_SEARSH):
  398. return True
  399. if self.appear_then_click(self.I_BF_SEARSH, interval=2):
  400. count += 1
  401. continue
  402. return False
  403. @cached_property
  404. def general_battle_config(self):
  405. gbc = GeneralBattleConfig()
  406. gbc.lock_team_enable = True
  407. gbc.preset_enable = False
  408. gbc.green_enable = False
  409. gbc.random_click_swipt_enable = False
  410. return gbc
  411. if __name__ == '__main__':
  412. from module.config.config import Config
  413. from module.device.device import Device
  414. import cv2
  415. config = Config('oas1')
  416. device = Device(config)
  417. task = ScriptTask(config, device)
  418. image = task.screenshot()
  419. # print(task.roi_appear_ball(task.I_BF_LOCAL_3_LITTLE_KURO.roi_back, image))
  420. # print(task.ball_number())
  421. con = config.bondling_fairyland
  422. # task.run_catch(con.bondling_config, con.bondling_switch_soul, con.battle_config)
  423. # task.run_stone(True, con.bondling_config.bondling_stone_class)
  424. task.run_search(con.bondling_config)