fix: 修复开战前等待,修复错误终止任务

This commit is contained in:
秋云
2025-05-13 20:37:56 +08:00
parent dafad69e8a
commit d136e94e5c
5 changed files with 90 additions and 43 deletions

View File

@@ -2,7 +2,7 @@ const DEFAULT_RUNS = 10;
const DEFAULT_PERIOD = 25;
const DEFAULT_BASE_RUNS = 50;
const BENCHMARK_HOUR = "T04:00:00";
const DEFAULT_OCR_TIMEOUT_SECONDS = 10;
const DEFAULT_OCR_TIMEOUT_SECONDS = 30;
const DEFAULT_FIGHT_TIMEOUT_SECONDS = 120;
(async function () {
@@ -77,7 +77,6 @@ async function AutoPath(locationName) {
} catch (error) {
log.error(`执行 ${locationName} 路径时发生错误: ${error.message}`);
}
await sleep(2000);
}
// 计算运行时长
@@ -106,6 +105,10 @@ async function AutoFriendshipDev(times, ocrTimeout, fightTimeout) {
let startFirstTime = Date.now();
for (let i = 0; i < times; i++) {
await AutoPath('触发点');
// 启动路径导航任务
let pathTaskPromise = AutoPath('盗宝团');
// OCR检测
let ocrStatus = false;
let ocrStartTime = Date.now();
while (Date.now() - ocrStartTime < ocrTimeout * 1000 && !ocrStatus) {
@@ -119,16 +122,54 @@ async function AutoFriendshipDev(times, ocrTimeout, fightTimeout) {
|| res.text.includes("鬼鬼祟祟")
|| res.text.includes("盗宝团")) {
ocrStatus = true;
log.info("检测到突发任务触发");
break;
}
}
if (!ocrStatus) {
await sleep(1000);
}
}
if(ocrStatus){
log.info("检测到突发任务触发")
await AutoPath('盗宝团');
const cts = new CancellationTokenSource();
try {
const cts = new CancellationTokenSource();
// 设置最大等待时间为15秒
const maxWaitTime = 15000;
const waitStartTime = Date.now();
// 校验距离如果距离小于10米则认为已经到达目的地
const targetX = -2756.67;
const targetY = -3467.63;
const maxDistance = 10; // 10米距离判定
// 等待角色到达指定位置附近
let isNearTarget = false;
let pathTaskFinished = false;
// 简单监控路径任务完成
pathTaskPromise.then(() => {
pathTaskFinished = true;
log.info("路径任务已完成");
}).catch(error => {
pathTaskFinished = true;
log.error(`路径任务出错: ${error}`);
});
// 等待角色到达目标位置或超时
while (!isNearTarget && !pathTaskFinished && (Date.now() - waitStartTime < maxWaitTime)) {
const pos = genshin.getPositionFromMap();
if (pos) {
const distance = Math.sqrt(Math.pow(pos.x - targetX, 2) + Math.pow(pos.y - targetY, 2));
if (distance <= maxDistance) {
isNearTarget = true;
log.info(`已到达目标点附近,距离: ${distance.toFixed(2)}`);
break;
}
}
await sleep(1000);
}
log.info("开始战斗...");
const battleTask = dispatcher.RunTask(new SoloTask("AutoFight"), cts);
@@ -136,6 +177,7 @@ async function AutoFriendshipDev(times, ocrTimeout, fightTimeout) {
log.info(`战斗任务已结束,战斗结果:${fightResult}`);
cts.cancel();
} catch (error) {
cts.cancel();
log.error(`执行过程中出错: ${error}`);
}
const estimatedCompletion = CalculateEstimatedCompletion(startFirstTime, i + 1, times);
@@ -148,7 +190,6 @@ async function AutoFriendshipDev(times, ocrTimeout, fightTimeout) {
log.info(`未识别到突发任务(岛上无贼),盗宝团好感结束`);
break;
}
}
log.info('盗宝团好感已完成');
}
@@ -207,36 +248,46 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000) {
const failureKeywords = ["失败"];
const eventKeyword = ["岛上", "无贼","消灭","鬼鬼祟祟","盗宝团"];
let notFind = 0;
while (Date.now() - fightStartTime < timeout) {
try {
// 简化OCR检测只使用一个try-catch块
let result = captureGameRegion().find(RecognitionObject.ocr(850, 150, 200, 80));
let result2 = captureGameRegion().find(RecognitionObject.ocr(0, 200, 300, 300));
let text = result.text;
let text2 = result2.text;
// 检查成功关键词
for (let keyword of successKeywords) {
if (text.includes(keyword)) {
log.info("检测到战斗成功关键词: {0}", keyword);
return true;
}
}
// 检查失败关键词
for (let keyword of failureKeywords) {
if (text.includes(keyword)) {
log.warn("检测到战斗失败关键词: {0}", keyword);
return false;
}
}
// 检查事件关键词
let find = 0;
for(let keyword of eventKeyword) {
if (text2.includes(keyword)) {
find++;
}
}
if(find === 0) {
notFind++;
log.info("未检测到任务触发关键词:{0} 次", notFind);
}else{
notFind = 0;
}
if (notFind > 10) {
log.warn("不在任务触发区域,战斗失败");
return false;
@@ -244,9 +295,13 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000) {
}
catch (error) {
log.error("OCR过程中出错: {0}", error);
// 出错后继续循环,不进行额外嵌套处理
}
await sleep(1000); // 检查间隔
// 统一的检查间隔
await sleep(1000);
}
log.warn("在超时时间内未检测到战斗结果");
return false;
}