From b6a44296579d0c9910450110f5554d2e2290da2e Mon Sep 17 00:00:00 2001 From: Menci Date: Sat, 18 Feb 2017 22:29:03 +0800 Subject: [PATCH] Add rejudge to submission page --- models/judge_state.js | 34 ++++++++++++++++++++++++++++++++++ modules/submission.js | 32 ++++++++++++++++++++++++++++++++ views/submission_content.ejs | 15 +++++++++++++-- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/models/judge_state.js b/models/judge_state.js index 60fdc29..ef1fd74 100644 --- a/models/judge_state.js +++ b/models/judge_state.js @@ -171,6 +171,40 @@ class JudgeState extends Model { } } + async rejudge() { + await this.loadRelationships(); + + let oldStatus = this.status; + + this.status = 'Waiting'; + this.score = 0; + this.total_time = 0; + this.max_memory = 0; + this.pending = true; + this.result = { status: "Waiting", total_time: 0, max_memory: 0, score: 0, case_num: 0, compiler_output: "", pending: true }; + await this.save(); + + let WaitingJudge = syzoj.model('waiting_judge'); + let waiting_judge = await WaitingJudge.create({ + judge_id: this.id + }); + + await waiting_judge.save(); + + if (this.type === 0) { + if (oldStatus === 'Accepted') { + this.problem.ac_num--; + await this.user.refreshSubmitInfo(); + await this.user.save(); + } + + await this.problem.save(); + } else if (this.type === 1) { + let contest = await Contest.fromID(this.type_info); + await contest.newSubmission(this); + } + } + getModel() { return model; } } diff --git a/modules/submission.js b/modules/submission.js index e64db90..841352f 100644 --- a/modules/submission.js +++ b/modules/submission.js @@ -32,6 +32,7 @@ app.get('/submissions', async (req, res) => { let paginate = syzoj.utils.paginate(await JudgeState.count(where), req.query.page, syzoj.config.page.judge_state); let judge_state = await JudgeState.query(paginate, where, [['submit_time', 'desc']]); + await judge_state.forEachAsync(async obj => obj.loadRelationships()); await judge_state.forEachAsync(async obj => obj.hidden = !(await obj.isAllowedSeeResultBy(res.locals.user))); await judge_state.forEachAsync(async obj => obj.allowedSeeCode = await obj.isAllowedSeeCodeBy(res.locals.user)); @@ -56,6 +57,8 @@ app.get('/submissions/:id/ajax', async (req, res) => { let judge_state = await JudgeState.fromID(req.params.id); if (!judge_state) throw 'No such judge state'; + await judge_state.loadRelationships(); + judge_state.hidden = !(await judge_state.isAllowedSeeResultBy(res.locals.user)); judge_state.allowedSeeCode = await judge_state.isAllowedSeeCodeBy(res.locals.user); @@ -75,11 +78,14 @@ app.get('/submission/:id', async (req, res) => { let id = parseInt(req.params.id); let judge = await JudgeState.fromID(id); + await judge.loadRelationships(); + judge.codeLength = judge.code.length; judge.code = await syzoj.utils.highlight(judge.code, syzoj.config.languages[judge.language].highlight); if (judge.result.compiler_output) judge.result.compiler_output = syzoj.utils.ansiToHTML(judge.result.compiler_output); judge.allowedSeeResult = await judge.isAllowedSeeResultBy(res.locals.user); judge.allowedSeeCode = await judge.isAllowedSeeCodeBy(res.locals.user); + judge.allowedRejudge = await judge.problem.isAllowedEditBy(res.locals.user); res.render('submission', { judge: judge @@ -97,11 +103,14 @@ app.get('/submission/:id/ajax', async (req, res) => { let id = parseInt(req.params.id); let judge = await JudgeState.fromID(id); + await judge.loadRelationships(); + judge.codeLength = judge.code.length; judge.code = await syzoj.utils.highlight(judge.code, syzoj.config.languages[judge.language].highlight); if (judge.result.compiler_output) judge.result.compiler_output = syzoj.utils.ansiToHTML(judge.result.compiler_output); judge.allowedSeeResult = await judge.isAllowedSeeResultBy(res.locals.user); judge.allowedSeeCode = await judge.isAllowedSeeCodeBy(res.locals.user); + judge.allowedRejudge = await judge.problem.isAllowedEditBy(res.locals.user); res.render('submission_content', { judge: judge @@ -113,3 +122,26 @@ app.get('/submission/:id/ajax', async (req, res) => { }); } }); + +app.get('/submission/:id/rejudge', async (req, res) => { + try { + let id = parseInt(req.params.id); + let judge = await JudgeState.fromID(id); + + if (judge.pending) throw 'Can\'t rejudge a pending submission'; + + await judge.loadRelationships(); + + let allowedRejudge = await judge.problem.isAllowedEditBy(res.locals.user); + if (!allowedRejudge) throw 'Permission denied'; + + await judge.rejudge(); + + res.redirect(syzoj.utils.makeUrl(['submission', id])); + } catch (e) { + syzoj.log(e); + res.render('error', { + err: e + }); + } +}); diff --git a/views/submission_content.ejs b/views/submission_content.ejs index 32ad795..0bdec16 100644 --- a/views/submission_content.ejs +++ b/views/submission_content.ejs @@ -63,7 +63,12 @@ for (let s of judge.result.subtasks) { <% if (judge.allowedSeeCode) { %> -
<%- judge.code %>
+
+ <% if (judge.allowedRejudge) { %> + 重新评测 + <% } %> +
<%- judge.code %>
+
-<% if (!isPending(judge.status)) { %>
<% } %> +<% if (!isPending(judge.status)) { %>
<% } %>