Files
bettergi-scripts-list/repo/js/AutoCommission/lib/steps/auto-fight.js
躁动的氨气 204825f37d 强制所有脚本进行dispose(如发生提前释放问题请自行修复) (#2292)
* 强制dispose

* 强制dispose(版本号更新)
2025-11-02 13:27:59 +08:00

95 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 自动战斗处理器
(function () {
StepProcessorLoader.register("定时自动战斗", async function (step, context) {
/**
* 等待战斗结束通过识别“team.png”模板
* @param {number} timeout - 超时时间毫秒默认30秒
* @returns {Promise<boolean>} - 识别到战斗结束返回true超时返回false
*/
var waitFight = async function (timeout = 30000, intervals = 5000) {
let team = null; // 模板图像Mat对象
let cap = null; // 当前屏幕截图Mat对象
let teamRO = null; // 模板匹配识别对象
try {
team = file.readImageMatSync("Data/team.png");
teamRO = RecognitionObject.TemplateMatch(team);
teamRO.useMask = true; // 忽略绿色区域 (RGB: 0,255,0)
const startTime = Date.now();
// 循环检测直到超时
while (Date.now() - startTime < timeout) {
// 按下 L 键 打开队伍
keyPress("l");
await sleep(1000);
// 截取当前游戏区域
cap = captureGameRegion();
// 查找模板
if (cap.find(teamRO)) {
log.info("识别到战斗结束");
cap.dispose();
// 取消 打开队伍
keyPress("l");
return true;
}
cap.dispose();
log.info("未识别到战斗结束");
await sleep(intervals);
}
// 超时未识别
return false;
} catch (e) {
log.error(e);
} finally {
// 释放资源
team?.Dispose();
cap?.Dispose();
}
}
try {
var timeout = step.data && step.data.timeout;
var intervals = step.data && step.data.intervals;
let args = [];
if (timeout) { args.push(timeout) };
if (intervals) { args.push(intervals) };
// 创建取消令牌
let cts = new CancellationTokenSource();
log.info("开始战斗");
let fightTask = dispatcher.RunTask(new SoloTask("AutoFight"), cts);
// 等待战斗结束或超时动态传参默认超时30秒间隔10秒
await waitFight(...args);
// 超时或战斗结束,取消战斗任务
cts.cancel();
return true;
} catch (error) {
// 捕获并记录执行过程中的异常
log.error("处理自动战斗(自定时长版)步骤时出错: {error}", error.message);
return false;
}
});
})();
/*
JSON使用示例:
{
"type": "定时自动战斗",
"data": {
"timeout": 3000, //可选参数单位为毫秒默认30秒
"intervals": 1000 //可选参数单位为毫秒默认10秒
},
"note": "开始自动战斗-自定时长版"
}
*/