mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
主页
This commit is contained in:
1 parent
ce3d11b999
commit
b1153b641a
13 files changed
+163
-42
No files matched your search
+52
-1
@@ -1,8 +1,59 @@
|
||||
const { Route, Handler } = require('../service/server');
|
||||
const contest = require('../model/contest');
|
||||
const user = require('../model/user');
|
||||
const training = require('../model/training');
|
||||
const { PERM_VIEW_TRAINING, PERM_VIEW_CONTEST, PERM_VIEW_DISCUSSION } = require('../permission');
|
||||
const { CONTESTS_ON_MAIN, TRAININGS_ON_MAIN, DISCUSSIONS_ON_MAIN } = require('../options').constants;
|
||||
|
||||
class HomeHandler extends Handler {
|
||||
async contest() {
|
||||
if (this.user.hasPerm(PERM_VIEW_CONTEST)) {
|
||||
const tdocs = await contest.getMulti()
|
||||
.limit(CONTESTS_ON_MAIN)
|
||||
.toArray();
|
||||
const tsdict = await contest.getListStatus(
|
||||
this.user._id, tdocs.map((tdoc) => tdoc._id),
|
||||
);
|
||||
return [tdocs, tsdict];
|
||||
}
|
||||
return [[], {}];
|
||||
}
|
||||
|
||||
async training() {
|
||||
if (this.user.hasPerm(PERM_VIEW_TRAINING)) {
|
||||
const tdocs = await training.getMulti()
|
||||
.sort('_id', 1)
|
||||
.limit(TRAININGS_ON_MAIN)
|
||||
.toArray();
|
||||
const tsdict = await training.getListStatus(
|
||||
this.user._id, tdocs.map((tdoc) => tdoc._id),
|
||||
);
|
||||
return [tdocs, tsdict];
|
||||
}
|
||||
return [[], {}];
|
||||
}
|
||||
|
||||
async discussion() {
|
||||
// TODO(masnn)
|
||||
// if (this.user.hasPerm(PERM_VIEW_DISCUSSION)) {
|
||||
// const ddocs = await discussion.getMulti()
|
||||
// .limit(DISCUSSIONS_ON_MAIN)
|
||||
// .toArray();
|
||||
// const vndict = await discussion.getListVnodes(map(discussion.node_id, ddocs));
|
||||
// return [ddocs, vndict];
|
||||
// }
|
||||
return [[], {}];
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.body = {};
|
||||
const [[tdocs, tsdict], [trdocs, trsdict], [ddocs, vndict]] = await Promise.all([
|
||||
this.contest(), this.training(), this.discussion(),
|
||||
]);
|
||||
const udict = await user.getList(ddocs.map((ddoc) => ddoc.owner));
|
||||
this.response.template = 'main.html';
|
||||
this.response.body = {
|
||||
tdocs, tsdict, trdocs, trsdict, ddocs, vndict, udict,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+38
-9
@@ -1,6 +1,9 @@
|
||||
const { PERM_JUDGE } = require('../permission');
|
||||
const record = require('../model/record');
|
||||
const problem = require('../model/problem');
|
||||
const builtin = require('../model/builtin');
|
||||
const contest = require('../model/contest');
|
||||
const user = require('../model/user');
|
||||
const bus = require('../service/bus');
|
||||
const queue = require('../service/queue');
|
||||
const {
|
||||
@@ -9,42 +12,66 @@ const {
|
||||
|
||||
queue.assert('judge');
|
||||
|
||||
async function _postJudge(rdoc) {
|
||||
const accept = rdoc.status === builtin.STATUS_ACCEPTED;
|
||||
bus.publish('record_change', rdoc);
|
||||
const tasks = [];
|
||||
if (rdoc.tid) {
|
||||
tasks.push(
|
||||
contest.updateStatus(rdoc.tid, rdoc.uid, rdoc._id, rdoc.pid, accept, rdoc.score),
|
||||
);
|
||||
}
|
||||
if (!rdoc.rejudged) {
|
||||
if (await problem.updateStatus(rdoc.pid, rdoc.uid, rdoc._id, rdoc.status)) {
|
||||
if (accept) {
|
||||
tasks.push(
|
||||
problem.inc(rdoc.pid, 'nAccept', 1),
|
||||
user.inc(rdoc.uid, 'nAccept', 1),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
}
|
||||
|
||||
async function next(body) {
|
||||
let rdoc = await record.get(body.rid);
|
||||
const $set = {};
|
||||
const $push = {};
|
||||
if (body.case) {
|
||||
rdoc.testCases.push(body.case);
|
||||
$set.testCases = rdoc.testCases;
|
||||
$push.testCases = body.case;
|
||||
}
|
||||
if (body.judge_text) {
|
||||
rdoc.judgeTexts.push(body.judge_text);
|
||||
$set.judgeTexts = rdoc.judgeTexts;
|
||||
$push.judgeTexts = body.judge_text;
|
||||
}
|
||||
if (body.compiler_text) {
|
||||
rdoc.compilerTexts.push(body.compiler_text);
|
||||
$set.compilerTexts = rdoc.compilerTexts;
|
||||
$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;
|
||||
if (body.memory_kb) $set.memory = body.memory_kb;
|
||||
rdoc = await record.update(body.rid, $set);
|
||||
rdoc = await record.update(body.rid, $set, $push);
|
||||
bus.publish('record_change', rdoc);
|
||||
}
|
||||
async function end(body) {
|
||||
let rdoc = await record.get(body.rid);
|
||||
const $set = {};
|
||||
const $push = {};
|
||||
if (body.case) {
|
||||
rdoc.testCases.push(body.case);
|
||||
$set.testCases = rdoc.testCases;
|
||||
$push.testCases = body.case;
|
||||
}
|
||||
if (body.judge_text) {
|
||||
rdoc.judgeTexts.push(body.judge_text);
|
||||
$set.judgeTexts = rdoc.judgeTexts;
|
||||
$push.judgeTexts = body.judge_text;
|
||||
}
|
||||
if (body.compiler_text) {
|
||||
rdoc.compilerTexts.push(body.compiler_text);
|
||||
$set.compilerTexts = rdoc.compilerTexts;
|
||||
$push.compilerTexts = body.compiler_text;
|
||||
}
|
||||
if (body.status) $set.status = body.status;
|
||||
if (body.score) $set.score = body.score;
|
||||
@@ -52,8 +79,9 @@ async function end(body) {
|
||||
if (body.memory_kb) $set.memory = body.memory_kb;
|
||||
$set.judgeAt = new Date();
|
||||
$set.judger = body.judger;
|
||||
rdoc = await record.update(body.rid, $set);
|
||||
bus.publish('record_change', rdoc);
|
||||
rdoc = await record.update(body.rid, $set, $push);
|
||||
await _postJudge(rdoc);
|
||||
rdoc = await record.update(body.rid, $set, $push);
|
||||
}
|
||||
|
||||
class JudgeHandler extends Handler {
|
||||
@@ -87,6 +115,7 @@ class JudgeHandler extends Handler {
|
||||
}
|
||||
|
||||
async postEnd() {
|
||||
this.request.body.judger = this.user._id;
|
||||
await end(this.request.body);
|
||||
this.response.body = {};
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ const {
|
||||
PERM_CREATE_PROBLEM, PERM_READ_PROBLEM_DATA, PERM_EDIT_PROBLEM,
|
||||
PERM_JUDGE, PERM_VIEW_PROBLEM_SOLUTION, PERM_CREATE_PROBLEM_SOLUTION,
|
||||
PERM_EDIT_PROBLEM_SOLUTION, PERM_DELETE_PROBLEM_SOLUTION, PERM_EDIT_PROBLEM_SOLUTION_REPLY,
|
||||
PERM_REPLY_PROBLEM_SOLUTION,
|
||||
PERM_REPLY_PROBLEM_SOLUTION, PERM_LOGGEDIN,
|
||||
} = require('../permission');
|
||||
|
||||
queue.assert('judge');
|
||||
@@ -32,9 +32,13 @@ class ProblemHandler extends Handler {
|
||||
async get({ page = 1, category = null }) {
|
||||
this.response.template = 'problem_main.html';
|
||||
const q = {};
|
||||
let psdict = {};
|
||||
if (category) q.category = category;
|
||||
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
|
||||
const pdocs = await problem.getMany(q, { pid: 1 }, page, constants.PROBLEM_PER_PAGE);
|
||||
if (this.user.hasPerm(PERM_LOGGEDIN)) {
|
||||
psdict = await problem.getListStatus(this.user._id, pdocs.map((pdoc) => pdoc._id));
|
||||
}
|
||||
this.response.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
@@ -42,6 +46,7 @@ class ProblemHandler extends Handler {
|
||||
],
|
||||
page,
|
||||
pdocs,
|
||||
psdict,
|
||||
category: '',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -54,12 +54,14 @@ class RecordDetailHandler extends Handler {
|
||||
if (rdoc.uid !== this.user.uid && !this.user.hasPerm(PERM_READ_RECORD_CODE)) {
|
||||
rdoc.code = null;
|
||||
}
|
||||
const pdoc = await problem.getById(rdoc.pid);
|
||||
this.response.body = {
|
||||
path: [
|
||||
['Hydro', '/'],
|
||||
['record_detail', null],
|
||||
],
|
||||
rdoc,
|
||||
pdoc,
|
||||
show_status: true,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ class TrainingDetailHandler extends TrainingHandler {
|
||||
for (const pid in psdict) {
|
||||
const psdoc = psdict[pid];
|
||||
if (psdoc.status) {
|
||||
if (psdoc.status === builtin.status.STATUS_ACCEPTED) donePids.add(pid);
|
||||
if (psdoc.status === builtin.STATUS_ACCEPTED) donePids.add(pid);
|
||||
else progPids.add(pid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +112,12 @@ async function edit(_id, $set) {
|
||||
if (!pdoc) throw new ProblemNotFoundError(_id);
|
||||
return pdoc;
|
||||
}
|
||||
async function inc(_id, field, n) {
|
||||
await coll.findOneAndUpdate({ _id }, { $inc: { [field]: n } });
|
||||
const pdoc = await getById(_id);
|
||||
if (!pdoc) throw new ProblemNotFoundError(_id);
|
||||
return pdoc;
|
||||
}
|
||||
function count(query) {
|
||||
return coll.find(query).count();
|
||||
}
|
||||
@@ -134,6 +140,14 @@ async function getListStatus(uid, pids) {
|
||||
for (const psdoc of psdocs) r[psdoc.pid] = psdoc;
|
||||
return r;
|
||||
}
|
||||
async function updateStatus(pid, uid, rid, status) {
|
||||
const res = await collStatus.findOneAndUpdate(
|
||||
{ pid, uid },
|
||||
{ $set: { rid, status } },
|
||||
{ upsert: true },
|
||||
);
|
||||
return res.value;
|
||||
}
|
||||
async function setTestdata(_id, readStream) {
|
||||
const pdoc = await getById(_id);
|
||||
const f = gridfs.openUploadStream('data.zip');
|
||||
@@ -148,6 +162,7 @@ async function setTestdata(_id, readStream) {
|
||||
|
||||
module.exports = {
|
||||
add,
|
||||
inc,
|
||||
get,
|
||||
getMany,
|
||||
edit,
|
||||
@@ -159,4 +174,5 @@ module.exports = {
|
||||
getListStatus,
|
||||
getMultiStatus,
|
||||
setTestdata,
|
||||
updateStatus,
|
||||
};
|
||||
@@ -39,9 +39,12 @@ function getMany(query, sort, page, limit) {
|
||||
return coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit)
|
||||
.toArray();
|
||||
}
|
||||
async function update(rid, $set) {
|
||||
async function update(rid, $set, $push) {
|
||||
const _id = new ObjectID(rid);
|
||||
await coll.findOneAndUpdate({ _id }, { $set });
|
||||
const upd = {};
|
||||
if ($set && Object.keys($set).length) upd.$set = $set;
|
||||
if ($push && Object.keys($push).length) upd.$push = $push;
|
||||
await coll.findOneAndUpdate({ _id }, upd);
|
||||
const rdoc = await coll.findOne({ _id });
|
||||
if (!rdoc) throw new RecordNotFoundError(rid);
|
||||
return rdoc;
|
||||
|
||||
+15
-2
@@ -6,6 +6,18 @@ const db = require('../service/db.js');
|
||||
const coll = db.collection('traning');
|
||||
const collStatus = db.collection('training.status');
|
||||
|
||||
function getStatus(tid, uid) {
|
||||
return collStatus.findOne({ tid, uid });
|
||||
}
|
||||
function getMultiStatus(query) {
|
||||
return collStatus.find(query);
|
||||
}
|
||||
async function getListStatus(uid, tids) {
|
||||
const psdocs = await getMultiStatus({ uid, pid: { $in: Array.from(new Set(tids)) } }).toArray();
|
||||
const r = {};
|
||||
for (const psdoc of psdocs) r[psdoc.pid] = psdoc;
|
||||
return r;
|
||||
}
|
||||
async function enroll(tid, uid) {
|
||||
try {
|
||||
await collStatus.insertOne({ tid, uid, enroll: 1 });
|
||||
@@ -93,8 +105,9 @@ module.exports = {
|
||||
return tdoc;
|
||||
},
|
||||
getMulti: (query) => coll.find(query),
|
||||
getMultiStatus: (query) => collStatus.find(query),
|
||||
getStatus: (tid, uid) => collStatus.findOne({ tid, uid }),
|
||||
getMultiStatus,
|
||||
getStatus,
|
||||
enroll,
|
||||
setStatus,
|
||||
getListStatus,
|
||||
};
|
||||
@@ -89,6 +89,11 @@ async function changePassword(uid, currentPassword, newPassword) {
|
||||
$set: { salt, hash: pwhash.hash(newPassword, salt) },
|
||||
});
|
||||
}
|
||||
async function inc(_id, field, n) {
|
||||
await coll.findOneAndUpdate({ _id }, { $inc: { [field]: n } });
|
||||
const udoc = await getById(_id);
|
||||
return udoc;
|
||||
}
|
||||
async function create({
|
||||
uid, mail, uname, password, regip = '127.0.0.1', role = 'default',
|
||||
}) {
|
||||
@@ -129,6 +134,7 @@ module.exports = {
|
||||
getById,
|
||||
getByUname,
|
||||
getMany,
|
||||
inc,
|
||||
setById,
|
||||
setEmail,
|
||||
setPassword,
|
||||
|
||||
@@ -43,6 +43,9 @@ let options = {
|
||||
SOLUTION_PER_PAGE: 20,
|
||||
CONTEST_PER_PAGE: 20,
|
||||
TRAINING_PER_PAGE: 10,
|
||||
CONTESTS_ON_MAIN: 5,
|
||||
TRAININGS_ON_MAIN: 5,
|
||||
DISCUSSIONS_ON_MAIN: 20,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
{% macro render_problem_title(pdoc, tdoc=none, show_tags=true, show_invisible_flag=true, rp=none, invalid=false) %}
|
||||
{%- if not invalid %}
|
||||
<a
|
||||
{%- if tdoc is none %}
|
||||
href="/p/{{ pdoc.pid }}"
|
||||
{%- else %}
|
||||
{%- if tdoc %}
|
||||
href="/c/{{ tdoc._id }}/p/{{ pdoc._id }}"
|
||||
{%- else %}
|
||||
href="/p/{{ pdoc.pid }}"
|
||||
{%- endif %}
|
||||
>
|
||||
{%- endif %}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<h1 class="section__title">{{ _('Discussion Nodes') }}</h1>
|
||||
</div>
|
||||
<div class="section__body"><ul class="group-list">
|
||||
{% for category, nodes in discussion_nodes.items() %}
|
||||
{% for category, nodes in discussionNodes.items() %}
|
||||
<li class="group-list__item">
|
||||
<h2 class="section__title">{{ category }}</h2>
|
||||
<ol class="chip-list">
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
{% set no_path_section = true %}
|
||||
{% set page_name = "domain_main" %}
|
||||
{% extends "layout/basic.html" %}
|
||||
{% import "components/contest.html" as contest with context %}
|
||||
{% block content %}
|
||||
{% if handler.domain['bulletin'] %}
|
||||
{% if system['bulletin'] %}
|
||||
<div class="row">
|
||||
<div class="large-12 columns">
|
||||
<div class="section">
|
||||
<div class="section__body typo">
|
||||
{{ handler.domain['bulletin']|markdown }}
|
||||
{{ system['bulletin']|markdown }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -15,9 +16,7 @@
|
||||
{% endif %}
|
||||
<div class="row">
|
||||
<div class="large-9 columns">
|
||||
{% if not tdocs %}
|
||||
<!-- no contest -->
|
||||
{% else %}
|
||||
{% if tdocs.length %}
|
||||
<div class="section page--contest_main">
|
||||
<div class="section__header">
|
||||
<h1 class="section__title">{{ _('Contest') }}</h1>
|
||||
@@ -28,15 +27,15 @@
|
||||
<div class="media">
|
||||
<div class="media__left medium">
|
||||
<div class="contest__date numbox">
|
||||
<div class="numbox__num large">{{ datetime_span(tdoc['begin_at'], false, '%d') }}</div>
|
||||
<div class="numbox__text">{{ datetime_span(tdoc['begin_at'], false, '%Y-%m') }}</div>
|
||||
<div class="numbox__num large">{{ datetime_span(tdoc['beginAt'], false, '%d')|safe }}</div>
|
||||
<div class="numbox__text">{{ datetime_span(tdoc['beginAt'], false, '%Y-%m')|safe }}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="media__body medium">
|
||||
<h1 class="contest__title"><a href="/c/{{ tdoc._id }}" data-emoji-enabled>{{ tdoc.title }}</a></h1>
|
||||
<ul class="supplementary list">
|
||||
<li>
|
||||
<a href="{{ reverse_url('contest_main') }}?rule={{ tdoc['rule'] }}" class="contest-type-tag"><span class="icon icon-award"></span>{{ vj4.constant.contest.RULE_TEXTS[tdoc['rule']] }}</a>
|
||||
<a href="/c?rule={{ tdoc['rule'] }}" class="contest-type-tag"><span class="icon icon-award"></span>{{ model.contest.RULES[tdoc.rule].TEXT }}</a>
|
||||
</li>
|
||||
<li>
|
||||
<span class="icon icon-schedule--fill text-blue"></span> {{ contest.render_duration(tdoc) }} {{ _('hour(s)') }}
|
||||
@@ -57,14 +56,13 @@
|
||||
</ol>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if not trdocs %}
|
||||
<!-- no training -->
|
||||
{% else %}
|
||||
{% if trdocs.length %}
|
||||
<div class="section page--training_main">
|
||||
<div class="section__header">
|
||||
<h1 class="section__title">{{ _('Training') }}</h1>
|
||||
</div>
|
||||
{% with tdocs=trdocs, tsdict=trsdict %}
|
||||
{% set tdocs = trdocs %}
|
||||
{% set tsdict = trsdict %}
|
||||
<ol class="section__list all primary training__list">
|
||||
{% for tdoc in tdocs %}
|
||||
<li class="section__list__item training__item">
|
||||
@@ -76,19 +74,19 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="media__body middle">
|
||||
<h1 class="training__title"><a href="{{ reverse_url('training_detail', tid=tdoc._id) }}" data-emoji-enabled>{{ tdoc.title }}</a></h1>
|
||||
<h1 class="training__title"><a href="/t/{{ tdoc._id }}" data-emoji-enabled>{{ tdoc.title }}</a></h1>
|
||||
<div class="training__intro typo">
|
||||
<p>{{ tdoc['content'] }}</p>
|
||||
</div>
|
||||
<ul class="supplementary list training__progress">
|
||||
<li>
|
||||
<span class="icon icon-flag text-blue"></span> {{ _('{0} sections').format(tdoc['dag']|length) }}, {{ _('{0} problems').format(training.get_pids(tdoc)|length) }}
|
||||
<span class="icon icon-flag text-blue"></span> {{ _('{0} sections').format(tdoc['dag']|length) }}, {{ _('{0} problems').format(model.training.getPids(tdoc)|length) }}
|
||||
</li>
|
||||
<li>
|
||||
{% if tsdict[tdoc._id]['enroll'] %}
|
||||
{% if not tsdict[tdoc._id]['done'] %}
|
||||
<span class="icon training-status--icon progress"></span>
|
||||
<span class="training-status--text progress">{{ _('Completed') }} {{ (100 * tsdict[tdoc._id]['done_pids']|length / training.get_pids(tdoc)|length)|round|int }}%</span>
|
||||
<span class="training-status--text progress">{{ _('Completed') }} {{ (100 * tsdict[tdoc._id]['donePids']|length / model.training.getPids(tdoc)|length)|round|int }}%</span>
|
||||
{% else %}
|
||||
<span class="icon training-status--icon done"></span>
|
||||
<span class="training-status--text done">{{ _('Completed') }} 100%</span>
|
||||
@@ -103,12 +101,9 @@
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ol>
|
||||
{% endwith %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if not ddocs %}
|
||||
<!-- no discussion -->
|
||||
{% else %}
|
||||
{% if ddocs.length %}
|
||||
<div class="section page--discussion_main">
|
||||
<div class="section__header">
|
||||
<h1 class="section__title">{{ _('Discussion') }}</h1>
|
||||
@@ -119,9 +114,8 @@
|
||||
</div>
|
||||
<div class="large-3 columns">
|
||||
{% if handler.hasPerm(perm.PERM_VIEW_DISCUSSION) %}
|
||||
{% include 'discussion_nodes_widget.html' %}
|
||||
{#{% include 'discussion_nodes_widget.html' %} // TODO(masnn) #}
|
||||
{% endif %}
|
||||
{% if domain_id == vj4.model.builtin.DOMAIN_ID_SYSTEM %}
|
||||
<div class="section side">
|
||||
<div class="section__header">
|
||||
<h1 class="section__title">{{ _('Recommended') }}</h1>
|
||||
@@ -153,7 +147,6 @@
|
||||
</li>
|
||||
</ul></div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in new issue
Block a user