From 72747e50e1b64ded91507f5a60818e9a8835dcb9 Mon Sep 17 00:00:00 2001 From: t123yh Date: Thu, 24 Aug 2017 11:18:52 +0800 Subject: [PATCH] Finish. --- libs/judger.js | 2 +- libs/submissions_process.js | 2 +- models/judge_state.js | 18 ++++++++++++++---- modules/api_v2.js | 12 ++++++++++-- modules/contest.js | 4 ++-- modules/problem.js | 3 ++- modules/submission.js | 4 ++-- package-lock.json | 13 +++++++++++++ package.json | 1 + views/submission.ejs | 12 +++++++----- views/submissions.ejs | 2 +- views/submissions_item.ejs | 4 ++-- 12 files changed, 56 insertions(+), 21 deletions(-) diff --git a/libs/judger.js b/libs/judger.js index 640236d..b220ccc 100644 --- a/libs/judger.js +++ b/libs/judger.js @@ -31,7 +31,7 @@ module.exports.judge = async function (judge_state, problem, priority) { const req = { content: { - taskId: judge_state.id, + taskId: judge_state.task_id, testData: problem.id.toString(), type: type, priority: priority, diff --git a/libs/submissions_process.js b/libs/submissions_process.js index 30677cd..5c13bb8 100644 --- a/libs/submissions_process.js +++ b/libs/submissions_process.js @@ -5,7 +5,7 @@ const getSubmissionInfo = (s, displayConfig) => ({ problemName: s.problem.title, problemId: s.problem_id, language: displayConfig.showCode ? ((s.language != null && s.language !== '') ? syzoj.config.languages[s.language].show : null) : null, - codeSize: displayConfig.showCode ? syzoj.utils.formatSize(s.code.length) : null, + codeSize: displayConfig.showCode ? syzoj.utils.formatSize(s.code_length) : null, submitTime: syzoj.utils.formatDate(s.submit_time), }); diff --git a/models/judge_state.js b/models/judge_state.js index 8f0d325..f8cf279 100644 --- a/models/judge_state.js +++ b/models/judge_state.js @@ -20,6 +20,7 @@ 'use strict'; let Sequelize = require('sequelize'); +const randomstring = require('randomstring'); let db = syzoj.db; let User = syzoj.model('user'); @@ -36,8 +37,10 @@ let model = db.define('judge_state', { language: { type: Sequelize.STRING(20) }, status: { type: Sequelize.STRING(50) }, + task_id: { type: Sequelize.STRING(50) }, score: { type: Sequelize.INTEGER }, total_time: { type: Sequelize.INTEGER }, + code_length: { type: Sequelize.INTEGER }, pending: { type: Sequelize.BOOLEAN }, max_memory: { type: Sequelize.INTEGER }, @@ -82,6 +85,7 @@ class JudgeState extends Model { static async create(val) { return JudgeState.fromRecord(JudgeState.model.build(Object.assign({ code: '', + code_length: 0, language: null, user_id: 0, problem_id: 0, @@ -96,7 +100,8 @@ class JudgeState extends Model { total_time: null, max_memory: null, status: 'Waiting', - result: null + result: null, + task_id: randomstring.generate(10) }, val))); } @@ -156,14 +161,15 @@ class JudgeState extends Model { let oldStatus = this.status; this.status = 'Waiting'; - this.score = 0; + this.score = null; if (this.language) { // language is empty if it's a submit-answer problem - this.total_time = 0; - this.max_memory = 0; + this.total_time = null; + this.max_memory = null; } this.pending = true; this.result = {}; + this.task_id = randomstring.generate(10); await this.save(); /* @@ -192,7 +198,11 @@ class JudgeState extends Model { await contest.newSubmission(this); } + try { await Judger.judge(this, this.problem, 1); + } catch (err) { + throw new ErrorMessage("无法开始评测。"); + } }); } diff --git a/modules/api_v2.js b/modules/api_v2.js index 909f5a9..f79d5af 100644 --- a/modules/api_v2.js +++ b/modules/api_v2.js @@ -87,7 +87,11 @@ app.apiRouter.post('/api/v2/judge/compiled', async (req, res) => { let data = req.body; let JudgeState = syzoj.model('judge_state'); - let judge_state = await JudgeState.fromID(req.body.taskId); + let judge_state = await JudgeState.findOne({ where: { task_id: req.body.taskId } }); + if (!judge_state) { + res.send({ return: 1 }); // The task might have been rejudging. + return; + } judge_state.compilation = req.body.result; await judge_state.save(); @@ -104,7 +108,11 @@ app.apiRouter.post('/api/v2/judge/finished', async (req, res) => { let data = req.body; let JudgeState = syzoj.model('judge_state'); - let judge_state = await JudgeState.fromID(req.body.taskId); + let judge_state = await JudgeState.findOne({ where: { task_id: req.body.taskId } }); + if (!judge_state) { + res.send({ return: 1 }); // The task might have been rejudging. + return; + } // await judge_state.updateResult(JSON.parse(req.body)); judge_state.score = data.score; judge_state.pending = false; diff --git a/modules/contest.js b/modules/contest.js index 611ce07..0a210f1 100644 --- a/modules/contest.js +++ b/modules/contest.js @@ -349,7 +349,7 @@ app.get('/contest/:id/submissions', async (req, res) => { items: judge_state.map(x => ({ info: getSubmissionInfo(x, displayConfig), token: (getRoughResult(x, displayConfig) == null) ? jwt.sign({ - taskId: x.id, + taskId: x.task_id, type: pushType, displayConfig: displayConfig }, syzoj.config.judge_token) : null, @@ -404,7 +404,7 @@ app.get('/contest/submission/:id', async (req, res) => { code: (displayConfig.showCode && judge.problem.type !== 'submit-answer') ? judge.code.toString("utf8") : '', detailResult: processOverallResult(judge.result, displayConfig), socketToken: (displayConfig.showDetailResult && judge.pending) ? jwt.sign({ - taskId: judge.id, + taskId: judge.task_id, displayConfig: displayConfig, type: 'detail' }, syzoj.config.judge_token) : null, diff --git a/modules/problem.js b/modules/problem.js index fd9b11e..20bc9e0 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -594,7 +594,7 @@ app.post('/problem/:id/submit', app.multer.fields([{ name: 'answer', maxCount: 1 if (!file.md5) throw new ErrorMessage('上传答案文件失败。'); judge_state = await JudgeState.create({ code: file.md5, - max_memory: size, + code_length: size, language: null, user_id: res.locals.user.id, problem_id: req.params.id @@ -612,6 +612,7 @@ app.post('/problem/:id/submit', app.multer.fields([{ name: 'answer', maxCount: 1 judge_state = await JudgeState.create({ code: code, + code_length: code.length, language: req.body.language, user_id: res.locals.user.id, problem_id: req.params.id diff --git a/modules/submission.js b/modules/submission.js index 83bed92..b2c71bc 100644 --- a/modules/submission.js +++ b/modules/submission.js @@ -110,7 +110,7 @@ app.get('/submissions', async (req, res) => { items: judge_state.map(x => ({ info: getSubmissionInfo(x, displayConfig), token: (getRoughResult(x, displayConfig) == null) ? jwt.sign({ - taskId: x.id, + taskId: x.task_id, type: 'rough', displayConfig: displayConfig }, syzoj.config.judge_token) : null, @@ -161,7 +161,7 @@ app.get('/submission/:id', async (req, res) => { code: (judge.problem.type !== 'submit-answer') ? judge.code.toString("utf8") : '', detailResult: processOverallResult(judge.result, displayConfig), socketToken: judge.pending ? jwt.sign({ - taskId: judge.id, + taskId: judge.task_id, type: 'detail', displayConfig: displayConfig }, syzoj.config.judge_token) : null, diff --git a/package-lock.json b/package-lock.json index 18c4566..b0d4d34 100644 --- a/package-lock.json +++ b/package-lock.json @@ -105,6 +105,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "array-uniq": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/array-uniq/-/array-uniq-1.0.2.tgz", + "integrity": "sha1-X8w3OSB3VyPP1k1lxkvvU7+eum0=" + }, "asn1": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.3.tgz", @@ -2196,6 +2201,14 @@ "resolved": "https://registry.npmjs.org/random-js/-/random-js-1.0.8.tgz", "integrity": "sha1-lo/WiabyXWwKrHZig94vaIycGQo=" }, + "randomstring": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/randomstring/-/randomstring-1.1.5.tgz", + "integrity": "sha1-bfBij3XL1ZMpMNn+OrTpVqGFGMM=", + "requires": { + "array-uniq": "1.0.2" + } + }, "range-parser": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.0.tgz", diff --git a/package.json b/package.json index 4c6f436..4add2af 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "mysql": "^2.11.1", "node-7z": "^0.4.0", "pygmentize-bundled-cached": "^1.1.0", + "randomstring": "^1.1.5", "request": "^2.74.0", "request-promise": "^4.1.1", "sendmail": "^1.1.1", diff --git a/views/submission.ejs b/views/submission.ejs index c27f905..9800ac6 100644 --- a/views/submission.ejs +++ b/views/submission.ejs @@ -20,7 +20,7 @@ - + @@ -39,7 +39,7 @@
-
得分 +
得分 {{ Math.trunc(subtask.score) }}
@@ -221,13 +221,13 @@ if (token != null) { socket.on('connect', () => { socket.on('start', () => { vueApp.roughData.running = true; + console.log("Judge start!"); vueApp.detailResult = {}; }); socket.on('update', (p) => { console.log("Delta: " + JSON.stringify(p)); if (p.from === currentVersion) { currentVersion = p.to; - console.log("Before patching: " + JSON.stringify(vueApp)); jsondiffpatch.patch(vueApp.detailResult, p.delta); } else { // Some packets are dropped. Let's reset. socket.close(); @@ -235,13 +235,14 @@ if (token != null) { } }); socket.on('finish', (p) => { - console.log("Finish: " + JSON.stringify(p)); + console.log("Judge finished"); vueApp.roughData.running = false; vueApp.roughData.result = p.roughResult; vueApp.detailResult = p.result; socket.close(); }); socket.emit('join', token, (data) => { + console.log("join! " + JSON.stringify(data)); if (data && data.ok) { if (data.finished) { vueApp.roughData.result = data.roughResult; @@ -252,7 +253,8 @@ if (token != null) { } else { if (data.running) { vueApp.roughData.running = true; - vueApp.detailResult = data.current; + vueApp.detailResult = data.current.content; + currentVersion = data.current.version; } } } else { diff --git a/views/submissions.ejs b/views/submissions.ejs index fc0ed3b..a6b144f 100644 --- a/views/submissions.ejs +++ b/views/submissions.ejs @@ -95,7 +95,7 @@ - + diff --git a/views/submissions_item.ejs b/views/submissions_item.ejs index 4a46370..b0dac97 100644 --- a/views/submissions_item.ejs +++ b/views/submissions_item.ejs @@ -67,8 +67,8 @@ Vue.component('submission-item', { {{ data.info.submitTime }}
- - + +