forked from Eluch/Soccribe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
330 lines (301 loc) · 10.8 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
let WebSocketServer = require('websocket').server;
let http = require('http');
let fs = require('fs');
let nStatic = require('node-static');
let nf = require('./named_functions');
const uuid = require('uuid/v4');
let hash = 'no-hash-found';
try {
hash = fs.readFileSync('hash', 'utf8').trim();
} catch (e) {
hash = uuid();
}
const SERVER_PORT = 80;
const SERVER_UUID = hash;
const DEBUG_MODE = !!+process.env.DEBUG_MODE;
const PATH_LAST_CHOSEN_PLAYERS = './storage/lastChosenPlayers.json';
let fileServer = new nStatic.Server('./public');
let server = http.createServer(function (request, response) {
let filePath = '.' + request.url;
if (filePath === './') {
filePath = './public/index.html';
fs.readFile(filePath, function (error, content) {
if (error) {
response.writeHead(500);
response.end();
} else {
let versionedContent = content.toString().replace(/\[SERVER-UUID]/g, SERVER_UUID);
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(versionedContent, 'utf-8');
}
});
} else {
if (request.url === '/favicon.ico') request.url = '/favicons/favicon-0.ico';
fileServer.serve(request, response);
}
});
server.listen(SERVER_PORT, function () {
console.log('Server listening on port: ' + SERVER_PORT);
});
let wsServer = new WebSocketServer({
httpServer: server
});
let clients = {};
let subscribers = [];
let lastChosenPlayers = null;
const GAME_PREP_COUNTDOWN_NAME = 'before-game';
const GAME_PREP_COUNTDOWN_SEC_BEFORE_START = 10;
let gamePrepSeconds = 0;
const TEN_ALERT_COUNTDOWN_NAME = 'ten-alert-countdown';
const TEN_ALERT_TIMEOUT_SECONDS = +process.env.TEN_ALERT_TIMEOUT_SECONDS || 300;
let tenAlertTimeLeft = 0;
// loading lastChosenPlayers from file
if (fs.existsSync(PATH_LAST_CHOSEN_PLAYERS)) {
let contents = fs.readFileSync(PATH_LAST_CHOSEN_PLAYERS, 'utf8');
lastChosenPlayers = JSON.parse(contents);
}
function sendToAll(obj) {
let msg = JSON.stringify(obj);
for (let key in clients) {
if (!clients.hasOwnProperty(key)) continue;
let client = clients[key];
client.connection.sendUTF(msg);
}
}
function sendToConnection(connection, obj) {
connection.sendUTF(JSON.stringify(obj));
}
function shuffleArray(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
wsServer.on('request', function (request) {
let connection = request.accept(null, request.origin);
connection.uuid = uuid();
clients[connection.uuid] = {
connection: connection,
name: null,
gameType: 0
};
console.log('Client connected with uuid: ' + connection.uuid + ' (' + connection.remoteAddress + ')');
for (let key in clients) {
if (!clients.hasOwnProperty(key)) continue;
if (key === connection.uuid) continue;
if (clients[key].name === null) continue;
let client = clients[key];
sendToConnection(connection, {
pid: 'player-connected',
uuid: client.connection.uuid,
name: client.name,
gameType: client.gameType
});
}
for (let i = 0; i < subscribers.length; i++) {
let client = clients[subscribers[i]];
sendToConnection(connection, {
pid: 'player-subscribed',
uuid: client.connection.uuid,
name: client.name,
gameType: client.gameType,
amount: subscribers.length
});
}
if (lastChosenPlayers !== null) {
sendToConnection(connection, lastChosenPlayers);
}
function handleName(connection, obj) {
let client = clients[connection.uuid];
let clientIsSubscribed = subscribers.indexOf(connection.uuid) !== -1;
let oldGameType = client.gameType;
let newGameType = +obj.gameType;
if (client.name === null) {
sendToConnection(connection, {
pid: 'player-accepted',
tenAlertAvailable: tenAlertTimeLeft === 0
});
}
if (obj.name.length > 10) obj.name = obj.name.substr(0, 10);
client.name = obj.name;
client.gameType = newGameType;
sendToAll({
pid: 'player-connected',
uuid: connection.uuid,
name: obj.name,
gameType: +obj.gameType
});
if (clientIsSubscribed && oldGameType !== newGameType) triggerGamePrepCountdown();
}
function triggerGamePrepCountdown() {
nf.clearNamedInterval(GAME_PREP_COUNTDOWN_NAME);
let selectedSubscribers = getSubscribersWeightedByGameType();
if (selectedSubscribers.length < 4) {
sendToAll({
pid: 'game-countdown',
sec: -1,
});
return;
}
gamePrepSeconds = GAME_PREP_COUNTDOWN_SEC_BEFORE_START;
sendToAll({
pid: 'game-countdown',
sec: gamePrepSeconds,
});
nf.setNamedInterval(GAME_PREP_COUNTDOWN_NAME, function () {
gamePrepSeconds--;
sendToAll({
pid: 'game-countdown',
sec: gamePrepSeconds,
});
if (gamePrepSeconds === 0) {
nf.clearNamedInterval(GAME_PREP_COUNTDOWN_NAME);
scrambleSubscribersAndStartGame();
}
}, 1000);
}
function getSubscribersWeightedByGameType() {
let categories = {};
let player;
for (let i = 0; i < subscribers.length; i++) {
player = clients[subscribers[i]];
if (categories[player.gameType] === undefined) {
categories[player.gameType] = [];
}
categories[player.gameType].push(subscribers[i]);
}
for (let key in categories) {
if (!categories.hasOwnProperty(key)) continue;
if (categories[key].length >= 4) return categories[key];
}
return [];
}
function handleSubscribe(connection) {
if (clients[connection.uuid].name === null) return;
if (!DEBUG_MODE && subscribers.indexOf(connection.uuid) !== -1) return;
subscribers.push(connection.uuid);
sendToConnection(connection, {
pid: 'subscribe-accepted'
});
sendToAll({
pid: 'player-subscribed',
uuid: connection.uuid,
name: clients[connection.uuid].name,
gameType: clients[connection.uuid].gameType,
amount: subscribers.length
});
triggerGamePrepCountdown();
}
function scrambleSubscribersAndStartGame() {
// GAME START
let selectedSubscribers = getSubscribersWeightedByGameType();
shuffleArray(selectedSubscribers);
if (selectedSubscribers.length % 2 === 1) selectedSubscribers.splice(-1, 1);
let names = [];
let names_str;
let i;
for (i = 0; i < selectedSubscribers.length; i++) {
names.push(clients[selectedSubscribers[i]].name);
}
if (names.length >= 4) {
names_str = `Red: ${names[0]}, ${names[1]}\nBlue: ${names[2]}, ${names[3]}`;
for (i = 5; i < selectedSubscribers.length; i += 2) {
names_str += `\nChallenger: ${names[i - 1]}, ${names[i]}`;
}
} else {
names_str = names.join(', ');
}
console.log('Game started: ' + names_str.replace(/\n/gm, '; '));
for (i = 0; i < selectedSubscribers.length; i++) {
sendToConnection(clients[selectedSubscribers[i]].connection, {
pid: 'game',
notification: names_str
});
}
sendToAll(lastChosenPlayers = {
pid: 'chosen-players',
date: new Date().getTime(),
gameType: clients[selectedSubscribers[0]].gameType,
names: names
});
sendToAll({pid: 'unsubscribe-all'});
for (i = 0; i < subscribers.length; i++) {
if (selectedSubscribers.indexOf(subscribers[i]) === -1) {
sendToConnection(clients[subscribers[i]].connection, {pid: 'unsubscribe-accepted'});
}
}
subscribers = [];
fs.writeFile(PATH_LAST_CHOSEN_PLAYERS, JSON.stringify(lastChosenPlayers), err => {
if (err) console.error(err);
});
}
function handleUnsubscribe(connection) {
let index = subscribers.indexOf(connection.uuid);
if (index === -1) return;
subscribers.splice(index, 1);
sendToConnection(connection, {
pid: 'unsubscribe-accepted'
});
sendToAll({
pid: 'player-unsubscribed',
uuid: connection.uuid,
amount: subscribers.length
});
triggerGamePrepCountdown();
}
function handleGetServerUUID(connection) {
sendToConnection(connection, {
pid: 'server-uuid',
debug: DEBUG_MODE,
uuid: SERVER_UUID
});
}
function handleTenAlert() {
if (tenAlertTimeLeft > 0) return;
sendToAll({pid: 'ten-alert-sound'});
tenAlertTimeLeft = TEN_ALERT_TIMEOUT_SECONDS;
nf.setNamedInterval(TEN_ALERT_COUNTDOWN_NAME, function () {
tenAlertTimeLeft--;
if (tenAlertTimeLeft === 0) {
nf.clearNamedInterval(TEN_ALERT_COUNTDOWN_NAME);
sendToAll({pid: 'ten-alert-available'});
}
}, 1000);
}
connection.on('message', function (message) {
if (message.type === 'utf8') {
try {
let obj = JSON.parse(message.utf8Data);
if (obj.pid === undefined) return;
switch (obj.pid) {
case 'name':
return handleName(connection, obj);
case 'subscribe':
return handleSubscribe(connection);
case 'unsubscribe':
return handleUnsubscribe(connection);
case 'get-server-uuid':
return handleGetServerUUID(connection);
case 'ten-alert':
return handleTenAlert();
default:
console.error(`Unhandled pid: ${obj.pid}`);
}
} catch (e) {
console.error(e);
}
}
});
connection.on('close', function () {
// close user connection
handleUnsubscribe(connection);
delete clients[connection.uuid];
console.log('Client disconnected with uuid: ' + connection.uuid);
sendToAll({
pid: 'player-disconnected',
uuid: connection.uuid,
amount: subscribers.length
});
});
});