script_task.py 20 KB

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