算法平台判别器。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.0 KiB

7 years ago
import _ = require('lodash');
import winston = require('winston');
7 years ago
export interface JudgeResultSubmit {
taskId: number;
time: number;
memory: number;
score: number;
statusNumber: number;
statusString: string;
result: string;
}
import { JudgeResult, TaskResult, TaskStatus, ErrorType, SubtaskResult, TestCaseResult, TestCaseDetails } from './interfaces';
const compileError = "Compile Error",
systemError = "System Error",
testdataError = "No Testdata";
export const statusToString = {};
statusToString[TaskResult.Accepted] = "Accepted";
statusToString[TaskResult.WrongAnswer] = "Wrong Answer";
statusToString[TaskResult.PartiallyCorrect] = "Partially Correct";
statusToString[TaskResult.MemoryLimitExceeded] = "Memory Limit Exceeded";
statusToString[TaskResult.TimeLimitExceeded] = "Time Limit Exceeded";
statusToString[TaskResult.OutputLimitExceeded] = "Output Limit Exceeded";
statusToString[TaskResult.RuntimeError] = "Runtime Error";
statusToString[TaskResult.FileError] = "File Error";
statusToString[TaskResult.JudgementFailed] = "Judgement Failed";
statusToString[TaskResult.InvalidInteraction] = "Invalid Interaction";
export function firstNonAC(t: TaskResult[]): TaskResult {
if (t.every(v => v === TaskResult.Accepted)) {
return TaskResult.Accepted
} else {
return t.find(r => r !== TaskResult.Accepted);
}
}
export function convertResult(id: number, source: JudgeResult): JudgeResultSubmit {
winston.debug(`Converting result for ${id}`, source);
7 years ago
let time = -1,
memory = -1,
score = 0,
done = true,
statusString = null;
if (source.compileStatus === TaskStatus.Failed) {
statusString = compileError;
score = 0;
7 years ago
} else if (source.error != null) {
done = false;
score = NaN;
7 years ago
if (source.error === ErrorType.TestDataError) {
statusString = testdataError;
} else {
statusString = systemError;
}
} else if (source.subtasks != null) {
if (source.subtasks.some(s => s.score === NaN)) {
score = NaN;
7 years ago
statusString = systemError;
} else {
score = _.sum(source.subtasks.map(s => s.score));
const forEveryTestcase = function <TParam>(map: (v: TestCaseDetails) => TParam, reduce: (v: TParam[]) => TParam): TParam {
return reduce(source.subtasks.map(s => reduce(s.cases.filter(c => c.result != null).map(c => map(c.result)))));
}
time = forEveryTestcase(c => c.time, _.sum);
memory = forEveryTestcase(c => c.memory, _.max);
const finalResult = forEveryTestcase(c => c.type, firstNonAC);
statusString = statusToString[finalResult];
7 years ago
}
}
const result = {
7 years ago
taskId: id,
time: time,
memory: memory,
score: score,
statusNumber: done ? TaskStatus.Done : TaskStatus.Failed,
statusString: statusString,
result: JSON.stringify(source)
};
winston.debug(`Result for ${id}`, result);
return result;
7 years ago
}