Skip to content

Commit

Permalink
Merge branch 'master' into version
Browse files Browse the repository at this point in the history
  • Loading branch information
dungwinux authored Mar 5, 2019
2 parents 43042ec + 9b98476 commit 6f6eb66
Show file tree
Hide file tree
Showing 25 changed files with 502 additions and 72 deletions.
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: 1.{build}
skip_tags: true
pull_requests:
do_not_increment_build_number: true
branches:
Expand Down
4 changes: 2 additions & 2 deletions src/config/contest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";

const contestConfig = require("../util/config/readCtConfig");
const contestConfig = require("../util/config/contestConfig");

module.exports = contestConfig.config();
module.exports = contestConfig.read();
4 changes: 2 additions & 2 deletions src/config/kon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const { existsSync, mkdirSync } = require("fs");

const KonConfig = require("../util/config/readKonConfig");
const KonConfig = require("../util/config/KonConfig");

// Require valid folder to work
const taskFolder = "Tasks";

if (!existsSync(taskFolder)) mkdirSync(taskFolder);

module.exports = {
judgers: KonConfig.config(),
judgers: KonConfig.read(),
tasks: taskFolder
};
4 changes: 2 additions & 2 deletions src/config/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require("path");
const { existsSync, mkdirSync, writeFileSync } = require("fs");

const serverConfig = require("../util/config/readServerConfig");
const serverConfig = require("../util/config/serverConfig");

const staticFolder = path.join(__dirname, "../../public");
if (!existsSync(staticFolder)) mkdirSync(staticFolder);
Expand All @@ -12,7 +12,7 @@ const sampleHTML =
if (!existsSync(staticFolder + "/index.html"))
writeFileSync(staticFolder + "/index.html", sampleHTML);

