diff --git a/models/contest.js b/models/contest.js index 300b00d..ea19bd7 100644 --- a/models/contest.js +++ b/models/contest.js @@ -123,9 +123,8 @@ class Contest extends Model { let problems = await this.getProblems(); if (!problems.includes(judge_state.problem_id)) throw new ErrorMessage('当前比赛中无此题目。'); - let player; - await syzoj.utils.lock(['Contest::newSubmission', 'create_player', judge_state.user_id], async () => { - player = await ContestPlayer.findInContest({ + await syzoj.utils.lock(['Contest::newSubmission', judge_state.user_id], async () => { + let player = await ContestPlayer.findInContest({ contest_id: this.id, user_id: judge_state.user_id }); @@ -136,14 +135,14 @@ class Contest extends Model { user_id: judge_state.user_id }); } - }); - await player.updateScore(judge_state); - await player.save(); + await player.updateScore(judge_state); + await player.save(); - await this.loadRelationships(); - await this.ranklist.updatePlayer(this, player); - await this.ranklist.save(); + await this.loadRelationships(); + await this.ranklist.updatePlayer(this, player); + await this.ranklist.save(); + }); } async isRunning(now) { diff --git a/models/contest_player.js b/models/contest_player.js index 044e23b..ac7c402 100644 --- a/models/contest_player.js +++ b/models/contest_player.js @@ -69,99 +69,100 @@ class ContestPlayer extends Model { } async updateScore(judge_state) { - await syzoj.utils.lock(['ContestPlayer::updateScore', this.id], async () => { - await this.loadRelationships(); - if (this.contest.type === 'ioi') { - if (!judge_state.pending) { - if (!this.score_details[judge_state.problem_id]) { - this.score_details[judge_state.problem_id] = { - score: judge_state.score, - judge_id: judge_state.id, - submissions: {} - }; - } - - this.score_details[judge_state.problem_id].submissions[judge_state.id] = { - judge_id: judge_state.id, + await this.loadRelationships(); + if (this.contest.type === 'ioi') { + if (!judge_state.pending) { + if (!this.score_details[judge_state.problem_id]) { + this.score_details[judge_state.problem_id] = { score: judge_state.score, - time: judge_state.submit_time + judge_id: judge_state.id, + submissions: {} }; + } - let arr = Object.values(this.score_details[judge_state.problem_id].submissions); - arr.sort((a, b) => a.time - b.time); - - let maxScoreSubmission = null; - for (let x of arr) { - if (!maxScoreSubmission || x.score >= maxScoreSubmission.score && maxScoreSubmission.score < 100) { - maxScoreSubmission = x; - } - } + this.score_details[judge_state.problem_id].submissions[judge_state.id] = { + judge_id: judge_state.id, + score: judge_state.score, + time: judge_state.submit_time + }; - this.score_details[judge_state.problem_id].judge_id = maxScoreSubmission.judge_id; - this.score_details[judge_state.problem_id].score = maxScoreSubmission.score; - this.score_details[judge_state.problem_id].time = maxScoreSubmission.time; + let arr = Object.values(this.score_details[judge_state.problem_id].submissions); + arr.sort((a, b) => a.time - b.time); - this.score = 0; - for (let x in this.score_details) { - this.score += this.score_details[x].score; + let maxScoreSubmission = null; + for (let x of arr) { + if (!maxScoreSubmission || x.score >= maxScoreSubmission.score && maxScoreSubmission.score < 100) { + maxScoreSubmission = x; } } - } else if (this.contest.type === 'noi') { - this.score_details[judge_state.problem_id] = { - score: judge_state.score, - judge_id: judge_state.id - }; + + this.score_details[judge_state.problem_id].judge_id = maxScoreSubmission.judge_id; + this.score_details[judge_state.problem_id].score = maxScoreSubmission.score; + this.score_details[judge_state.problem_id].time = maxScoreSubmission.time; + this.score = 0; for (let x in this.score_details) { this.score += this.score_details[x].score; } - } else if (this.contest.type === 'acm') { - if (!judge_state.pending) { - if (!this.score_details[judge_state.problem_id]) { - this.score_details[judge_state.problem_id] = { - accepted: false, - unacceptedCount: 0, - acceptedTime: 0, - judge_id: 0, - submissions: {} - }; - } + } + } else if (this.contest.type === 'noi') { + if (this.score_details[judge_state.problem_id] && this.score_details[judge_state.problem_id].judge_id > judge_state.id) return; - this.score_details[judge_state.problem_id].submissions[judge_state.id] = { - judge_id: judge_state.id, - accepted: judge_state.status === 'Accepted', - compiled: judge_state.status !== 'Compile Error', - time: judge_state.submit_time + this.score_details[judge_state.problem_id] = { + score: judge_state.score, + judge_id: judge_state.id + }; + + this.score = 0; + for (let x in this.score_details) { + this.score += this.score_details[x].score; + } + } else if (this.contest.type === 'acm') { + if (!judge_state.pending) { + if (!this.score_details[judge_state.problem_id]) { + this.score_details[judge_state.problem_id] = { + accepted: false, + unacceptedCount: 0, + acceptedTime: 0, + judge_id: 0, + submissions: {} }; + } - let arr = Object.values(this.score_details[judge_state.problem_id].submissions); - arr.sort((a, b) => a.time - b.time); - - this.score_details[judge_state.problem_id].unacceptedCount = 0; - this.score_details[judge_state.problem_id].judge_id = 0; - this.score_details[judge_state.problem_id].accepted = 0; - for (let x of arr) { - if (x.accepted) { - this.score_details[judge_state.problem_id].accepted = true; - this.score_details[judge_state.problem_id].acceptedTime = x.time; - this.score_details[judge_state.problem_id].judge_id = x.judge_id; - break; - } else if (x.compiled) { - this.score_details[judge_state.problem_id].unacceptedCount++; - } - } + this.score_details[judge_state.problem_id].submissions[judge_state.id] = { + judge_id: judge_state.id, + accepted: judge_state.status === 'Accepted', + compiled: judge_state.status !== 'Compile Error', + time: judge_state.submit_time + }; - if (!this.score_details[judge_state.problem_id].accepted) { - this.score_details[judge_state.problem_id].judge_id = arr[arr.length - 1].judge_id; + let arr = Object.values(this.score_details[judge_state.problem_id].submissions); + arr.sort((a, b) => a.time - b.time); + + this.score_details[judge_state.problem_id].unacceptedCount = 0; + this.score_details[judge_state.problem_id].judge_id = 0; + this.score_details[judge_state.problem_id].accepted = 0; + for (let x of arr) { + if (x.accepted) { + this.score_details[judge_state.problem_id].accepted = true; + this.score_details[judge_state.problem_id].acceptedTime = x.time; + this.score_details[judge_state.problem_id].judge_id = x.judge_id; + break; + } else if (x.compiled) { + this.score_details[judge_state.problem_id].unacceptedCount++; } + } - this.score = 0; - for (let x in this.score_details) { - if (this.score_details[x].accepted) this.score++; - } + if (!this.score_details[judge_state.problem_id].accepted) { + this.score_details[judge_state.problem_id].judge_id = arr[arr.length - 1].judge_id; + } + + this.score = 0; + for (let x in this.score_details) { + if (this.score_details[x].accepted) this.score++; } } - }); + } } getModel() { return model; }