switch_soul.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 or list[tuple]):
  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, 0.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 not self.appear(self.I_SOU_SWITCH_SURE):
  121. break
  122. continue
  123. if not self.appear_then_click(target_team, interval=1):
  124. logger.warning(f'Click team {team} failed in group {group}')
  125. logger.info(f'Switch soul_one group {group} team {team}')
  126. def switch_souls(self, target: tuple or list[tuple]) -> None:
  127. """
  128. 切换御魂
  129. :param target: [(1, 1), (2, 2), (3, 3), (4, 4)] 或者是单独的一个元组(4, 4) 第一个是组, 第二个是队伍
  130. :return:
  131. """
  132. if isinstance(target, tuple):
  133. target = [target]
  134. for group, team in target:
  135. group = int(group)
  136. team = int(team)
  137. self.switch_soul_one(group, team)
  138. def exit_shikigami_records(self) -> None:
  139. """
  140. 退出式神录的界面
  141. :return:
  142. """
  143. while 1:
  144. self.screenshot()
  145. if not self.appear(self.I_SOU_CHECK_IN):
  146. break
  147. if self.appear_then_click(self.I_RECORD_SOUL_BACK, interval=1):
  148. continue
  149. logger.info('Exit shikigami records')
  150. def run_switch_soul_by_name(self, groupName, teamName):
  151. """
  152. 保证在式神录的界面
  153. :return:
  154. """
  155. if isinstance(groupName, str) and isinstance(teamName, str):
  156. self.click_preset()
  157. self.switch_soul_by_name(groupName, teamName)
  158. def switch_soul_by_name(self, groupName, teamName):
  159. """
  160. 保证在式神录的界面
  161. :return:
  162. """
  163. logger.hr('Switch soul by name')
  164. # 滑动至分组最上层
  165. last_group_text = ''
  166. while 1:
  167. self.screenshot()
  168. compare1 = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  169. now_group_text = str([result.ocr_text for result in compare1])
  170. if now_group_text == last_group_text:
  171. break
  172. self.swipe(self.S_SS_GROUP_SWIPE_UP, 2)
  173. sleep(2.5)
  174. last_group_text = now_group_text
  175. logger.info('Swipe to top of group')
  176. # 判断有无目标分组
  177. while 1:
  178. self.screenshot()
  179. # 获取当前分组名
  180. results = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  181. text1 = [result.ocr_text for result in results]
  182. # 判断当前分组有无目标分组
  183. result = set(text1).intersection({groupName})
  184. # 有则跳出检测
  185. if result and len(result) > 0:
  186. break
  187. self.swipe(self.S_SS_GROUP_SWIPE_DOWN)
  188. sleep(1.5)
  189. logger.info('Swipe down to find target group')
  190. # 选中分组
  191. while 1:
  192. self.screenshot()
  193. self.O_SS_GROUP_NAME.keyword = groupName
  194. if self.ocr_appear_click(self.O_SS_GROUP_NAME):
  195. break
  196. logger.info(f'Select group {groupName}')
  197. # 滑动至阵容最上层
  198. last_team_text = ''
  199. while 1:
  200. self.screenshot()
  201. compare1 = self.O_SS_TEAM_NAME.detect_and_ocr(self.device.image)
  202. now_team_text = str([result.ocr_text for result in compare1])
  203. # 向上滑动
  204. if now_team_text == last_team_text:
  205. break
  206. self.swipe(self.S_SS_TEAM_SWIPE_DOWN, 1.5)
  207. sleep(2)
  208. last_team_text = now_team_text
  209. logger.info('Swipe to top of team')
  210. # 判断当前分组有无目标阵容
  211. while 1:
  212. self.screenshot()
  213. # 获取当前阵容名
  214. results = self.O_SS_TEAM_NAME.detect_and_ocr(self.device.image)
  215. text1 = [result.ocr_text for result in results]
  216. # 判断当前分组有无目标阵容
  217. result = set(text1).intersection({teamName})
  218. # 有则跳出检测
  219. if result and len(result) > 0:
  220. break
  221. self.swipe(self.S_SS_TEAM_SWIPE_UP)
  222. logger.info('Swipe up to find target team')
  223. # 选中分组
  224. while 1:
  225. self.screenshot()
  226. self.O_SS_TEAM_NAME.keyword = teamName
  227. if self.ocr_appear_click(self.O_SS_TEAM_NAME):
  228. break
  229. logger.info(f'Select team {teamName}')
  230. # 切换御魂
  231. cnt_click: int = 0
  232. self.O_SS_TEAM_NAME.keyword = teamName
  233. while 1:
  234. self.screenshot()
  235. if cnt_click >= 4:
  236. break
  237. if self.appear_then_click(self.I_SOU_SWITCH_SURE, interval=0.8):
  238. continue
  239. if self.ocr_appear_click_by_rule(self.O_SS_TEAM_NAME, self.I_SOU_CLICK_PRESENT, interval=1.5):
  240. cnt_click += 1
  241. continue
  242. logger.info(f'Switch soul_one group {groupName} team {teamName}')
  243. def ocr_appear_click_by_rule(self,
  244. target: RuleOcr,
  245. action: Union[RuleClick, RuleLongClick] = None,
  246. interval: float = None,
  247. duration: float = None) -> bool:
  248. """
  249. ocr识别目标,如果目标存在,则触发动作
  250. :param target:
  251. :param action:
  252. :param interval:
  253. :param duration:
  254. :return:
  255. """
  256. appear = self.ocr_appear(target, interval)
  257. if not appear:
  258. return False
  259. x1, y1, w1, h1 = target.area
  260. x, y = action.coord()
  261. self.device.click(x=x, y=y1, control_name=target.name)
  262. return True
  263. if __name__ == '__main__':
  264. from module.config.config import Config
  265. from module.device.device import Device
  266. c = Config('oas1')
  267. d = Device(c)
  268. s = SwitchSoul(c, d)
  269. s.click_preset()
  270. # s.switch_soul_one(4, 1)
  271. # s.switch_soul_by_name('契灵', '茨球')
  272. s.switch_soul_by_name('默认分组', '队伍5')