mirror of
https://github.com/babalae/bettergi-scripts-list.git
synced 2026-11-16 19:39:51 +00:00
* refactor(AutoPlan): 重构体力计划相关变量命名 - 将 autoOrderSet 参数重命名为 auto_plan_set - 将 autoFightOrderSet 变量重命名为 auto_plan_set - 将 from 变量重命名为 auto_plan_list - 更新相关日志输出中的变量名称 - 统一使用 plan 相关术语以提高代码一致性 fix(AutoPlan): 修复异常处理逻辑 - 添加配置检查来控制异常抛出行为 - 实现排除异常与循环计划的互斥逻辑 - 确保在特定配置下正确抛出异常 fix(AutoPlan): 修复检查运行中的异常处理 - 在 try-catch 块中添加错误捕获机制 - 使用 Log.error 记录异常消息 - 确保程序在发生错误时能够正常继续执行 finally 块 fix(AutoPlan): 调整添加按钮的位置和尺寸 - 将按钮x坐标从1373调整为1248 - 将按钮y坐标从22调整为21 - 将按钮宽度从52调整为50 - 将按钮高度从49调整为50 * refactor(AutoPlan): 注释掉检查运行配置中的通知选项 - 临时禁用 adventurer handbook 使用选项 - 保留 transient resin 使用功能 - 移除 notification 通知功能的配置项 - 优化配置键值构建逻辑 feat(AutoPlan): 移除通知和冒险之证功能并更新版本 - 注释掉 useAdventurerHandbook 和 isNotification 配置项 - 移除相关的参数解析和赋值逻辑 - 更新插件版本从 0.1.3 到 0.1.4 - 更新 bgi_version 从 0.63.0 到 0.64.2 * refactor(AutoPlan): 调整配置参数结构并更新版本 - 移除使用冒险家之书和详细通知配置选项 - 修正复活重试次数字段名(rviveRetryCount -> reviveRetryCount) - 更新配置表格参数编号,调整Boss类型参数起始位置 - 添加0.1.4版本更新记录,适配BGI本体API改动 - 修复OCR识别问题并更新版本兼容性说明 * docs(AutoPlan): 更新文档中的图片路径 - 将所有图片路径从 md/ 目录移动到 assets/md/ 目录 - 更新 README.md 中 check 和 record 图片的引用路径 - 更新 bgi-tools 登录和配置界面截图的引用路径 - 更新圣遗物阈值检查和执行记录功能截图的引用路径 * fix(security): 更新插件密钥配置 - 替换了旧的加密密钥为新的安全密钥 - 修复了密钥轮换后的主要密钥配置 - 更新了备用密钥的存储值以确保安全性
710 lines
29 KiB
JavaScript
710 lines
29 KiB
JavaScript
import {getJsonPath, toMainUi, throwError, findImgAndClick, Log, drawBoxDebug} from "./tool";
|
||
//====================================================
|
||
const genshinJson = {
|
||
width: 1920,//genshin.width,
|
||
height: 1080,//genshin.height,
|
||
}
|
||
// const MinPhysical = settings.minPhysical?parseInt(settings.minPhysical+''):parseInt(20+'')
|
||
// const OpenModeCountMin = settings.openModeCountMin
|
||
// let AlreadyRunsCount=0
|
||
// let NeedRunsCount=0
|
||
const TemplateOrcJson = {x: 1568, y: 16, width: 225, height: 60,}
|
||
|
||
// ==================== 常量定义 ====================
|
||
|
||
// 树脂图标识别对象
|
||
const RESIN_ICONS = {
|
||
ORIGINAL: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/resin/original_resin.png")),
|
||
CONDENSED: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/resin/condensed_resin.png")),
|
||
FRAGILE: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/resin/fragile_resin.png")),
|
||
TRANSIENT: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/resin/transient_resin.png")),
|
||
REPLENISH_BUTTON: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/replenish_resin_button.png"))
|
||
};
|
||
|
||
// 普通数字识别对象(1-4)
|
||
const NUMBER_ICONS = [
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num1.png")), value: 1},
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num2.png")), value: 2},
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num3.png")), value: 3},
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num4.png")), value: 4}
|
||
];
|
||
|
||
// 白色数字识别对象(0-5,用于浓缩树脂)
|
||
const WHITE_NUMBER_ICONS = [
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num0_white.png")), value: 0},
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num1_white.png")), value: 1},
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num2_white.png")), value: 2},
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num3_white.png")), value: 3},
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num4_white.png")), value: 4},
|
||
{ro: RecognitionObject.TemplateMatch(file.ReadImageMatSync("assets/num/num5_white.png")), value: 5}
|
||
];
|
||
|
||
// 配置常量
|
||
const CONFIG = {
|
||
RECOGNITION_TIMEOUT: 2000, // 图像识别超时时间(毫秒)
|
||
SLEEP_INTERVAL: 500, // 循环间隔时间(毫秒)
|
||
UI_DELAY: 1500, // UI操作延迟时间(毫秒)
|
||
MAP_ZOOM_LEVEL: 6, // 地图缩放级别
|
||
|
||
// 点击坐标
|
||
COORDINATES: {
|
||
MAP_SWITCH: {x: 1840, y: 1020}, // 地图右下角切换按钮
|
||
MONDSTADT: {x: 1420, y: 180}, // 蒙德选择按钮
|
||
AVOID_SELECTION: {x: 1090, y: 450} // 避免选中效果的点击位置
|
||
},
|
||
|
||
// OCR识别区域配置
|
||
OCR_REGIONS: {
|
||
ORIGINAL_RESIN: {width: 200, height: 40},
|
||
CONDENSED_RESIN: {width: 90, height: 40},
|
||
OTHER_RESIN: {width: 0, height: 60} // width会根据图标宽度动态设置
|
||
}
|
||
};
|
||
const PHYSICAL = {
|
||
MAX: 200
|
||
}
|
||
|
||
//====================================================
|
||
export class Physical {
|
||
/**
|
||
* 从字符串中提取数字并转换为整数
|
||
* @param {string} str - 需要处理的字符串
|
||
* @param {number} [defaultValue=0] - 当无法提取数字时的默认返回值
|
||
* @returns {number} 返回提取到的数字,如果无法提取则返回默认值
|
||
*/
|
||
static async saveOnlyNumber(str, defaultValue = 0) {
|
||
// 使用正则表达式匹配字符串中的所有数字
|
||
// \d+ 匹配一个或多个数字
|
||
// .join('') 将匹配到的数字数组连接成一个字符串
|
||
// parseInt 将连接后的字符串转换为整数
|
||
try {
|
||
return parseInt(str.match(/\d+/g).join(''));
|
||
} catch (e) {
|
||
// 如果发生异常(例如输入不是字符串或无法匹配数字),返回默认值
|
||
return defaultValue
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 识别游戏中原粹树脂(体力)的功能函数
|
||
* @param {boolean} [opToMainUi=false] - 是否操作返回主界面
|
||
* @param {boolean} [openMap=false] - 是否打开地图界面
|
||
* @param {number} [minPhysical=20] - 最小可执行体力值
|
||
* @param {boolean} [isResinExhaustionMode=true] - 是否启用体力识别模式
|
||
* @returns {Promise<Object|undefined>} 返回包含识别结果的对象或undefined
|
||
*/
|
||
static async ocrPhysical(opToMainUi = false, openMap = false, minPhysical = 20, isResinExhaustionMode = true) {
|
||
// 检查是否启用体力识别功能,如果未启用则直接返回默认结果
|
||
if (!isResinExhaustionMode) {
|
||
Log.info(`===未启用===`)
|
||
return {
|
||
ok: true,
|
||
min: 0,
|
||
current: 0,
|
||
}
|
||
}
|
||
Log.debug(`===开始识别原粹树脂===`)
|
||
let ms = 1000 // 定义操作延迟时间(毫秒)
|
||
// 如果需要操作返回主界面
|
||
if (opToMainUi) {
|
||
await sleep(ms)
|
||
await toMainUi(); // 切换到主界面
|
||
}
|
||
|
||
// 如果需要打开地图界面
|
||
if (openMap) {
|
||
await sleep(ms)
|
||
//打开地图界面
|
||
await keyPress('M')
|
||
}
|
||
await sleep(ms)
|
||
Log.debug(`===[点击+]===`)
|
||
// //点击+ 按钮 x=1264,y=39,width=18,height=19
|
||
let add_buttonJSON = getJsonPath('add_button');
|
||
let add_objJson = {
|
||
path: `${add_buttonJSON.path}${add_buttonJSON.name}${add_buttonJSON.type}`,
|
||
x: 1248,
|
||
y: 21,
|
||
width: 50,
|
||
height: 50,
|
||
}
|
||
//1248, 21, 50, 50
|
||
//
|
||
// let templateMatchAddButtonRo = RecognitionObject.TemplateMatch(file.ReadImageMatSync(`${add_objJson.path}`), add_objJson.x, add_objJson.y, add_objJson.width, add_objJson.height);
|
||
// let regionA = captureGameRegion()
|
||
// // let deriveCrop = regionA.DeriveCrop(add_objJson.x, add_objJson.y, add_objJson.width, add_objJson.height);
|
||
// try {
|
||
// let buttonA = regionA.find(templateMatchAddButtonRo);
|
||
//
|
||
// await sleep(ms)
|
||
// if (!buttonA.isExist()) {
|
||
// Log.error(`${add_objJson.path}匹配异常`)
|
||
// throwError(`${add_objJson.path}匹配异常`)
|
||
// }
|
||
// await buttonA.click()
|
||
// } finally {
|
||
// // deriveCrop.dispose()
|
||
// regionA.dispose()
|
||
// }
|
||
await drawBoxDebug(settings.debug, add_objJson,20)
|
||
const addClick = await findImgAndClick(`${add_objJson.path}`, add_objJson.x, add_objJson.y, add_objJson.width, add_objJson.height);
|
||
if (addClick === null) {
|
||
Log.error(`${add_objJson.path}匹配异常`)
|
||
return undefined
|
||
}
|
||
await sleep(ms)
|
||
|
||
Log.debug(`===[定位原粹树脂]===`)
|
||
//定位月亮
|
||
let jsonPath = getJsonPath('yue');
|
||
let tmJson = {
|
||
path: `${jsonPath.path}${jsonPath.name}${jsonPath.type}`,
|
||
x: TemplateOrcJson.x,
|
||
y: TemplateOrcJson.y,
|
||
width: TemplateOrcJson.width,
|
||
height: TemplateOrcJson.height,
|
||
}
|
||
let templateMatchButtonRo = RecognitionObject.TemplateMatch(file.ReadImageMatSync(`${tmJson.path}`), tmJson.x, tmJson.y, tmJson.width, tmJson.height);
|
||
let region = captureGameRegion()
|
||
let button
|
||
try {
|
||
button = region.find(templateMatchButtonRo);
|
||
await sleep(ms)
|
||
if ((!button) || !button.isExist()) {
|
||
Log.error(`${tmJson.path} 匹配异常`)
|
||
// throwError(`${tmJson.path} 匹配异常`)
|
||
return undefined
|
||
}
|
||
} finally {
|
||
region.dispose()
|
||
}
|
||
|
||
|
||
Log.debug(`===[识别原粹树脂]===`)
|
||
//识别体力 x=1625,y=31,width=79,height=30 / x=1689,y=35,width=15,height=26
|
||
let ocr_obj = {
|
||
// x: 1623,
|
||
x: button.x + button.width,
|
||
// y: 32,
|
||
y: button.y,
|
||
// width: 61,
|
||
width: Math.abs(genshinJson.width - button.x - button.width),
|
||
height: 26
|
||
}
|
||
|
||
Log.debug(`ocr_obj: x={x},y={y},width={width},height={height}`, ocr_obj.x, ocr_obj.y, ocr_obj.width, ocr_obj.height)
|
||
let region3 = captureGameRegion()
|
||
|
||
try {
|
||
let recognitionObjectOcr = RecognitionObject.ocr(ocr_obj.x, ocr_obj.y, ocr_obj.width, ocr_obj.height);
|
||
let res = region3.find(recognitionObjectOcr);
|
||
|
||
Log.debug(`[OCR原粹树脂]识别结果: ${res.text}, 原始坐标: x=${res.x}, y=${res.y},width:${res.width},height:${res.height}`);
|
||
let text = "0"
|
||
|
||
if (!res.text.includes('/')) {
|
||
//识别异常处理 误识别 /200 => 1200 (/被误识别为1)
|
||
const regExp = `(\\d+)${PHYSICAL.MAX}`
|
||
//1.移除200
|
||
const match = res.text.match(regExp);
|
||
if (match) {
|
||
//2.移除 误识别 /=>1
|
||
text = match[1].substring(0, match[1].length - 1);
|
||
}
|
||
} else {
|
||
text = res.text.split('/')[0]
|
||
}
|
||
|
||
|
||
let current = await Physical.saveOnlyNumber(text)
|
||
let execute = (current - minPhysical) >= 0
|
||
Log.debug(`最小可执行原粹树脂:{min},原粹树脂:{key}`, minPhysical, current,)
|
||
|
||
// await keyPress('VK_ESCAPE')
|
||
return {
|
||
ok: execute,
|
||
min: minPhysical,
|
||
current: current,
|
||
}
|
||
} catch (e) {
|
||
// throwError(`识别失败,err:${e.message}`)
|
||
Log.error(`识别失败,err:${e.message}`)
|
||
return undefined
|
||
} finally {
|
||
region3.dispose()
|
||
//返回地图操作
|
||
if (opToMainUi) {
|
||
await toMainUi(); // 切换到主界面
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 打开地图界面的静态异步方法
|
||
* 该方法用于在游戏中打开地图并进行相关设置
|
||
*/
|
||
static async openMap() {
|
||
// 记录日志信息,表示正在打开地图界面
|
||
Log.info("打开地图界面");
|
||
// 模拟按下M键打开地图
|
||
await keyPress("M");
|
||
// 等待UI界面加载完成,等待时间由配置文件中的UI_DELAY决定
|
||
await sleep(CONFIG.UI_DELAY);
|
||
|
||
// 切换到国家选择界面
|
||
// click(CONFIG.COORDINATES.MAP_SWITCH.x, CONFIG.COORDINATES.MAP_SWITCH.y);
|
||
// await sleep(CONFIG.UI_DELAY);
|
||
|
||
// 选择蒙德
|
||
// click(CONFIG.COORDINATES.MONDSTADT.x, CONFIG.COORDINATES.MONDSTADT.y);
|
||
// await sleep(CONFIG.UI_DELAY);
|
||
// await switchtoCountrySelection(CONFIG.COORDINATES.MONDSTADT.x, CONFIG.COORDINATES.MONDSTADT.y)
|
||
|
||
// 设置地图缩放级别,排除识别干扰
|
||
await genshin.setBigMapZoomLevel(CONFIG.MAP_ZOOM_LEVEL);
|
||
Log.info("地图界面设置完成");
|
||
}
|
||
|
||
/**
|
||
* 统计浓缩树脂数量
|
||
* @returns {number} 浓缩树脂数量
|
||
*/
|
||
static async countCondensedResin() {
|
||
const condensedResin = await Physical.recognizeImage(RESIN_ICONS.CONDENSED);
|
||
if (!condensedResin) {
|
||
Log.warn(`未找到浓缩树脂图标`);
|
||
return 0;
|
||
}
|
||
|
||
const ocrRegion = {
|
||
x: condensedResin.x,
|
||
y: condensedResin.y,
|
||
width: CONFIG.OCR_REGIONS.CONDENSED_RESIN.width,
|
||
height: CONFIG.OCR_REGIONS.CONDENSED_RESIN.height
|
||
};
|
||
|
||
// 首先尝试OCR识别
|
||
const ocrCount = await Physical.recognizeNumberByOCR(ocrRegion, /\d+/);
|
||
if (ocrCount !== null) {
|
||
Log.info(`浓缩树脂数量: ${ocrCount}`);
|
||
return ocrCount;
|
||
}
|
||
|
||
// OCR识别失败,尝试白色数字图片识别
|
||
Log.info(`OCR识别浓缩树脂失败,尝试白色数字图片识别`);
|
||
const imageCount = await Physical.recognizeWhiteNumberByImage(ocrRegion);
|
||
if (imageCount !== null) {
|
||
Log.info(`浓缩树脂数量(白色数字图片识别): ${imageCount}`);
|
||
return imageCount;
|
||
}
|
||
|
||
Log.info(`白色数字图片识别识别浓缩树脂失败,尝试在说明界面获取`);
|
||
// 点击浓缩树脂打开说明界面统计
|
||
condensedResin.click();
|
||
await sleep(CONFIG.UI_DELAY);
|
||
let captureRegion = captureGameRegion();
|
||
let textList = null;
|
||
try {
|
||
// OCR识别整个界面的文本
|
||
let ocrRo = RecognitionObject.Ocr(0, 0, captureRegion.width, captureRegion.height);
|
||
textList = captureRegion.findMulti(ocrRo);
|
||
|
||
for (const res of textList) {
|
||
if (res.text.includes("当前拥有")) {
|
||
const match = res.text.match(/当前拥有\s*([0-5ss])/);
|
||
if (match && match[1]) {
|
||
const count = parseInt(match[1]);
|
||
Log.info(`浓缩树脂数量(说明界面): ${count}`);
|
||
keyPress("ESCAPE");
|
||
await sleep(CONFIG.UI_DELAY);
|
||
return count;
|
||
}
|
||
}
|
||
}
|
||
} finally {
|
||
if (textList && typeof textList.dispose === 'function') {
|
||
textList?.dispose();
|
||
}
|
||
captureRegion?.dispose();
|
||
}
|
||
|
||
Log.warn(`未能识别浓缩树脂数量`);
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* 统计须臾树脂数量
|
||
* @returns {number} 须臾树脂数量
|
||
*/
|
||
static async countTransientResin() {
|
||
const transientResin = await Physical.recognizeImage(RESIN_ICONS.TRANSIENT);
|
||
if (!transientResin) {
|
||
Log.warn(`未找到须臾树脂图标`);
|
||
return 0;
|
||
}
|
||
|
||
const ocrRegion = {
|
||
x: transientResin.x,
|
||
y: transientResin.y + transientResin.height,
|
||
width: transientResin.width,
|
||
height: CONFIG.OCR_REGIONS.OTHER_RESIN.height
|
||
};
|
||
|
||
return await Physical.countResinWithFallback(ocrRegion, "须臾树脂", Physical.recognizeNumberByImage);
|
||
}
|
||
|
||
/**
|
||
* 统计脆弱树脂数量
|
||
* @returns {number} 脆弱树脂数量
|
||
*/
|
||
static async countFragileResin() {
|
||
const fragileResin = await Physical.recognizeImage(RESIN_ICONS.FRAGILE);
|
||
if (!fragileResin) {
|
||
Log.warn(`未找到脆弱树脂图标`);
|
||
return 0;
|
||
}
|
||
|
||
const ocrRegion = {
|
||
x: fragileResin.x,
|
||
y: fragileResin.y + fragileResin.height,
|
||
width: fragileResin.width,
|
||
height: CONFIG.OCR_REGIONS.OTHER_RESIN.height
|
||
};
|
||
|
||
return await Physical.countResinWithFallback(ocrRegion, "脆弱树脂", Physical.recognizeNumberByImage);
|
||
}
|
||
|
||
|
||
/**
|
||
* 统计游戏中的所有树脂类型的数量
|
||
* 包括原粹树脂、浓缩树脂、须臾树脂和脆弱树脂
|
||
* @returns {Promise<{originalResinCount: 0,// 原粹树脂数量
|
||
* condensedResinCount: 0,// 浓缩树脂数量
|
||
* transientResinCount: 0,// 须臾树脂数量
|
||
* fragileResinCount: 0// 脆弱树脂数量
|
||
* }>} 返回包含各种树脂数量的对象
|
||
*/
|
||
static async countAllResin() {
|
||
let shouldRestoreMainUi = false // 标记是否需要恢复主界面
|
||
try {
|
||
// setGameMetrics(1920, 1080, 1); // 设置游戏显示参数
|
||
// Log.info("开始统计树脂数量"); // 记录开始统计的日志
|
||
let resinCounts = { // 存储各种树脂数量的对象
|
||
original: 0, // 原粹树脂数量
|
||
transient: 0, // 须臾树脂数量
|
||
fragile: 0, // 脆弱树脂数量
|
||
condensed: 0 // 浓缩树脂数量
|
||
}
|
||
await toMainUi(); // 切换到主界面
|
||
await sleep(CONFIG.UI_DELAY); // 等待界面加载
|
||
shouldRestoreMainUi = true // 设置需要恢复主界面的标志
|
||
// 打开地图界面统计原粹/浓缩树脂
|
||
await Physical.openMap(); // 打开地图界面
|
||
await sleep(CONFIG.UI_DELAY); // 等待界面加载
|
||
let tryPass = true; // 标记第一次尝试是否成功
|
||
try {
|
||
// Log.info("[开始]统计补充树脂界面中的树脂"); // 记录开始统计的日志
|
||
resinCounts.original = await Physical.countOriginalResin(false, false); // 统计原粹树脂数量
|
||
moveMouseTo(CONFIG.COORDINATES.AVOID_SELECTION.x, CONFIG.COORDINATES.AVOID_SELECTION.y) // 移动鼠标到指定位置
|
||
await sleep(500); // 等待500毫秒
|
||
resinCounts.transient = await Physical.countTransientResin(); // 统计须臾树脂数量
|
||
resinCounts.fragile = await Physical.countFragileResin(); // 统计脆弱树脂数量
|
||
Log.info("[完成]统计补充树脂界面中的树脂"); // 记录完成统计的日志
|
||
// 点击避免选中效果影响统计
|
||
click(CONFIG.COORDINATES.AVOID_SELECTION.x, CONFIG.COORDINATES.AVOID_SELECTION.y); // 点击指定位置
|
||
} catch (e) {
|
||
tryPass = false // 如果发生异常,标记第一次尝试失败
|
||
}
|
||
await sleep(CONFIG.UI_DELAY); // 等待界面加载
|
||
Log.info("开始统计地图界面中的树脂"); // 记录开始统计的日志
|
||
if (!tryPass) {
|
||
// 如果第一次尝试失败,则切换到蒙德
|
||
await Physical.switchtoCountrySelection(CONFIG.COORDINATES.MONDSTADT.x, CONFIG.COORDINATES.MONDSTADT.y) // 切换到蒙德
|
||
resinCounts.original = await Physical.countOriginalResin(!tryPass); // 重新统计原粹树脂数量
|
||
}
|
||
resinCounts.condensed = await Physical.countCondensedResin(); // 统计浓缩树脂数量
|
||
if (!tryPass) {
|
||
// 打开补充树脂界面统计须臾/脆弱树脂
|
||
await Physical.openReplenishResinUi(); // 打开补充树脂界面
|
||
await sleep(CONFIG.UI_DELAY); // 等待界面加载
|
||
|
||
// 点击避免选中效果影响统计
|
||
click(CONFIG.COORDINATES.AVOID_SELECTION.x, CONFIG.COORDINATES.AVOID_SELECTION.y); // 点击指定位置
|
||
await sleep(500); // 等待500毫秒
|
||
|
||
Log.info("开始统计补充树脂界面中的树脂"); // 记录开始统计的日志
|
||
resinCounts.transient = await Physical.countTransientResin(); // 统计须臾树脂数量
|
||
resinCounts.fragile = await Physical.countFragileResin(); // 统计脆弱树脂数量
|
||
}
|
||
// 显示结果
|
||
Physical.displayResults(resinCounts); // 显示统计结果
|
||
|
||
// 返回主界面
|
||
await genshin.returnMainUi(); // 返回主界面
|
||
await sleep(CONFIG.UI_DELAY); // 等待界面加载
|
||
|
||
Log.info("树脂统计完成"); // 记录完成统计的日志
|
||
return { // 返回包含各种树脂数量的对象
|
||
originalResinCount: resinCounts.original, // 原粹树脂数量
|
||
condensedResinCount: resinCounts.condensed, // 浓缩树脂数量
|
||
transientResinCount: resinCounts.transient, // 须臾树脂数量
|
||
fragileResinCount: resinCounts.fragile // 脆弱树脂数量
|
||
};
|
||
|
||
} catch (error) { // 捕获异常
|
||
Log.error(`统计树脂数量时发生异常: ${error.message}`); // 记录错误信息
|
||
throw error; // 抛出异常
|
||
} finally { // 无论是否发生异常都会执行
|
||
if (shouldRestoreMainUi) { // 如果需要恢复主界面
|
||
await toMainUi(); // 切换到主界面
|
||
await sleep(CONFIG.UI_DELAY); // 等待界面加载
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 切换到国家选择界面的异步方法
|
||
* @param {number} x - 目标位置的x坐标
|
||
* @param {number} y - 目标位置的y坐标
|
||
* @returns {Promise<void>} - 返回一个Promise,表示异步操作的完成
|
||
*/
|
||
static async switchtoCountrySelection(x, y) {
|
||
// 切换到国家选择界面
|
||
click(CONFIG.COORDINATES.MAP_SWITCH.x, CONFIG.COORDINATES.MAP_SWITCH.y);
|
||
await sleep(CONFIG.UI_DELAY);
|
||
click(x, y);
|
||
await sleep(CONFIG.UI_DELAY);
|
||
}
|
||
|
||
/**
|
||
* 打开补充树脂界面
|
||
*/
|
||
static async openReplenishResinUi() {
|
||
Log.info("尝试打开补充树脂界面");
|
||
const replenishResinButton = await Physical.recognizeImage(RESIN_ICONS.REPLENISH_BUTTON);
|
||
if (replenishResinButton) {
|
||
replenishResinButton.Click();
|
||
Log.info("成功打开补充树脂界面");
|
||
} else {
|
||
Log.warn("未找到补充树脂按钮");
|
||
}
|
||
}
|
||
|
||
static displayResults(results) {
|
||
const resultText = `原粹:${results.original} 浓缩:${results.condensed} 须臾:${results.transient} 脆弱:${results.fragile}`;
|
||
|
||
Log.info(`============ 树脂统计结果 ============`);
|
||
Log.info(`原粹树脂数量: ${results.original}`);
|
||
Log.info(`浓缩树脂数量: ${results.condensed}`);
|
||
Log.info(`须臾树脂数量: ${results.transient}`);
|
||
Log.info(`脆弱树脂数量: ${results.fragile}`);
|
||
Log.info(`====================================`);
|
||
}
|
||
|
||
static async countOriginalResin(tryOriginalMode, opToMainUi, openMap) {
|
||
if (tryOriginalMode) {
|
||
Log.info("尝试使用原始模式");
|
||
return await Physical.countOriginalResinBackup()
|
||
} else {
|
||
Log.info('尝试使用优化模式');
|
||
let ocr_physical = await Physical.ocrPhysical(opToMainUi, openMap);
|
||
|
||
Log.debug(`ocrPhysical: {0}`, JSON.stringify(ocr_physical))
|
||
await sleep(600)
|
||
// ocrPhysical = false//模拟异常
|
||
if (ocr_physical/* && ocrPhysical.ok*/) {
|
||
return ocr_physical?.current;
|
||
} else {
|
||
//异常 退出至地图 尝试使用原始模式
|
||
// await keyPress("VK_ESCAPE")
|
||
Log.error(`ocrPhysical error`);
|
||
throw new Error("ocrPhysical error");
|
||
}
|
||
}
|
||
}
|
||
|
||
static async countOriginalResinBackup() {
|
||
const originalResin = await Physical.recognizeImage(RESIN_ICONS.ORIGINAL);
|
||
if (!originalResin) {
|
||
Log.warn(`未找到原粹树脂图标`);
|
||
return 0;
|
||
}
|
||
|
||
const ocrRegion = {
|
||
x: originalResin.x,
|
||
y: originalResin.y,
|
||
width: CONFIG.OCR_REGIONS.ORIGINAL_RESIN.width,
|
||
height: CONFIG.OCR_REGIONS.ORIGINAL_RESIN.height
|
||
};
|
||
|
||
// 匹配 xxx/200 格式中的第一个数字(1-3位)
|
||
const count = await Physical.recognizeNumberByOCR(ocrRegion, /(\d{1,3})\/\d+/);
|
||
if (count !== null) {
|
||
Log.info(`原粹树脂数量: ${count}`);
|
||
return count;
|
||
}
|
||
|
||
Log.warn(`未能识别原粹树脂数量`);
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* 通用树脂计数函数(带图片识别回退)
|
||
* @param {Object} ocrRegion - OCR识别区域
|
||
* @param {string} resinType - 树脂类型名称
|
||
* @param {Function} fallbackFunction - 回退识别函数
|
||
* @returns {number} 树脂数量
|
||
*/
|
||
static async countResinWithFallback(ocrRegion, resinType, fallbackFunction) {
|
||
// 首先尝试OCR识别
|
||
const ocrCount = await Physical.recognizeNumberByOCR(ocrRegion, /\d+/);
|
||
if (ocrCount !== null) {
|
||
Log.info(`${resinType}数量: ${ocrCount}`);
|
||
return ocrCount;
|
||
}
|
||
|
||
// OCR识别失败,尝试图片识别
|
||
Log.info(`OCR识别${resinType}失败,尝试图片识别`);
|
||
const imageCount = await fallbackFunction(ocrRegion);
|
||
if (imageCount !== null) {
|
||
Log.info(`${resinType}数量(图片识别): ${imageCount}`);
|
||
return imageCount;
|
||
}
|
||
|
||
Log.warn(`未能识别${resinType}数量`);
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* 检查点是否在指定区域内
|
||
* @param {Object} point - 点坐标 {x, y}
|
||
* @param {Object} region - 区域 {x, y, width, height}
|
||
* @returns {boolean} 是否在区域内
|
||
*/
|
||
static isPointInRegion(point, region) {
|
||
return point.x >= region.x &&
|
||
point.x <= region.x + region.width &&
|
||
point.y >= region.y &&
|
||
point.y <= region.y + region.height;
|
||
}
|
||
|
||
/**
|
||
* 异步识别图像
|
||
* @param {Object} recognitionObject - 识别对象,包含图像特征信息
|
||
* @param {number} [timeout=CONFIG.RECOGNITION_TIMEOUT] - 识别超时时间(毫秒)
|
||
* @returns {Promise<Object|null>} 识别结果对象,若超时或失败则返回 null
|
||
*/
|
||
static async recognizeImage(recognitionObject, timeout = CONFIG.RECOGNITION_TIMEOUT) {
|
||
const startTime = Date.now();
|
||
|
||
// 在超时时间内循环尝试识别
|
||
while (Date.now() - startTime < timeout) {
|
||
let gameRegion = undefined
|
||
try {
|
||
gameRegion = captureGameRegion();
|
||
const imageResult = gameRegion.find(recognitionObject);
|
||
if (imageResult.isExist()) {
|
||
return imageResult;
|
||
}
|
||
} catch (error) {
|
||
Log.error(`识别图像时发生异常: ${error.message}`);
|
||
} finally {
|
||
// 确保游戏区域资源被正确释放
|
||
if (gameRegion) {
|
||
gameRegion.dispose();
|
||
}
|
||
}
|
||
await sleep(CONFIG.SLEEP_INTERVAL);
|
||
}
|
||
|
||
Log.warn(`经过多次尝试,仍然无法识别图像`);
|
||
return null;
|
||
}
|
||
|
||
static recognizeNumberByOCR(ocrRegion, pattern) {
|
||
let resList = null;
|
||
let captureRegion = null;
|
||
try {
|
||
const ocrRo = RecognitionObject.ocr(ocrRegion.x, ocrRegion.y, ocrRegion.width, ocrRegion.height);
|
||
captureRegion = captureGameRegion();
|
||
resList = captureRegion.findMulti(ocrRo);
|
||
|
||
if (!resList || resList.length === 0) {
|
||
Log.warn("OCR未识别到任何文本");
|
||
return null;
|
||
}
|
||
|
||
for (const res of resList) {
|
||
if (!res || !res.text) {
|
||
continue;
|
||
}
|
||
|
||
const numberMatch = res.text.match(pattern);
|
||
if (numberMatch) {
|
||
const number = parseInt(numberMatch[1] || numberMatch[0]);
|
||
if (!isNaN(number)) {
|
||
return number;
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
} catch (error) {
|
||
Log.error(`OCR识别时发生异常: ${error.message}`);
|
||
return null;
|
||
} finally {
|
||
if (resList && typeof resList.dispose === 'function') {
|
||
resList.dispose();
|
||
}
|
||
if (captureRegion && typeof captureRegion.dispose === 'function') {
|
||
captureRegion.dispose();
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 在指定区域内识别数字图片
|
||
* @param {Object} ocrRegion - OCR识别区域
|
||
* @param {Array} numberIcons - 数字图标数组
|
||
* @param {string} LogPrefix - 日志前缀
|
||
* @returns {number|null} 识别到的数字或null
|
||
*/
|
||
static async recognizeNumberInRegion(ocrRegion, numberIcons, LogPrefix = "") {
|
||
try {
|
||
for (const numObj of numberIcons) {
|
||
try {
|
||
// 直接链式调用,避免内存管理问题
|
||
const numResult = captureGameRegion().find(numObj.ro);
|
||
if (numResult && Physical.isPointInRegion(numResult, ocrRegion)) {
|
||
Log.info(`${LogPrefix}通过图片识别到数字: ${numObj.value}`);
|
||
return numObj.value;
|
||
}
|
||
} catch (error) {
|
||
Log.error(`${LogPrefix}识别数字图片时发生异常: ${error.message}`);
|
||
}
|
||
}
|
||
} catch (error) {
|
||
Log.error(`${LogPrefix}识别数字区域时发生异常: ${error.message}`);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
/**
|
||
* 普通数字图片识别函数
|
||
* @param {Object} ocrRegion - OCR识别区域
|
||
* @returns {number|null} 识别到的数字或null
|
||
*/
|
||
static async recognizeNumberByImage(ocrRegion) {
|
||
return await Physical.recognizeNumberInRegion(ocrRegion, NUMBER_ICONS, "普通数字 - ");
|
||
}
|
||
|
||
/**
|
||
* 白色数字图片识别函数(用于浓缩树脂)
|
||
* @param {Object} ocrRegion - OCR识别区域
|
||
* @returns {number|null} 识别到的数字或null
|
||
*/
|
||
static async recognizeWhiteNumberByImage(ocrRegion) {
|
||
return await Physical.recognizeNumberInRegion(ocrRegion, WHITE_NUMBER_ICONS, "白色数字 - ");
|
||
}
|
||
}
|