| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- # This Python file uses the following encoding: utf-8
- # @author runhey
- # github https://github.com/runhey
- from time import sleep
- from typing import Union
- from module.atom.click import RuleClick
- from module.atom.long_click import RuleLongClick
- from module.atom.ocr import RuleOcr
- from module.base.timer import Timer
- from tasks.base_task import BaseTask
- from tasks.Component.GeneralInvite.assets import GeneralInviteAssets
- from tasks.Component.GeneralInvite.config_invite import InviteConfig, InviteNumber, FindMode
- from tasks.Component.SwitchSoul.assets import SwitchSoulAssets
- from module.logger import logger
- def switch_parser(switch_str: str) -> tuple:
- switch_list = switch_str.split(',')
- if len(switch_list) != 2:
- raise ValueError('Switch_str must be 2 length')
- return int(switch_list[0]), int(switch_list[1])
- class SwitchSoul(BaseTask, SwitchSoulAssets):
- def run_switch_soul(self, target: tuple | list[tuple] | str):
- """
- 保证在式神录的界面
- :return:
- """
- if isinstance(target, str):
- try:
- target = switch_parser(target)
- except ValueError:
- logger.error('Switch soul config error')
- return
- self.click_preset()
- self.switch_souls(target)
- def click_preset(self) -> None:
- """
- 点击预设
- :return:
- """
- while 1:
- self.screenshot()
- if self.appear(self.I_SOU_SWITCH_1):
- break
- if self.appear(self.I_SOU_SWITCH_2):
- break
- if self.appear(self.I_SOU_SWITCH_3):
- break
- if self.appear(self.I_SOU_SWITCH_4):
- break
- if self.appear(self.I_SOU_TEAM_PRESENT):
- break
- if self.appear(self.I_SOUL_PRESET):
- self.click(self.I_SOUL_PRESET, interval=3)
- continue
- logger.info('Click preset in switch soul')
- def switch_soul_one(self, group: int, team: int) -> None:
- """
- 设置一个队伍的预设御魂
- :param group: 只能是[1-7]
- :param team: 只能是[1-4]
- :return:
- """
- def get_group_assets(group: int) -> tuple:
- match = {
- 1: tuple([self.C_SOU_GROUP_1, self.I_SOU_CHECK_GROUP_1]),
- 2: tuple([self.C_SOU_GROUP_2, self.I_SOU_CHECK_GROUP_2]),
- 3: tuple([self.C_SOU_GROUP_3, self.I_SOU_CHECK_GROUP_3]),
- 4: tuple([self.C_SOU_GROUP_4, self.I_SOU_CHECK_GROUP_4]),
- 5: tuple([self.C_SOU_GROUP_5, self.I_SOU_CHECK_GROUP_5]),
- 6: tuple([self.C_SOU_GROUP_6, self.I_SOU_CHECK_GROUP_6]),
- 7: tuple([self.C_SOU_GROUP_7, self.I_SOU_CHECK_GROUP_7]),
- }
- return match[group]
- def get_team_asset(team: int):
- match = {
- 1: self.I_SOU_SWITCH_1,
- 2: self.I_SOU_SWITCH_2,
- 3: self.I_SOU_SWITCH_3,
- 4: self.I_SOU_SWITCH_4,
- }
- return match[team]
- # 滑动至分组最上层(分組過多, 导致第一个分组显示不全)
- cur_text = ""
- while 1:
- self.screenshot()
- compare1 = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
- ocr_text = str([result.ocr_text for result in compare1])
- # 相等时 滑动到最上层
- if cur_text == ocr_text:
- break
- cur_text = ocr_text
- # 向上滑动
- self.swipe(self.S_SS_GROUP_SWIPE_UP, 1.5)
- # 等待滑动动画
- sleep(0.5)
- if group < 1 or group > 7:
- raise ValueError('Switch soul_one group must be in [1-7]')
- if team < 1 or team > 4:
- raise ValueError('Switch soul_one team must be in [1-4]')
- # 这一步是选择组
- target_click, target_check = get_group_assets(group)
- # while 1:
- # self.screenshot()
- # if self.click(target_click, interval=1):
- # continue
- # if self.appear(target_check):
- # break
- # 2023.8.5 修改为无反馈的点击切换
- for i in range(2):
- self.click(target_click)
- sleep(0.5)
- # 点击队伍
- target_team = get_team_asset(team)
- for i in range(3):
- sleep(0.8)
- self.screenshot()
- if self.appear(self.I_SOU_SWITCH_SURE):
- while 1:
- self.click(self.I_SOU_SWITCH_SURE, 3)
- self.screenshot()
- if self.appear_then_click(self.I_CHECK_BLOCK, 3):
- continue
- if not self.appear(self.I_SOU_SWITCH_SURE):
- break
- continue
- if not self.appear_then_click(target_team, interval=3):
- logger.warning(f'Click team {team} failed in group {group}')
- # 兜底若还出现确认按钮则点击
- self.ui_click_until_disappear(self.I_SOU_SWITCH_SURE)
- logger.info(f'Switch soul_one group {group} team {team}')
- def switch_souls(self, target: tuple or list[tuple]) -> None:
- """
- 切换御魂
- :param target: [(1, 1), (2, 2), (3, 3), (4, 4)] 或者是单独的一个元组(4, 4) 第一个是组, 第二个是队伍
- :return:
- """
- if isinstance(target, tuple):
- target = [target]
- for group, team in target:
- group = int(group)
- team = int(team)
- self.switch_soul_one(group, team)
- def exit_shikigami_records(self) -> None:
- """
- 退出式神录的界面
- :return:
- """
- while 1:
- self.screenshot()
- if not self.appear(self.I_SOU_CHECK_IN):
- break
- if self.appear_then_click(self.I_RECORD_SOUL_BACK, interval=3.5):
- continue
- logger.info('Exit shikigami records')
- def run_switch_soul_by_name(self, groupName, teamName):
- """
- 保证在式神录的界面
- :return:
- """
- if isinstance(groupName, str) and isinstance(teamName, str):
- self.click_preset()
- self.switch_soul_by_name(groupName, teamName)
- def switch_soul_by_name(self, groupName, teamName):
- """
- 保证在式神录的界面
- :return:
- """
- logger.hr('Switch soul by name')
- # 滑动至分组最上层
- last_group_text = ''
- while 1:
- self.screenshot()
- compare1 = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
- now_group_text = str([result.ocr_text for result in compare1])
- if now_group_text == last_group_text:
- break
- self.swipe(self.S_SS_GROUP_SWIPE_UP, 2)
- sleep(2.5)
- last_group_text = now_group_text
- logger.info('Swipe to top of group')
- # 判断有无目标分组
- while 1:
- self.screenshot()
- # 获取当前分组名
- results = self.O_SS_GROUP_NAME.detect_and_ocr(self.device.image)
- text1 = [result.ocr_text for result in results]
- # 判断当前分组有无目标分组
- result = set(text1).intersection({groupName})
- # 有则跳出检测
- if result and len(result) > 0:
- break
- self.swipe(self.S_SS_GROUP_SWIPE_DOWN)
- sleep(1.5)
- logger.info('Swipe down to find target group')
- # 选中分组
- while 1:
- self.screenshot()
- self.O_SS_GROUP_NAME.keyword = groupName
- if self.ocr_appear_click(self.O_SS_GROUP_NAME):
- break
- logger.info(f'Select group {groupName}')
- # 滑动至阵容最上层
- last_team_text = ''
- while 1:
- self.screenshot()
- compare1 = self.O_SS_TEAM_NAME.detect_and_ocr(self.device.image)
- now_team_text = str([result.ocr_text for result in compare1])
- # 向上滑动
- if now_team_text == last_team_text:
- break
- self.swipe(self.S_SS_TEAM_SWIPE_DOWN, 1.5)
- sleep(2)
- last_team_text = now_team_text
- logger.info('Swipe to top of team')
- # 判断当前分组有无目标阵容
- while 1:
- self.screenshot()
- # 获取当前阵容名
- results = self.O_SS_TEAM_NAME.detect_and_ocr(self.device.image)
- text1 = [result.ocr_text for result in results]
- # 判断当前分组有无目标阵容
- result = set(text1).intersection({teamName})
- # 有则跳出检测
- if result and len(result) > 0:
- break
- self.swipe(self.S_SS_TEAM_SWIPE_UP, 0.3)
- logger.info('Swipe up to find target team')
- # 选中分组
- while 1:
- self.screenshot()
- self.O_SS_TEAM_NAME.keyword = teamName
- if self.ocr_appear_click(self.O_SS_TEAM_NAME):
- break
- logger.info(f'Select team {teamName}')
- # 切换御魂
- cnt_click: int = 0
- self.O_SS_TEAM_NAME.keyword = teamName
- while 1:
- self.screenshot()
- if cnt_click >= 4:
- break
- if self.appear_then_click(self.I_SOU_SWITCH_SURE, interval=0.8):
- continue
- if self.ocr_appear_click_by_rule(self.O_SS_TEAM_NAME, self.I_SOU_CLICK_PRESENT, interval=1.5):
- cnt_click += 1
- continue
- logger.info(f'Switch soul_one group {groupName} team {teamName}')
- def ocr_appear_click_by_rule(self,
- target: RuleOcr,
- action: Union[RuleClick, RuleLongClick] = None,
- interval: float = None,
- duration: float = None) -> bool:
- """
- ocr识别目标,如果目标存在,则触发动作
- :param target:
- :param action:
- :param interval:
- :param duration:
- :return:
- """
- appear = self.ocr_appear(target, interval)
- if not appear:
- return False
- x1, y1, w1, h1 = target.area
- x, y = action.coord()
- self.device.click(x=x, y=y1, control_name=target.name)
- return True
- if __name__ == '__main__':
- from module.config.config import Config
- from module.device.device import Device
- c = Config('oas1')
- d = Device(c)
- s = SwitchSoul(c, d)
- s.click_preset()
- # s.switch_soul_one(4, 1)
- # s.switch_soul_by_name('契灵', '茨球')
- s.switch_soul_by_name('默认分组', '队伍5')
|