Commit e8db339b authored by nanahira's avatar nanahira

fix formidable version and somehow rework tournament.js

parent 1d606a2c
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"bunyan": "^1.8.14", "bunyan": "^1.8.14",
"challonge": "^2.2.0", "challonge": "^2.2.0",
"deepmerge": "^4.2.2", "deepmerge": "^4.2.2",
"formidable": "^2.0.1", "formidable": "^1.2.6",
"geoip-country-lite": "^1.0.0", "geoip-country-lite": "^1.0.0",
"jszip": "^3.5.0", "jszip": "^3.5.0",
"load-json-file": "^6.2.0", "load-json-file": "^6.2.0",
...@@ -169,11 +169,6 @@ ...@@ -169,11 +169,6 @@
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
}, },
"node_modules/asap": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
"integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY="
},
"node_modules/asn1": { "node_modules/asn1": {
"version": "0.2.4", "version": "0.2.4",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz",
...@@ -723,15 +718,6 @@ ...@@ -723,15 +718,6 @@
"node": ">=0.10" "node": ">=0.10"
} }
}, },
"node_modules/dezalgo": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz",
"integrity": "sha1-f3Qt4Gb8dIvI24IFad3c5Jvw1FY=",
"dependencies": {
"asap": "^2.0.0",
"wrappy": "1"
}
},
"node_modules/dotenv": { "node_modules/dotenv": {
"version": "8.2.0", "version": "8.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
...@@ -892,30 +878,14 @@ ...@@ -892,30 +878,14 @@
} }
}, },
"node_modules/formidable": { "node_modules/formidable": {
"version": "2.0.1", "version": "1.2.6",
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.0.1.tgz", "resolved": "https://registry.npmjs.org/formidable/-/formidable-1.2.6.tgz",
"integrity": "sha512-rjTMNbp2BpfQShhFbR3Ruk3qk2y9jKpvMW78nJgx8QKtxjDVrwbZG+wvDOmVbifHyOUOQJXxqEy6r0faRrPzTQ==", "integrity": "sha512-KcpbcpuLNOwrEjnbpMC0gS+X8ciDoZE1kkqzat4a8vrprf+s9pKNQ/QIwWfbfs4ltgmFl3MD177SNTkve3BwGQ==",
"dependencies": { "deprecated": "Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau",
"dezalgo": "1.0.3",
"hexoid": "1.0.0",
"once": "1.4.0",
"qs": "6.9.3"
},
"funding": { "funding": {
"url": "https://ko-fi.com/tunnckoCore/commissions" "url": "https://ko-fi.com/tunnckoCore/commissions"
} }
}, },
"node_modules/formidable/node_modules/qs": {
"version": "6.9.3",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.3.tgz",
"integrity": "sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw==",
"engines": {
"node": ">=0.6"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/fs-minipass": { "node_modules/fs-minipass": {
"version": "1.2.7", "version": "1.2.7",
"resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz", "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz",
...@@ -1118,14 +1088,6 @@ ...@@ -1118,14 +1088,6 @@
"resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz", "resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz",
"integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk=" "integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk="
}, },
"node_modules/hexoid": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz",
"integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==",
"engines": {
"node": ">=8"
}
},
"node_modules/highlight.js": { "node_modules/highlight.js": {
"version": "9.18.3", "version": "9.18.3",
"resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-9.18.3.tgz", "resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-9.18.3.tgz",
...@@ -3188,11 +3150,6 @@ ...@@ -3188,11 +3150,6 @@
} }
} }
}, },
"asap": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
"integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY="
},
"asn1": { "asn1": {
"version": "0.2.4", "version": "0.2.4",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz",
...@@ -3594,15 +3551,6 @@ ...@@ -3594,15 +3551,6 @@
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz",
"integrity": "sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=" "integrity": "sha1-+hN8S9aY7fVc1c0CrFWfkaTEups="
}, },
"dezalgo": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz",
"integrity": "sha1-f3Qt4Gb8dIvI24IFad3c5Jvw1FY=",
"requires": {
"asap": "^2.0.0",
"wrappy": "1"
}
},
"dotenv": { "dotenv": {
"version": "8.2.0", "version": "8.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
...@@ -3727,22 +3675,9 @@ ...@@ -3727,22 +3675,9 @@
} }
}, },
"formidable": { "formidable": {
"version": "2.0.1", "version": "1.2.6",
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.0.1.tgz", "resolved": "https://registry.npmjs.org/formidable/-/formidable-1.2.6.tgz",
"integrity": "sha512-rjTMNbp2BpfQShhFbR3Ruk3qk2y9jKpvMW78nJgx8QKtxjDVrwbZG+wvDOmVbifHyOUOQJXxqEy6r0faRrPzTQ==", "integrity": "sha512-KcpbcpuLNOwrEjnbpMC0gS+X8ciDoZE1kkqzat4a8vrprf+s9pKNQ/QIwWfbfs4ltgmFl3MD177SNTkve3BwGQ=="
"requires": {
"dezalgo": "1.0.3",
"hexoid": "1.0.0",
"once": "1.4.0",
"qs": "6.9.3"
},
"dependencies": {
"qs": {
"version": "6.9.3",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.3.tgz",
"integrity": "sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw=="
}
}
}, },
"fs-minipass": { "fs-minipass": {
"version": "1.2.7", "version": "1.2.7",
...@@ -3908,11 +3843,6 @@ ...@@ -3908,11 +3843,6 @@
"resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz", "resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz",
"integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk=" "integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk="
}, },
"hexoid": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz",
"integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g=="
},
"highlight.js": { "highlight.js": {
"version": "9.18.3", "version": "9.18.3",
"resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-9.18.3.tgz", "resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-9.18.3.tgz",
......
...@@ -6,34 +6,34 @@ ...@@ -6,34 +6,34 @@
不带参数运行时,会建立一个服务器,调用API执行对应操作 不带参数运行时,会建立一个服务器,调用API执行对应操作
*/ */
var http = require('http'); const http = require('http');
var https = require('https'); const https = require('https');
var fs = require('fs'); const fs = require('fs');
var url = require('url'); const url = require('url');
var request = require('request'); const request = require('request');
var formidable = require('formidable'); const formidable = require('formidable');
var _ = require('underscore'); const _ = require('underscore');
_.str = require('underscore.string'); _.str = require('underscore.string');
_.mixin(_.str.exports()); _.mixin(_.str.exports());
var loadJSON = require('load-json-file').sync; const loadJSON = require('load-json-file').sync;
var auth = require('./ygopro-auth.js'); const auth = require('./ygopro-auth.js');
var settings = loadJSON('./config/config.json'); const settings = loadJSON('./config/config.json');
config = settings.modules.tournament_mode; config = settings.modules.tournament_mode;
challonge_config = settings.modules.challonge; challonge_config = settings.modules.challonge;
ssl_config = settings.modules.http.ssl; ssl_config = settings.modules.http.ssl;
var challonge; let challonge;
if (challonge_config.enabled) { if (challonge_config.enabled) {
challonge = require('challonge').createClient(challonge_config.options); challonge = require('challonge').createClient(challonge_config.options);
} }
let _async = require("async"); const _async = require("async");
const os = require("os"); const os = require("os");
const PROCESS_COUNT = os.cpus().length; const PROCESS_COUNT = os.cpus().length;
//http长连接 //http长连接
var responder; let responder;
config.wallpapers=[""]; config.wallpapers=[""];
request({ request({
...@@ -48,9 +48,9 @@ request({ ...@@ -48,9 +48,9 @@ request({
} }
else { else {
config.wallpapers=[]; config.wallpapers=[];
for (var i in body.images) { for (const i in body.images) {
var wallpaper=body.images[i]; const wallpaper=body.images[i];
var img={ const img={
"url": "http://s.cn.bing.net"+wallpaper.urlbase+"_768x1366.jpg", "url": "http://s.cn.bing.net"+wallpaper.urlbase+"_768x1366.jpg",
"desc": wallpaper.copyright "desc": wallpaper.copyright
} }
...@@ -60,7 +60,7 @@ request({ ...@@ -60,7 +60,7 @@ request({
}); });
//输出反馈信息,如有http长连接则输出到http,否则输出到控制台 //输出反馈信息,如有http长连接则输出到http,否则输出到控制台
var sendResponse = function(text) { const sendResponse = function(text) {
text=""+text; text=""+text;
if (responder) { if (responder) {
text=text.replace(/\n/g,"<br>"); text=text.replace(/\n/g,"<br>");
...@@ -72,10 +72,10 @@ var sendResponse = function(text) { ...@@ -72,10 +72,10 @@ var sendResponse = function(text) {
} }
//读取指定卡组 //读取指定卡组
var readDeck = function(deck_name, deck_full_path) { const readDeck = async function(deck_name, deck_full_path) {
var deck={}; const deck={};
deck.name=deck_name; deck.name=deck_name;
deck_text = fs.readFileSync(deck_full_path, { encoding: "ASCII" }); deck_text = await fs.promises.readFile(deck_full_path, { encoding: "ASCII" });
deck_array = deck_text.split("\n"); deck_array = deck_text.split("\n");
deck.main = []; deck.main = [];
deck.extra = []; deck.extra = [];
...@@ -98,20 +98,19 @@ var readDeck = function(deck_name, deck_full_path) { ...@@ -98,20 +98,19 @@ var readDeck = function(deck_name, deck_full_path) {
} }
//读取指定文件夹中所有卡组 //读取指定文件夹中所有卡组
var getDecks = function(callback) { const getDecks = function(callback) {
var decks=[]; const decks=[];
_async.auto({ _async.auto({
readDir: (done) => { readDir: (done) => {
fs.readdir(config.deck_path, done); fs.readdir(config.deck_path, done);
}, },
handleDecks: ["readDir", (results, done) => { handleDecks: ["readDir", (results, done) => {
const decks_list = results.readDir; const decks_list = results.readDir;
_async.each(decks_list, (deck_name, _done) => { _async.each(decks_list, async(deck_name) => {
if (_.endsWith(deck_name, ".ydk")) { if (_.endsWith(deck_name, ".ydk")) {
var deck = 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();
}, done) }, done)
}] }]
}, (err) => { }, (err) => {
...@@ -120,14 +119,14 @@ var getDecks = function(callback) { ...@@ -120,14 +119,14 @@ var getDecks = function(callback) {
} }
var delDeck = function (deck_name, callback) { const delDeck = function (deck_name, callback) {
if (deck_name.startsWith("../") || deck_name.match(/\/\.\.\//)) { //security issue if (deck_name.startsWith("../") || deck_name.match(/\/\.\.\//)) { //security issue
callback("Invalid deck"); callback("Invalid deck");
} }
fs.unlink(config.deck_path + deck_name, callback); fs.unlink(config.deck_path + deck_name, callback);
} }
var clearDecks = function (callback) { const clearDecks = function (callback) {
_async.auto({ _async.auto({
deckList: (done) => { deckList: (done) => {
fs.readdir(config.deck_path, done); fs.readdir(config.deck_path, done);
...@@ -139,16 +138,16 @@ var clearDecks = function (callback) { ...@@ -139,16 +138,16 @@ var clearDecks = function (callback) {
}, callback); }, callback);
} }
var UploadToChallonge = function () { const UploadToChallonge = function () {
if (!challonge) { if (!challonge) {
sendResponse("未开启Challonge模式。"); sendResponse("未开启Challonge模式。");
return false; return false;
} }
sendResponse("开始读取玩家列表。"); sendResponse("开始读取玩家列表。");
var decks_list = fs.readdirSync(config.deck_path); const decks_list = fs.readdirSync(config.deck_path);
var player_list = []; const player_list = [];
for (var k in decks_list) { for (const k in decks_list) {
var deck_name = decks_list[k]; const deck_name = decks_list[k];
if (_.endsWith(deck_name, ".ydk")) { if (_.endsWith(deck_name, ".ydk")) {
player_list.push(deck_name.slice(0, deck_name.length - 4)); player_list.push(deck_name.slice(0, deck_name.length - 4));
} }
...@@ -185,11 +184,11 @@ var UploadToChallonge = function () { ...@@ -185,11 +184,11 @@ var UploadToChallonge = function () {
return true; return true;
} }
var receiveDecks = function(files, callback) { const receiveDecks = function(files, callback) {
var result = []; const result = [];
_async.eachSeries(files, (file, done) => { _async.eachSeries(files, async(file) => {
if (_.endsWith(file.name, ".ydk")) { if (_.endsWith(file.name, ".ydk")) {
var deck = readDeck(file.name, file.path); const deck = await readDeck(file.name, file.path);
if (deck.main.length >= 40) { if (deck.main.length >= 40) {
fs.createReadStream(file.path).pipe(fs.createWriteStream(config.deck_path + file.name)); fs.createReadStream(file.path).pipe(fs.createWriteStream(config.deck_path + file.name));
result.push({ result.push({
...@@ -210,7 +209,6 @@ var receiveDecks = function(files, callback) { ...@@ -210,7 +209,6 @@ var receiveDecks = function(files, callback) {
status: "不是卡组文件" status: "不是卡组文件"
}); });
} }
done();
}, (err) => { }, (err) => {
callback(err, result); callback(err, result);
}); });
...@@ -218,7 +216,7 @@ var receiveDecks = function(files, callback) { ...@@ -218,7 +216,7 @@ var receiveDecks = function(files, callback) {
//建立一个http服务器,接收API操作 //建立一个http服务器,接收API操作
async function requestListener(req, res) { async function requestListener(req, res) {
var u = url.parse(req.url, true); const u = url.parse(req.url, true);
/*if (u.query.password !== config.password) { /*if (u.query.password !== config.password) {
res.writeHead(403); res.writeHead(403);
...@@ -232,15 +230,17 @@ async function requestListener(req, res) { ...@@ -232,15 +230,17 @@ async function requestListener(req, res) {
res.end("Auth Failed."); res.end("Auth Failed.");
return; return;
} }
var form = new formidable.IncomingForm(); const form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) { form.parse(req, function(err, fields, files) {
receiveDecks(files, (err, result) => { receiveDecks(files, (err, result) => {
if (err) { if (err) {
res.writeHead(200, { console.error(`Upload error: ${err}`);
res.writeHead(500, {
"Access-Control-Allow-origin": "*", "Access-Control-Allow-origin": "*",
'content-type': 'text/plain' 'content-type': 'text/plain'
}); });
res.end(err.toString()); res.end(JSON.stringify({error: err.toString()}));
return;
} }
res.writeHead(200, { res.writeHead(200, {
"Access-Control-Allow-origin": "*", "Access-Control-Allow-origin": "*",
...@@ -338,7 +338,7 @@ async function requestListener(req, res) { ...@@ -338,7 +338,7 @@ async function requestListener(req, res) {
return; return;
} }
res.writeHead(200); res.writeHead(200);
var result=UploadToChallonge(); const result = UploadToChallonge();
res.end(u.query.callback+'("操作完成。");'); res.end(u.query.callback+'("操作完成。");');
} }
else { else {
......
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