-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
148 lines (128 loc) · 4.53 KB
/
index.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 express = require('express');
var app = express();
var mongo = require('mongodb');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongodbUrl = "mongodb://10.64.2.11:27017/ysdndb";
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(express.static(__dirname + '/dist'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
mongo.MongoClient.connect(mongodbUrl, function(err, db) {
db.collection('ysdnlog').find({ }, {tailable:true, awaitdata:true, numberOfRetries:-1}).each(function(err, doc) {
io.sockets.emit('new', JSON.stringify(doc));
console.log("mongotail:" + JSON.stringify(doc));
});
});
app.get('/', function(req, res){
res.render('signIn');
});
app.get('/display', function(req, res){
res.render('display');
});
app.get('/students', function(req, res){
mongo.MongoClient.connect(mongodbUrl, function(err, db) {
getstudent(db, function(result) {
if(result == null) { return res.json('{ }'); }
return res.json(result);
});
});
});
app.post('/choose', function(req, res, next){
var student = req.body.student;
var challenge = -1;
if(req.body.challenge.substring(9) === "1") { challenge = 1; }
else if(req.body.challenge.substring(9) === "2") { challenge = 2; }
console.log(student);
console.log(challenge);
mongo.MongoClient.connect(mongodbUrl, function(err, db) {
getgroup(db, challenge, function(result) {
var group = result.font;
setstudent(db, student, group, challenge, function(result) {
setgroup(db, student, challenge, group, function(r) {
if(r.result.ok) {
var name = student.first + " " + student.last;
return res.json( { "name": name, "challenge": challenge, "group": group, "s_id": student._id } );
} else { return res.json( { } ); }
});
});
});
});
});
app.get('/board', function(req, res){
return res.render("board");
});
app.get('/groups', function(req, res){
mongo.MongoClient.connect(mongodbUrl, function(err, db) {
getgroups(db, function(result) {
if(result == null) { return res.json('{ }'); }
return res.json(result);
});
});
});
var getstudent = function(db, callback) {
db.collection('students').find( { "matched": false }).toArray(function(err, doc) {
if(doc == null) { return callback(null); }
else { return callback(doc); }
});
}
var setstudent = function(db, student, group, challenge, callback) {
db.collection('students').updateOne( { "email": student.email }, { $set: { "challenge": challenge, "group": group, "matched": true } }, function(err, doc) {
if(doc == null) { return callback(false); }
else { return callback(true); }
});
}
var setgroup = function(db, student, challenge, group, callback) {
var fn = student.first + " " + student.last;
db.collection('groups').updateOne( { "font": group }, { $set: { "challenge": challenge }, $inc: { "scount": 1 }, $push: { "members": fn } }, function(err, doc) {
db.collection('ysdnlog').insertOne( { "name": fn, "group":group, "challenge": challenge }, function(err, result) {
callback(result);
});
});
}
var getgroup = function(db, challenge, callback) {
console.log("Choosing...");
db.collection('groups').find( { "challenge": challenge, "scount": 1 }).toArray(function(err, doc) {
if(doc.length == 0) {
console.log("No existing group of Challenge" + challenge + ". Finding empty groups.");
db.collection('groups').find( {"scount": 0 }).toArray(function(err, doc) {
if(doc.length == 0) {
console.log("No empty groups. Forcing full group.");
db.collection('groups').find( { "challenge" : challenge, "scount": 2 }).toArray(function(err, doc) {
var rand = Math.floor(Math.random() * (doc.length - 1));
return callback(doc[rand]);
});
}
else {
var rand = Math.floor(Math.random() * (doc.length - 1));
return callback(doc[rand]);
}
});
}
else {
console.log("Found!");
var rand = Math.floor(Math.random() * (doc.length - 1));
return callback(doc[rand]);
}
});
}
var getgroups = function(db, callback) {
db.collection('groups').find().toArray(function(err, doc) {
if(doc == null) { return callback(null); }
else { return callback(doc); }
});
}
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});