-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
292 lines (224 loc) · 7.63 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
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
var sticky = require('sticky-session');
var cluster = require('cluster');
var redis = require('redis').createClient();
var port = process.env.PORT || 3000;
if (cluster.isMaster) {
redis.flushdb();
}
sticky(function() {
var crypto = require('crypto');
var express = require('express');
var redisAdapter = require('socket.io-redis');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var redis = require('redis').createClient();
app.use(express.static(__dirname + '/public'));
io.adapter(redisAdapter({ host: 'localhost', port: 6379 }));
io.on('connection', function(socket) {
socket.on('login', function(username) {
if (socket.user) return;
username = username.trim();
if (username.length > 16) return;
socket.user = {
id: uid(),
username: username
};
socket.emit('login', socket.user);
redis.incr('num users', function(err, numUsers) {
io.emit('num users', numUsers);
});
joinLobby(socket);
});
socket.on('message', function(message) {
if (!socket.user) return;
if (!socket.roomId) return;
if (!message) return;
var eventName = 'lobby' === socket.roomId ? 'lobby message' : 'message';
socket.broadcast.to(socket.roomId).emit(eventName, socket.user, message);
});
socket.on('add room', function() {
if (!socket.user) return;
var room = new Room(socket.user);
redis.hsetnx('rooms', room.id, JSON.stringify(room), function(err, result) {
if (err || !result) return;
join(socket, room.id);
});
});
socket.on('join room', function(roomId) {
if (!socket.user) return;
join(socket, roomId);
});
socket.on('leave room', function() {
if (!socket.user) return;
joinLobby(socket);
});
socket.on('start game', function() {
if (!socket.user) return;
redis.hget('rooms', socket.roomId, function(err, data) {
if (err) return;
var room = JSON.parse(data);
// check the game starter by owner
if (!room.users.length || room.users[0].id !== socket.user.id) return;
redis.hdel('rooms', room.id, function(err) {
if (err) return;
io.in(room.id).emit('game started', room);
socket.broadcast.to('lobby').emit('room removed', room.id);
redis.hset('active games', room.id, JSON.stringify(new Room(socket.user)), function(err) {
if (err) return;
// add new active game
setTimeout(function() {
redis.hget('active games', room.id, function(err, data) {
if (err) return;
var newRoom = JSON.parse(data);
if (!newRoom) return;
redis.hgetall('points', function(err, points) {
if (err) return;
var best = newRoom.users.reduce(function(obj, user) {
var p = parseInt(points[user.id], 10);
if (p > obj.max) {
return { user: user, max: p };
}
return obj;
}, { max: -Infinity, user: null });
io.of('/game').emit('winner', best.user);
});
});
}, 180000);
});
});
});
});
socket.on('disconnect', function() {
leave(socket, function() {
if (socket.user) {
delete socket.user;
redis.decr('num users', function(err, numUsers) {
io.emit('num users', numUsers);
});
}
});
});
});
io.of('/game').on('connection', function(socket) {
socket.on('join', function(data) {
socket.user = data.userData;
redis.hget('active games', data.roomData.id, function(err, _data) {
if (err) return;
var room = JSON.parse(_data);
socket.room = room;
socket.join(data.roomData.id);
redis.hset('uid to sid', socket.user.id, socket.id);
room.users.push(socket.user);
redis.hset('active games', data.roomData.id, JSON.stringify(room));
});
});
socket.on('player:sync', function(data) {
if (!socket.user || !socket.room) return;
redis.hset('points', socket.user.id, data.points);
socket.to(socket.room.id).emit('player:sync', { id: socket.user.id, motion: data.motion, health: data.health, points: data.points, username: data.username });
});
socket.on('player:hit', function(playerID) {
redis.hget('uid to sid', playerID, function(err, sid) {
if (err) return;
socket.to(sid).emit('player:hit', { damage: 10 });
});
});
socket.on('disconnect', function() {
socket.to(socket.room.id).emit('player:disconnected', socket.user.id);
redis.hdel('uid to sid', socket.user.id);
redis.hdel('active games', socket.room.id);
socket.leave(socket.room.id);
socket.room = null;
});
});
function Room(user) {
this.id = user.id;
this.name = user.username + '\'s game';
this.users = [];
}
function uid() {
return crypto.randomBytes(16).toString('hex');
}
function rooms(callback) {
redis.hgetall('rooms', function(err, roomMap) {
if (err) return callback(err);
var rooms = Object.keys(roomMap || {}).map(function(roomId) {
return JSON.parse(roomMap[roomId]);
});
callback(null, rooms);
});
}
function joinLobby(socket) {
leave(socket, function(err) {
if (err) return;
socket.join('lobby', function(err) {
if (err) return;
socket.roomId = 'lobby';
rooms(function(err, rooms) {
socket.emit('join lobby', rooms);
});
});
});
}
function join(socket, roomId) {
redis.hget('rooms', roomId, function(err, data) {
if (err) return;
var room = JSON.parse(data);
leave(socket);
socket.join(room.id, function(err) {
if (err) return;
room.users.push(socket.user);
redis.hset('rooms', room.id, JSON.stringify(room), function(err) {
if (err) return;
socket.roomId = room.id;
socket.emit('join room', room);
socket.broadcast.to('lobby').emit('room updated', room);
socket.broadcast.to(room.id).emit('user joined', socket.user);
});
});
});
}
function leave(socket, callback) {
callback = callback || function() {};
var roomId = socket.roomId;
if (!roomId) return callback();
socket.leave(roomId);
socket.roomId = null;
if ('lobby' === roomId) {
socket.emit('leave lobby');
callback()
return;
} else {
socket.emit('leave room');
}
redis.hget('rooms', roomId, function(err, data) {
if (err) return callback(err);
var room = JSON.parse(data);
if (!room) return callback();
var index = -1;
room.users.some(function(user, i, users) {
if (user.id === socket.user.id) {
users.splice(i, 1);
index = i;
return true;
}
});
if (!~index) return callback();
if (index === 0) {
// remove the room when the user is the creator of it
redis.hdel('rooms', room.id);
socket.broadcast.to('lobby').emit('room removed', roomId);
socket.broadcast.to(roomId).emit('room closed');
} else {
redis.hset('rooms', roomId, JSON.stringify(room));
socket.broadcast.to('lobby').emit('room changed', room);
socket.broadcast.to(roomId).emit('user left', socket.user);
}
callback()
});
}
return http;
}).listen(port, function() {
console.log('Server listening on port ' + port);
});