Commit 86fffcae authored by nanahira's avatar nanahira

remake challonge upload

parent 7fe9cd70
Pipeline #13262 passed with stages
in 18 minutes and 3 seconds
...@@ -16,6 +16,7 @@ const _ = require('underscore'); ...@@ -16,6 +16,7 @@ const _ = require('underscore');
_.str = require('underscore.string'); _.str = require('underscore.string');
_.mixin(_.str.exports()); _.mixin(_.str.exports());
const loadJSON = require('load-json-file').sync; const loadJSON = require('load-json-file').sync;
const axios = require('axios');
const auth = require('./ygopro-auth.js'); const auth = require('./ygopro-auth.js');
...@@ -24,10 +25,6 @@ config = settings.modules.tournament_mode; ...@@ -24,10 +25,6 @@ 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;
let challonge;
if (challonge_config.enabled) {
challonge = require('challonge').createClient(challonge_config.options);
}
const _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;
...@@ -138,8 +135,8 @@ const clearDecks = function (callback) { ...@@ -138,8 +135,8 @@ const clearDecks = function (callback) {
}, callback); }, callback);
} }
const UploadToChallonge = function () { const UploadToChallonge = async function () {
if (!challonge) { if (!challonge_config.enabled) {
sendResponse("未开启Challonge模式。"); sendResponse("未开启Challonge模式。");
return false; return false;
} }
...@@ -157,30 +154,25 @@ const UploadToChallonge = function () { ...@@ -157,30 +154,25 @@ const UploadToChallonge = function () {
return false; return false;
} }
sendResponse("读取玩家列表完毕,共有" + player_list.length + "名玩家。"); sendResponse("读取玩家列表完毕,共有" + player_list.length + "名玩家。");
sendResponse("开始上传玩家列表至Challonge。"); try {
_async.each(player_list, (player_name, done) => { sendResponse("开始清空 Challonge 玩家列表。");
sendResponse("正在上传玩家 " + player_name + " 至Challonge。"); await axios.delete(`https://api.challonge.com/v1/tournaments/${challonge_config.tournament_id}/participants/clear.json`, {
challonge.participants.create({ params: {
id: challonge_config.tournament_id, api_key: challonge_config.options.apiKey
participant: {
name: player_name
},
callback: (err, data) => {
if (err) {
sendResponse("玩家 "+player_name+" 上传失败:"+err.text);
} else {
if (data.participant) {
sendResponse("玩家 "+player_name+" 上传完毕,其Challonge ID是 "+data.participant.id+"");
} else {
sendResponse("玩家 "+player_name+" 上传完毕。");
}
}
done();
} }
}); });
}, (err) => { sendResponse("开始上传玩家列表至 Challonge。");
for (const chunk of _.chunk(player_list, 10)) {
sendResponse(`开始上传玩家 ${chunk.join(', ')} 至 Challonge。`);
await axios.post(`https://api.challonge.com/v1/tournaments/${challonge_config.tournament_id}/participants/bulk_add.json`, {
api_key: challonge_config.options.apiKey,
participants: chunk.map(name => ({ name })),
}, axiosPostConfig);
}
sendResponse("玩家列表上传完成。"); sendResponse("玩家列表上传完成。");
}); } catch (e) {
sendResponse("Challonge 上传失败:" + e.message);
}
return true; return true;
} }
...@@ -338,7 +330,7 @@ async function requestListener(req, res) { ...@@ -338,7 +330,7 @@ async function requestListener(req, res) {
return; return;
} }
res.writeHead(200); res.writeHead(200);
const result = UploadToChallonge(); const result = await 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