From 59c3c092e029546a4996374a8d9b7c63f9aa1304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B7=E4=B8=AA=E5=90=8D=E5=AD=97=E5=A5=BD=E9=9A=BE?= =?UTF-8?q?=E7=9A=84=E5=96=B5?= <25520958+MisakaAldrich@users.noreply.github.com> Date: Thu, 28 Aug 2025 17:00:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=8B=E7=A7=8B=E4=BA=91=E8=AF=B4=E8=BF=87?= =?UTF-8?q?=E6=B0=94=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- archive/js/AutoFightAsync/main.js | 50 +++++++++++++++++++++++++ archive/js/AutoFightAsync/manifest.json | 18 +++++++++ 2 files changed, 68 insertions(+) create mode 100644 archive/js/AutoFightAsync/main.js create mode 100644 archive/js/AutoFightAsync/manifest.json diff --git a/archive/js/AutoFightAsync/main.js b/archive/js/AutoFightAsync/main.js new file mode 100644 index 000000000..625c0bb39 --- /dev/null +++ b/archive/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/archive/js/AutoFightAsync/manifest.json b/archive/js/AutoFightAsync/manifest.json new file mode 100644 index 000000000..7bd406edd --- /dev/null +++ b/archive/js/AutoFightAsync/manifest.json @@ -0,0 +1,18 @@ +{ + "manifest_version": 1, + "name": "AutoFightAsync", + "version": "1.0", + "bgi_version": "0.34.6", + "description": "异步调用独立战斗任务,根据OCR结果判断战斗是否结束,不建议直接使用,仅供其他脚本作者参考", + "tags": [ + "示例" + ], + "authors": [ + { + "name": "秋云", + "links": "https://github.com/physligl" + } + ], + "settings_ui": "settings.json", + "main": "main.js" +} \ No newline at end of file