From a1b5ad0ddd564c29320f152d3ef6baf7a0483253 Mon Sep 17 00:00:00 2001 From: t123yh Date: Fri, 4 Aug 2017 20:19:19 +0800 Subject: [PATCH] Judge v3 --- config-example.json | 1 + models/judge_state.js | 7 ++++++- modules/api_v2.js | 24 ++++++++++++++++++++++++ modules/problem.js | 16 ++++++++-------- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/config-example.json b/config-example.json index 74501b1..8a55fb5 100644 --- a/config-example.json +++ b/config-example.json @@ -151,5 +151,6 @@ } ], "session_secret": "233", + "judge_server_addr": "http://127.0.0.1:5284", "judge_token": "233" } diff --git a/models/judge_state.js b/models/judge_state.js index d4369fd..6cdf122 100644 --- a/models/judge_state.js +++ b/models/judge_state.js @@ -20,6 +20,7 @@ 'use strict'; let Sequelize = require('sequelize'); +const judger = require('../modules/judge'); let db = syzoj.db; let User = syzoj.model('user'); @@ -211,9 +212,10 @@ class JudgeState extends Model { 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 }; + this.result = { }; await this.save(); + /* let WaitingJudge = syzoj.model('waiting_judge'); let waiting_judge = await WaitingJudge.create({ judge_id: this.id, @@ -222,6 +224,7 @@ class JudgeState extends Model { }); await waiting_judge.save(); + */ if (oldStatus === 'Accepted') { await this.user.refreshSubmitInfo(); @@ -237,6 +240,8 @@ class JudgeState extends Model { let contest = await Contest.fromID(this.type_info); await contest.newSubmission(this); } + + await judger.judge(this, 1); }); } diff --git a/modules/api_v2.js b/modules/api_v2.js index 1cbbda7..8902ed8 100644 --- a/modules/api_v2.js +++ b/modules/api_v2.js @@ -178,3 +178,27 @@ app.apiRouter.post('/api/v2/judge/update/:id', async (req, res) => { res.status(500).send(e); } }); + +app.apiRouter.post('/api/v2/judge/update2', async (req, res) => { + try { + if (req.get('Token') !== syzoj.config.judge_token) return res.status(403).send({ err: 'Incorrect token' }); + const data = req.body; + + let JudgeState = syzoj.model('judge_state'); + let judge_state = await JudgeState.fromID(req.body.taskId); + // await judge_state.updateResult(JSON.parse(req.body)); + judge_state.score = data.score; + judge_state.pending = false; + judge_state.status = data.statusString; + this.total_time = data.time; + this.max_memory = data.memory; + this.result = data.result; + await judge_state.save(); + await judge_state.updateRelatedInfo(); + + res.send({ return: 0 }); + } catch (e) { + syzoj.log(e); + res.status(500).send(e); + } +}); diff --git a/modules/problem.js b/modules/problem.js index a49398b..c21b20a 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -28,6 +28,8 @@ let ProblemTag = syzoj.model('problem_tag'); let ProblemTagMap = syzoj.model('problem_tag_map'); let Article = syzoj.model('article'); +const judger = require('../modules/judge'); + app.get('/problems', async (req, res) => { try { let where = {}; @@ -194,7 +196,7 @@ app.get('/problem/:id', async (req, res) => { problem.allowedManage = await problem.isAllowedManageBy(res.locals.user); if (problem.is_public || problem.allowedEdit) { - await syzoj.utils.markdown(problem, [ 'description', 'input_format', 'output_format', 'example', 'limit_and_hint' ]); + await syzoj.utils.markdown(problem, ['description', 'input_format', 'output_format', 'example', 'limit_and_hint']); } else { throw new ErrorMessage('您没有权限进行此操作。'); } @@ -635,13 +637,11 @@ app.post('/problem/:id/submit', app.multer.fields([{ name: 'answer', maxCount: 1 } await judge_state.updateRelatedInfo(true); - let waiting_judge = await WaitingJudge.create({ - judge_id: judge_state.id, - priority: 1, - type: 'submission' - }); - - await waiting_judge.save(); + try { + await judger.judge(judge_state, contest_id ? 3 : 2); + } catch (err) { + throw new ErrorMessage(`无法开始评测:${err.toString()}`); + } res.redirect(syzoj.utils.makeUrl(['submission', judge_state.id])); } catch (e) {