You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/handler/record.js

147 lines
4.7 KiB
JavaScript

5 years ago
const {
PERM_READ_RECORD_CODE, PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD,
PERM_REJUDGE, PERM_VIEW_PROBLEM_HIDDEN,
} = require('../permission');
const problem = require('../model/problem');
const record = require('../model/record');
const contest = require('../model/contest');
5 years ago
const system = require('../model/system');
5 years ago
const user = require('../model/user');
const bus = require('../service/bus');
const {
Route, Handler, Connection, ConnectionHandler,
} = require('../service/server');
class RecordListHandler extends Handler {
async get({ page = 1 }) {
this.response.template = 'record_main.html';
5 years ago
const q = {};
5 years ago
const rdocs = await record.getMany(q, { _id: -1 }, page, await system.get('RECORD_PER_PAGE'));
5 years ago
const pdict = {};
const udict = {};
const ulist = [];
const plist = [];
for (const rdoc of rdocs) {
ulist[rdoc.uid] = user.getById(rdoc.uid);
plist[rdoc.pid] = problem.get(rdoc.pid, this.user._id);
}
await Promise.all(ulist);
await Promise.all(plist);
for (const udoc of ulist) {
udict[udoc._id] = udoc;
}
for (const pdoc of plist) {
pdict[pdoc._id] = pdoc;
}
5 years ago
const path = [
['Hydro', '/'],
['record_main', null],
];
this.response.body = {
5 years ago
path, page, rdocs, pdict, udict,
};
}
}
5 years ago
class RecordDetailHandler extends Handler {
async get({ rid }) {
this.response.template = 'record_detail.html';
5 years ago
const rdoc = await record.get(rid);
if (rdoc.hidden) this.checkPerm(PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
if (rdoc.uid !== this.user.uid && !this.user.hasPerm(PERM_READ_RECORD_CODE)) {
rdoc.code = null;
}
5 years ago
const pdoc = await problem.getById(rdoc.pid);
this.response.body = {
path: [
['Hydro', '/'],
5 years ago
['record_detail', null],
],
5 years ago
rdoc,
5 years ago
pdoc,
5 years ago
show_status: true,
};
}
}
5 years ago
class RecordRejudgeHandler extends Handler {
async post({ rid }) {
this.checkPerm(PERM_REJUDGE);
5 years ago
const rdoc = await record.get(rid);
if (rdoc) await record.rejudge(rid);
this.back();
}
}
5 years ago
class RecordConnectionHandler extends ConnectionHandler {
async prepare() {
bus.subscribe(['record_change'], this.onRecordChange);
}
5 years ago
async message(msg) {
5 years ago
for (const rid of msg.rids) {
const rdoc = await record.get(rid); // eslint-disable-line no-await-in-loop
await this.onRecordChange({ value: rdoc }); // eslint-disable-line no-await-in-loop
}
}
5 years ago
async cleanup() {
bus.unsubscribe(['record_change'], this.onRecordChange);
}
5 years ago
async onRecordChange(data) {
5 years ago
const rdoc = data.value;
if (rdoc.tid && rdoc.tid.toString() !== this.tid) return;
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([user.getById(rdoc.uid), problem.getById(rdoc.pid)]);
if (pdoc.hidden && !this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
}
}
5 years ago
5 years ago
class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(ConnectionHandler) {
async prepare({ rid }) {
5 years ago
const rdoc = await record.get(rid);
5 years ago
if (rdoc.tid) {
5 years ago
const tdoc = await contest.get(rdoc.tid);
5 years ago
if (!this.canShowRecord(tdoc)) {
this.close();
return;
}
5 years ago
}
this.rid = rid;
bus.subscribe(['record_change'], this.onRecordChange);
this.onRecordChange({ value: rdoc });
}
5 years ago
async onRecordChange(data) {
5 years ago
const rdoc = data.value;
if (rdoc._id.toString() !== this.rid) return;
this.send({
status_html: await this.renderHTML('record_detail_status.html', { rdoc }),
5 years ago
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc }),
});
}
5 years ago
async cleanup() {
bus.unsubscribe(['record_change'], this.onRecordChange);
}
}
5 years ago
async function apply() {
Route('/r', module.exports.RecordListHandler);
Route('/r/:rid', module.exports.RecordDetailHandler);
Route('/r/:rid/rejudge', module.exports.RecordRejudgeHandler);
Connection('/record-conn', module.exports.RecordConnectionHandler);
Connection('/record-detail-conn', module.exports.RecordDetailConnectionHandler);
}
global.Hydro.handler.record = module.exports = {
RecordListHandler,
RecordDetailHandler,
RecordRejudgeHandler,
RecordConnectionHandler,
RecordDetailConnectionHandler,
apply,
};