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

@@ -1,6 +1,6 @@
{ {
"info": { "info": {
"name": "盗宝团", "name": "触发点",
"type": "collect", "type": "collect",
"author": "HZYgrandma", "author": "HZYgrandma",
"version": "1.0", "version": "1.0",
@@ -24,14 +24,6 @@
"move_mode": "walk", "move_mode": "walk",
"action": "exit_and_relogin" "action": "exit_and_relogin"
}, },
{
"id": 3,
"x": -2739.71,
"y": -3399.06,
"type": "path",
"move_mode": "dash",
"action": ""
},
{ {
"id": 4, "id": 4,
"x": -2738.67, "x": -2738.67,

View File

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

View File

@@ -1,7 +1,7 @@
{ {
"manifest_version": 1, "manifest_version": 1,
"name": "盗宝团好感:自动好感度&刷盗宝团&卡时间", "name": "盗宝团好感:自动好感度&刷盗宝团&卡时间",
"version": "1.2.1", "version": "1.2.2",
"tags": ["好感", "盗宝团", "突发事件"], "tags": ["好感", "盗宝团", "突发事件"],
"description": "通过突发事件【岛上无贼】刷好感度,刷盗宝团材料,小怪锄地,卡时间等,请配合战斗脚本使用。在 HZYgrandma & 愚溪的原始脚本上改编。当前版本可能无法以任意方式拾取掉落物,如果有拾取掉落物需求,请勿更新。", "description": "通过突发事件【岛上无贼】刷好感度,刷盗宝团材料,小怪锄地,卡时间等,请配合战斗脚本使用。在 HZYgrandma & 愚溪的原始脚本上改编。当前版本可能无法以任意方式拾取掉落物,如果有拾取掉落物需求,请勿更新。",
"authors": [ "authors": [

View File

@@ -2,7 +2,7 @@
{ {
"name": "qiuQiuRen", "name": "qiuQiuRen",
"type": "checkbox", "type": "checkbox",
"label": "是否清理丘丘人【默认否,选是会清理丘丘人之后再开始盗宝团好感】" "label": "是否清理丘丘人\n【默认否,选是会清理丘丘人之后再开始盗宝团好感】"
}, },
{ {
"name": "partyName", "name": "partyName",
@@ -12,31 +12,31 @@
{ {
"name": "runTimes", "name": "runTimes",
"type": "input-text", "type": "input-text",
"label": "(选填)运行次数【默认10次卡时间模式默认50次】" "label": "(选填)运行次数\n【默认10次卡时间模式默认50次】"
}, },
{ {
"name": "waitTimeMode", "name": "waitTimeMode",
"type": "checkbox", "type": "checkbox",
"label": "卡时间模式【基于运行次数、基准日期、周期确定需要运行次数】" "label": "卡时间模式\n【基于运行次数、基准日期、周期确定需要运行次数】"
}, },
{ {
"name": "waitTimeModeDay", "name": "waitTimeModeDay",
"type": "input-text", "type": "input-text",
"label": "卡时间模式基准日期【格式参考2025-01-01】" "label": "卡时间模式基准日期\n【格式参考2025-01-01】"
}, },
{ {
"name": "waitTimeModePeriod", "name": "waitTimeModePeriod",
"type": "input-text", "type": "input-text",
"label": "(选填)卡时间模式周期【默认25天】" "label": "(选填)卡时间模式周期\n【默认25天】"
}, },
{ {
"name": "ocrTimeout", "name": "ocrTimeout",
"type": "input-text", "type": "input-text",
"label": "OCR超时时间选填,默认为10秒" "label": "OCR超时时间\n【选填,默认为30秒,如果经常提前判定未识别到任务,请适当调大】"
}, },
{ {
"name": "fightTimeOut", "name": "fightTimeOut",
"type": "input-text", "type": "input-text",
"label": "战斗超时时间选填默认为120秒" "label": "战斗超时时间\n【选填默认为120秒"
} }
] ]