forked from likhter/planningpoker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
188 lines (157 loc) · 5.83 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
import { Server } from 'socket.io';
import { Server as StaticServer } from 'node-static';
import { createServer } from 'http';
const PORT = process.env.PORT || 3000;
const file = new StaticServer('./pub'),
server = createServer(function(req, res) {
req.addListener('end', function() {
file.serve(req, res);
}).resume();
}).listen(PORT, () => { console.log(`Listening on ${PORT}`)});
const io = new Server(server);
const socketDataFieldName = '_data';
// @TODO pull utilities into separate file
function sendError(socket, msg, data = {}) {
socket.emit('server_error', {
msg: msg,
...data
});
}
function setData(socket, data) {
if (!socket[socketDataFieldName]) {
socket[socketDataFieldName] = {};
}
for (const key in data) {
socket[socketDataFieldName][key] = data[key];
}
}
function getData(socket, fields) {
const ret = {};
if (typeof socket[socketDataFieldName] === 'undefined') {
return undefined;
}
if (! (fields instanceof Array)) {
fields = [fields];
}
fields.forEach(function(fieldName) {
ret[fieldName] = socket[socketDataFieldName][fieldName];
});
return ret;
}
function isUsernameValid(username) {
return /^[a-z0-9_\.-]+$/i.test(username);
}
function generateRoomName() {
return Math.floor(Math.random() * 9000 + 1000).toString(36);
}
function isAuthorized(socket) {
return socket[socketDataFieldName] ? !!socket[socketDataFieldName].name : false;
}
function roomExists(roomName) {
console.log('Available Rooms (incl private lobbies) =', io.sockets.adapter.rooms.keys());
if (roomName == '') {
return false; // no access to empty room
}
return !!io.sockets.adapter.rooms.get(roomName);
}
function isVoteValid(vote) {
return [0, 0.5, 1, 2, 3, 5, 5, 5, 5, 5, 8, 13].indexOf(vote) != -1;
}
function isUserIn(socket, roomName) {
return Array.from(io.sockets.adapter.rooms.get(roomName).keys()).indexOf(socket.id) != -1;
}
io.sockets.on('connection', function(socket) {
console.log(socket.id, ': CONNECT new socket');
socket.on('set_name', function(data) {
if (isAuthorized(socket)) {
return sendError(socket, 'Already authorized');
}
if (!data) { return; }
if (!data.name || !isUsernameValid(data.name)) {
return sendError(socket, 'Username is empty or invalid');
}
setData(socket, { name: data.name, id: socket.id });
socket.emit('hello', { id: socket.id });
});
socket.on('join', async function(data) {
console.log(socket.id, ': JOIN', data || 'new room');
if (!isAuthorized(socket)) {
return sendError(socket, 'Not authorized');
}
let roomName;
if ( data && (roomName = data.roomName) ) {
if (!roomExists(roomName)) {
console.log(socket.id, ': JOIN failed, room', roomName, 'does not exist');
return sendError(socket, 'Room with such name does not exist', { roomName });
}
} else {
roomName = generateRoomName();
}
setData(socket, { vote: undefined });
// @TODO: check if this user is already inside
await socket.join(roomName);
socket.emit('welcome', {
roomName: roomName,
users: Array.from(io.sockets.adapter.rooms.get(roomName).keys()).map(function(s) {
// use the key to get the data from a specific socket (user)
return getData(io.sockets.sockets.get(s), ['id', 'name', 'vote' ]);
})
});
io.sockets.in(roomName).emit('joined', getData(socket, ['id', 'name', 'vote']));
});
socket.on('vote', function(data) {
if (!isAuthorized(socket)) {
return sendError(socket, 'Not authorized');
}
if (!data) { return; }
const roomName = data.roomName,
vote = data.vote;
if (!isUserIn(socket, roomName)) {
return sendError(socket, 'You are not in this room');
}
if (!isVoteValid(vote)) {
return sendError(socket, 'Your vote is invalid');
}
setData(socket, {vote:vote});
const voteData = getData(socket, ['id']);
voteData.vote = vote;
io.sockets.in(roomName).emit('user_voted', voteData);
});
socket.on('reset', function(data) {
if (!isAuthorized(socket)) {
return sendError(socket, 'Not authorized');
}
if (!data) { return; }
const roomName = data.roomName;
if (!isUserIn(socket, roomName)) {
return sendError(socket, `You are not in room '${roomName}'`);
}
setData(socket, { vote: undefined, isRevealed: false });
io.sockets.in(roomName).emit('reset', getData(socket, ['id']));
});
socket.on('reveal', function(data) {
if (!isAuthorized(socket)) {
return sendError(socket, 'Not authorized');
}
if (!data) { return; }
const roomName = data.roomName;
if (!isUserIn(socket, roomName)) {
return sendError(socket, `You are not in room '${roomName}'`);
}
setData(socket, { isRevealed: true });
io.sockets.in(roomName).emit('reveal', getData(socket, ['id']));
});
socket.on('disconnecting', function() {
if (!isAuthorized(socket)) {
return; // no reason to send error message here;
}
io.sockets.adapter.rooms.forEach(function(value, key) {
// find all rooms where client was ...
if (io.sockets.adapter.rooms.get(key).has(socket.id)) {
// ...& quit the room (emit to client to clean up the UI)
console.log(socket.id, ': DISCONNECT leaving from room', key);
io.sockets.in(key).emit('quit', getData(socket, ['id']));
}
})
});
});