mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
Hello Typescript!
This commit is contained in:
1 parent
c9b6412527
commit
ba03d7a7bf
98 files changed
+3186
-2616
No files matched your search
@@ -0,0 +1,173 @@
|
||||
import yaml from 'js-yaml';
|
||||
import { parseTimeMS, parseMemoryMB } from '../utils';
|
||||
import * as record from '../model/record';
|
||||
import * as problem from '../model/problem';
|
||||
import * as builtin from '../model/builtin';
|
||||
import * as contest from '../model/contest';
|
||||
import * as user from '../model/user';
|
||||
import * as task from '../model/task';
|
||||
import * as bus from '../service/bus';
|
||||
import {
|
||||
Route, Handler, Connection, ConnectionHandler,
|
||||
} from '../service/server';
|
||||
|
||||
async function _postJudge(rdoc) {
|
||||
const accept = rdoc.status === builtin.STATUS.STATUS_ACCEPTED;
|
||||
bus.publish('record_change', rdoc);
|
||||
if (rdoc.type === 'run') return;
|
||||
const tasks = [];
|
||||
if (rdoc.tid) {
|
||||
tasks.push(
|
||||
contest.updateStatus(
|
||||
rdoc.domainId, rdoc.tid, rdoc.uid,
|
||||
rdoc._id, rdoc.pid, accept, rdoc.score, rdoc.ttype,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (await problem.updateStatus(rdoc.domainId, rdoc.pid, rdoc.uid, rdoc._id, rdoc.status)) {
|
||||
if (accept && !rdoc.rejudged) {
|
||||
tasks.push(
|
||||
problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1),
|
||||
user.incDomain(rdoc.domainId, rdoc.uid, 'nAccept', 1),
|
||||
);
|
||||
}
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
}
|
||||
|
||||
export async function next(body) {
|
||||
let rdoc = await record.get(body.domainId, body.rid);
|
||||
const $set: any = {};
|
||||
const $push: any = {};
|
||||
if (body.case) {
|
||||
const c: any = {};
|
||||
c.memory = body.case.memory_kb || body.case.memory;
|
||||
c.time = body.case.time_ms || body.case.time;
|
||||
c.judgeText = body.case.judge_text || body.case.judgeText || body.case.message;
|
||||
c.status = body.case.status;
|
||||
rdoc.testCases.push(c);
|
||||
$push.testCases = c;
|
||||
}
|
||||
if (body.judge_text || body.message) {
|
||||
rdoc.judgeTexts.push(body.judge_text || body.message);
|
||||
$push.judgeTexts = body.judge_text || body.message;
|
||||
}
|
||||
if (body.compiler_text) {
|
||||
rdoc.compilerTexts.push(body.compiler_text);
|
||||
$push.compilerTexts = body.compiler_text;
|
||||
}
|
||||
if (body.status) $set.status = body.status;
|
||||
if (body.score) $set.score = body.score;
|
||||
if (body.time_ms) $set.time = body.time_ms || body.time;
|
||||
if (body.memory_kb) $set.memory = body.memory_kb || body.memory;
|
||||
if (body.progress) $set.progress = body.progress;
|
||||
rdoc = await record.update(body.domainId, body.rid, $set, $push);
|
||||
bus.publish('record_change', rdoc);
|
||||
}
|
||||
|
||||
export async function end(body) {
|
||||
let rdoc = await record.get(body.domainId, body.rid);
|
||||
const $set: any = {};
|
||||
const $push: any = {};
|
||||
const $unset = { progress: '' };
|
||||
if (body.judge_text) {
|
||||
rdoc.judgeTexts.push(body.judge_text);
|
||||
$push.judgeTexts = body.judge_text;
|
||||
}
|
||||
if (body.compiler_text) {
|
||||
rdoc.compilerTexts.push(body.compiler_text);
|
||||
$push.compilerTexts = body.compiler_text;
|
||||
}
|
||||
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();
|
||||
$set.judger = body.judger;
|
||||
rdoc = await record.update(body.domainId, body.rid, $set, $push, $unset);
|
||||
await _postJudge(rdoc);
|
||||
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: any = 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;
|
||||
const tasks = [];
|
||||
let t = await task.getFirst({ type: 'judge' });
|
||||
while (t) {
|
||||
tasks.push(t);
|
||||
t = await task.getFirst({ type: 'judge' }); // eslint-disable-line no-await-in-loop
|
||||
}
|
||||
this.response.body = { tasks };
|
||||
}
|
||||
|
||||
async postNext() {
|
||||
await next(this.request.body);
|
||||
}
|
||||
|
||||
async postEnd() {
|
||||
this.request.body.judger = this.user._id;
|
||||
await end(this.request.body);
|
||||
}
|
||||
}
|
||||
|
||||
class JudgeConnectionHandler extends ConnectionHandler {
|
||||
async message(msg) {
|
||||
if (msg.key === 'next') await next(msg);
|
||||
else if (msg.key === 'end') {
|
||||
await end({ judger: this.user._id, ...msg });
|
||||
this.processing = null;
|
||||
const t = await task.getFirst({ type: 'judge' });
|
||||
this.send({ task: t });
|
||||
this.processing = t;
|
||||
}
|
||||
}
|
||||
|
||||
async cleanup() {
|
||||
if (this.processing) {
|
||||
await record.reset(this.processing.domainId, this.processing.rid);
|
||||
task.add(this.processing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function apply() {
|
||||
Route('pretest', '/pretest', PretestHandler);
|
||||
Route('judge', '/judge', JudgeHandler, builtin.PRIV.PRIV_JUDGE);
|
||||
Connection('judge_conn', '/judge/conn', JudgeConnectionHandler, builtin.PRIV.PRIV_JUDGE);
|
||||
}
|
||||
|
||||
apply.next = next;
|
||||
apply.end = end;
|
||||
|
||||
global.Hydro.handler.judge = apply;
|
||||
Reference in new issue
Block a user