算法平台判别器。
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.

90 lines
3.4 KiB

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