Commit ffe6f9a1 authored by nanahira's avatar nanahira

Merge branch 'msg-encode' of ../srvpro into develop

parents 8c03717a ab9e3c3d
Pipeline #42908 passed with stages
in 7 minutes and 46 seconds
...@@ -9,6 +9,7 @@ const load_constants_1 = __importDefault(require("./load-constants")); ...@@ -9,6 +9,7 @@ const load_constants_1 = __importDefault(require("./load-constants"));
const ygopro_msg_encode_1 = require("ygopro-msg-encode"); const ygopro_msg_encode_1 = require("ygopro-msg-encode");
const ygopro_msg_struct_compat_1 = require("./ygopro-msg-struct-compat"); const ygopro_msg_struct_compat_1 = require("./ygopro-msg-struct-compat");
const proto_structs_json_1 = __importDefault(require("./data/proto_structs.json")); const proto_structs_json_1 = __importDefault(require("./data/proto_structs.json"));
const utility_1 = require("./utility");
class Handler { class Handler {
constructor(handler, synchronous) { constructor(handler, synchronous) {
this.handler = handler; this.handler = handler;
...@@ -38,15 +39,7 @@ class LegacyStructInst { ...@@ -38,15 +39,7 @@ class LegacyStructInst {
return; return;
const inst = (0, ygopro_msg_struct_compat_1.applyYGOProMsgStructCompat)(new this.cls().fromPayload(this.buffer)); const inst = (0, ygopro_msg_struct_compat_1.applyYGOProMsgStructCompat)(new this.cls().fromPayload(this.buffer));
inst[field] = value; inst[field] = value;
const parsed = Buffer.from(inst.toPayload()); (0, utility_1.overwriteBuffer)(this.buffer, inst.toPayload());
if (parsed.length >= this.buffer.length) {
// slice it down
parsed.copy(this.buffer, 0, 0, this.buffer.length);
}
else {
// copy a small part only
parsed.copy(this.buffer, 0, 0, parsed.length);
}
} }
} }
exports.LegacyStructInst = LegacyStructInst; exports.LegacyStructInst = LegacyStructInst;
......
...@@ -4,6 +4,7 @@ import net from "net"; ...@@ -4,6 +4,7 @@ import net from "net";
import { YGOProCtos, YGOProCtosBase, YGOProStoc, YGOProStocBase } from "ygopro-msg-encode"; import { YGOProCtos, YGOProCtosBase, YGOProStoc, YGOProStocBase } from "ygopro-msg-encode";
import { applyYGOProMsgStructCompat, fromPartialCompat } from "./ygopro-msg-struct-compat"; import { applyYGOProMsgStructCompat, fromPartialCompat } from "./ygopro-msg-struct-compat";
import legacyProtoStructs from "./data/proto_structs.json"; import legacyProtoStructs from "./data/proto_structs.json";
import { overwriteBuffer } from "./utility";
class Handler { class Handler {
...@@ -82,14 +83,7 @@ export class LegacyStructInst { ...@@ -82,14 +83,7 @@ export class LegacyStructInst {
if (!this.buffer || !this.cls) return; if (!this.buffer || !this.cls) return;
const inst = applyYGOProMsgStructCompat(new this.cls().fromPayload(this.buffer)); const inst = applyYGOProMsgStructCompat(new this.cls().fromPayload(this.buffer));
inst[field] = value; inst[field] = value;
const parsed = Buffer.from(inst.toPayload()); overwriteBuffer(this.buffer, inst.toPayload());
if (parsed.length >= this.buffer.length) {
// slice it down
parsed.copy(this.buffer, 0, 0, this.buffer.length);
} else {
// copy a small part only
parsed.copy(this.buffer, 0, 0, parsed.length);
}
} }
} }
......
This diff is collapsed.
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
"author": "zh99998 <zh99998@gmail.com>, mercury233 <me@mercury233.me>, Nanahira <78877@qq.com>", "author": "zh99998 <zh99998@gmail.com>, mercury233 <me@mercury233.me>, Nanahira <78877@qq.com>",
"dependencies": { "dependencies": {
"aragami": "^1.2.5", "aragami": "^1.2.5",
"async": "^3.2.0",
"axios": "^0.19.2", "axios": "^0.19.2",
"bunyan": "^1.8.14", "bunyan": "^1.8.14",
"deepmerge": "^4.2.2", "deepmerge": "^4.2.2",
...@@ -27,8 +26,6 @@ ...@@ -27,8 +26,6 @@
"mysql": "^2.18.1", "mysql": "^2.18.1",
"node-os-utils": "^1.3.2", "node-os-utils": "^1.3.2",
"p-queue": "^6.6.2", "p-queue": "^6.6.2",
"pg": "^6.4.2",
"q": "^1.5.1",
"querystring": "^0.2.0", "querystring": "^0.2.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"request": "^2.88.2", "request": "^2.88.2",
...@@ -38,7 +35,7 @@ ...@@ -38,7 +35,7 @@
"underscore.string": "^3.3.6", "underscore.string": "^3.3.6",
"ws": "^8.9.0", "ws": "^8.9.0",
"ygopro-deck-encode": "^1.0.15", "ygopro-deck-encode": "^1.0.15",
"ygopro-msg-encode": "^1.1.5", "ygopro-msg-encode": "^1.1.6",
"ygopro-yrp-encode": "^1.0.1" "ygopro-yrp-encode": "^1.0.1"
}, },
"license": "AGPL-3.0", "license": "AGPL-3.0",
...@@ -51,7 +48,6 @@ ...@@ -51,7 +48,6 @@
"webhook": "node ygopro-webhook.js" "webhook": "node ygopro-webhook.js"
}, },
"devDependencies": { "devDependencies": {
"@types/async": "^3.2.25",
"@types/bunyan": "^1.8.8", "@types/bunyan": "^1.8.8",
"@types/formidable": "^3.4.6", "@types/formidable": "^3.4.6",
"@types/ip6addr": "^0.2.3", "@types/ip6addr": "^0.2.3",
......
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.overwriteBuffer = void 0;
exports.retry = retry; exports.retry = retry;
async function retry(fn, count, delayFn = (attempt) => Math.pow(2, attempt) * 100) { async function retry(fn, count, delayFn = (attempt) => Math.pow(2, attempt) * 100) {
let lastError; let lastError;
...@@ -18,3 +19,13 @@ async function retry(fn, count, delayFn = (attempt) => Math.pow(2, attempt) * 10 ...@@ -18,3 +19,13 @@ async function retry(fn, count, delayFn = (attempt) => Math.pow(2, attempt) * 10
// 如果全部尝试失败,抛出最后一个错误 // 如果全部尝试失败,抛出最后一个错误
throw lastError; throw lastError;
} }
const overwriteBuffer = (buf, _input) => {
const input = Buffer.isBuffer(_input) ? _input : Buffer.from(_input);
if (input.length >= buf.length) {
input.copy(buf, 0, 0, buf.length);
}
else {
input.copy(buf, 0, 0, input.length);
}
};
exports.overwriteBuffer = overwriteBuffer;
...@@ -20,3 +20,13 @@ export async function retry<T>( ...@@ -20,3 +20,13 @@ export async function retry<T>(
// 如果全部尝试失败,抛出最后一个错误 // 如果全部尝试失败,抛出最后一个错误
throw lastError; throw lastError;
} }
export const overwriteBuffer = (buf: Buffer, _input: Buffer | Uint8Array) => {
const input = Buffer.isBuffer(_input) ? _input : Buffer.from(_input);
if (input.length >= buf.length) {
input.copy(buf, 0, 0, buf.length);
} else {
input.copy(buf, 0, 0, input.length);
}
}
...@@ -34,7 +34,6 @@ var cardHTMLs=[]; ...@@ -34,7 +34,6 @@ var cardHTMLs=[];
var responder; var responder;
//URL里的更新时间戳 //URL里的更新时间戳
var dataver = moment().format("YYYYMMDDHHmmss"); var dataver = moment().format("YYYYMMDDHHmmss");
const _async = require("async");
var sqlJsPromise; var sqlJsPromise;
var SQL = null; var SQL = null;
...@@ -53,6 +52,8 @@ function ensureSqlJs() { ...@@ -53,6 +52,8 @@ function ensureSqlJs() {
return sqlJsPromise; return sqlJsPromise;
} }
var execAsync = util.promisify(exec);
//输出反馈信息,如有http长连接则输出到http,否则输出到控制台 //输出反馈信息,如有http长连接则输出到http,否则输出到控制台
var sendResponse = function(text) { var sendResponse = function(text) {
text=""+text; text=""+text;
...@@ -221,7 +222,8 @@ var loadDb = function(db_file, callback) { ...@@ -221,7 +222,8 @@ var loadDb = function(db_file, callback) {
} }
//将cardHTMLs中内容更新到指定列表页,同步 //将cardHTMLs中内容更新到指定列表页,同步
var writeToFile = function(message, callback) { var writeToFile = async function(message, callback) {
try {
var fileContent=fs.readFileSync(config.html_path+config.html_filename, {"encoding":"utf-8"}); var fileContent=fs.readFileSync(config.html_path+config.html_filename, {"encoding":"utf-8"});
var newContent=cardHTMLs.join("\n"); var newContent=cardHTMLs.join("\n");
fileContent=fileContent.replace(/<tbody class="auto-generated">[\w\W]*<\/tbody>/,'<tbody class="auto-generated">\n'+newContent+'\n</tbody>'); fileContent=fileContent.replace(/<tbody class="auto-generated">[\w\W]*<\/tbody>/,'<tbody class="auto-generated">\n'+newContent+'\n</tbody>');
...@@ -231,47 +233,44 @@ var writeToFile = function(message, callback) { ...@@ -231,47 +233,44 @@ var writeToFile = function(message, callback) {
message="<li>"+moment().format('L HH:mm')+"<ul><li>"+message.split("!换行符!").join("</li><li>")+"</li></ul></li>"; message="<li>"+moment().format('L HH:mm')+"<ul><li>"+message.split("!换行符!").join("</li><li>")+"</li></ul></li>";
fileContent=fileContent.replace(/<ul class="auto-generated">/,'<ul class="auto-generated">\n'+message); fileContent=fileContent.replace(/<ul class="auto-generated">/,'<ul class="auto-generated">\n'+message);
} }
_async.auto({ await fs.promises.writeFile(config.html_path + config.html_filename, fileContent);
write: (done) => { // if (!config.cdn.enabled) {
fs.writeFile(config.html_path + config.html_filename, fileContent, done) // await util.promisify(copyImages)();
}/*, // }
copy: ["write", (results, done) => {
if (!config.cdn.enabled) {
copyImages(done);
} else {
done();
}
}]*/
}, (err) => {
if (!err) {
sendResponse("列表更新完成。"); sendResponse("列表更新完成。");
} callback(null);
} catch (err) {
callback(err); callback(err);
}) }
} }
//读取指定文件夹里所有数据库,异步 //读取指定文件夹里所有数据库,异步
var loadAllDbs = function(callback) { var loadAllDbs = async function(callback) {
try {
cardHTMLs=[]; cardHTMLs=[];
_async.auto({ var files = await fs.promises.readdir(config.db_path + "expansions/");
files: (done) => { var list = files.filter((filename) => {
fs.readdir(config.db_path + "expansions/", done); return filename.slice(-4) === ".cdb" && (!config.only_show_dbs || config.only_show_dbs.length == 0 || config.only_show_dbs[filename]);
}, }).map(filename => config.db_path + "expansions/" + filename);
loadDbs: ["files", (results, done) => { for (var i = 0; i < list.length; i++) {
_async.each(results.files.filter((filename) => { await util.promisify(loadDb)(list[i]);
return filename.slice(-4) === ".cdb" && (!config.only_show_dbs || config.only_show_dbs.length == 0 || config.only_show_dbs[filename]) }
} ).map(filename => config.db_path + "expansions/" + filename), loadDb, done); callback(null);
}] } catch (err) {
}, callback); callback(err);
}
} }
function execCommands(commands, callback) { async function execCommands(commands, callback) {
_async.eachSeries(commands, (command, done) => { try {
exec(command, (err) => { for (var i = 0; i < commands.length; i++) {
done(err); await execAsync(commands[i]);
}); }
}, callback); callback(null);
} catch (err) {
callback(err);
}
} }
//从远程更新数据库,异步 //从远程更新数据库,异步
...@@ -303,22 +302,17 @@ var fetchDatas = function() { ...@@ -303,22 +302,17 @@ var fetchDatas = function() {
} }
//更新本地网页到服务器,异步 //更新本地网页到服务器,异步
var pushDatas = function(callback) { var pushDatas = async function(callback) {
try {
if (config.cdn.enabled) { if (config.cdn.enabled) {
_async.auto({ await util.promisify(uploadCDN)(config.cdn.local, config.cdn.remote + "/" + dataver);
local: (done) => { await util.promisify(uploadCDN)(config.db_path + "pics", config.cdn.pics_remote + "pics");
uploadCDN(config.cdn.local, config.cdn.remote + "/" + dataver, done);
},
pics: ["local", (results, done) => {
uploadCDN(config.db_path + "pics", config.cdn.pics_remote + "pics", done);
}],
push: ["local", "pics", (results, done) => {
sendResponse("CDN上传全部完成。"); sendResponse("CDN上传全部完成。");
pushHTMLs(done); }
}] await util.promisify(pushHTMLs)();
}, callback); callback(null);
} else { } catch (err) {
pushHTMLs(callback); callback(err);
} }
} }
...@@ -432,15 +426,13 @@ function run7z(params, cwd, callback) { ...@@ -432,15 +426,13 @@ function run7z(params, cwd, callback) {
} }
//生成更新包,异步 //生成更新包,异步
var packDatas = function (callback) { var packDatas = async function (callback) {
file_path = config.html_path; file_path = config.html_path;
if (config.cdn.enabled) { if (config.cdn.enabled) {
file_path = config.cdn.local; file_path = config.cdn.local;
} }
try {
_async.auto({ await util.promisify(execCommands)([
preCommands: (done) => {
execCommands([
'rm -rf "' + config.db_path +'expansions/' + config.ypk_name + '"', 'rm -rf "' + config.db_path +'expansions/' + config.ypk_name + '"',
'rm -rf "' + config.db_path +'expansions/script"', 'rm -rf "' + config.db_path +'expansions/script"',
'rm -rf "' + config.db_path +'expansions/pics"', 'rm -rf "' + config.db_path +'expansions/pics"',
...@@ -453,64 +445,40 @@ var packDatas = function (callback) { ...@@ -453,64 +445,40 @@ var packDatas = function (callback) {
'cp -r "' + config.db_path + 'script" "' + config.db_path + 'expansions/script"', 'cp -r "' + config.db_path + 'script" "' + config.db_path + 'expansions/script"',
'cp -r "' + config.db_path + 'pics" "' + config.db_path + 'picture/card"', 'cp -r "' + config.db_path + 'pics" "' + config.db_path + 'picture/card"',
'cp -r "' + config.db_path + 'field" "' + config.db_path + 'picture/field"' 'cp -r "' + config.db_path + 'field" "' + config.db_path + 'picture/field"'
], done); ]);
}, await util.promisify(run7z)(["a", "-tzip", "-x!*.ypk", config.ypk_name, "*"], config.db_path + "expansions/");
run7zYPK: ["preCommands", (results, done) => { await util.promisify(run7z)(["a", "-x!*.zip", "-x!.git", "-x!LICENSE", "-x!README.md",
run7z(["a", "-tzip", "-x!*.ypk", config.ypk_name, "*"], config.db_path + "expansions/", done);
}],
run7zPC: ["run7zYPK", (results, done) => {
run7z(["a", "-x!*.zip", "-x!.git", "-x!LICENSE", "-x!README.md",
"-x!cdb", "-x!picture", "-x!field", "-x!script", "-x!pics", "-x!cdb", "-x!picture", "-x!field", "-x!script", "-x!pics",
"-x!expansions/pics", "-x!expansions/script", "-x!expansions/*.cdb", "-x!expansions/*.conf", "-x!expansions/pics", "-x!expansions/script", "-x!expansions/*.cdb", "-x!expansions/*.conf",
"ygosrv233-pre.zip", "*"], config.db_path, done); "ygosrv233-pre.zip", "*"], config.db_path);
}], await util.promisify(execCommands)([
run7zMobile: ["run7zYPK", (results, done) => {
run7z(["a", "-x!*.zip", "-x!.git", "-x!LICENSE", "-x!README.md",
"-x!cdb", "-x!picture", "-x!field", "-x!script", "-x!pics",
"-x!expansions/pics", "-x!expansions/script", "-x!expansions/*.cdb", "-x!expansions/*.conf",
"ygosrv233-pre-mobile.zip", "*"], config.db_path, done);
}],
run7zPro2: ["preCommands", (results, done) => {
run7z(["a", "-x!*.zip", "-x!.git", "-x!LICENSE", "-x!README.md",
"-x!expansions", "-x!pics", "-x!field",
"ygosrv233-pre-2.zip", "*"], config.db_path, done);
}],
commandsAfterPC: ["run7zPC", (results, done) => {
execCommands([
'mv -f "' + config.db_path + 'ygosrv233-pre.zip" "' + file_path + '"' 'mv -f "' + config.db_path + 'ygosrv233-pre.zip" "' + file_path + '"'
], (err) => { ]);
if (!err) {
sendResponse("电脑更新包打包完成。"); sendResponse("电脑更新包打包完成。");
} await util.promisify(run7z)(["a", "-x!*.zip", "-x!.git", "-x!LICENSE", "-x!README.md",
done(err); "-x!cdb", "-x!picture", "-x!field", "-x!script", "-x!pics",
}); "-x!expansions/pics", "-x!expansions/script", "-x!expansions/*.cdb", "-x!expansions/*.conf",
}], "ygosrv233-pre-mobile.zip", "*"], config.db_path);
commandsAfterMobile: ["run7zPC", "run7zMobile", (results, done) => { await util.promisify(execCommands)([
execCommands([
'mv -f "' + config.db_path +'ygosrv233-pre-mobile.zip" "'+ file_path +'"', 'mv -f "' + config.db_path +'ygosrv233-pre-mobile.zip" "'+ file_path +'"',
'rm -rf "' + config.db_path +'expansions/' + config.ypk_name + '"', 'rm -rf "' + config.db_path +'expansions/' + config.ypk_name + '"',
'rm -rf "' + config.db_path +'expansions/script"', 'rm -rf "' + config.db_path +'expansions/script"',
'rm -rf "' + config.db_path +'expansions/pics"' 'rm -rf "' + config.db_path +'expansions/pics"'
], (err) => { ]);
if (!err) {
sendResponse("手机更新包打包完成。"); sendResponse("手机更新包打包完成。");
} await util.promisify(run7z)(["a", "-x!*.zip", "-x!.git", "-x!LICENSE", "-x!README.md",
done(err); "-x!expansions", "-x!pics", "-x!field",
}); "ygosrv233-pre-2.zip", "*"], config.db_path);
}], await util.promisify(execCommands)([
commandsAfterPro2: ["run7zPro2", (results, done) => {
execCommands([
'mv -f "' + config.db_path + 'ygosrv233-pre-2.zip" "' + file_path + '"', 'mv -f "' + config.db_path + 'ygosrv233-pre-2.zip" "' + file_path + '"',
'rm -rf "' + config.db_path +'cdb"', 'rm -rf "' + config.db_path +'cdb"',
'rm -rf "' + config.db_path +'picture"' 'rm -rf "' + config.db_path +'picture"'
], (err) => { ]);
if (!err) {
sendResponse("Pro2更新包打包完成。"); sendResponse("Pro2更新包打包完成。");
callback(null);
} catch (err) {
callback(err);
} }
done(err);
});
}]
}, callback);
} }
//建立一个http服务器,接收API操作 //建立一个http服务器,接收API操作
......
This diff is collapsed.
This diff is collapsed.
...@@ -51,7 +51,6 @@ const axios_1 = __importDefault(require("axios")); ...@@ -51,7 +51,6 @@ const axios_1 = __importDefault(require("axios"));
const formidable = __importStar(require("formidable")); const formidable = __importStar(require("formidable"));
const load_json_file_1 = require("load-json-file"); const load_json_file_1 = require("load-json-file");
const challonge_1 = require("./challonge"); const challonge_1 = require("./challonge");
const asyncLib = __importStar(require("async"));
const ygopro_deck_encode_1 = __importDefault(require("ygopro-deck-encode")); const ygopro_deck_encode_1 = __importDefault(require("ygopro-deck-encode"));
const auth = __importStar(require("./ygopro-auth")); const auth = __importStar(require("./ygopro-auth"));
const underscore_1 = __importDefault(require("underscore")); const underscore_1 = __importDefault(require("underscore"));
...@@ -114,27 +113,21 @@ const readDeck = async function (deck_name, deck_full_path) { ...@@ -114,27 +113,21 @@ const readDeck = async function (deck_name, deck_full_path) {
return deck; return deck;
}; };
//读取指定文件夹中所有卡组 //读取指定文件夹中所有卡组
const getDecks = function (callback) { const getDecks = async function (callback) {
try {
const decks = []; const decks = [];
asyncLib.auto({ const decks_list = await fs.promises.readdir(config.deck_path);
readDir: (done) => { for (const deck_name of decks_list) {
fs.readdir(config.deck_path, done);
},
handleDecks: [
"readDir",
(results, done) => {
const decks_list = results.readDir;
asyncLib.each(decks_list, async (deck_name) => {
if (deck_name.endsWith(".ydk")) { if (deck_name.endsWith(".ydk")) {
const deck = await readDeck(deck_name, config.deck_path + deck_name); const deck = await readDeck(deck_name, config.deck_path + deck_name);
decks.push(deck); decks.push(deck);
} }
}, done); }
}, callback(null, decks);
], }
}, (err) => { catch (err) {
callback(err, decks); callback(err, []);
}); }
}; };
const delDeck = function (deck_name, callback) { const delDeck = function (deck_name, callback) {
if (deck_name.startsWith("../") || deck_name.match(/\/\.\.\//)) { if (deck_name.startsWith("../") || deck_name.match(/\/\.\.\//)) {
...@@ -143,19 +136,19 @@ const delDeck = function (deck_name, callback) { ...@@ -143,19 +136,19 @@ const delDeck = function (deck_name, callback) {
} }
fs.unlink(config.deck_path + deck_name, callback); fs.unlink(config.deck_path + deck_name, callback);
}; };
const clearDecks = function (callback) { const clearDecks = async function (callback) {
asyncLib.auto({ try {
deckList: (done) => { const decks_list = await fs.promises.readdir(config.deck_path);
fs.readdir(config.deck_path, done); for (const deck_name of decks_list) {
}, await new Promise((resolve, reject) => {
removeAll: [ delDeck(deck_name, (err) => (err ? reject(err) : resolve()));
"deckList", });
(results, done) => { }
const decks_list = results.deckList; callback(null);
asyncLib.each(decks_list, delDeck, done); }
}, catch (err) {
], callback(err);
}, callback); }
}; };
const UploadToChallonge = async function () { const UploadToChallonge = async function () {
if (!challonge_config.enabled) { if (!challonge_config.enabled) {
...@@ -194,9 +187,10 @@ const UploadToChallonge = async function () { ...@@ -194,9 +187,10 @@ const UploadToChallonge = async function () {
} }
return true; return true;
}; };
const receiveDecks = function (files, callback) { const receiveDecks = async function (files, callback) {
try {
const result = []; const result = [];
asyncLib.eachSeries(files, async (file) => { for (const file of files) {
if (file.name.endsWith(".ydk")) { if (file.name.endsWith(".ydk")) {
const deck = await readDeck(file.name, file.path); const deck = await readDeck(file.name, file.path);
if (deck.main.length >= 40) { if (deck.main.length >= 40) {
...@@ -219,9 +213,12 @@ const receiveDecks = function (files, callback) { ...@@ -219,9 +213,12 @@ const receiveDecks = function (files, callback) {
status: "不是卡组文件", status: "不是卡组文件",
}); });
} }
}, (err) => { }
callback(err, result); callback(null, result);
}); }
catch (err) {
callback(err, []);
}
}; };
//建立一个http服务器,接收API操作 //建立一个http服务器,接收API操作
async function requestListener(req, res) { async function requestListener(req, res) {
......
...@@ -15,7 +15,6 @@ import * as formidable from "formidable"; ...@@ -15,7 +15,6 @@ import * as formidable from "formidable";
import { sync as loadJSON } from "load-json-file"; import { sync as loadJSON } from "load-json-file";
import defaultConfig from "./data/default_config.json"; import defaultConfig from "./data/default_config.json";
import { Challonge } from "./challonge"; import { Challonge } from "./challonge";
import * as asyncLib from "async";
import YGOProDeckEncode from "ygopro-deck-encode"; import YGOProDeckEncode from "ygopro-deck-encode";
import * as auth from "./ygopro-auth"; import * as auth from "./ygopro-auth";
import _ from "underscore"; import _ from "underscore";
...@@ -82,34 +81,20 @@ const readDeck = async function (deck_name: string, deck_full_path: string) { ...@@ -82,34 +81,20 @@ const readDeck = async function (deck_name: string, deck_full_path: string) {
}; };
//读取指定文件夹中所有卡组 //读取指定文件夹中所有卡组
const getDecks = function (callback: (err: Error | null, decks: any[]) => void) { const getDecks = async function (callback: (err: Error | null, decks: any[]) => void) {
try {
const decks: any[] = []; const decks: any[] = [];
asyncLib.auto( const decks_list = await fs.promises.readdir(config.deck_path);
{ for (const deck_name of decks_list) {
readDir: (done: (err: NodeJS.ErrnoException | null, files?: string[]) => void) => {
fs.readdir(config.deck_path, done);
},
handleDecks: [
"readDir",
(results: any, done: (err?: Error | null) => void) => {
const decks_list = results.readDir as string[];
asyncLib.each(
decks_list,
async (deck_name: string) => {
if (deck_name.endsWith(".ydk")) { if (deck_name.endsWith(".ydk")) {
const deck = await readDeck(deck_name, config.deck_path + deck_name); const deck = await readDeck(deck_name, config.deck_path + deck_name);
decks.push(deck); decks.push(deck);
} }
},
done
);
},
],
},
(err: Error | null) => {
callback(err, decks);
} }
); callback(null, decks);
} catch (err) {
callback(err as Error, []);
}
}; };
const delDeck = function (deck_name: string, callback: (err?: NodeJS.ErrnoException | null) => void) { const delDeck = function (deck_name: string, callback: (err?: NodeJS.ErrnoException | null) => void) {
...@@ -120,22 +105,18 @@ const delDeck = function (deck_name: string, callback: (err?: NodeJS.ErrnoExcept ...@@ -120,22 +105,18 @@ const delDeck = function (deck_name: string, callback: (err?: NodeJS.ErrnoExcept
fs.unlink(config.deck_path + deck_name, callback); fs.unlink(config.deck_path + deck_name, callback);
}; };
const clearDecks = function (callback: (err?: Error | null) => void) { const clearDecks = async function (callback: (err?: Error | null) => void) {
asyncLib.auto( try {
{ const decks_list = await fs.promises.readdir(config.deck_path);
deckList: (done: (err: NodeJS.ErrnoException | null, files?: string[]) => void) => { for (const deck_name of decks_list) {
fs.readdir(config.deck_path, done); await new Promise<void>((resolve, reject) => {
}, delDeck(deck_name, (err) => (err ? reject(err) : resolve()));
removeAll: [ });
"deckList", }
(results: any, done: (err?: Error | null) => void) => { callback(null);
const decks_list = results.deckList as string[]; } catch (err) {
asyncLib.each(decks_list, delDeck as any, done); callback(err as Error);
}, }
],
},
callback
);
}; };
const UploadToChallonge = async function () { const UploadToChallonge = async function () {
...@@ -179,14 +160,13 @@ const UploadToChallonge = async function () { ...@@ -179,14 +160,13 @@ const UploadToChallonge = async function () {
return true; return true;
}; };
const receiveDecks = function ( const receiveDecks = async function (
files: any, files: any,
callback: (err: Error | null, result: Array<{ file: string; status: string }>) => void callback: (err: Error | null, result: Array<{ file: string; status: string }>) => void
) { ) {
try {
const result: Array<{ file: string; status: string }> = []; const result: Array<{ file: string; status: string }> = [];
asyncLib.eachSeries( for (const file of files) {
files,
async (file: any) => {
if (file.name.endsWith(".ydk")) { if (file.name.endsWith(".ydk")) {
const deck = await readDeck(file.name, file.path); const deck = await readDeck(file.name, file.path);
if (deck.main.length >= 40) { if (deck.main.length >= 40) {
...@@ -207,11 +187,11 @@ const receiveDecks = function ( ...@@ -207,11 +187,11 @@ const receiveDecks = function (
status: "不是卡组文件", status: "不是卡组文件",
}); });
} }
},
(err: Error | null) => {
callback(err, result);
} }
); callback(null, result);
} catch (err) {
callback(err as Error, []);
}
}; };
//建立一个http服务器,接收API操作 //建立一个http服务器,接收API操作
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment