比赛计分板

pull/1/head
masnn 5 years ago
parent 9bf3343a64
commit f73859a42f

@ -86,6 +86,24 @@ class ValidationError extends ForbiddenError {
else this.params = [field0, field1];
}
}
class ContestNotAttendedError extends ForbiddenError {
constructor(tid) {
super('You haven\'t attended this contest yet.');
this.params = [tid];
}
}
class ContestNotLiveError extends ForbiddenError {
constructor(tid) {
super('This contest is not live.');
this.params = [tid];
}
}
class ContestScoreboardHiddenError extends ForbiddenError {
constructor(tid) {
super('Contest scoreboard is not visible.');
this.params = [tid];
}
}
class ProblemNotFoundError extends NotFoundError {
constructor(pid) {
super('ProblemNotFoundError');
@ -116,7 +134,6 @@ class ContestNotFoundError extends NotFoundError {
this.params = [cid];
}
}
class ProblemDataNotFoundError extends NotFoundError {
constructor(pid) {
super('Data of problem {0} not found.');
@ -130,7 +147,8 @@ module.exports = {
OpcountExceededError, PermissionError, NoProblemError,
ValidationError, ProblemNotFoundError, TrainingNotFoundError,
ContestNotFoundError, RecordNotFoundError, SolutionNotFoundError,
AlreadyVotedError
AlreadyVotedError, ContestNotAttendedError, ContestNotLiveError,
ContestScoreboardHiddenError
};
/*
@ -266,24 +284,6 @@ class ContestAlreadyAttendedError(ForbiddenError):
return "You've already attended this contest."
class ContestNotAttendedError(ForbiddenError):
@property
def message(self):
return "You haven't attended this contest yet."
class ContestScoreboardHiddenError(ForbiddenError):
@property
def message(self):
return 'Contest scoreboard is not visible.'
class ContestNotLiveError(ForbiddenError):
@property
def message(self):
return 'This contest is not live.'
class HomeworkScoreboardHiddenError(ForbiddenError):
@property
def message(self):

@ -1,6 +1,8 @@
const
{ ContestNotLiveError, ValidationError, ProblemNotFoundError } = require('../error'),
{ PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD, PERM_CREATE_CONTEST } = require('../permission'),
{ ContestNotLiveError, ValidationError, ProblemNotFoundError,
ContestNotAttendedError,ContestScoreboardHiddenError } = require('../error'),
{ PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD, PERM_CREATE_CONTEST,
PERM_EDIT_CONTEST } = require('../permission'),
{ constants } = require('../options'),
paginate = require('../lib/paginate'),
contest = require('../model/contest'),
@ -24,6 +26,16 @@ class ContestHandler extends Handler {
if (allowPermOverride && this.canViewHiddenScoreboard(tdoc)) return true;
return false;
}
async getScoreboard(tid, isExport = false) {
let [tdoc, tsdocs] = await contest.getAndListStatus(tid);
if (!this.canShowScoreboard(tdoc)) throw new ContestScoreboardHiddenError(tid);
let uids = [];
for (let tsdoc of tsdocs) uids.push(tsdoc.uid);
let [udict, pdict] = await Promise.all([user.getList(uids), problem.getList(tdoc['pids'])]);
let ranked_tsdocs = contest.RULES[tdoc.rule].rank(tsdocs);
let rows = contest.RULES[tdoc.rule].scoreboard(isExport, this.translate, tdoc, ranked_tsdocs, udict, pdict);
return tdoc, rows, udict;
}
async verifyProblems(pids) {
let pdocs = await problem.getMulti({ _id: { $in: pids } }).sort({ doc_id: 1 }).toArray();
if (pids.length != pdocs.length)
@ -74,19 +86,19 @@ class ContestDetailHandler extends ContestHandler {
if (tsdoc) {
attended = tsdoc.attend == 1;
for (let pdetail in tsdoc.detail || [])
psdict[pdetail['pid']] = pdetail;
psdict[pdetail.pid] = pdetail;
if (this.canShowRecord(this.tdoc)) {
let q = [];
for (let i in psdict) q.push(psdict[i].rid);
rdict = await record.getList(q, { getHidden: true });
rdict = await record.getList(q);
} else
for (let i in psdict)
rdict[psdict[i].rid] = { _id: psdict[i].rid };
} else attended = false;
let udict = await user.get_dict([this.tdoc.owner]);
let udict = await user.getList([this.tdoc.owner]);
let path = [
['contest_main', '/c'],
[this.tdoc['title'], null, true]
[this.tdoc.title, null, true]
];
this.response.body = {
path, tdoc: this.tdoc, tsdoc, attended, udict, pdict, psdict, rdict, page
@ -98,10 +110,116 @@ class ContestDetailHandler extends ContestHandler {
this.back();
}
}
class ContestScoreboardHandler extends ContestDetailHandler {
async get({ tid }) {
let [tdoc, rows, udict] = await this.getScoreboard(tid);
let path = [
['contest_main', '/c'],
[tdoc.title, `/c/${tid}`, true],
['contest_scoreboard', null]
];
this.response.template = 'contest_scoreboard.html';
this.response.body = { tdoc, rows, path, udict };
}
}
class ContestEditHandler extends ContestDetailHandler {
async prepare({ tid }) {
let tdoc = await contest.get(tid);
if (!tdoc.owner != this.user._id) this.checkPerm(PERM_EDIT_CONTEST);
}
async get() {
this.response.template = 'contest_edit.html';
let rules = {};
for (let i in contest.RULES)
rules[i] = contest.RULES[i].TEXT;
let duration = (this.tdoc.endAt.getTime() - this.tdoc.beginAt.getTime()) / 3600 / 1000;
let path = [
['contest_main', '/c'],
[this.tdoc.title, `/c/${this.tdoc._id}`, true],
['contest_edit', null]
];
let dt = this.tdoc.beginAt;
this.response.body = {
rules, tdoc: this.tdoc, duration, path, pids: this.tdoc.pids.join(','),
date_text: `${dt.getFullYear()}-${dt.getMonth()}-${dt.getDate()}`,
time_text: `${dt.getHours()}:${dt.getMinutes()}`,
page_name: 'contest_edit'
};
}
async post({ beginAtDate, beginAtTime, duration, title, content, rule, pids }) {
let beginAt, endAt;
try {
beginAt = Date.parse(`${beginAtDate}T${beginAtTime}+8:00`);
} catch (e) {
throw new ValidationError('beginAtDate', 'beginAtTime');
}
endAt = new Date(beginAt + duration * 3600 * 1000);
if (beginAt >= endAt) throw new ValidationError('duration');
await this.verifyProblems(pids);
await contest.edit(this.tdoc._id, title, content, rule, beginAt, endAt, pids);
if (this.tdoc.beginAt != beginAt || this.tdoc.endAt != endAt
|| Array.isDiff(this.tdoc.pids, pids) || this.tdoc.rule != rule)
await contest.recalcStatus(this.tdoc._id);
if (this.preferJson) this.response.body = { tid: this.tdoc._id };
else this.response.redirect = `/c/${this.tdoc._id}`;
}
}
class ContestProblemHandler extends ContestDetailHandler {
constructor(ctx) {
super(ctx);
this.response.template = '';
async prepare({ tid, pid }) {
[this.tdoc, this.pdoc] = await Promise.all([contest.get(tid), problem.get({ pid, uid: this.user._id })]);
[this.tsdoc, this.udoc] = await Promise.all([
contest.getStatus(this.tdoc._id, this.user._id),
user.getById(this.tdoc.owner)
]);
this.attended = this.tsdoc && this.tsdoc.attend == 1;
this.response.template = 'problem_detail.html';
if (contest.is_done(this.tdoc)) {
if (!this.attended) throw new ContestNotAttendedError(this.tdoc._id);
if (!contest.is_ongoing(this.tdoc)) throw new ContestNotLiveError(this.tdoc._id);
}
if (!this.tdoc.pids.includes(pid)) throw new ProblemNotFoundError(pid, this.tdoc._id);
}
async get({ tid }) {
let path = [
['contest_main', '/c'],
[this.tdoc.title, `/c/${tid}`, true],
[this.pdoc.title, null, true]
];
this.response.body = {
tdoc: this.tdoc, pdoc: this.pdoc, tsdoc: this.tsdoc,
udoc: this.udoc, attended: this.attended, path
};
}
}
class ContestProblemSubmitHandler extends ContestProblemHandler {
async get({ tid, pid }) {
let rdocs = [];
if (this.canShowRecord(this.tdoc))
rdocs = await record.getUserInProblemMulti(this.user._id, this.pdoc._id, true)
.sort({ _id: -1 }).limit(10).toArray();
this.response.template = 'problem_submit.html';
let path = [
['contest_main', '/c'],
[this.tdoc.title, `/c/${tid}`, true],
[this.pdoc.title, `/c/${tid}/p/${pid}`, true],
['contest_detail_problem_submit', null]
];
this.response.body = {
tdoc: this.tdoc, pdoc: this.pdoc, tsdoc: this.tsdoc,
udoc: this.udoc, attended: this.attend, path, rdocs
};
}
async post({ tid, lang, code }) {
await this.limitRate('add_record', 60, 100);
let rid = await record.add({ pid: this.pdoc._id, uid: this.user._id, lang, code, tid: this.tdoc._id, hidden: true });
await contest.updateStatus(this.tdoc._id, this.user._id, rid, this.pdoc._id, false, 0);
if (!this.canShowRecord(this.tdoc)) {
this.response.body = { tid };
this.response.redirect = `/c/${tid}`;
} else {
this.response.body = { rid };
this.response.redirect = `/r/${rid}`;
}
}
}
class ContestCreateHandler extends ContestHandler {
@ -141,6 +259,9 @@ class ContestCreateHandler extends ContestHandler {
}
Route('/c', ContestListHandler);
Route('/c/:cid', ContestDetailHandler);
Route('/c/:cid/p/:pid', ContestProblemHandler);
Route('/c/:tid', ContestDetailHandler);
Route('/c/:tid/edit', ContestEditHandler);
Route('/c/:tid/scoreboard', ContestScoreboardHandler);
Route('/c/:tid/p/:pid', ContestProblemHandler);
Route('/c/:tid/p/:pid/submit', ContestProblemSubmitHandler);
Route('/contest/create', ContestCreateHandler);

@ -61,6 +61,11 @@ async function reset(rid) {
async function count(query) {
return await coll.find(query).count();
}
async function getList(rids) {
let r = {};
for (let rid of rids) r[rid] = await get(rid);
return r;
}
module.exports = {
add,
@ -68,5 +73,6 @@ module.exports = {
getMany,
update,
count,
reset
reset,
getList
};

@ -14,3 +14,16 @@ String.random = function (digit) {
str += map[Math.floor(Math.random() * 62)];
return str;
};
/**
* @param {Array} a
* @param {Array} b
*/
Array.isDiff = function isDiff(a, b) {
if (a.length != b.length) return true;
a.sort();
b.sort();
for (let i in a)
if (a[i] != b[i]) return true;
return false;
};

Loading…
Cancel
Save