forked from vsp4/codechef-rating-predictor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
148 lines (130 loc) · 3.64 KB
/
server.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
var http = require("http");
var https = require("https");
var util = require("util");
var cheerio = require("cheerio");
var express = require("express");
var app = express();
app.set('view engine', 'ejs');
//from snipplr
function elapsedTime (createdAt)
{
var ageInSeconds = (new Date().getTime() - new Date(createdAt).getTime()) / 1000;
var s = function(n) { return n == 1 ? '' : 's' };
if (ageInSeconds < 0) {
return 'just now';
}
if (ageInSeconds < 60) {
var n = ageInSeconds;
return n + ' second' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60) {
var n = Math.floor(ageInSeconds/60);
return n + ' minute' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60 * 24) {
var n = Math.floor(ageInSeconds/60/60);
return n + ' hour' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60 * 24 * 7) {
var n = Math.floor(ageInSeconds/60/60/24);
return n + ' day' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60 * 24 * 31) {
var n = Math.floor(ageInSeconds/60/60/24/7);
return n + ' week' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60 * 24 * 365) {
var n = Math.floor(ageInSeconds/60/60/24/31);
return n + ' month' + s(n) + ' ago';
}
var n = Math.floor(ageInSeconds/60/60/24/365);
return n + ' year' + s(n) + ' ago';
}
require("./helper.js")();
MongoClient.connect(mongourl, function(err, db)
{
if (err)
{
throw err;
}
var datacollection = db.collection("data");
var lastupdatecollection = db.collection("lastupdate");
var checklist = db.collection("checklist");
var processor = require("./process.js");
app.get('/',function(req,res) {
res.send("Try <a href='/contest/OCT18B/all'>OCT18B</a>");
})
app.get('/add/:contest', function(req,res) {
var cid = req.params.contest;
checklist.findOneAndReplace(
{
contest: cid,
parse:['all']
},
{
contest: cid,
parse:['all']
},
{
upsert: true
},
function(err,result) {
if(result) {
processor(true);
res.redirect('/contest/'+cid+'/all');
} else {
res.render("error", {message: "Couldnot add to checklist"});
}
})
})
app.get('/contest/:contestid/:type', function(req, res)
{
processor();
checklist.findOne({contest: req.params.contestid},function(err,obj) {
if(err) {
return;
}
if(obj) {
lastupdatecollection.findOne({contest: req.params.contestid}, function (err, dateobj)
{
if (err)
throw err;
if (dateobj)
{
datacollection.find({contest: req.params.contestid, type: req.params.type}).sort({rank: 1}).toArray((err, result) => {
if (err)
throw err;
for (var i in result)
{
result[i].change = result[i].rating - result[i].previous;
}
var typename = req.params.type[0].toUpperCase() + req.params.type.slice(1);
res.render('rating', {elapsed: elapsedTime(dateobj.date), contest: req.params.contestid, type: req.params.type, typename: typename, result: result});
});
}
else
{
res.status(404);
res.render("error", {message: "We are currently calculating ratings for this contest!"});
}
});
}
else {
res.status(404);
var link = req.protocol + "://" + req.get('host') + "/add/" + req.params.contestid
res.render("error", {
message: "No contest predictions found for such contest! Click the button to add contest.",
link: link});
}
});
});
app.use(function(req, res)
{
processor();
res.status(500);
res.render("error", {message: "Invalid link!"});
});
app.listen(8080);
console.log('Listening on http://127.0.0.1:8080');
processor();
});