switch_soul.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_then_click(self.I_SOUL_PRESET, interval=1):
  52. continue
  53. logger.info('Click preset in switch soul')
  54. def switch_soul_one(self, group: int, team: int) -> None:
  55. """
  56. 设置一个队伍的预设御魂
  57. :param group: 只能是[1-7]
  58. :param team: 只能是[1-4]
  59. :return:
  60. """
  61. def get_group_assets(group: int) -> tuple:
  62. match = {
  63. 1: tuple([self.C_SOU_GROUP_1, self.I_SOU_CHECK_GROUP_1]),
  64. 2: tuple([self.C_SOU_GROUP_2, self.I_SOU_CHECK_GROUP_2]),
  65. 3: tuple([self.C_SOU_GROUP_3, self.I_SOU_CHECK_GROUP_3]),
  66. 4: tuple([self.C_SOU_GROUP_4, self.I_SOU_CHECK_GROUP_4]),
  67. 5: tuple([self.C_SOU_GROUP_5, self.I_SOU_CHECK_GROUP_5]),
  68. 6: tuple([self.C_SOU_GROUP_6, self.I_SOU_CHECK_GROUP_6]),
  69. 7: tuple([self.C_SOU_GROUP_7, self.I_SOU_CHECK_GROUP_7]),
  70. }
  71. return match[group]
  72. def get_team_asset(team: int):
  73. match = {
  74. 1: self.I_SOU_SWITCH_1,
  75. 2: self.I_SOU_SWITCH_2,
  76. 3: self.I_SOU_SWITCH_3,
  77. 4: self.I_SOU_SWITCH_4,
  78. }
  79. return match[team]
  80. # 滑动至分组最上层(分組過多, 导致第一个分组显示不全)
  81. while 1:
  82. self.screenshot()
  83. compare1 = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  84. text1 = str([result.ocr_text for result in compare1])
  85. # 向上滑动
  86. self.swipe(self.S_SS_GROUP_SWIPE_UP, 6)
  87. self.screenshot()
  88. compare2 = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  89. text2 = str([result.ocr_text for result in compare2])
  90. # 相等时 滑动到最上层
  91. if text1 == text2:
  92. break
  93. if group < 1 or group > 7:
  94. raise ValueError('Switch soul_one group must be in [1-7]')
  95. if team < 1 or team > 4:
  96. raise ValueError('Switch soul_one team must be in [1-4]')
  97. # 这一步是选择组
  98. target_click, target_check = get_group_assets(group)
  99. # while 1:
  100. # self.screenshot()
  101. # if self.click(target_click, interval=1):
  102. # continue
  103. # if self.appear(target_check):
  104. # break
  105. # 2023.8.5 修改为无反馈的点击切换
  106. for i in range(2):
  107. self.click(target_click)
  108. sleep(0.5)
  109. # 点击队伍
  110. target_team = get_team_asset(team)
  111. for i in range(3):
  112. sleep(0.8)
  113. self.screenshot()
  114. if self.appear_then_click(self.I_SOU_SWITCH_SURE, interval=1):
  115. continue
  116. if not self.appear_then_click(target_team, interval=1):
  117. logger.warning(f'Click team {team} failed in group {group}')
  118. logger.info(f'Switch soul_one group {group} team {team}')
  119. def switch_souls(self, target: tuple or list[tuple]) -> None:
  120. """
  121. 切换御魂
  122. :param target: [(1, 1), (2, 2), (3, 3), (4, 4)] 或者是单独的一个元组(4, 4) 第一个是组, 第二个是队伍
  123. :return:
  124. """
  125. if isinstance(target, tuple):
  126. target = [target]
  127. for group, team in target:
  128. group = int(group)
  129. team = int(team)
  130. self.switch_soul_one(group, team)
  131. def exit_shikigami_records(self) -> None:
  132. """
  133. 退出式神录的界面
  134. :return:
  135. """
  136. while 1:
  137. self.screenshot()
  138. if not self.appear(self.I_SOU_CHECK_IN):
  139. break
  140. if self.appear_then_click(self.I_RECORD_SOUL_BACK, interval=1):
  141. continue
  142. logger.info('Exit shikigami records')
  143. def run_switch_soul_by_name(self, groupName, teamName):
  144. """
  145. 保证在式神录的界面
  146. :return:
  147. """
  148. if isinstance(groupName, str) and isinstance(teamName, str):
  149. self.click_preset()
  150. self.switch_soul_by_name(groupName, teamName)
  151. def switch_soul_by_name(self, groupName, teamName):
  152. """
  153. 保证在式神录的界面
  154. :return:
  155. """
  156. logger.hr('Switch soul by name')
  157. # 滑动至分组最上层
  158. last_group_text = ''
  159. while 1:
  160. self.screenshot()
  161. compare1 = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  162. now_group_text = str([result.ocr_text for result in compare1])
  163. if now_group_text == last_group_text:
  164. break
  165. self.swipe(self.S_SS_GROUP_SWIPE_UP, 2)
  166. sleep(2.5)
  167. last_group_text = now_group_text
  168. logger.info('Swipe to top of group')
  169. # 判断有无目标分组
  170. while 1:
  171. self.screenshot()
  172. # 获取当前分组名
  173. results = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
  174. text1 = [result.ocr_text for result in results]
  175. # 判断当前分组有无目标分组
  176. result = set(text1).intersection({groupName})
  177. # 有则跳出检测
  178. if result and len(result) > 0:
  179. break
  180. self.swipe(self.S_SS_GROUP_SWIPE_DOWN)
  181. sleep(1.5)
  182. logger.info('Swipe down to find target group')
  183. # 选中分组
  184. while 1:
  185. self.screenshot()
  186. self.O_SS_GROUP_NAME.keyword = groupName
  187. if self.ocr_appear_click(self.O_SS_GROUP_NAME):
  188. break
  189. logger.info(f'Select group {groupName}')
  190. # 滑动至阵容最上层
  191. last_team_text = ''
  192. while 1:
  193. self.screenshot()
  194. compare1 = self.O_SS_TEAM_NAME.detect_and_ocr(self.device.image)
  195. now_team_text = str([result.ocr_text for result in compare1])
  196. # 向上滑动
  197. if now_team_text == last_team_text:
  198. break
  199. self.swipe(self.S_SS_TEAM_SWIPE_DOWN, 1.5)
  200. sleep(2)
  201. last_team_text = now_team_text
  202. logger.info('Swipe to top of team')
  203. # 判断当前分组有无目标阵容
  204. while 1:
  205. self.screenshot()
  206. # 获取当前阵容名
  207. results = self.O_SS_TEAM_NAME.detect_and_ocr(self.device.image)
  208. text1 = [result.ocr_text for result in results]
  209. # 判断当前分组有无目标阵容
  210. result = set(text1).intersection({teamName})
  211. # 有则跳出检测
  212. if result and len(result) > 0:
  213. break
  214. self.swipe(self.S_SS_TEAM_SWIPE_UP)
  215. logger.info('Swipe up to find target team')
  216. # 选中分组
  217. while 1:
  218. self.screenshot()
  219. self.O_SS_TEAM_NAME.keyword = teamName
  220. if self.ocr_appear_click(self.O_SS_TEAM_NAME):
  221. break
  222. logger.info(f'Select team {teamName}')
  223. # 切换御魂
  224. for i in range(5):
  225. sleep(0.8)
  226. self.screenshot()
  227. self.O_SS_TEAM_NAME.keyword = teamName
  228. if self.ocr_appear_click_by_rule(self.O_SS_TEAM_NAME, self.I_SOU_CLICK_PRESENT, interval=1):
  229. continue
  230. if self.appear_then_click(self.I_SOU_SWITCH_SURE, interval=1):
  231. break
  232. logger.info(f'Switch soul_one group {groupName} team {teamName}')
  233. def ocr_appear_click_by_rule(self,
  234. target: RuleOcr,
  235. action: Union[RuleClick, RuleLongClick] = None,
  236. interval: float = None,
  237. duration: float = None) -> bool:
  238. """
  239. ocr识别目标,如果目标存在,则触发动作
  240. :param target:
  241. :param action:
  242. :param interval:
  243. :param duration:
  244. :return:
  245. """
  246. appear = self.ocr_appear(target, interval)
  247. if not appear:
  248. return False
  249. x1, y1, w1, h1 = target.area
  250. x, y, w, h = action.roi_back
  251. self.device.click(x=x, y=y1, control_name=target.name)
  252. return True
  253. if __name__ == '__main__':
  254. from module.config.config import Config
  255. from module.device.device import Device
  256. c = Config('oas1')
  257. d = Device(c)
  258. s = SwitchSoul(c, d)
  259. s.click_preset()
  260. # s.switch_soul_one(4, 1)
  261. # s.switch_soul_by_name('契灵', '茨球')
  262. s.switch_soul_by_name('默认分组', '队伍5')