-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.js
executable file
·303 lines (247 loc) · 7.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* Module dependencies.
*/
var express = require('express');
var path = require('path');
var expressValidator = require('express-validator');
var uuid = require('uuid');
var ent = require('ent');
/**
* Load controllers.
*/
var homeController = require('./controllers/home');
/**
* List of random names
*/
var randomNames = require('./config/names');
/**
* Create Express server & Socket.IO
*/
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
/**
* Express configuration.
*/
var hour = 3600000;
var day = (hour * 24);
var week = (day * 7);
var month = (day * 30);
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(require('connect-assets')({
src: 'public',
helperContext: app.locals
}));
app.use(express.compress());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.json());
app.use(express.urlencoded());
app.use(expressValidator());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public'), { maxAge: week }));
app.use(function(req, res) {
res.status(404);
res.render('404');
});
app.use(express.errorHandler());
/**
* Application routes.
*/
app.get('/', homeController.index);
app.get('/room/:room', homeController.room);
/**
* Start Express server.
*/
server.listen(app.get('port'), function() {
console.log("✔ Express server listening on port %d in %s mode", app.get('port'), app.settings.env);
});
/**
* Socket.IO
* ________________________
*/
io.configure(function() {
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
var people = {};
var rooms = {};
//var currentUserStory = {};
io.sockets.on('connection', function(socket) {
// We stock socket's id in the people array with "user" as it's name
people[socket.id] = {"name" : randomNames.names[Math.floor(Math.random() * 49) + 1].name, "room" : undefined};
/**
* Newly connected client
*/
//console.log(socket.id+' : Socket connected');
socket.on('room',function(data){
var peopleInRoom = {};
var room = ent.encode(data.room);
// Join room
socket.join(room);
// Set the room of the current client
people[socket.id].room = room;
// Check if room id defined
if (rooms[room] === undefined){
// If not define it and set it's userStory to undefined
rooms[room] = {"name" : room, "currentUserStory" : undefined, "cardsRevealed" : false, "lastMessages" : []};
}
// Check if client already has a name
if (data.name !== undefined){
data.name = ent.encode(data.name.trim());
if(data.name != ''){
people[socket.id].name = data.name;
}
}
// Display last messages sent
rooms[room].lastMessages.forEach(function(data){
if(data.author == people[socket.id].name){
socket.emit('message', {msg: data.msg, author : null, me : true, server : true});
}else{
socket.emit('message', {msg: data.msg, author: data.author, me : false, server : true});
}
});
// Show for each room who is online in it
io.sockets.clients(room).forEach(function (socket) {
peopleInRoom[socket.id] = people[socket.id];
});
// Send the list of participants to newly connected socket
socket.emit('participants', {people: peopleInRoom, id: socket.id});
// Send the current User Story if one is already here
if (rooms[room].currentUserStory != undefined) {
socket.emit('newUserStory', rooms[room].currentUserStory);
}
// Then broadcast the array in order to list all participants in main.js
socket.broadcast.to(room).emit('participants', {people: peopleInRoom, connect: people[socket.id].name});
});
/**
* Client changes his name
*/
socket.on('newName',function(data){
// Check name (not empty, not full of spaces, no XSS)
newName = ent.encode(data.newName.trim());
if(newName != ''){
people[socket.id].name = newName;
io.sockets.in(data.room).emit('participants', {people: people});
}
});
/**
* Client chooses his card
*/
socket.on('cardSelected',function(data){
// Only change card if cards are not revealed yet
if(rooms[data.room].cardsRevealed === false){
var peopleInRoom = {};
people[socket.id].card = ent.encode(data.card);
io.sockets.clients(data.room).forEach(function (socket) {
peopleInRoom[socket.id] = people[socket.id];
});
io.sockets.in(data.room).emit('cardSelected', peopleInRoom);
}
});
/**
* Client changes User Story
*/
socket.on('newUserStory', function(data){
// If the user story is blank, set it to 'User story'
if(data.userStory == ''){
data.userStory = 'User story';
}
rooms[data.room].currentUserStory = ent.encode(data.userStory.trim());
io.sockets.in(data.room).emit('newUserStory', rooms[data.room].currentUserStory);
});
/**
* Reveal cards to all clients
*/
socket.on('revealCards', function(data){
// Only reveal cards if all players chose their's
if(checkCards(data.room) === true){
io.sockets.in(data.room).emit('revealCards');
rooms[data.room].cardsRevealed = true;
}
});
/**
* Play Again
*/
socket.on('playAgain', function(data){
// Only play again if all players chose cards and the cards are revealed
if(checkCards(data.room) === true && rooms[data.room].cardsRevealed === true){
var peopleInRoom = {};
// Set all cards to undefined
io.sockets.clients(data.room).forEach(function (socket) {
people[socket.id].card = undefined;
peopleInRoom[socket.id] = people[socket.id];
});
rooms[data.room].cardsRevealed = false;
io.sockets.in(data.room).emit('playAgain', peopleInRoom);
}
});
/**
* Send message
*/
socket.on('message', function(data){
var msg = ent.encode(data.msg).trim();
// Save last 10 message on server
var message = {msg: msg, author : people[socket.id].name};
if(rooms[data.room].lastMessages.length < 10){
rooms[data.room].lastMessages.push(message);
}else{
rooms[data.room].lastMessages.shift();
rooms[data.room].lastMessages.push(message);
}
// Only send message if not empty
if(msg != ''){
socket.broadcast.to(data.room).emit('message', {msg: msg, author: people[socket.id].name});
}
});
/**
* Client disconnects
*/
// If someones disconnects
socket.on('disconnect', function() {
var user = people[socket.id].name;
var room = people[socket.id].room;
var peopleInRoom = {};
// Delete it's reference in the people array
delete people[socket.id];
io.sockets.clients(room).forEach(function (socket) {
peopleInRoom[socket.id] = people[socket.id];
});
// Then broadcast that someone disconnected, with the remaining participants
socket.broadcast.to(room).emit('participants', {people: peopleInRoom, disconnect: user});
console.log(socket.id+' : Socket disconnected');
});
});
/**
* Functions
* ________________________
*/
function checkCards(room){
var i = 0;
var cards = 0;
io.sockets.clients(room).forEach(function (socket) {
if (people[socket.id].card !== undefined){
cards++;
}
i++;
});
if (i == cards){
return true;
}else{
return false;
}
}