切换队伍角色优化 (#861)

-新增全为空时,直接跳过执行;
-修复角色为空时,依旧会执行替换角色的问题。
This commit is contained in:
呱呱z
2025-05-18 07:53:46 +08:00
committed by GitHub
parent cb29db81d0
commit a613fb4eb0

View File

@@ -25,15 +25,10 @@ async function scrollPage(totalDistance, stepDistance = 10, delayMs = 5) {
[460, 538], [460, 538],
[792, 538], [792, 538],
[1130, 538], [1130, 538],
[1462, 538] [1462, 538],
]; ];
const positionSettings = [ const positionSettings = [settings.position1, settings.position2, settings.position3, settings.position4];
settings.position1,
settings.position2,
settings.position3,
settings.position4
];
// 识别对象定义 // 识别对象定义
const roTeamConfig = RecognitionObject.TemplateMatch(file.ReadImageMatSync(`Assets/RecognitionObject/队伍配置.png`), 0, 0, 1920, 1080); const roTeamConfig = RecognitionObject.TemplateMatch(file.ReadImageMatSync(`Assets/RecognitionObject/队伍配置.png`), 0, 0, 1920, 1080);
@@ -43,6 +38,13 @@ async function scrollPage(totalDistance, stepDistance = 10, delayMs = 5) {
let openPairingTries = 0; let openPairingTries = 0;
let totalOpenPairingTries = 0; let totalOpenPairingTries = 0;
// 在进入角色切换逻辑前进行检测,如果所有角色设置均为空则直接退出
if (positionSettings.every((item) => !item || item.trim() === "")) {
log.info("未设置任何角色,跳过切换队伍步骤");
await genshin.returnMainUi();
return;
}
async function openPairingInterface() { async function openPairingInterface() {
while (openPairingTries < 3) { while (openPairingTries < 3) {
keyPress("l"); keyPress("l");
@@ -60,26 +62,28 @@ async function scrollPage(totalDistance, stepDistance = 10, delayMs = 5) {
openPairingTries = 0; openPairingTries = 0;
return openPairingInterface(); return openPairingInterface();
} else { } else {
log.error('无法打开配对界面,任务结束'); log.error("无法打开配对界面,任务结束");
return false; return false;
} }
} }
if (!await openPairingInterface()) { if (!(await openPairingInterface())) {
return; return;
} }
// 角色切换逻辑
for (let i = 0; i < positionSettings.length; i++) { for (let i = 0; i < positionSettings.length; i++) {
let rolenum = i+1; let rolenum = i + 1;
const selectedCharacter = positionSettings[i]; const selectedCharacter = positionSettings[i];
if (selectedCharacter === "") { if (!selectedCharacter || selectedCharacter.trim() === "") {
log.info(`未设置${rolenum}号位角色,跳过`); log.info(`未设置${rolenum}号位角色,跳过`);
continue; continue;
} }
const [x, y] = positionCoordinates[i]; const [x, y] = positionCoordinates[i];
click(x, y); click(x, y);
log.info(`开始设置${rolenum}号位角色`); log.info(`开始设置${rolenum}号位角色`);
await sleep(1000);
await sleep(1000);
let characterFound = false; let characterFound = false;
let pageTries = 0; let pageTries = 0;
@@ -88,12 +92,15 @@ async function scrollPage(totalDistance, stepDistance = 10, delayMs = 5) {
while (pageTries < 20) { while (pageTries < 20) {
// 尝试识别所有可能的角色文件名 // 尝试识别所有可能的角色文件名
for (let num = 1; ; num++) { for (let num = 1; ; num++) {
const paddedNum = num.toString().padStart(2, '0'); const paddedNum = num.toString().padStart(2, "0");
const characterFileName = `${selectedCharacter}${paddedNum}`; const characterFileName = `${selectedCharacter}${paddedNum}`;
try { try {
const characterRo = RecognitionObject.TemplateMatch( const characterRo = RecognitionObject.TemplateMatch(
file.ReadImageMatSync(`Assets/characterimage/${characterFileName}.png`), file.ReadImageMatSync(`Assets/characterimage/${characterFileName}.png`),
0, 0, 1920, 1080 0,
0,
1920,
1080
); );
const characterResult = captureGameRegion().find(characterRo); const characterResult = captureGameRegion().find(characterRo);
if (characterResult.isExist()) { if (characterResult.isExist()) {
@@ -101,11 +108,11 @@ async function scrollPage(totalDistance, stepDistance = 10, delayMs = 5) {
// 计算向右偏移70像素、向下偏移70像素的位置 // 计算向右偏移70像素、向下偏移70像素的位置
const targetX = characterResult.x + 35; const targetX = characterResult.x + 35;
const targetY = characterResult.y + 35; const targetY = characterResult.y + 35;
// 边界检查,确保坐标在屏幕范围内 // 边界检查,确保坐标在屏幕范围内
const safeX = Math.min(Math.max(targetX, 0), 1920); const safeX = Math.min(Math.max(targetX, 0), 1920);
const safeY = Math.min(Math.max(targetY, 0), 1080); const safeY = Math.min(Math.max(targetY, 0), 1080);
click(safeX, safeY); click(safeX, safeY);
await sleep(500); // 点击角色后等待0.5秒 await sleep(500); // 点击角色后等待0.5秒
characterFound = true; characterFound = true;
@@ -120,7 +127,7 @@ async function scrollPage(totalDistance, stepDistance = 10, delayMs = 5) {
if (characterFound) { if (characterFound) {
break; break;
} }
// 如果不是最后一次尝试,尝试滚动页面 // 如果不是最后一次尝试,尝试滚动页面
if (pageTries < 15) { if (pageTries < 15) {
log.info("当前页面没有目标角色,滚动页面"); log.info("当前页面没有目标角色,滚动页面");
@@ -128,7 +135,7 @@ async function scrollPage(totalDistance, stepDistance = 10, delayMs = 5) {
} }
pageTries++; pageTries++;
} }
if (!characterFound) { if (!characterFound) {
log.error(`未找到【${selectedCharacter}`); log.error(`未找到【${selectedCharacter}`);
continue; continue;
@@ -137,18 +144,18 @@ async function scrollPage(totalDistance, stepDistance = 10, delayMs = 5) {
// 识别"更换"或"加入"按钮 // 识别"更换"或"加入"按钮
const replaceResult = captureGameRegion().find(roReplace); const replaceResult = captureGameRegion().find(roReplace);
const joinResult = captureGameRegion().find(roJoin); const joinResult = captureGameRegion().find(roJoin);
if (replaceResult.isExist() || joinResult.isExist()) { if (replaceResult.isExist() || joinResult.isExist()) {
await sleep(300); await sleep(300);
click(68, 1020); click(68, 1020);
keyPress("VK_LBUTTON"); keyPress("VK_LBUTTON");
await sleep(500); await sleep(500);
} else { } else {
log.error(`该角色已在队伍中,无需切换`); log.error(`该角色已在队伍中,无需切换`);
} }
await sleep(500); await sleep(500);
} }
// 返回主界面 // 返回主界面
await genshin.returnMainUi(); await genshin.returnMainUi();
})(); })();