js: 异步自动战斗,基于OCR识别以判断战斗结束 (#562)

This commit is contained in:
秋云
2025-04-13 09:44:45 +08:00
committed by GitHub
parent 0f1f09e43d
commit 8ba70db546
2 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
async function recognizeTextInRegion(ocrRegion, timeout = 2 * 60 * 1000) {
let startTime = Date.now();
const successKeywords = ["挑战达成", "战斗胜利", "挑战成功"];
const failureKeywords = ["挑战失败"];
while (Date.now() - startTime < timeout) {
try {
let result = captureGameRegion().find(RecognitionObject.ocr(ocrRegion.x, ocrRegion.y, ocrRegion.width, ocrRegion.height));
let text = result.text;
for (let keyword of successKeywords) {
if (text.includes(keyword)) {
log.info("检测到战斗成功关键词: {0}", keyword);
return true;
}
}
for (let keyword of failureKeywords) {
if (text.includes(keyword)) {
log.warn("检测到战斗失败关键词: {0}", keyword);
return false;
}
}
}
catch (error) {
log.error("OCR过程中出错: {0}", error);
}
await sleep(1000); // 检查间隔
}
log.warn("在超时时间内未检测到战斗结果");
return false;
}
(async function () {
await genshin.returnMainUi();
keyPress("F");
// 上面是地脉测试使用的代码 正式使用请注释掉
const cts = new CancellationTokenSource();
try {
log.info("开始执行自动战斗任务...");
const battleTask = dispatcher.RunTask(new SoloTask("AutoFight"), cts);
const ocrRegionX = 850;
const ocrRegionY = 230;
const ocrRegionWidth = 1040 - 850;
const ocrRegionHeight = 300 - 230;
let ocrRegion = { x: ocrRegionX, y: ocrRegionY, width: ocrRegionWidth, height: ocrRegionHeight };
let fightResult = await recognizeTextInRegion(ocrRegion) ? "成功" : "失败";
log.info(`战斗任务已结束,战斗结果:${fightResult}`);
cts.cancel();
} catch (error) {
log.error(`执行过程中出错: ${error}`);
}
})();

View File

@@ -0,0 +1,15 @@
{
"manifest_version": 1,
"name": "AutoFightAsync",
"version": "1.0",
"bgiversion" : "0.34.6",
"description": "异步调用独立战斗任务根据OCR结果判断战斗是否结束不建议直接使用仅供其他脚本作者参考",
"authors": [
{
"name": "秋云",
"link": "https://github.com/phydligl"
}
],
"settings_ui": "settings.json",
"main": "main.js"
}