-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
188 lines (153 loc) · 5.38 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Load required packages
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var passport = require('passport');
var mainController = require('./controllers/main');
var debTController = require('./controllers/debateTime');
var request = require('request');
var async = require('async');
var parseString = require('xml2js').parseString;
var fs = require('fs');
// Create our Express application
var app = express();
var http = require('http');
var server = http.createServer(app).listen(8000, function(){
console.log('Express server listening on port ' + 8000);
});
var io = require('socket.io').listen(server);
io.on('connection', function(socket){
socket.on('viewing debate', function(msg){
var debates = mainController.getCurrentDebates();
console.log("viewing: " +msg["id"]);
for(var i = 0; i<debates.length; i++){
if(debates[i]["id"] === parseInt(msg["id"])){
debates[i]["viewerINC"]++;
debates[i]["viewers"].push(msg["id"] + "viewer"+ (debates[i]["viewerINC"]-1));
io.emit('set viewer', {"viewerID": (msg["id"] + "viewer"+ (debates[i]["viewerINC"]-1)), "id":parseInt(msg["id"])});
console.log(debates[i]["viewerINC"])
return;
}
}
});
socket.on('debator ready', function(msg) {
var debates = mainController.getDebates();
var id = parseInt(msg["id"])
console.log(debates);
var debate = "not found";
for(var i = 0; i<debates.length; i++){
console.log(debates[i]["id"]);
if(debates[i]["id"] === id){
console.log("found correctly");
debates[i]["readyDebators"].push(msg['debator']);
debate = debates[i]
break;
}
}
if(debate == "not found"){
debates = mainController.getCurrentDebates();
for(var i = 0; i<debates.length; i++){
if(debates[i]["id"] === id){
console.log("found correctly");
debates[i]["readyDebators"].push(msg['debator']);
debate = debates[i]
// var time = debTController.getNextDelayTime(debate);
// io.emit("set sender", {"id":id, "debator":"first", "viewers":debate["viewers"], "speakingTime":time})
// debate["myTimeout"] = setTimeout(startSwitching, time, socket, id);
startSwitching(socket, id);
break;
}
}
}
})
socket.on('ready', function(msg){
debates = mainController.getCurrentDebates();
for(var i = 0; i<debates.length; i++){
if(debates[i]["id"] == msg["id"]){
io.emit('call viewer', {"viewerID": msg["viewerID"], "id":msg["id"], "debateState":debates[i]["debateState"]});
}
}
});
socket.on('end early', function(msg){
debates = mainController.getCurrentDebates();
for(var i = 0; i<debates.length; i++){
if(debates[i]["id"] === msg["id"]){
if(debates[i]["debateState"]%2 == 1 && msg["secret"] == debates[i]["key1"]){
clearTimeout(debates[i]["myTimeout"]);
debates[i]["myTimeout"] = startSwitching(socket, msg["id"]);
return;
}else if(debates[i]["debateState"]%2 == 0 && msg["secret"] == debates[i]["key2"]){
clearTimeout(debates[i]["myTimeout"]);
debates[i]["myTimeout"] = startSwitching(socket, msg["id"]);
return;
}
}
}
console.log("wrong presser or key, could not end early");
});
});
function startSwitching(socket, id){
var viewers = [];
var curDevs = mainController.getCurrentDebates();
for(var i = 0; i<curDevs.length; i++){
if(curDevs[i].id === id){
viewers = curDevs[i]["viewers"];
}
}
if(debate["debateState"]%2 == 0){
var nextTime = debTController.getNextDelayTime(debate)
if(nextTime != -1){
io.emit('set sender', {'debator':'first', "viewers":viewers, "id":id, "speakingTime":nextTime});
debate["myTimeout"] = setTimeout(startSwitching, nextTime, socket, id);
debate["debateState"]++;
}else{
io.emit("debate over", {"id":id})
}
} else {
var nextTime = debTController.getNextDelayTime(debate)
if(nextTime != -1){
io.emit('set sender', {'debator':'second', "viewers":viewers, "id":id, "speakingTime":nextTime});
debate["myTimeout"] = setTimeout(startSwitching, nextTime, socket, id);
debate["debateState"]++;
}else{
io.emit("debate over", {"id":id})
}
}
}
app.set("view engine", "ejs");
// Use the body-parser package in our application
app.use(bodyParser.urlencoded({
extended: true
}));
// Add headers
app.use(function (req, res, next) {
//console.log("here");
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Authorization");
next();
});
// Create our Express router
var router = express.Router();
app.use(express.static(__dirname + "/public"));
console.log(__dirname + "/public")
router.route('/')
.get(mainController.getMain);
router.route('/create')
.get(mainController.getCreate)
.post(mainController.createDebate);
router.route('/join')
.get(mainController.getJoin);
router.route('/view')
.get(mainController.getView);
router.route('/viewDebate/:id')
.get(mainController.viewDebate);
router.route('/debateScreen/:id/:key')
.get(mainController.getDebateScreen);
router.route('/joinDebate/:id')
.post(mainController.joinDebate);
// Register all our routes with /api
app.use('/', router);
var port = process.env.PORT || 3000;
// Start the server
app.listen(port);