mirror of
https://github.com/babalae/bettergi-scripts-list.git
synced 2026-11-16 19:39:51 +00:00
* feat(campaignArea): 添加长期训练点识别功能 - 新增 ocrLongTermTrainingPoints 函数用于识别长期训练点数和过期时间 - 添加 drawBox 和 drawAndClearBox 工具函数用于调试框绘制 - 在 campaignArea 区域配置中新增长期训练点相关坐标 - 在每日委托通知中添加长期训练点数量和过期提醒 - 优化 OCR 识别区域配置,支持调试模式下的可视化显示 - 修复活动页面滚动时的调试框绘制问题 - 更新设置选项,添加开发者模式开关 * feat(ocr): 优化地图任务OCR识别功能 - 添加滚动识别机制,支持长列表任务的完整扫描 - 实现前后两次识别结果对比,避免重复内容无限循环 - 增加鼠标滚轮控制,自动滚动查看下方任务内容 - 重构识别逻辑为循环结构,提升任务匹配准确性 - 添加调试日志,便于追踪识别过程和滚动操作 - 修复单次识别可能遗漏任务的问题 * refactor(ActivitySwitchNotice): 重构OCR任务识别逻辑提高准确性 - 移除过时的previousNameList和currentNameList变量 - 使用Set数据结构优化文本去重逻辑 - 实现页面重合率检测防止重复扫描 - 添加最大扫描页数限制防止死循环 - 优化滚动逻辑从while循环改为while循环带安全限制 - 改进资源释放机制确保region对象正确dispose - 添加详细的调试日志便于问题排查 - 修复OCR识别结果去重机制避免重复添加相同任务 * refactor(ActivitySwitchNotice): 优化活动页面滚动扫描逻辑 - 移除多余的空行以改善代码格式 - 添加 overlapThreshold 常量用于控制页面重复检测阈值 - 使用常量替代硬编码数值提高代码可维护性 - 保持原有重合率检测逻辑的同时增强代码可读性 * fix(ActivitySwitchNotice): 修复任务切换通知中的页面稳定等待问题 - 在mapMission.js中将页面稳定等待时间从2毫秒调整为200毫秒 - 在settings.json中添加新的活动选项:食材加工奖励、锻造奖励、至冬冒险笔记奖励和绘想游迹 - 更新默认活动配置以包含新增的活动类型 * refactor(ActivitySwitchNotice): 优化活动页面滚动和鼠标操作策略 - 注释掉原有的moveMouseTo(0, 20)安全位置移动 - 将scrollPagesByActivity调用替换为verticalScroll循环实现精确滚动 - 在页面扫描开始前添加鼠标点击操作到指定区域 - 调整重叠阈值从0.7提升到1以优化页面识别逻辑 - 修改页面滚动策略为小步距多次滚动以提高稳定性 * chore(version): 更新插件版本和BGI版本依赖 - 将插件版本从 0.1.8 升级到 0.1.9 - 将 BGI 版本从 0.61.0 升级到 0.63.0 * docs(ActivitySwitchNotice): 更新 README 文档以反映最新功能和架构变更 - 添加了多选提醒日支持的说明(0.1.1版本) - 新增每日委托奖励/原粹树脂/长期训练点提醒功能说明(0.0.7/0.1.9版本) - 添加地图识别任务提醒功能说明(0.0.8版本) - 新增圣遗物剩余空间检测提醒功能说明(0.1.5版本) - 添加新活动通知功能说明(0.1.0版本) - 更新项目架构图,显示新增的mapMission.js和HolyRelics.js模块 - 修改主流程图,体现地图识别和圣遗物检查的新顺序 - 更新工具模块关系图,展示新的模块依赖关系 - 重新整理配置参数表,添加debug、newActivityNotice、mapMissionKeys等新配置项 - 更新独立通知配置说明,添加bgi-tools授权配置选项 - 增加自定义通知和bgi-tools通知的配置示例 - 补充新增模块的详细功能说明文档 - 更新工作原理步骤,反映新的功能执行顺序 - 添加0.1.8和0.1.9版本的更新日志信息
449 lines
13 KiB
JavaScript
449 lines
13 KiB
JavaScript
/**
|
||
* 通用找文本(OCR)
|
||
* @param {string|string[]} text 目标文本(单个文本或文本列表,列表时需全部匹配)
|
||
* @param {number} [x=0] OCR 区域左上角 X
|
||
* @param {number} [y=0] OCR 区域左上角 Y
|
||
* @param {number} [w=1920] OCR 区域宽度
|
||
* @param {number} [h=1080] OCR 区域高度
|
||
* @param {number} [attempts=5] OCR 尝试次数
|
||
* @param {number} [interval=50] 每次 OCR 之间的等待间隔(毫秒)
|
||
*
|
||
* @returns
|
||
* - RecognitionResult | null
|
||
*/
|
||
export async function findText(
|
||
text,
|
||
x = 0,
|
||
y = 0,
|
||
w = 1920,
|
||
h = 1080,
|
||
attempts = 5,
|
||
interval = 50
|
||
) {
|
||
const keywords = (Array.isArray(text) ? text : [text])
|
||
.map(t => t.toLowerCase());
|
||
|
||
for (let i = 0; i < attempts; i++) {
|
||
const gameRegion = captureGameRegion();
|
||
try {
|
||
const ro = RecognitionObject.Ocr(x, y, w, h);
|
||
const results = gameRegion.findMulti(ro);
|
||
|
||
for (let j = 0; j < results.count; j++) {
|
||
const res = results[j];
|
||
if (!res.isExist() || !res.text) continue;
|
||
|
||
const ocrText = res.text.toLowerCase();
|
||
const matched = keywords.every(k => ocrText.includes(k));
|
||
if (matched) {
|
||
return res;
|
||
}
|
||
}
|
||
} finally {
|
||
gameRegion.dispose();
|
||
}
|
||
|
||
await sleep(interval);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 通用找文本并点击(OCR)
|
||
* @param {string} text 目标文本(单个文本)
|
||
* @param {number} [x=0] OCR 区域左上角 X
|
||
* @param {number} [y=0] OCR 区域左上角 Y
|
||
* @param {number} [w=1920] OCR 区域宽度
|
||
* @param {number} [h=1080] OCR 区域高度
|
||
* @param {number} [attempts=5] OCR 尝试次数
|
||
* @param {number} [interval=50] 每次 OCR 之间的等待间隔(毫秒)
|
||
* @param {number} [preClickDelay=50] 点击前等待时间(毫秒)
|
||
* @param {number} [postClickDelay=50] 点击后等待时间(毫秒)
|
||
*
|
||
* @returns
|
||
* - RecognitionResult | null
|
||
*/
|
||
export async function findTextAndClick(
|
||
text,
|
||
x = 0,
|
||
y = 0,
|
||
w = 1920,
|
||
h = 1080,
|
||
attempts = 5,
|
||
interval = 50,
|
||
preClickDelay = 50,
|
||
postClickDelay = 50
|
||
) {
|
||
const keyword = text.toLowerCase();
|
||
|
||
for (let i = 0; i < attempts; i++) {
|
||
const gameRegion = captureGameRegion();
|
||
try {
|
||
const ro = RecognitionObject.Ocr(x, y, w, h);
|
||
const results = gameRegion.findMulti(ro);
|
||
|
||
for (let j = 0; j < results.count; j++) {
|
||
const res = results[j];
|
||
if (
|
||
res.isExist() &&
|
||
res.text &&
|
||
res.text.toLowerCase().includes(keyword)
|
||
) {
|
||
await sleep(preClickDelay);
|
||
res.click();
|
||
await sleep(postClickDelay);
|
||
return res;
|
||
}
|
||
}
|
||
} finally {
|
||
gameRegion.dispose();
|
||
}
|
||
|
||
await sleep(interval);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 通用找图/找RO(支持图片文件路径、Mat)
|
||
* @param {string|Mat} target 图片路径或已构造的 Mat
|
||
* @param {number} [x=0] 识别区域左上角 X
|
||
* @param {number} [y=0] 识别区域左上角 Y
|
||
* @param {number} [w=1920] 识别区域宽度
|
||
* @param {number} [h=1080] 识别区域高度
|
||
* @param {number} [timeout=1000] 识别时间上限(毫秒)
|
||
* @param {number} [interval=50] 每次识别之间的等待间隔(毫秒)
|
||
*
|
||
* @returns
|
||
* - RecognitionResult | null
|
||
*/
|
||
export async function findImg(
|
||
target,
|
||
x = 0,
|
||
y = 0,
|
||
w = 1920,
|
||
h = 1080,
|
||
timeout = 1000,
|
||
interval = 50
|
||
) {
|
||
const ro =
|
||
typeof target === 'string'
|
||
? RecognitionObject.TemplateMatch(
|
||
file.readImageMatSync(target),
|
||
x, y, w, h
|
||
)
|
||
: RecognitionObject.TemplateMatch(
|
||
target,
|
||
x, y, w, h
|
||
);
|
||
|
||
const start = Date.now();
|
||
|
||
while (Date.now() - start <= timeout) {
|
||
const gameRegion = captureGameRegion();
|
||
try {
|
||
const res = gameRegion.find(ro);
|
||
if (!res.isEmpty()) {
|
||
return res;
|
||
}
|
||
} catch (e) {
|
||
log.error(e.toString());
|
||
} finally {
|
||
gameRegion.dispose();
|
||
}
|
||
|
||
await sleep(interval);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
|
||
/**
|
||
* 通用找图并点击(支持图片文件路径、Mat)
|
||
* @param {string|Mat} target 图片路径或已构造的 Mat
|
||
* @param {number} [x=0] 识别区域左上角 X
|
||
* @param {number} [y=0] 识别区域左上角 Y
|
||
* @param {number} [w=1920] 识别区域宽度
|
||
* @param {number} [h=1080] 识别区域高度
|
||
* @param {number} [timeout=1000] 识别时间上限(毫秒)
|
||
* @param {number} [interval=50] 每次识别之间的等待间隔(毫秒)
|
||
* @param {number} [preClickDelay=50] 点击前等待时间(毫秒)
|
||
* @param {number} [postClickDelay=50] 点击后等待时间(毫秒)
|
||
*
|
||
* @returns
|
||
* - RecognitionResult | null
|
||
*/
|
||
export async function findImgAndClick(
|
||
target,
|
||
x = 0,
|
||
y = 0,
|
||
w = 1920,
|
||
h = 1080,
|
||
timeout = 1000,
|
||
interval = 50,
|
||
preClickDelay = 50,
|
||
postClickDelay = 50
|
||
) {
|
||
const ro =
|
||
typeof target === 'string'
|
||
? RecognitionObject.TemplateMatch(
|
||
file.readImageMatSync(target),
|
||
x, y, w, h
|
||
)
|
||
: RecognitionObject.TemplateMatch(
|
||
target,
|
||
x, y, w, h
|
||
);
|
||
|
||
const start = Date.now();
|
||
|
||
while (Date.now() - start <= timeout) {
|
||
const gameRegion = captureGameRegion();
|
||
try {
|
||
const res = gameRegion.find(ro);
|
||
if (!res.isEmpty()) {
|
||
await sleep(preClickDelay);
|
||
res.click();
|
||
await sleep(postClickDelay);
|
||
return res;
|
||
}
|
||
} finally {
|
||
gameRegion.dispose();
|
||
}
|
||
|
||
await sleep(interval);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
|
||
/**
|
||
* 使用OCR技术在指定区域内查找文本
|
||
* @param {number} x - 区域左上角x坐标
|
||
* @param {number} y - 区域左上角y坐标
|
||
* @param {number} width - 区域宽度
|
||
* @param {number} height - 区域高度
|
||
* @returns {Promise<Region|null>} 返回找到的文本,如果没有找到则返回null
|
||
*/
|
||
export async function OcrFind(x, y, width, height) {
|
||
let captureRegion = captureGameRegion(); // 获取游戏区域截图
|
||
try {
|
||
// 创建OCR识别对象,指定识别区域
|
||
const recognitionObject = RecognitionObject.Ocr(x, y, width, height);
|
||
// 在截图上执行OCR识别
|
||
const result = captureRegion.find(recognitionObject);
|
||
// 返回识别到的文本,如果未识别到则返回undefined
|
||
return result
|
||
} finally {
|
||
// 确保截图资源被正确释放,防止内存泄漏
|
||
if (captureRegion) {
|
||
captureRegion.dispose(); // 释放截图资源
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 使用OCR技术在指定区域内查找文本内容
|
||
* @param {number} x - 区域左上角x坐标
|
||
* @param {number} y - 区域左上角y坐标
|
||
* @param {number} width - 区域宽度
|
||
* @param {number} height - 区域高度
|
||
* @returns {Promise<Region[]>} 返回找到的文本内容数组
|
||
*/
|
||
export async function OcrFindList(x, y, width, height) {
|
||
let captureRegion = captureGameRegion(); // 获取游戏区域截图
|
||
try {
|
||
// 创建OCR识别对象,指定识别区域
|
||
const recognitionObject = RecognitionObject.Ocr(x, y, width, height);
|
||
// 在截图区域中查找多个匹配项
|
||
const resList = captureRegion.findMulti(recognitionObject);
|
||
return resList // 返回找到的文本列表
|
||
} finally {
|
||
// 确保释放截图资源,防止内存泄漏
|
||
if (captureRegion) {
|
||
captureRegion.dispose(); // 释放截图资源
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取当前日期的星期信息
|
||
* @param {boolean} [calibrationGameRefreshTime=true] 是否进行游戏刷新时间校准
|
||
* @returns {Object} 返回包含星期数字和星期名称的对象
|
||
*/
|
||
export async function getDayOfWeek(calibrationGameRefreshTime = true) {
|
||
// 获取当前日期对象
|
||
let today = new Date();//4点刷新 所以要减去4小时
|
||
if (calibrationGameRefreshTime) {
|
||
today.setHours(today.getHours() - 4); // 减去 4 小
|
||
}
|
||
// 获取当前日期是星期几(0代表星期日,1代表星期一,以此类推)
|
||
const day = today.getDay();
|
||
// 创建包含星期名称的数组
|
||
const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
||
let weekDay = `${weekDays[day]}`;
|
||
|
||
log.debug(`今天是[{day}]`, day)
|
||
log.debug(`今天是[{weekDays}]`, weekDay)
|
||
// 返回包含星期数字和对应星期名称的对象
|
||
return {
|
||
day: day,
|
||
dayOfWeek: weekDay
|
||
}
|
||
}
|
||
|
||
// 判断是否在主界面的函数
|
||
export const isInMainUI = () => {
|
||
let captureRegion = captureGameRegion();
|
||
|
||
try {
|
||
const res = captureRegion.Find(RecognitionObject.TemplateMatch(
|
||
file.ReadImageMatSync("assets/paimon_menu.png"),
|
||
0,
|
||
0,
|
||
640,
|
||
216
|
||
));
|
||
return !res.isEmpty();
|
||
} finally {
|
||
if (captureRegion) {
|
||
captureRegion.dispose();
|
||
}
|
||
}
|
||
};
|
||
|
||
export async function toMainUi() {
|
||
let ms = 300
|
||
let index = 1
|
||
await sleep(ms);
|
||
while (!isInMainUI()) {
|
||
await sleep(ms);
|
||
await genshin.returnMainUi(); // 如果未启用,则返回游戏主界面
|
||
await sleep(ms);
|
||
if (index > 3) {
|
||
throw new Error(`多次尝试返回主界面失败`);
|
||
}
|
||
index += 1
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 打开背包(检测过期物品)
|
||
*/
|
||
export async function openBag() {
|
||
const openBagKey = settings.openBagKey || "B";
|
||
await toMainUi();
|
||
await keyPress(openBagKey);
|
||
await sleep(500);
|
||
const expiredText = await findText("物品过期", 870, 280, 170, 40, 2);
|
||
if (expiredText) {
|
||
log.info("检测到过期物品,关闭弹窗");
|
||
await sleep(500);
|
||
await click(980, 750);
|
||
}
|
||
await sleep(50);
|
||
}
|
||
export class Record {
|
||
/**
|
||
* 读取指定路径的记录文件
|
||
* @param {string} path - 记录文件的路径
|
||
* @returns {Array} 解析后的记录数组,若文件为空或解析失败则返回空数组
|
||
*/
|
||
static read(path) {
|
||
// a = {
|
||
// uid: 1,
|
||
// time: "yyyy-mm-dd",
|
||
// key: "type:value"
|
||
// }
|
||
let list = []
|
||
try {
|
||
list = JSON.parse(file.readTextSync(path))
|
||
} catch (e) {
|
||
log.warn(`(账号未运行过无记录文件 请忽略该异常),读取记录文件失败,{0}`, e.message)
|
||
}
|
||
return list
|
||
}
|
||
|
||
/**
|
||
* 在列表中检查指定记录是否存在
|
||
* @param {Array} list - 记录列表,默认为空数组
|
||
* @param {Object} item - 用于匹配的记录项,包含 key, time, uid 属性
|
||
* @returns {boolean}
|
||
*/
|
||
static existInList(list = [], item) {
|
||
const ts = item?.id ? list.filter(i => i.id === item.id && i.time === item.time && i.uid === item.uid) : list.filter(i => i.key === item.key && i.time === item.time && i.uid === item.uid);
|
||
return ts !== null && ts.length > 0
|
||
}
|
||
|
||
/**
|
||
* 检查指定记录是否存在
|
||
* @param {string} path - 记录文件的路径
|
||
* @param {Object} item - 用于匹配的记录项,包含 key, time, uid 属性
|
||
* @returns {boolean}
|
||
*/
|
||
static exist(path, item) {
|
||
const ts = Record.read(path).filter(i => i.key === item.key && i.time === item.time && i.uid === item.uid);
|
||
return ts !== null && ts.length > 0
|
||
}
|
||
|
||
/**
|
||
* 将记录列表写入指定路径的文件
|
||
* @param {string} path - 目标文件的路径
|
||
* @param {Array} list - 要写入的记录列表
|
||
* @returns {*} 返回 parse 变量(注:当前代码中 parse 未在此函数内定义)
|
||
*/
|
||
static write(path, list) {
|
||
file.writeTextSync(path, JSON.stringify(list))
|
||
}
|
||
}
|
||
|
||
export async function drawBox(show,result, delay = 200, pen ){
|
||
if (show){
|
||
await drawAndClearBox(result, delay, pen)
|
||
}
|
||
}
|
||
|
||
export async function drawAndClearBox(result, delay = 200, pen ) {
|
||
const ro1 = captureGameRegion();
|
||
try {
|
||
const drawRegion = ro1.DeriveCrop(result.x, result.y, result.width, result.height);
|
||
if (pen){
|
||
drawRegion.DrawSelf("icon", pen);
|
||
}else {
|
||
drawRegion.DrawSelf("icon",new Pen(Color.Red, 2));
|
||
}
|
||
await sleep(delay);
|
||
drawRegion.dispose()
|
||
} finally {
|
||
ro1.dispose();
|
||
}
|
||
|
||
/* await sleep(delay);
|
||
|
||
const ro2 = captureGameRegion();
|
||
try {
|
||
const drawRegion2 = ro2.DeriveCrop(result.x, result.y, result.width, result.height);
|
||
try {
|
||
if (pen){
|
||
drawRegion2.DrawSelf("icon", pen);
|
||
}else {
|
||
drawRegion2.DrawSelf("icon",new Pen(Color.Red, 2));
|
||
}
|
||
} finally {
|
||
drawRegion2.dispose();
|
||
}
|
||
} finally {
|
||
ro2.dispose();
|
||
}*/
|
||
}
|
||
export function formatDate(date = new Date()) {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
}
|