fix: 修复战斗任务取消逻辑,增加异常处理

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
秋云
2026-05-01 21:02:44 +08:00
parent 94cc989f88
commit 02b22be73f

View File

@@ -663,7 +663,7 @@ async function executeBattleTasks(fightTimeout, enemyType, cts, battlePointCoord
// 同步战斗模式:依赖配置组的“战斗结束检测”让 AutoFight 自行退出
// 额外增加 watchdog超过 fightTimeout 仍未退出则取消任务,避免无限战斗
const maxDetectMs = Math.max(0, Number(fightTimeout) * 1000);
battleTask = dispatcher.RunTask(new SoloTask("AutoFight"), cts);
battleTask = dispatcher.runTask(new SoloTask("AutoFight"), cts);
const battleWrapped = battleTask
.then(value => ({ kind: "battle_fulfilled", value }))
.catch(error => ({ kind: "battle_rejected", error }));
@@ -691,7 +691,7 @@ async function executeBattleTasks(fightTimeout, enemyType, cts, battlePointCoord
}
} else {
// 异步战斗模式:并发启动战斗 + OCR 检测结果;检测到结果后取消战斗任务
battleTask = dispatcher.RunTask(new SoloTask("AutoFight"), cts);
battleTask = dispatcher.runTask(new SoloTask("AutoFight"), cts);
battleDetectTask = waitForBattleResult(fightTimeout * 1000, enemyType, cts, battlePointCoords);
const maxDetectMs = Math.max(0, Number(fightTimeout) * 1000);
const graceMs = Math.min(8000, maxDetectMs);
@@ -1189,7 +1189,7 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000, enemyType = "盗宝
text2 = text2 ? text2.replace(/\s+/g, "") : "";
if (enemyType === "蕈兽" && text2.includes("维沙瓦")) {
log.info("战斗结果:成功");
cts.cancel();
try{ cts.cancel(); } catch{} // 取消任务
return "success";
}
@@ -1199,7 +1199,7 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000, enemyType = "盗宝
if (text.includes(keyword)) {
log.info("检测到战斗成功关键词: {0}", keyword);
log.info("战斗结果:成功");
cts.cancel(); // 取消任务
try{ cts.cancel(); } catch{} // 取消任务
return "success";
}
}
@@ -1209,7 +1209,7 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000, enemyType = "盗宝
for (let keyword of failureKeywords) {
if (text.includes(keyword)) {
log.warn("检测到战斗失败关键词: {0}", keyword);
cts.cancel(); // 取消任务
try{ cts.cancel(); } catch{} // 取消任务
return "failure";
}
}
@@ -1243,12 +1243,12 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000, enemyType = "盗宝
if (nearBattlePoint) {
log.info("触发关键词消失但仍在战斗点附近,视为本轮结束");
cts.cancel();
try{ cts.cancel(); } catch{} // 取消任务
return "success";
}
log.warn("不在任务触发区域,战斗失败");
cts.cancel(); // 取消任务
try{ cts.cancel(); } catch{} // 取消任务
return "out_of_area";
}
@@ -1269,7 +1269,7 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000, enemyType = "盗宝
}
log.warn("在超时时间内未检测到战斗结果");
cts.cancel(); // 取消任务
try{ cts.cancel(); } catch{} // 取消任务
throw createScriptError(ERROR_CODES.BATTLE_TIMEOUT, ERR_MESSAGES.BATTLE_TIMEOUT);
}