problem pretest

This commit is contained in:
undefined committed 2020-06-28 13:05:40 +08:00
1 parent dd5a212422
commit 554698189c
17 files changed
+127 -57

No files matched your search

+35 -1
View File
@@ -1,4 +1,6 @@
const yaml = require('js-yaml');
const { PERM_JUDGE } = require('../permission');
const { parseTimeMS, parseMemoryMB } = require('../utils');
const record = require('../model/record');
const problem = require('../model/problem');
const builtin = require('../model/builtin');
@@ -13,6 +15,7 @@ const {
async function _postJudge(rdoc) {
const accept = rdoc.status === builtin.STATUS_ACCEPTED;
bus.publish('record_change', rdoc);
if (rdoc.type === 'run') return;
const tasks = [];
if (rdoc.tid) {
tasks.push(
@@ -26,7 +29,7 @@ async function _postJudge(rdoc) {
if (accept && !rdoc.rejudged) {
tasks.push(
problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1),
user.inc(rdoc.domainId, rdoc.uid, 'nAccept', 1),
user.incDomain(rdoc.domainId, rdoc.uid, 'nAccept', 1),
);
}
}
@@ -78,6 +81,8 @@ async function end(body) {
}
if (body.status) $set.status = body.status;
if (body.score) $set.score = body.score;
if (body.stdout) $set.stdout = body.stdout;
if (body.stderr) $set.stderr = body.stderr;
if (body.time_ms) $set.time = body.time_ms;
if (body.memory_kb) $set.memory = body.memory_kb;
$set.judgeAt = new Date();
@@ -87,6 +92,34 @@ async function end(body) {
rdoc = await record.update(body.domainId, body.rid, $set, $push);
}
class PretestHandler extends Handler {
async post({
domainId, pid, code, lang, time = '1s', memory = '256m', input = '',
}) {
if (pid) {
const pdoc = await problem.get(domainId, pid);
if (pdoc.config) {
const config = yaml.safeLoad(pdoc.config);
if (config.time) time = config.time;
if (config.memory) memory = config.memory;
}
}
const rid = await record.add(domainId, {
pid: pid || String.random(16),
uid: this.user._id,
type: 'run',
time: parseTimeMS(time),
memory: parseMemoryMB(memory),
input,
lang,
code,
hidden: true,
});
this.response.body = { rid };
this.response.redirect = this.url('record_detail', { rid });
}
}
class JudgeHandler extends Handler {
async get({ check = false }) {
if (check) return;
@@ -130,6 +163,7 @@ class JudgeConnectionHandler extends ConnectionHandler {
}
async function apply() {
Route('pretest', '/pretest', PretestHandler);
Route('judge', '/judge', JudgeHandler, PERM_JUDGE);
Connection('judge_conn', '/judge/conn', JudgeConnectionHandler, PERM_JUDGE);
}
+1 -1
View File
@@ -66,13 +66,13 @@ class SystemScriptHandler extends SystemHandler {
async post({ domainId, id, args = '{}' }) {
args = JSON.parse(args);
// TODO Do not use console.log
const rid = await record.add(domainId, {
pid: id,
uid: this.user._id,
lang: null,
code: null,
status: STATUS.STATUS_JUDGING,
hidden: true,
}, false);
async function report(data) {
judge.next({ domainId, rid, ...data });
+2 -2
View File
@@ -224,7 +224,7 @@ class ProblemPretestConnectionHandler extends ConnectionHandler {
async prepare({ domainId, pid }) {
this.pid = pid.toString();
this.domainId = domainId;
bus.subscribe(['record_change'], this.onRecordChange);
bus.subscribe(['record_change'], this, 'onRecordChange');
}
async onRecordChange(data) {
@@ -239,7 +239,7 @@ class ProblemPretestConnectionHandler extends ConnectionHandler {
}
async cleanup() {
bus.unsubscribe(['record_change'], this.onRecordChange);
bus.unsubscribe(['record_change'], this, 'onRecordChange');
}
}
+6 -4
View File
@@ -19,7 +19,7 @@ class RecordListHandler extends RecordHandler {
domainId, page = 1, pid, tid, uidOrName,
}) {
this.response.template = 'record_main.html';
const q = { tid };
const q = { tid, hidden: false };
if (uidOrName) {
q.$or = [
{ unameLower: uidOrName.toLowerCase() },
@@ -64,6 +64,7 @@ class RecordDetailHandler extends RecordHandler {
if (rdoc.uid !== this.user.uid && !this.user.hasPerm(PERM_READ_RECORD_CODE)) {
rdoc.code = null;
}
if (rdoc.type !== 'run') rdoc.stdout = rdoc.stderr = '';
const [pdoc, udoc] = await Promise.all([
problem.get(domainId, rdoc.pid, null, false),
user.getById(domainId, rdoc.uid),
@@ -76,7 +77,8 @@ class RecordDetailHandler extends RecordHandler {
udoc,
rdoc,
pdoc,
show_status: true,
// TODO protect
showStatus: true,
};
}
}
@@ -144,14 +146,14 @@ class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(Connecti
return;
}
}
this.rid = rid;
this.rid = rid.toString();
bus.subscribe(['record_change'], this, 'onRecordChange');
this.onRecordChange({ value: rdoc });
}
async onRecordChange(data) {
const rdoc = data.value;
if (!rdoc._id.equals(this.rid)) return;
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 }),