script_task.py 20 KB

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