script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 cached_property import cached_property
  6. from tasks.base_task import BaseTask
  7. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  8. from tasks.GameUi.game_ui import GameUi
  9. from tasks.BondlingFairyland.config import (BondlingFairyland, BondlingMode,
  10. BondlingClass,
  11. BondlingSwitchSoul, BondlingConfig)
  12. from tasks.BondlingFairyland.assets import BondlingFairylandAssets
  13. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul, switch_parser
  14. from module.atom.image import RuleImage
  15. from module.logger import logger
  16. class ScriptTask(GameUi, GeneralBattle, SwitchSoul, BondlingFairylandAssets):
  17. ball_pos_list = [None, None, None, None, None] # 用于记录每一个位置的球是否出现
  18. first_catch = True # 用于记录是否是第一次捕捉
  19. def run(self):
  20. pass
  21. def run_search(self):
  22. """
  23. 运行探查
  24. :return:
  25. """
  26. def run_catch(self, bondling_config: BondlingConfig, bondling_switch_soul: BondlingSwitchSoul):
  27. """
  28. 执行捕捉的(确保进入了结契界面)
  29. :return:
  30. (1) 盘子没了,返回False (退出页面是结契界面)
  31. (2) 时间到了,返回False (退出页面是结契界面)
  32. (3) 挑战次数到了,返回False (退出页面是结契界面)
  33. (4) 捕获成功,返回True (退出页面是结契界面)
  34. """
  35. self.lock_team()
  36. if self.first_catch:
  37. self.first_catch = False
  38. self.capture_setting(bondling_config.bondling_mode)
  39. target_plate = None
  40. match bondling_config.bondling_mode:
  41. case BondlingMode.MODE2: target_plate = self.O_B_LOW_NUMBER
  42. case BondlingMode.MODE3: target_plate = self.O_B_MEDIUM_NUMBER
  43. def check_plate_number(target_plate):
  44. self.screenshot()
  45. cu, res, total = target_plate.ocr(self.device.image)
  46. if cu == 0 and cu + res == total:
  47. logger.warning(f'No plate number, exit')
  48. return False
  49. return True
  50. if not check_plate_number(target_plate):
  51. return False
  52. def switch_soul(bondling_class: BondlingClass):
  53. # 按照结契的来切换御魂
  54. group_team: str = None
  55. match bondling_class:
  56. case BondlingClass.AZURE_BASAN: group_team = bondling_switch_soul.azure_basan_switch
  57. case BondlingClass.SNOWBALL: group_team = bondling_switch_soul.snowball_switch
  58. case BondlingClass.LITTLE_KURO: group_team = bondling_switch_soul.little_kuro_switch
  59. case BondlingClass.TOMB_GUARD: group_team = bondling_switch_soul.tomb_guard_switch
  60. group_team = switch_parser(group_team)
  61. if group_team == [-1, -1]:
  62. logger.info(f'{bondling_class.name } switch soul is not set, skip')
  63. return False
  64. self.enter_shikigami_records()
  65. self.run_switch_soul(tuple(group_team))
  66. # 返回的时候可能会多点一次导致 返回到探查界面
  67. self.exit_shikigami_records()
  68. bondling_class = self.get_bondling_class()
  69. first_switch_soul = True
  70. if bondling_switch_soul.auto_switch_soul:
  71. first_switch_soul = False
  72. switch_soul(bondling_class)
  73. @cached_property
  74. def balls_target(self) -> list[RuleImage]:
  75. """
  76. 获取所有的球的图片
  77. :return:
  78. """
  79. result = [self.I_BF_LOCAL_1_AZURE_BASAN,
  80. self.I_BF_LOCAL_2_SNOWBALL,
  81. self.I_BF_LOCAL_3_LITTLE_KURO,
  82. self.I_BF_LOCAL_5_TOMB_GUARD]
  83. return result
  84. @cached_property
  85. def balls_roi(self) -> list[tuple]:
  86. """
  87. 获取球的五个位置是识别区域
  88. :return:
  89. """
  90. result = [self.I_BF_LOCAL_1_AZURE_BASAN.roi_back,
  91. self.I_BF_LOCAL_2_SNOWBALL.roi_back,
  92. self.I_BF_LOCAL_3_LITTLE_KURO.roi_back,
  93. self.I_BF_LOCAL_4_NONE.roi_back,
  94. self.I_BF_LOCAL_5_TOMB_GUARD.roi_back]
  95. return result
  96. def roi_appear_ball(self, roi: tuple, image: np.ndarray) -> tuple | None:
  97. """
  98. 判断某个固定的区域是否出现了球
  99. :param roi:
  100. :param image:
  101. :return: 如果出现了,那就返回可以点击的区域,如果没有出现,那就返回None
  102. """
  103. for ball in self.balls_target:
  104. ball.roi_back = list(roi)
  105. if ball.match(image, threshold=0.8):
  106. logger.info(f'Find ball {ball.name}')
  107. logger.info(f'Ball roi {ball.roi_front}')
  108. return ball.roi_front
  109. return None
  110. def ball_number(self) -> int:
  111. """
  112. !!! 弃用,原因是发现进行探查的时候会出现: 你的阴阳师会挡住球的位置,导致识别不到球
  113. 获取当前球的数量, 探查阶段优化使用的,但是在你抓完契灵后记得清空
  114. :return:
  115. """
  116. self.screenshot()
  117. count = 0
  118. for index, ball_pos in enumerate(self.ball_pos_list):
  119. if ball_pos is not None:
  120. count += 1
  121. continue
  122. appear_roi = self.roi_appear_ball(self.balls_roi[index], self.device.image)
  123. if appear_roi is not None:
  124. count += 1
  125. self.ball_pos_list[index] = appear_roi
  126. return count
  127. def use_stone(self, bondling_class: BondlingClass) -> bool:
  128. """
  129. 使用契灵石
  130. :param bondling_class:
  131. :return: 如果使用成功,那就返回True 如果已经有五个了返回False
  132. """
  133. return False
  134. def click_search(self) -> bool:
  135. """
  136. 点击探查
  137. :return: 如果进入战斗返回True,如果五个球满了返回False
  138. """
  139. return False
  140. def ball_click(self, index: int) -> bool:
  141. """
  142. 点击球, 进去结契战斗的界面
  143. :param index:
  144. :return:
  145. """
  146. def get_click_target(ind: int):
  147. if ind > 5 or ind < 1:
  148. raise ValueError('index must be 1-5')
  149. match = {
  150. 1: self.C_STONE_1,
  151. 2: self.C_STONE_2,
  152. 3: self.C_STONE_3,
  153. 4: self.C_STONE_4,
  154. 5: self.C_STONE_5,
  155. }
  156. return match[ind]
  157. click_target = get_click_target(index)
  158. click_count = 0
  159. while 1:
  160. self.screenshot()
  161. if self.appear(self.I_CLICK_CAPTION):
  162. return True
  163. if click_count >= 4:
  164. return False
  165. # 点击
  166. self.click(click_target, interval=1)
  167. click_count += 1
  168. def capture_setting(self, mode: BondlingMode) -> None:
  169. """
  170. 第一次进入任务的时候触发这一项方法:为了确保用户的选择默认是正确的
  171. 在结契的跳转页面,进入结契设置,按照模式来勾选
  172. :param mode:
  173. :return:
  174. """
  175. if mode == BondlingMode.MODE1:
  176. return None
  177. logger.info(f'Capture setting mode: {mode}')
  178. while 1:
  179. self.screenshot()
  180. if self.appear(self.I_CAPTION_ENSURE):
  181. break
  182. if self.appear_then_click(self.I_CLICK_CAPTION, interval=1):
  183. continue
  184. while 1:
  185. self.screenshot()
  186. if self.appear(self.I_C_AUTO_TRUE):
  187. break
  188. if self.appear_then_click(self.I_C_AUTO_FALSE, interval=1):
  189. continue
  190. target_true = None
  191. target_false = None
  192. target_first = None
  193. if mode == BondlingMode.MODE3:
  194. target_true = self.I_C_MIDUM_TRUE
  195. target_false = self.I_C_MIDUM_FALSE
  196. target_first = self.I_C_FIRST_ENABLE
  197. elif mode == BondlingMode.MODE2:
  198. target_true = self.I_C_LOW_TRUE
  199. target_false = self.I_C_LOW_FALSE
  200. target_first = self.I_C_FIRST_DISABLE
  201. while 1:
  202. self.screenshot()
  203. if self.appear(target_true):
  204. break
  205. if self.appear_then_click(target_false, interval=1):
  206. continue
  207. while 1:
  208. self.screenshot()
  209. if not self.appear(target_first):
  210. break
  211. if self.appear_then_click(target_first, interval=1):
  212. continue
  213. # 点击确定退出
  214. while 1:
  215. self.screenshot()
  216. if not self.appear(self.I_CAPTION_ENSURE):
  217. break
  218. if self.appear_then_click(self.I_CAPTION_ENSURE, interval=1):
  219. continue
  220. def enter_shikigami_records(self) -> None:
  221. """
  222. 进入式神录(在探查或者是结契的界面)
  223. :return:
  224. """
  225. while 1:
  226. self.screenshot()
  227. if self.appear(self.I_CHECK_RECORDS):
  228. break
  229. if self.appear_then_click(self.I_BF_RECORDS, interval=1):
  230. continue
  231. if self.appear_then_click(self.I_BALL_RECORDS, interval=1):
  232. continue
  233. def lock_team(self):
  234. """
  235. 锁定队伍(在探查或者是结契的界面)
  236. :return:
  237. """
  238. while 1:
  239. self.screenshot()
  240. if self.appear(self.I_BALL_LOCK):
  241. break
  242. if self.appear(self.I_BF_LOCK):
  243. break
  244. if self.appear_then_click(self.I_BALL_UNLOCK, interval=1):
  245. continue
  246. if self.appear_then_click(self.I_BF_UNLOCK, interval=1):
  247. continue
  248. def get_bondling_class(self) -> BondlingClass or None:
  249. """
  250. 获取契灵的种类 (这个不应该一开始就调用因为这些会有出场动画)
  251. :return:
  252. """
  253. self.screenshot()
  254. if self.appear(self.I_TOMB_GUARD):
  255. return BondlingClass.TOMB_GUARD
  256. elif self.appear(self.I_SNOWBALL):
  257. return BondlingClass.SNOWBALL
  258. elif self.appear(self.I_LITTLE_KURO):
  259. return BondlingClass.LITTLE_KURO
  260. elif self.appear(self.I_AZURE_BASAN, threshold=0.7):
  261. return BondlingClass.AZURE_BASAN
  262. return None
  263. if __name__ == '__main__':
  264. from module.config.config import Config
  265. from module.device.device import Device
  266. import cv2
  267. config = Config('oas1')
  268. device = Device(config)
  269. task = ScriptTask(config, device)
  270. image = task.screenshot()
  271. # print(task.roi_appear_ball(task.I_BF_LOCAL_3_LITTLE_KURO.roi_back, image))
  272. # print(task.ball_number())
  273. con = config.bondling_fairyland
  274. task.run_catch(con.bondling_config, con.bondling_switch_soul)