-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
87 lines (85 loc) · 2.8 KB
/
app.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
var fs= require("fs")
const { exec }=require('child_process');
const { execSync }=require('child_process');
var lang_commands=require('./lang_commands.js');
checkers={};
require("./checkers.js");
const args = process.argv.reduce((a, b, index) => {
if (/^[^=]*=[^=]*$/.test(b)) {
const arr = b.split('=')
a[arr[0]] = arr[1]
}
return a;
}, {})
//---------------------------------------------
const datadir="./data/";
const tempdir="./temp/";
var lang=args.lang//语言
var pid=args.pid//题号
var code=args.code;//代码路径
var compile_time_out=50000//编译时间上限
var pgname=tempdir+Math.floor(Math.random()*10000);
var probcfg=JSON.parse(fs.readFileSync(datadir+pid+"/"+"config.json"));
var tl=probcfg.time_limit;//时限
var checker=checkers[probcfg.checker];
var _=0;
//---------------------------------------------
var score=0;//分数
exec(lang_commands[lang][0].replace("<code>",code).replace("<program>",pgname),{timeout:compile_time_out},function (error, stdout, stderr) {
if(stderr){//编译报错
console.log(stderr);
console.log('CE');
process.exit()
}
try{
if(error.killed){//如果被杀死说明编译器死掉了
console.log('CF');//"Compiling failed"
process.exit()
}
}
catch(e){}
var n=1;
while(fs.existsSync(datadir+pid+"/"+n+".in"))n++;
function hd(){
_++;
if(_==n-1){
console.log(score);
process.exit(0);
}
};
for(var i=1;i<n;i++){//每一组数据
function bb(i){
var inF=datadir+pid+"/"+i+".in"; //输入文件
var outF=tempdir+i+".out" //输出文件
var ansF=datadir+pid+"/"+i+".out" //答案文件
console.log("Start test "+i);
var pro=exec(lang_commands[lang][1].replace("<program>",pgname),{timeout:tl},
function(error,stdout,stderr){
if(error!=null){
if(error.killed)
console.log('Test '+i+' : TLE')//程序超时被杀死
else
console.log('Test '+i+' : RE')//自己异常退出
hd();
return;
}
var result=checker(stdout.toString(),fs.readFileSync(ansF).toString());
if(result==100){
score+=probcfg.score[i-1];
console.log('Test '+i+' : AC');
}else if(result==0){
console.log('Test '+i+' : WA');
}
else{
score+=probcfg.score[i-1]*(result/100);
console.log('Test '+i+' : PC '+probcfg.score[i-1]*(result/100));
}
hd();
}
);
pro.stdin.write(fs.readFileSync(inF).toString());//给程序输入
pro.stdin.end();
}
bb(i);
}
})