switch_soul.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from time import sleep
  5. from typing import Union
  6. from module.atom.click import RuleClick
  7. from module.atom.long_click import RuleLongClick
  8. from module.atom.ocr import RuleOcr
  9. from module.base.timer import Timer
  10. from tasks.base_task import BaseTask
  11. from tasks.Component.GeneralInvite.assets import GeneralInviteAssets
  12. from tasks.Component.GeneralInvite.config_invite import InviteConfig, InviteNumber, FindMode
  13. from tasks.Component.SwitchSoul.assets import SwitchSoulAssets
  14. from module.logger import logger
  15. def switch_parser(switch_str: str) -> tuple:
  16. switch_list = switch_str.split(',')
  17. if len(switch_list) != 2:
  18. raise ValueError('Switch_str must be 2 length')
  19. return int(switch_list[0]), int(switch_list[1])
  20. class SwitchSoul(BaseTask, SwitchSoulAssets):
  21. def run_switch_soul(self, target: tuple | list[tuple] | str):
  22. """
  23. 保证在式神录的界面
  24. :return:
  25. """
  26. if isinstance(target, str):
  27. try:
  28. target = switch_parser(target)
  29. except ValueError:
  30. logger.error('Switch soul config error')
  31. return
  32. self.click_preset()
  33. self.switch_souls(target)
  34. def click_preset(self) -> None:
  35. """
  36. 点击预设
  37. :return:
  38. """
  39. while 1:
  40. self.screenshot()
  41. if self.appear(self.I_SOU_SWITCH_1):
  42. break
  43. if self.appear(self.I_SOU_SWITCH_2):
  44. break
  45. if self.appear(self.I_SOU_SWITCH_3):
  46. break
  47. if self.appear(self.I_SOU_SWITCH_4):
  48. break
  49. if self.appear(self.I_SOU_TEAM_PRESENT):
  50. break
  51. if self.appear(self.I_SOUL_PRESET):
  52. self.click(self.I_SOUL_PRESET, interval=3)
  53. continue
  54. logger.info('Click preset in switch soul')
  55. def switch_soul_one(self, group: int, team: int) -> None:
  56. """
  57. 设置一个队伍的预设御魂
  58. :param group: 只能是[1-7]
  59. :param team: 只能是[1-4]
  60. :return:
  61. """
  62. def get_group_assets(group: int) -> tuple:
  63. match = {
  64. 1: tuple([self.C_SOU_GROUP_1, self.I_SOU_CHECK_GROUP_1]),
  65. 2: tuple([self.C_SOU_GROUP_2, self.I_SOU_CHECK_GROUP_2]),
  66. 3: tuple([self.C_SOU_GROUP_3, self.I_SOU_CHECK_GROUP_3]),
  67. 4: tuple([self.C_SOU_GROUP_4, self.I_SOU_CHECK_GROUP_4]),
  68. 5: tuple([self.C_SOU_GROUP_5, self.I_SOU_CHECK_GROUP_5]),
  69. 6: tuple([self.C_SOU_GROUP_6, self.I_SOU_CHECK_GROUP_6]),
  70. 7: tuple([self.C_SOU_GROUP_7, self.I_SOU_CHECK_GROUP_7]),
  71. }
  72. return match[group]
  73. def get_team_asset(team: int):
  74. match = {
  75. 1: self.I_SOU_SWITCH_1,
  76. 2: self.I_SOU_SWITCH_2,
  77. 3: self.I_SOU_SWITCH_3,
  78. 4: self.I_SOU_SWITCH_4,
  79. }
  80. return match[team]
  81. # 滑动至分组最上层(分組過多, 导致第一个分组显示不全)
  82. cur_text = ""
  83. while 1:
  84. self.screenshot()
  85. compare1 = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  86. ocr_text = str([result.ocr_text for result in compare1])
  87. # 相等时 滑动到最上层
  88. if cur_text == ocr_text:
  89. break
  90. cur_text = ocr_text
  91. # 向上滑动
  92. self.swipe(self.S_SS_GROUP_SWIPE_UP, 1.5)
  93. # 等待滑动动画
  94. sleep(0.5)
  95. if group < 1 or group > 7:
  96. raise ValueError('Switch soul_one group must be in [1-7]')
  97. if team < 1 or team > 4:
  98. raise ValueError('Switch soul_one team must be in [1-4]')
  99. # 这一步是选择组
  100. target_click, target_check = get_group_assets(group)
  101. # while 1:
  102. # self.screenshot()
  103. # if self.click(target_click, interval=1):
  104. # continue
  105. # if self.appear(target_check):
  106. # break
  107. # 2023.8.5 修改为无反馈的点击切换
  108. for i in range(2):
  109. self.click(target_click)
  110. sleep(0.5)
  111. # 点击队伍
  112. target_team = get_team_asset(team)
  113. for i in range(3):
  114. sleep(0.8)
  115. self.screenshot()
  116. if self.appear(self.I_SOU_SWITCH_SURE):
  117. while 1:
  118. self.click(self.I_SOU_SWITCH_SURE, 3)
  119. self.screenshot()
  120. if self.appear_then_click(self.I_CHECK_BLOCK, 3):
  121. continue
  122. if not self.appear(self.I_SOU_SWITCH_SURE):
  123. break
  124. continue
  125. if not self.appear_then_click(target_team, interval=3):
  126. logger.warning(f'Click team {team} failed in group {group}')
  127. # 兜底若还出现确认按钮则点击
  128. self.ui_click_until_disappear(self.I_SOU_SWITCH_SURE)
  129. logger.info(f'Switch soul_one group {group} team {team}')
  130. def switch_souls(self, target: tuple or list[tuple]) -> None:
  131. """
  132. 切换御魂
  133. :param target: [(1, 1), (2, 2), (3, 3), (4, 4)] 或者是单独的一个元组(4, 4) 第一个是组, 第二个是队伍
  134. :return:
  135. """
  136. if isinstance(target, tuple):
  137. target = [target]
  138. for group, team in target:
  139. group = int(group)
  140. team = int(team)
  141. self.switch_soul_one(group, team)
  142. def exit_shikigami_records(self) -> None:
  143. """
  144. 退出式神录的界面
  145. :return:
  146. """
  147. while 1:
  148. self.screenshot()
  149. if not self.appear(self.I_SOU_CHECK_IN):
  150. break
  151. if self.appear_then_click(self.I_RECORD_SOUL_BACK, interval=3.5):
  152. continue
  153. logger.info('Exit shikigami records')
  154. def run_switch_soul_by_name(self, groupName, teamName):
  155. """
  156. 保证在式神录的界面
  157. :return:
  158. """
  159. if isinstance(groupName, str) and isinstance(teamName, str):
  160. self.click_preset()
  161. self.switch_soul_by_name(groupName, teamName)
  162. def switch_soul_by_name(self, groupName, teamName):
  163. """
  164. 保证在式神录的界面
  165. :return:
  166. """
  167. logger.hr('Switch soul by name')
  168. # 滑动至分组最上层
  169. last_group_text = ''
  170. while 1:
  171. self.screenshot()
  172. compare1 = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  173. now_group_text = str([result.ocr_text for result in compare1])
  174. if now_group_text == last_group_text:
  175. break
  176. self.swipe(self.S_SS_GROUP_SWIPE_UP, 2)
  177. sleep(2.5)
  178. last_group_text = now_group_text
  179. logger.info('Swipe to top of group')
  180. # 判断有无目标分组
  181. while 1:
  182. self.screenshot()
  183. # 获取当前分组名
  184. results = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  185. text1 = [result.ocr_text for result in results]
  186. # 判断当前分组有无目标分组
  187. result = set(text1).intersection({groupName})
  188. # 有则跳出检测
  189. if result and len(result) > 0:
  190. break
  191. self.swipe(self.S_SS_GROUP_SWIPE_DOWN)
  192. sleep(1.5)
  193. logger.info('Swipe down to find target group')
  194. # 选中分组
  195. while 1:
  196. self.screenshot()
  197. self.O_SS_GROUP_NAME.keyword = groupName
  198. if self.ocr_appear_click(self.O_SS_GROUP_NAME):
  199. break
  200. logger.info(f'Select group {groupName}')
  201. # 滑动至阵容最上层
  202. last_team_text = ''
  203. while 1:
  204. self.screenshot()
  205. compare1 = self.O_SS_TEAM_NAME.detect_and_ocr(self.device.image)
  206. now_team_text = str([result.ocr_text for result in compare1])
  207. # 向上滑动
  208. if now_team_text == last_team_text:
  209. break
  210. self.swipe(self.S_SS_TEAM_SWIPE_DOWN, 1.5)
  211. sleep(2)
  212. last_team_text = now_team_text
  213. logger.info('Swipe to top of team')
  214. # 判断当前分组有无目标阵容
  215. while 1:
  216. self.screenshot()
  217. # 获取当前阵容名
  218. results = self.O_SS_TEAM_NAME.detect_and_ocr(self.device.image)
  219. text1 = [result.ocr_text for result in results]
  220. # 判断当前分组有无目标阵容
  221. result = set(text1).intersection({teamName})
  222. # 有则跳出检测
  223. if result and len(result) > 0:
  224. break
  225. self.swipe(self.S_SS_TEAM_SWIPE_UP, 0.3)
  226. logger.info('Swipe up to find target team')
  227. # 选中分组
  228. while 1:
  229. self.screenshot()
  230. self.O_SS_TEAM_NAME.keyword = teamName
  231. if self.ocr_appear_click(self.O_SS_TEAM_NAME):
  232. break
  233. logger.info(f'Select team {teamName}')
  234. # 切换御魂
  235. cnt_click: int = 0
  236. self.O_SS_TEAM_NAME.keyword = teamName
  237. while 1:
  238. self.screenshot()
  239. if cnt_click >= 4:
  240. break
  241. if self.appear_then_click(self.I_SOU_SWITCH_SURE, interval=0.8):
  242. continue
  243. if self.ocr_appear_click_by_rule(self.O_SS_TEAM_NAME, self.I_SOU_CLICK_PRESENT, interval=1.5):
  244. cnt_click += 1
  245. continue
  246. logger.info(f'Switch soul_one group {groupName} team {teamName}')
  247. def ocr_appear_click_by_rule(self,
  248. target: RuleOcr,
  249. action: Union[RuleClick, RuleLongClick] = None,
  250. interval: float = None,
  251. duration: float = None) -> bool:
  252. """
  253. ocr识别目标,如果目标存在,则触发动作
  254. :param target:
  255. :param action:
  256. :param interval:
  257. :param duration:
  258. :return:
  259. """
  260. appear = self.ocr_appear(target, interval)
  261. if not appear:
  262. return False
  263. x1, y1, w1, h1 = target.area
  264. x, y = action.coord()
  265. self.device.click(x=x, y=y1, control_name=target.name)
  266. return True
  267. if __name__ == '__main__':
  268. from module.config.config import Config
  269. from module.device.device import Device
  270. c = Config('oas1')
  271. d = Device(c)
  272. s = SwitchSoul(c, d)
  273. s.click_preset()
  274. # s.switch_soul_one(4, 1)
  275. # s.switch_soul_by_name('契灵', '茨球')
  276. s.switch_soul_by_name('默认分组', '队伍5')