From 22db16f70c33d32a858df95573ba182600c2392d Mon Sep 17 00:00:00 2001 From: Zebartin <16185081+Zebartin@users.noreply.github.com> Date: Sat, 1 Jun 2024 11:18:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=87=BA=E9=94=99=E6=97=B6=E4=BF=9D?= =?UTF-8?q?=E5=AD=98OCR=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NIKKE/CHANGELOG.md | 4 ++ NIKKE/NIKKEutils.js | 49 +++++++++++++++++++----- "NIKKE/NIKKE\346\227\245\345\270\270.js" | 19 +++++---- utils.js | 15 ++++++-- 4 files changed, 64 insertions(+), 23 deletions(-) diff --git a/NIKKE/CHANGELOG.md b/NIKKE/CHANGELOG.md index 5be0543..6f4d5eb 100644 --- a/NIKKE/CHANGELOG.md +++ b/NIKKE/CHANGELOG.md @@ -2,7 +2,11 @@ - 结束应用失败; - 爬塔战斗结束后卡住; +- 特拦队伍识别; 新增功能: +- 适配珍藏品派遣,脚本会选择第一个随机派遣,如果需特定妮姬珍藏品,自行在游戏里选好,之后游戏会保存选择结果; +- 出错时将OCR结果保存在文件里; + 具体使用方式详见[README](https://github.com/Zebartin/autoxjs-scripts/blob/master/NIKKE/README.md)。 diff --git a/NIKKE/NIKKEutils.js b/NIKKE/NIKKEutils.js index 500cef1..55cac9a 100644 --- a/NIKKE/NIKKEutils.js +++ b/NIKKE/NIKKEutils.js @@ -2,7 +2,7 @@ var { ocrUntilFound, clickRect, unlockIfNeed, requestScreenCaptureAuto, getDisplaySize, killApp, findImageByFeature, findContoursRect, - rgbToGray + rgbToGray, getRandomArea, ocrInfo } = require('./utils.js'); var firstBoot = true; var firstCheckAuto = true; @@ -22,6 +22,7 @@ if (typeof module === 'undefined') { } else { module.exports = { + saveError: saveError, 启动NIKKE: 启动NIKKE, 等待NIKKE加载: 等待NIKKE加载, 退出NIKKE: 退出NIKKE, @@ -34,9 +35,30 @@ else { checkAuto: checkAuto, }; } +function saveError(error) { + const errorPath = files.path(`./nikkerror/${Date.now()}/`); + const errorStrs = [ + `当前脚本版本:${NIKKEstorage.get('tagName', '无记录')}`, + error.message, + error.stack + ]; + files.ensureDir(errorPath); + if (ocrInfo.img) { + images.save(ocrInfo.img, files.join(errorPath, 'error.png')); + } + if (ocrInfo.result) { + errorStrs.push(''); + const res = ocrInfo.result.toArray(3); + for (let i = 0; i < res.length; ++i) { + errorStrs.push(`${res[i].bounds}\t"${res[i].text}"`); + } + } + files.write(files.join(errorPath, 'log.txt'), errorStrs.join('\n')); + log(`出错日志已保存到${errorPath}`); +} function 启动NIKKE() { let NIKKEstorage = storages.create("NIKKEconfig"); - let errorPath = files.path('./images/nikkerror/'); + let errorPath = files.path('./nikkerror/'); files.ensureDir(errorPath); if (firstBoot && NIKKEstorage.get('alreadyInGame', false)) { toastLog('已勾选“游戏已启动”选项\n请确保游戏此时正处于前台画面'); @@ -48,12 +70,19 @@ function 启动NIKKE() { home(); sleep(500); // 保证错误截图不要过多 - let maxErr = 20; - let errorImages = files.listDir(errorPath); - if (errorImages.length >= maxErr) { - errorImages.sort((a, b) => parseInt(b.split('.')[0]) - parseInt(a.split('.')[0])); - for (let f of errorImages.slice(maxErr)) - files.remove(files.join(errorPath, f)); + let maxErr = 10; + let errorDirs = files.listDir(errorPath); + if (errorDirs.length >= maxErr) { + errorDirs.sort((a, b) => parseInt(b.split('.')[0]) - parseInt(a.split('.')[0])); + for (let f of errorDirs.slice(maxErr)) { + log(`删除过期错误日志:${f}`); + let fp = files.join(errorPath, f); + if (files.isFile(fp)) { + files.remove(fp); + } else if (files.isDir(fp)) { + files.removeDir(files.join(errorPath, f)); + } + } } if (NIKKEstorage.get('mute', false)) { try { @@ -107,6 +136,8 @@ function 启动NIKKE() { function 等待NIKKE加载() { let [width, height] = getDisplaySize(); let manuallyEnter = true; + const continueBtn = getRandomArea(captureScreen(), [0.2, 0.2, 0.8, 0.8]); + continueBtn.text = 'TOUCH TO CONTINUE'; if (ocrUntilFound(res => { if (res.text.match(/(密|验证)码/) != null) { toastLog('未登录游戏,停止运行脚本'); @@ -137,7 +168,7 @@ function 等待NIKKE加载() { sleep(20000); } else if (res.text.match(/(登出|T.UCH|C.NT.NUE)/) != null) { - click(width / 2, height / 2); + clickRect(continueBtn, 1, 0); manuallyEnter = false; } else if (res.text.match(/(大厅|员招|物品栏)/) != null) { diff --git "a/NIKKE/NIKKE\346\227\245\345\270\270.js" "b/NIKKE/NIKKE\346\227\245\345\270\270.js" index 85a3152..b2a78ec 100644 --- "a/NIKKE/NIKKE\346\227\245\345\270\270.js" +++ "b/NIKKE/NIKKE\346\227\245\345\270\270.js" @@ -2,7 +2,7 @@ var { 启动NIKKE, 等待NIKKE加载, 退出NIKKE, mostSimilar, 返回首页, 关闭限时礼包, detectNikkes, NikkeToday, checkAuto, - reportEvent + reportEvent, saveError } = require('./NIKKEutils.js'); var { 模拟室 } = require('./模拟室.js'); var { @@ -67,15 +67,14 @@ function 日常() { toast(error.message); console.error(error.message); console.error(error.stack); - reportEvent('error', { - task: taskName, - tagName: NIKKEstorage.get('tagName', '无记录'), - }); - // 保存出错截图 - let filename = files.path(`./images/nikkerror/${Date.now()}.jpg`); - images.save(captureScreen(), filename); - log(`出错截图已保存到${filename}`); - log(`当前脚本版本:${NIKKEstorage.get('tagName', '无记录')}`) + if (!error.message.includes('加载')) { + reportEvent('error', { + task: taskName, + tagName: NIKKEstorage.get('tagName', '无记录'), + }); + // 保存出错内容 + saveError(error); + } if (alreadyRetry != maxRetry) { toastLog(`脚本出错,即将重试(${alreadyRetry + 1}/${maxRetry})`); sleep(3000); diff --git a/utils.js b/utils.js index 7af6f49..f384934 100644 --- a/utils.js +++ b/utils.js @@ -1,9 +1,14 @@ const 手机锁屏密码 = ''; +var ocrInfo = { + result: null, + img: null +}; if (typeof module === 'undefined') { getOcrRes(); } else { module.exports = { + ocrInfo: ocrInfo, scaleBack: scaleBack, ocrUntilFound: ocrUntilFound, clickRect: clickRect, @@ -97,11 +102,13 @@ function ocrUntilFound(found, retry, interval, options) { img && img.recycle(); img = newImg; } - let ocrRes = gmlkit.ocr(img, "zh"); - if (ocrRes == null || ocrRes.text == null) + // 回收上一张图 + ocrInfo.img && ocrInfo.img.recycle(); + ocrInfo.img = img; + ocrInfo.result = gmlkit.ocr(img, "zh"); + if (ocrInfo.result == null || ocrInfo.result.text == null) continue; - let res = found(ocrRes, img, scale); - img && img.recycle(); + let res = found(ocrInfo.result, img, scale); if (res || res === 0) { if (scale > 1) { if (Array.isArray(res)) {