-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
93 lines (85 loc) · 2.74 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
88
89
90
91
92
93
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var Team = require('./models/team');
var Judge = require('./models/judge');
app.use(bodyParser.urlencoded({extended: true}));
// mongoose.connect("mongodb://localhost/judging");
mongoose.connect("mongodb://uf0shymlfpq2ktltywlh:MI1f3I8ck6tctCNHQo1s@byvyroz6ewveb0q-mongodb.services.clever-cloud.com:27017/byvyroz6ewveb0q");
var round=2;
Team.find({},function(err,res){
if(err){
console.log(err);
}
else{
res.forEach(function (val){
scores = val.scores;
val.save();
});
}
});
app.get('/teams/:judgeid',function(req,res){
Judge.find({judge_id: req.params.judgeid},function(err,dbjudge){
if(err){
console.log(err);
}
else{
var jteams = dbjudge[0].teams;
Team.find({'team_id': {$in : jteams}},function(err,dbteams){
if(err){
console.log(err);
}
else{
dbteams.sort(function(a,b){
return a.team_id.localeCompare(b.team_id);
});
res.send({round: round, teams: dbteams});
}
});
}
});
});
app.post('/scores',function(req,res){
var score = req.body.score;
var uid = req.body.jid;
var tid = req.body.tid;
console.log(score);
Judge.findOne({judge_id: uid},function(err,dbjudge){
if(err){
console.log(err);
}
else{
var teamstobejudged = dbjudge.teams;
var idx = teamstobejudged.indexOf(tid);
teamstobejudged.splice(idx,1);
dbjudge.teams = teamstobejudged;
dbjudge.save(function(err,updatedJudge){
if(err){
console.log(err);
}
else{
Team.findOne({team_id: tid},function(err,dbteam){
if(err){
console.log(err);
}
else{
console.log(dbteam.scores[round-1]);
dbteam.scores[round-1].push({judge_id: dbjudge.judge_id,score: score});
dbteam.save(function(err,updatedTeam){
res.redirect('/teams/'+uid);
});
}
});
}
});
}
});
});
//Used for debugging locally
// app.listen(8080,function(){
// console.log("Server has Started!");
// });
app.listen(process.env.PORT,process.env.IP,function(){
console.log("Server has Started!");
});