fix(LeyLine): 4.1.4 取消自动任务报错

This commit is contained in:
秋云
2025-08-25 23:26:24 +08:00
parent 14061f0701
commit fbb9859c45
6 changed files with 115 additions and 75 deletions

View File

@@ -47,6 +47,11 @@ wgc的bug请使用bitblt截图方式脚本只支持这个
打开大地图,点击左下角设置,开启自定义标记。
脚本非正常结束运行时会出现该问题。
## 更新日志
### 4.1.4
修复取消自动任务报错
### 4.1.3
修复v8报错
### 4.1
通过冒险之证找花 - 测试
### 4.0

View File

@@ -28,6 +28,7 @@ const ocrRo3 = RecognitionObject.ocr(ocrRegion3.x, ocrRegion3.y, ocrRegion3.widt
const ocrRoThis = RecognitionObject.ocrThis;
/**
* 主函数 - 脚本入口点
* 1. 全局异常处理,记录日志并发送通知
*/
(async function () {
try {
@@ -37,7 +38,7 @@ const ocrRoThis = RecognitionObject.ocrThis;
// 全局错误捕获,记录并发送错误日志
log.error("出错了: {error}", error.message);
if (isNotification) {
notification.error("出错了: {error}", error.message);
notification.error("出错了: ${error.message}");
}
}
finally {
@@ -76,7 +77,6 @@ function initialize() {
"findLeyLineOutcrop.js",
"findLeyLineOutcropByBook.js",
"loadSettings.js",
"locateLeyLineOutcrop.js",
"processLeyLineOutcrop.js",
"recognizeTextInRegion.js"
];
@@ -131,6 +131,7 @@ async function prepareForLeyLineRun() {
async function runLeyLineChallenges() {
while (currentRunTimes < settings.timesValue) {
// 寻找地脉花位置
// 数据保存在全局变量中 leyLineXleyLineY
if (settings.useAdventurerHandbook) {
await findLeyLineOutcropByBook(settings.country, settings.leyLineOutcropType);
} else {
@@ -542,7 +543,18 @@ async function autoFight(timeout) {
logFightResult = fightResult ? "成功" : "失败";
log.info(`战斗结束,战斗结果:${logFightResult}`);
cts.cancel();
await fightTask;
try {
await fightTask;
} catch (error) {
// 忽略取消任务产生的异常
if (error.message && error.message.includes("取消")) {
log.debug("战斗任务已正常取消");
} else {
log.warn(`战斗任务结束时出现异常: ${error.message}`);
}
}
return fightResult;
}

View File

@@ -1,7 +1,7 @@
{
"manifest_version": 1,
"name": "全自动地脉花",
"version": "4.1.3",
"version": "4.1.4",
"tags": ["地脉花"],
"bgi_version": "0.44.7",
"description": "基于OCR图像识别的全自动刷取地脉花。\n💡更多信息请查看在线手册https://hcnsvf0s8d0s.feishu.cn/wiki/Tb1twpThLi7UlykqcYOcuccTnjJ \n\n----------注意事项----------\n●仅支持BetterGI 0.44.7 及以上版本!\n●部分地脉花因特殊原因不支持全自动具体的点位请在手册中查看。\n●树脂使用的优先级2倍原粹树脂 > 浓缩树脂 > 原粹树脂。\n●运行时会传送到七天神像设置中设置的七天神像需要关闭七天神像设置中的“是否就近七天神像恢复血量”并指定七天神像。\n●战斗策略注意调度器设置中地图追踪行走配置里的“允许在JsSpript中使用”和“覆盖JS中的自动战斗配置”只有在都打开的情况下脚本才会使用下面的战斗配置否则会使用独立任务中的战斗策略。战斗超时时间不能大于脚本自定义配置中的时间。\n\n如果遇到问题请先参照手册中的方法进行解决。",

View File

@@ -30,4 +30,38 @@ async function (country, type) {
// 如果到这里还没找到
throw new Error("寻找地脉花失败,已达最大重试次数");
}
/**
* 在地图上定位地脉花
* @param {string} type - 地脉花类型
* @returns {Promise<boolean>} 是否找到地脉花
*/
this.locateLeyLineOutcrop =
async function (type) {
await sleep(500); // 确保画面稳定
await genshin.setBigMapZoomLevel(3.0);
const iconPath = type === "蓝花(经验书)"
? "assets/icon/Blossom_of_Revelation.png"
: "assets/icon/Blossom_of_Wealth.png";
const flowerList = captureGameRegion().findMulti(RecognitionObject.TemplateMatch(file.ReadImageMatSync(iconPath)));
if (flowerList && flowerList.count > 0) {
currentFlower = flowerList[0];
const center = genshin.getPositionFromBigMap();
const mapZoomLevel = genshin.getBigMapZoomLevel();
const mapScaleFactor = 2.361;
leyLineX = (960 - currentFlower.x - 25) * mapZoomLevel / mapScaleFactor + center.x;
leyLineY = (540 - currentFlower.y - 25) * mapZoomLevel / mapScaleFactor + center.y;
log.info(`找到地脉花的坐标:(${leyLineX}, ${leyLineY})`);
return true;
} else {
log.warn("未找到地脉花");
return false;
}
}

View File

@@ -1,34 +0,0 @@
/**
* 在地图上定位地脉花
* @param {string} type - 地脉花类型
* @returns {Promise<boolean>} 是否找到地脉花
*/
this.locateLeyLineOutcrop =
async function (type) {
await sleep(500); // 确保画面稳定
await genshin.setBigMapZoomLevel(3.0);
const iconPath = type === "蓝花(经验书)"
? "assets/icon/Blossom_of_Revelation.png"
: "assets/icon/Blossom_of_Wealth.png";
const flowerList = captureGameRegion().findMulti(RecognitionObject.TemplateMatch(file.ReadImageMatSync(iconPath)));
if (flowerList && flowerList.count > 0) {
currentFlower = flowerList[0];
const flowerType = type === "蓝花(经验书)" ? "经验" : "摩拉";
const center = genshin.getPositionFromBigMap();
const mapZoomLevel = genshin.getBigMapZoomLevel();
const mapScaleFactor = 2.361;
leyLineX = (960 - currentFlower.x - 25) * mapZoomLevel / mapScaleFactor + center.x;
leyLineY = (540 - currentFlower.y - 25) * mapZoomLevel / mapScaleFactor + center.y;
log.info(`找到地脉花的坐标:(${leyLineX}, ${leyLineY})`);
return true;
} else {
log.warn("未找到地脉花");
return false;
}
}

View File

@@ -7,45 +7,68 @@
*/
this.processLeyLineOutcrop =
async function (timeout, targetPath, retries = 0) {
// 设置最大重试次数,防止死循环
const MAX_RETRIES = 3;
let captureRegion = null;
try {
// 检查重试次数,避免死循环
if (retries >= MAX_RETRIES) {
const errorMsg = `开启地脉花失败,已重试${MAX_RETRIES}次,终止处理`;
log.error("我辣么大一个地脉花哪去了?");
throw new Error(errorMsg);
}
// 如果超过最大重试次数,记录错误并返回,避免死循环
if (retries >= MAX_RETRIES) {
log.error(`开启地脉花失败,已重试${MAX_RETRIES}次,终止处理`);
log.error("我辣么大一个地脉花哪去了?");
throw new Error("开启地脉花失败");
}
let captureRegion = captureGameRegion();
let result = captureRegion.find(ocrRo2);
let result2 = captureRegion.find(ocrRo3);
if (result2.text.includes("地脉之花")) {
log.info("识别到地脉之花");
await switchToFriendshipTeamIfNeeded();
return;
}
if (result2.text.includes("地脉溢口")) {
log.info("识别到地脉溢口");
keyPress("F");
await sleep(300);
keyPress("F"); // 两次重试避免开花失败
await sleep(500);
} else if (result.text.includes("打倒所有敌人")) {
log.info("地脉花已经打开,直接战斗");
} else {
log.warn(`未识别到地脉花文本,当前重试次数: ${retries + 1}/${MAX_RETRIES}`);
try {
await pathingScript.runFile(targetPath);
await processLeyLineOutcrop(timeout, targetPath, retries + 1);
// 截图并识别
captureRegion = captureGameRegion();
const result = captureRegion.find(ocrRo2);
const result2 = captureRegion.find(ocrRo3);
// 检查地脉之花状态 - 已完成状态,准备领取奖励
if (result2.text.includes("地脉之花")) {
log.info("识别到地脉之花,准备领取奖励");
await switchToFriendshipTeamIfNeeded();
return;
} catch (error) {
throw new Error(`未识别到地脉花: ${error.message}`);
}
// 处理地脉溢口
if (result2.text.includes("地脉溢口")) {
log.info("识别到地脉溢口");
keyPress("F");
await sleep(300);
keyPress("F");
await sleep(500);
} else if (result.text.includes("打倒所有敌人")) {
log.info("地脉花已经打开,直接战斗");
} else {
// 未识别到目标,需要重新导航
log.warn("未识别到地脉花文本,尝试重新导航");
await pathingScript.runFile(targetPath);
return await this.processLeyLineOutcrop(timeout, targetPath, retries + 1);
}
// 执行战斗
const fightResult = await autoFight(timeout);
if (!fightResult) {
throw new Error("战斗失败");
}
// 完成后续操作
await switchToFriendshipTeamIfNeeded();
await autoNavigateToReward();
} catch (error) {
// 保留原始错误信息
const errorMsg = `地脉花处理失败 (重试${retries}/${MAX_RETRIES}): ${error.message}`;
log.error(errorMsg);
throw error;
} finally {
// 确保资源释放
if (captureRegion) {
try {
captureRegion.dispose();
} catch (disposeError) {
log.warn(`截图资源释放失败: ${disposeError.message}`);
}
}
}
if(!await autoFight(timeout)){
throw new Error("战斗失败");
}
await switchToFriendshipTeamIfNeeded();
await autoNavigateToReward();
}
}