forked from cheekujodhpur/ipho2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit_mark.txt
97 lines (94 loc) · 5.76 KB
/
submit_mark.txt
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
app.post('/submit_mark_T1',function(req,res){
var jsonString = '';
req.on('data',function(data)
{
jsonString += data;
});
req.on('end',function(){
var jsonData = JSON.parse('{"' + decodeURI(jsonString).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}');
var dbData = [];
for(var i in jsonData)
dbData.push(parseFloat(jsonData[i]));
MongoClient.connect("mongodb://localhost:27017/test",function(err,db)
{
if(err)
{
console.log(err);
return 0;
}
if(req.ip != null)
{
var ip = req.ip.toString();
}
else
{
console.log("Null IP Error.Carry on");
return;
}
console.log("Connection established to the server at mongodb://localhost:27017/test in response to " + ip.toString());
var marks = db.collection('marks_T1');
var users = db.collection('users');
var ourMarks_db = db.collection('ourMarks_T1');
var subparts = db.collection('subparts');
marks.update({"ip":ip},{$set:{"leaderMarks":dbData}},{upsert:true},function(err,result){
//TODO: the code to write the marks onto a file
users.find({"ip":ip}).toArray(function(err,items){
var user = items[0];
var leaderMarks = dbData;
var country_code = user.country_code;
var country_name = user.country_name;
//var students = user.students;
var students = ['Sirius Sharma','Rigel Armstrong','Saiph Ali Khan'];
subparts.find({"type":"t1"}).toArray(function(err,itemss){
var subpart_arr = itemss[0].subparts;
var maxMarks_arr = itemss[0].maxMarks;
ourMarks_db.find({"country_name":country_name}).toArray(function(err,items2){
var ourMarks = items2[0].leaderMarks; //Yes, the field name is still leaderMarks
var fileName = "mk/" + country_code.toString() + "_T1.html";
var com_fileName = "marks/" + country_code.toString() + "_T1.html";
var stream = fs.createWriteStream(fileName);
stream.once('open',function(fd){
var htmlstr = '';
var tdstr_subparts = '';
var tdstr_maxMarks = '';
htmlstr += '<!DOCTYPE html> <html lang="en"> <head> <meta charset = "utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>IPhO 2015 - Mumbai, India</title> <!--Bootstrap--> <link href = "/static/css/bootstrap.min.css" rel = "stylesheet" /> <!!--Custom CSS--> <link href = "/static/css/main.css" rel = "stylesheet" /> <link href = "media/favicon.ico" rel="shortcut icon" type="image/x-icon"/> <link href = "media/favicon.ico" rel="icon" type="image/x-icon"/> </head><body onload="window.print();">';
htmlstr += '<table class = "table table-striped">';
for(var i = 0;i<ourMarks.length/students.length;i++)
{
tdstr_subparts += '<td>'+subpart_arr[i]+'</td>';
tdstr_maxMarks += '<td><b>'+maxMarks_arr[i]+'</b></td>';
}
htmlstr += '<tr> <th>Students</th> <th>Code</th> </tr> <tr> <td colspan = "2"></td> <td>Subparts</td> '+tdstr_subparts+'</tr> <tr> <td colspan = "2"></td> <td>Maximum Marks</td> '+tdstr_maxMarks+'</tr>';
for(var i = 0;i<students.length;i++)
{
htmlstr += '<tr><td>'+students[i]+'</td><td>'+country_code+'_'+(i+1).toString()+'</td><td></td>';
for(var j = 0;j<ourMarks.length/students.length;j++)
{
htmlstr += '<td>'+ourMarks[i*ourMarks.length/students.length+j].toString() + '</td>';
}
htmlstr += '</tr><tr><td colspan="3"></td>';
for(var j = 0;j<ourMarks.length/students.length;j++)
{
htmlstr += '<td>'+leaderMarks[i*ourMarks.length/students.length+j].toString() + '</td>';
}
htmlstr += '</tr><tr><td colspan="3"></td>';
for(var j = 0;j<ourMarks.length/students.length;j++)
{
htmlstr += '<td>'+(ourMarks[i*ourMarks.length/students.length+j]-leaderMarks[i*ourMarks.length/students.length+j]).toFixed(1) + '</td>';
}
htmlstr += '</tr><tr style="height:30px;"></tr>';
}
htmlstr += '</table>';
htmlstr += '</body></html>';
stream.write(htmlstr);
stream.end();
});
res.json({"success":true,"filename":com_fileName});
db.close();
});
});
});
});
});
});
});