Skip to content

Commit

Permalink
feat: 出错时保存OCR结果
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebartin committed Jun 1, 2024
1 parent 95a04e3 commit 22db16f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
4 changes: 4 additions & 0 deletions NIKKE/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

- 结束应用失败;
- 爬塔战斗结束后卡住;
- 特拦队伍识别;

新增功能:

- 适配珍藏品派遣,脚本会选择第一个随机派遣,如果需特定妮姬珍藏品,自行在游戏里选好,之后游戏会保存选择结果;
- 出错时将OCR结果保存在文件里;

具体使用方式详见[README](https://github.com/Zebartin/autoxjs-scripts/blob/master/NIKKE/README.md)
49 changes: 40 additions & 9 deletions NIKKE/NIKKEutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +22,7 @@ if (typeof module === 'undefined') {
}
else {
module.exports = {
saveError: saveError,
启动NIKKE: 启动NIKKE,
等待NIKKE加载: 等待NIKKE加载,
退出NIKKE: 退出NIKKE,
Expand All @@ -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请确保游戏此时正处于前台画面');
Expand All @@ -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 {
Expand Down Expand Up @@ -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('未登录游戏,停止运行脚本');
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 9 additions & 10 deletions NIKKE/NIKKE日常.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var {
启动NIKKE, 等待NIKKE加载, 退出NIKKE,
mostSimilar, 返回首页, 关闭限时礼包,
detectNikkes, NikkeToday, checkAuto,
reportEvent
reportEvent, saveError
} = require('./NIKKEutils.js');
var { 模拟室 } = require('./模拟室.js');
var {
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 11 additions & 4 deletions utils.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 22db16f

Please sign in to comment.