This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudgeUtils.js
151 lines (120 loc) · 5.63 KB
/
judgeUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Node Modules
const fs = require("fs");
const { spawn, exec } = require('child_process');
// Cores
const CompileCore = require('./Core/Compile/CompileCore');
const PrepareCore = require('./Core/Prepare/PrepareCore');
const StageCore = require('./Core/Stage/StageCore');
const ExecCore = require('./Core/Execute/ExecCore');
const CleanupCore = require('./Core/Cleanup/CleanupCore');
require('epipebomb')()
function scoreOutput(output, expectedOutput, callback) {
let score = 0;
if (typeof output == 'undefined') {
console.log('[Error] Output is undefined.');
output = "error";
}
for(let i = 0; i < expectedOutput.length; i++) {
// Ignore newlines in the test cases
//console.log("*********LOOK AT THIS:" + typeof expectedOutput[i] + " - " + typeof output[i]);
console.log("[Debug] Judge Iteration " + i);
// Bug with C++ programs
if (typeof output[i] == 'undefined') {
console.log('[Error] Output[i] is undefined.');
output = "error";
break;
}
let strippedExpectedOuput = expectedOutput[i].replace(/(\r\n|\n|\r)/gm, "").trim();
let strippedOutput = output[i].replace(/(\r\n|\n|\r)/gm, "").trim();
if(strippedExpectedOuput === strippedOutput) {
console.log("[Debug] Right answer! Expected: " + strippedExpectedOuput + " and got: " + strippedOutput);
score++;
} else {
console.log("[Debug] Wrong answer! Expected: " + strippedExpectedOuput + " but got: " + strippedOutput);
callback(score, false, i);
return;
}
}
callback(score, true, -1);
return;
}
function judgeSubmission(problemID, userID, inputCode, lang, input, output, timelimit, callback) {
console.log("[Info] Judging a submission.");
let judgeResult = {accepted: false, time: -1, isTLE: false, isCompileError: false, otherError: false, errorAt: -1, score: -1, errorContent: ""};
// Create a submission request object.
// input & output are the expected test case
let submissionRequest = {problemID: problemID, userID: userID, inputCode: inputCode, lang: lang, input: input, output: output, timelimit: timelimit};
const problemRoot = './problems/' + problemID;
// Load the problem metadata (Timemout and Mem Limit)
//const problemMeta = JSON.parse(fs.readFileSync('1' + '/meta.json'));
// Load the test cases
//const testInput = fs.readFileSync(problemRoot + '/in.txt').toString().split("\n");
//const testOutput = fs.readFileSync(problemRoot + '/out.txt').toString().split("\n");
const testInput = input.replace('\r', '').split("\n");
const testOutput = output.replace('\r', '').split("\n");
// Place decoded JSON into request object
submissionRequest.input = testInput;
submissionRequest.output = testOutput;
console.log('[Info] Parsed Input and Output Cases');
console.log('[Debug] Input Cases: ' + testInput[0]);
console.log('[Debug] Input Cases 2: ' + testInput[1]);
console.log('[Debug] Output Cases: ' + testOutput[0]);
// Please ignore callback hell
// Execution Flow: Prepare -> Compile -> Stage -> Execute -> Cleanup
// Then respond with the score.
// decode and save the code.
let buff = new Buffer(inputCode, 'base64');
// Place into request
submissionRequest.inputCode = buff.toString('ascii');
if(submissionRequest.inputCode == "") {
callback(judgeResult);
return;
}
PrepareCore.prepareSubmission(submissionRequest, (prepareResult) => {
// Compile the input code (Inside firejail)
//firejail --apparmor --private --net=none --quiet
CompileCore.compileSubmission(submissionRequest, (compileResult, err) => {
console.log("***** We have compileda the file");
if(!compileResult) {
// Error in file compile (i.e CE), set it in the result object
judgeResult.isCompileError = true;
judgeResult.errorContent = err;
callback(judgeResult);
return;
}
// Mark the file as executable
StageCore.stageSubmission(submissionRequest, (stageResult) => {
console.log("***** We have staged the file");
ExecCore.execSubmission(submissionRequest, (execResult, inputProcessOutput) => {
console.log("***** We have exec the file");
console.log(inputProcessOutput[0]);
if(!execResult) {
judgeResult.isTLE = true;
callback(judgeResult);
return;
}
scoreOutput(inputProcessOutput, submissionRequest.output, function(score, isAccepeted, errorAt) {
if(isAccepeted === false) {
judgeResult.accepted = false;
judgeResult.errorAt = errorAt;
CleanupCore.cleanupSubmission(submissionRequest, (result) => {
callback(judgeResult);
return;
});
} else {
judgeResult.accepted = true;
judgeResult.score = score;
CleanupCore.cleanupSubmission(submissionRequest, (result) => {
callback(judgeResult);
return;
});
}
});
});
});
});
});
}
module.exports = {
judgeSubmission: judgeSubmission
};