mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
重构框架,增加比赛功能
This commit is contained in:
1 parent
0e590ebe0d
commit
9bf3343a64
54 files changed
+1196
-868
No files matched your search
+89
-81
@@ -1,99 +1,107 @@
|
||||
const
|
||||
bson = require('bson'),
|
||||
{ constants } = require('../options'),
|
||||
{ PERM_READ_RECORD_CODE, PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD,
|
||||
PERM_REJUDGE, PERM_VIEW_PROBLEM_HIDDEN } = require('../permission'),
|
||||
{ requirePerm } = require('../handler/tools'),
|
||||
problem = require('../model/problem'),
|
||||
record = require('../model/record'),
|
||||
user = require('../model/user'),
|
||||
bus = require('../service/bus'),
|
||||
queue = require('../service/queue'),
|
||||
{ GET, POST, SOCKET } = require('../service/server');
|
||||
{ Route, Handler, Connection, ConnectionHandler } = require('../service/server');
|
||||
|
||||
GET('/r', async ctx => {
|
||||
ctx.templateName = 'record_main.html';
|
||||
let q = {},
|
||||
page = ctx.query.page || 1;
|
||||
let rdocs = await record.getMany(q, { _id: -1 }, page, constants.RECORD_PER_PAGE);
|
||||
let pdict = {}, udict = {};
|
||||
for (let rdoc of rdocs) {
|
||||
udict[rdoc.uid] = await user.getById(rdoc.uid);
|
||||
pdict[rdoc.pid] = await problem.get({ pid: rdoc.pid, uid: ctx.state.user._id });
|
||||
class RecordListHandler extends Handler {
|
||||
async get({ page = 1 }) {
|
||||
this.response.template = 'record_main.html';
|
||||
let q = {};
|
||||
let rdocs = await record.getMany(q, { _id: -1 }, page, constants.RECORD_PER_PAGE);
|
||||
let pdict = {}, udict = {};
|
||||
for (let rdoc of rdocs) {
|
||||
udict[rdoc.uid] = await user.getById(rdoc.uid);
|
||||
pdict[rdoc.pid] = await problem.get({ pid: rdoc.pid, uid: this.user._id });
|
||||
}
|
||||
this.response.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['record_main', null]
|
||||
],
|
||||
page, rdocs, pdict, udict
|
||||
};
|
||||
}
|
||||
ctx.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['record_main', null]
|
||||
],
|
||||
page, rdocs, pdict, udict
|
||||
};
|
||||
});
|
||||
SOCKET('/record-conn', [], conn => {
|
||||
let tid = conn.params.tid;
|
||||
async function onRecordChange(data) {
|
||||
let rdoc = data.value;
|
||||
if (rdoc.tid && rdoc.tid.toString() != tid) return;
|
||||
let [udoc, pdoc] = await Promise.all([user.getById(rdoc.uid), problem.get({ pid: rdoc.pid })]);
|
||||
if (pdoc.hidden && !conn.state.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
|
||||
conn.send({ html: await conn.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
|
||||
}
|
||||
class RecordDetailHandler extends Handler {
|
||||
async get({ rid }) {
|
||||
this.response.template = 'record_detail.html';
|
||||
let 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;
|
||||
this.response.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['record_detail', null]
|
||||
],
|
||||
rdoc, show_status: true
|
||||
};
|
||||
}
|
||||
bus.subscribe(['record_change'], onRecordChange);
|
||||
conn.on('data', async message => {
|
||||
console.log(message);
|
||||
let { rids } = JSON.parse(message);
|
||||
for (let rid of rids) {
|
||||
}
|
||||
class RecordRejudgeHandler extends Handler {
|
||||
async post({ rid }) {
|
||||
this.checkPerm(PERM_REJUDGE);
|
||||
let rdoc = await record.get(rid);
|
||||
if (rdoc) {
|
||||
await record.reset(rid);
|
||||
await queue.push('judge', rid);
|
||||
}
|
||||
this.response.back();
|
||||
}
|
||||
}
|
||||
class RecordConnectionHandler extends ConnectionHandler {
|
||||
async prepare() {
|
||||
bus.subscribe(['record_change'], this.onRecordChange);
|
||||
}
|
||||
async message(msg) {
|
||||
for (let rid of msg.rids) {
|
||||
let rdoc = await record.get(rid);
|
||||
await onRecordChange({ value: rdoc });
|
||||
await this.onRecordChange({ value: rdoc });
|
||||
}
|
||||
});
|
||||
conn.on('close', () => {
|
||||
bus.unsubscribe(['record_change'], onRecordChange);
|
||||
});
|
||||
});
|
||||
GET('/r/:rid', async ctx => {
|
||||
ctx.templateName = 'record_detail.html';
|
||||
let uid = ctx.state.user._id, rid = new bson.ObjectID(ctx.params.rid);
|
||||
let rdoc = await record.get(rid);
|
||||
if (rdoc.hidden) ctx.checkPerm(PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
|
||||
if (rdoc.uid != uid && !ctx.state.user.hasPerm(PERM_READ_RECORD_CODE)) rdoc.code = null;
|
||||
ctx.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['record_detail', null]
|
||||
],
|
||||
rdoc, show_status: true
|
||||
};
|
||||
});
|
||||
SOCKET('/record-detail-conn', [], async conn => {
|
||||
let rdoc = await record.get(conn.params.rid);
|
||||
if (rdoc.tid)
|
||||
if (!await conn.rdoc_contest_visible(rdoc)) {
|
||||
conn.close();
|
||||
return;
|
||||
}
|
||||
async function onRecordChange(data) {
|
||||
}
|
||||
async cleanup() {
|
||||
bus.unsubscribe(['record_change'], this.onRecordChange);
|
||||
}
|
||||
async onRecordChange(data) {
|
||||
let rdoc = data.value;
|
||||
if (rdoc._id.toString() != conn.params.rid) return;
|
||||
conn.send({
|
||||
status_html: await conn.renderHTML('record_detail_status.html', { rdoc }),
|
||||
summary_html: await conn.renderHTML('record_detail_summary.html', { rdoc })
|
||||
if (rdoc.tid && rdoc.tid.toString() != this.tid) return;
|
||||
let [udoc, pdoc] = await Promise.all([user.getById(rdoc.uid), problem.get({ pid: 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 }) });
|
||||
}
|
||||
}
|
||||
class RecordDetailConnectionHandler extends ConnectionHandler {
|
||||
async prepare({ rid }) {
|
||||
let rdoc = await record.get(rid);
|
||||
if (rdoc.tid)
|
||||
if (!await this.rdocContestVisible(rdoc)) {
|
||||
this.close();
|
||||
return;
|
||||
}
|
||||
this.rid = rid;
|
||||
bus.subscribe(['record_change'], this.onRecordChange);
|
||||
this.onRecordChange({ value: rdoc });
|
||||
}
|
||||
async onRecordChange(data) {
|
||||
let rdoc = data.value;
|
||||
if (rdoc._id.toString() != this.rid) return;
|
||||
this.send({
|
||||
status_html: await this.renderHTML('record_detail_status.html', { rdoc }),
|
||||
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc })
|
||||
});
|
||||
}
|
||||
bus.subscribe(['record_change'], onRecordChange);
|
||||
onRecordChange({ value: rdoc });
|
||||
conn.on('close', () => {
|
||||
bus.unsubscribe(['record_change'], onRecordChange);
|
||||
});
|
||||
});
|
||||
POST('/r/:rid/rejudge', requirePerm(PERM_REJUDGE), async ctx => {
|
||||
let uid = ctx.state.user._id, rid = new bson.ObjectID(ctx.params.rid);
|
||||
let rdoc = await record.get(rid);
|
||||
if (rdoc.hidden) ctx.checkPerm(PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
|
||||
if (rdoc.uid != uid && !ctx.state.user.hasPerm(PERM_READ_RECORD_CODE)) rdoc.code = null;
|
||||
if (rdoc) {
|
||||
await record.reset(rid);
|
||||
await queue.push('judge', rid);
|
||||
async cleanup() {
|
||||
bus.unsubscribe(['record_change'], this.onRecordChange);
|
||||
}
|
||||
ctx.back();
|
||||
});
|
||||
}
|
||||
|
||||
Route('/r', RecordListHandler);
|
||||
Route('/r/:rid', RecordDetailHandler);
|
||||
Route('/r/:rid/rejudge', RecordRejudgeHandler);
|
||||
Connection('/record-conn', RecordConnectionHandler);
|
||||
Connection('/record-detail-conn', RecordDetailConnectionHandler);
|
||||
Reference in new issue
Block a user