const serverCfg = Object.assign(serverConfig.config(), {
const serverCfg = Object.assign(serverConfig(), {
staticFolder: staticFolder
});

Expand Down
14 changes: 11 additions & 3 deletions src/controller/initJudger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ function initJudger() {
judger
.clone(arcPath)
.then((res) => {
if (!res.status === 200)
throw Error("Kon isn't return 200");
Console.log(`Sucessfully cloned ${judger.serverAddress}`);
if (res.status === 403)
Console.log(`Kon was in used. ${judger.serverAddress}`);
else if (res.status === 500)
Console.log(
`Kon failed to process. ${judger.serverAddress}`
);
else if (res.status === 200)
Console.log(
`Sucessfully cloned ${judger.serverAddress}`
);
else throw Error("Kon did not respond");
})
.catch((err) => {
Console.log(
Expand Down
5 changes: 3 additions & 2 deletions src/controller/submitCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ async function sendCode(source_code_path, user_id, prob_name) {
Console.log(err.message);
});

// Temporary trigger
reloadSubs();
setImmediate(() => reloadSubs());
// Second trigger: 45 seconds
setTimeout(() => reloadSubs(), 45000);
} catch (err) {
Console.log(err.message);
}
Expand Down
6 changes: 6 additions & 0 deletions src/driver/kon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class Judger {
this.serverAddress = server_address;
this.probList = prob_list;
}
toJSON() {
return {
url: this.serverAddress,
prob: this.probList
};
}
/**
* Part 1: checking status of availability of judger
* @return {Promise<Boolean>} true if judger is available, false otherwise
Expand Down
23 changes: 19 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
const Enquirer = require("enquirer");
const Console = require("console");

require("./util/config/contestConfig").genIfNotExist();
require("./util/config/KonConfig").genIfNotExist();

const addUser = require("./prompt/addUser");
const contestOptions = require("./prompt/contestOptions");
const KonOptions = require("./prompt/KonOptions");

const enquirer = new Enquirer();

const mainChoices = {
"Start server": () => {},
"Add user": () => addUser(),
"Add user": addUser,
"Contest options": contestOptions,
"Kon's pair options": KonOptions,
Exit: () => {}
};

Expand All @@ -15,7 +23,7 @@ const mainChoices = {
*/
async function mainPrompt() {
return enquirer.prompt({
type: "autocomplete",
type: "select",
name: "main",
message: "How can I help you ?",
choices: Object.keys(mainChoices)
Expand All @@ -27,12 +35,20 @@ async function mainPrompt() {
*/
async function main() {
let res = {};

Console.log("MIRAI Wafter 1.5");
Console.log("Copyright (c) 2018 Vườn ươm A0. MIT License.");

try {
while (res.main !== "Exit") {
res = await mainPrompt();
await mainChoices[res.main]();
try {
await mainChoices[res.main]();
} catch (err) {
Console.log(
`Exited to menu${err.message ? `: ${err.message}` : ""}`
);
}

if (res.main === "Start server") {
require("./server");
Expand All @@ -41,7 +57,6 @@ async function main() {
}
} catch (err) {
Console.log(err.message);
process.exit();
}
process.exit();
}
Expand Down
36 changes: 36 additions & 0 deletions src/prompt/KonConfig/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";

const Enquirer = require("enquirer");
const validUrl = require("valid-url");
const Judger = require("../../driver/kon");
const contestConfig = require("../../util/config/contestConfig");

const enquirer = new Enquirer();

/**
* Kon addition prompt
*/
async function addKonPrompt(init = []) {
const _probList = contestConfig.read().probList;
const { url, prob } = await enquirer.prompt([
{
type: "input",
name: "url",
message: "URL",
// hint: "http:// or https:// url",
validate: (val) => !!validUrl.isWebUri(val)
},
{
type: "multiselect",
name: "prob",
message: "Problems handled by this Kon",
hint: "If none are chosen, Wafter will understand Kon accept all",
choices: _probList
}
]);
const newList = init;
newList.push(new Judger(url, prob));
return newList;
}

module.exports = addKonPrompt;
29 changes: 29 additions & 0 deletions src/prompt/KonConfig/manage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

const Enquirer = require("enquirer");

const enquirer = new Enquirer();

/**
* Kon management prompt
*/
async function manKonPrompt(init = []) {
const cached = init.reduce((obj, kon) => {
obj[kon.url] = kon;
return obj;
}, {});
const _konList = init.map((x) => ({
name: x.url,
hint: x.prob && x.prob.length > 0 ? JSON.stringify(x.prob) : "*"
}));
const { konList } = await enquirer.prompt({
type: "multiselect",
name: "konList",
message: "Manage Kon in use:",
initial: _konList,
choices: _konList
});
return konList.map((x) => cached[x]);
}

module.exports = manKonPrompt;
46 changes: 46 additions & 0 deletions src/prompt/KonOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const Console = require("console");
const Enquirer = require("enquirer");

const KonConfig = require("../util/config/KonConfig");

const add = require("./KonConfig/add");
const manage = require("./KonConfig/manage");

const enquirer = new Enquirer();

/**
* Prompt for contest's option
*/
async function KonOptionsPrompt() {
let KonOptionsContainer = {
"Add Kon": add,
"Manage Kon": manage
};

let KonOptionsChoices = [
"Add Kon",
{ name: "Manage Kon", disabled: KonConfig.read().length === 0 }
];

const { choice } = await enquirer.prompt({
type: "select",
name: "choice",
message: "Kon's Pair Options",
choices: KonOptionsChoices
});
const jsonized = JSON.parse(JSON.stringify(KonConfig.read()));
return KonOptionsContainer[choice](jsonized)
.then((newData) => {
KonConfig.update(newData);
Console.log("Kon's pair configuration saved");
})
.catch((err) => {
Console.log(
`Kon's pair configuration was not saved${
err.message ? `: ${err.message}` : ""
}`
);
});
}

module.exports = KonOptionsPrompt;
35 changes: 35 additions & 0 deletions src/prompt/contestConfig/ace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

const Enquirer = require("enquirer");

const enquirer = new Enquirer();

/**
* Control Code Extension prompt
*/
async function acePrompt(init = {}) {
let _ace = init.allowedCodeExt;

// Temporary solution: create list of language, then filter non-included
const langList = [".C", ".CPP", ".JAVA", ".KT", ".PAS", ".PY"];
const initAce = langList.filter((x) => _ace.indexOf(x) > -1);
const leftAce = _ace.filter((x) => langList.indexOf(x) === -1);
const { ace } = await enquirer.prompt({
type: "multiselect",
name: "ace",
message: "Set allowed code extension",
hint: "Use <space> to select, <return> to submit",
initial: initAce,
choices: [
{ name: ".C", hint: "- C" },
{ name: ".CPP", hint: "- C++" },
{ name: ".JAVA", hint: "- Java" },
{ name: ".KT", hint: "- Kotlin" },
{ name: ".PAS", hint: "- Pascal" },
{ name: ".PY", hint: "- Python" }
]
});
return { allowedCodeExt: ace.concat(leftAce) };
}

module.exports = acePrompt;
22 changes: 22 additions & 0 deletions src/prompt/contestConfig/manProbList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

const Enquirer = require("enquirer");

const enquirer = new Enquirer();

/**
* Problem management prompt
*/
async function manProbPrompt(init = {}) {
let _probList = init.probList;
const { probList } = await enquirer.prompt({
type: "multiselect",
name: "probList",
message: "Manage problems in contest:",
initial: _probList,
choices: _probList
});
return { probList };
}

module.exports = manProbPrompt;
23 changes: 23 additions & 0 deletions src/prompt/contestConfig/mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

const Enquirer = require("enquirer");
const score = require("../../util/score");

const enquirer = new Enquirer();

/**
* Contest mode prompt
*/
async function modePrompt(init = {}) {
const _mode = score.hasOwnProperty(init.mode) ? init.mode : null;
const { mode } = await enquirer.prompt({
type: "select",
name: "mode",
message: "Contest mode:",
initial: _mode,
choices: Object.keys(score)
});
return { mode };
}

module.exports = modePrompt;
22 changes: 22 additions & 0 deletions src/prompt/contestConfig/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

const Enquirer = require("enquirer");

const enquirer = new Enquirer();

/**
* Contest name prompt
*/
async function namePrompt(init = {}) {
const _name = init.name;
const { name } = await enquirer.prompt({
type: "input",
name: "name",
initial: _name,
message: "Contest name:"
});

return { name };
}

module.exports = namePrompt;
21 changes: 21 additions & 0 deletions src/prompt/contestConfig/probList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

const Enquirer = require("enquirer");

const enquirer = new Enquirer();

/**
* Contest problem addition prompt
*/
async function probListPrompt(init = {}) {
let _probList = init.probList;
const { probList } = await enquirer.prompt({
type: "list",
name: "probList",
message: "Add problem to contest:"
});
_probList = _probList.concat(probList);
return { probList: _probList };
}

module.exports = probListPrompt;
Loading

0 comments on commit 6f6eb66

Please sign in to comment.