From f9463795e407701d8d6fe7e3f1a0294ec190c84d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E4=BA=91?= Date: Sat, 12 Apr 2025 14:49:09 +0800 Subject: [PATCH] =?UTF-8?q?js:=20=E5=BC=82=E6=AD=A5=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=88=98=E6=96=97=EF=BC=8C=E5=9F=BA=E4=BA=8EOCR=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E4=BB=A5=E5=88=A4=E6=96=AD=E6=88=98=E6=96=97=E7=BB=93?= =?UTF-8?q?=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- repo/js/AutoFightAsync/main.js | 50 ++++++++++++++++++++++++++++ repo/js/AutoFightAsync/manifest.json | 15 +++++++++ 2 files changed, 65 insertions(+) create mode 100644 repo/js/AutoFightAsync/main.js create mode 100644 repo/js/AutoFightAsync/manifest.json diff --git a/repo/js/AutoFightAsync/main.js b/repo/js/AutoFightAsync/main.js new file mode 100644 index 00000000..4a759267 --- /dev/null +++ b/repo/js/AutoFightAsync/main.js @@ -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}`); + } +})(); \ No newline at end of file diff --git a/repo/js/AutoFightAsync/manifest.json b/repo/js/AutoFightAsync/manifest.json new file mode 100644 index 00000000..58653f92 --- /dev/null +++ b/repo/js/AutoFightAsync/manifest.json @@ -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" +} \ No newline at end of file