script_task.py 21 KB

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