script_task.py 20 KB

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