-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
499 lines (455 loc) · 18.6 KB
/
server.js
File metadata and controls
499 lines (455 loc) · 18.6 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// the requirements to connect and create an https websocket server (wss)
const fs = require('fs');
const http = require('http');
const https = require('https');
const privateKey = fs.readFileSync("./security/localhost-key.pem");
const certificate = fs.readFileSync("./security/localhost.pem");
const WebSocket = require("express-ws");
const express = require('express');
const compression = require("compression");
const cors = require("cors");
const app = express();
// allows us to use our modules for packet coding, protocol headers, and utility functions
const protocol = require("./public/json/protocol.json");
const p = require("./serverProtocol");
let sortedWordArray = require("./words").words;
// the player class
class Player {
constructor(socket, name) {
this.socket = socket;
this.name = name;
this.color = "#ff0000";
this.clue = "";
this.spectator = true;
this.waiting = false;
this.guessor = false;
this.host = false;
this.lobby = null;
this.clueFilterer = false;
this.removingOption = false;
}
}
// the class of our Lobby, which will hold players and the current word/state
class Lobby {
static lobbies = [];
constructor(code, hostingPlayer) {
this.code = code;
this.host = hostingPlayer;
this.players = [hostingPlayer];
this.waitingPlayers = [];
this.currentCard = "";
this.cardHistory = [];
this.points = 0;
this.cardsRemaining = 0;
this.cardOptions = [];
this.gamePhase = 0;
this.removingPlayers = [];
this.hints = [];
this.playerIndex = 0;
this.correctGuess = true;
hostingPlayer.socket.talk(p.encodePacket([protocol.client.lobbyJoined, this.code, 1], ["int8", "string", "int8"]));
this.updateState();
console.log(`${hostingPlayer.name} created a new lobby with code ${this.code}. There are ${Lobby.lobbies.length + 1} open lobbies now`);
}
static findCode(code, joiningPlayer) {
for (let i = 0; i < Lobby.lobbies.length; i++) {
if (Lobby.lobbies[i].code !== code) continue;
Lobby.lobbies[i].addPlayer(joiningPlayer);
return true;
}
return false;
}
addPlayer(joiningPlayer) {
this.waitingPlayers.push(joiningPlayer);
joiningPlayer.socket.talk(p.encodePacket([protocol.client.lobbyJoined, this.code, 0], ["int8", "string", "int8"]));
console.log(`${joiningPlayer.name} join the lobby with code ${this.code}`);
this.updateState();
}
kickPlayer(player) {
this.reset();
console.log(`player with name ${player.name} kicked!`);
player.socket.talk(p.encodePacket([protocol.client.kicked], ["int8"]))
this.players.splice(this.players.indexOf(player), 1);
player.lobby = null;
if (this.players.length === 0) {
Lobby.lobbies.splice(Lobby.lobbies.indexOf(this));
console.log(`Lobby with code ${this.code} deleted`);
return;
}
if (player.host) this.players[Math.floor(this.players.length * Math.random())].host = true;
this.reset();
this.updateState();
}
markCorrect() {
if (this.correctGuess) return;
if (this.gamePhase !== 5) return;
this.points++;
this.correctGuess = true;
this.updateState();
}
reset() {
this.gamePhase = 0;
this.currentCard = "";
this.cardOptions = [];
this.hints = [];
this.finalGuess = "";
this.players = this.players.concat(this.waitingPlayers);
this.waitingPlayers = [];
this.correctGuess = true;
for (let player of this.players) {
player.spectator = false;
player.waiting = false;
player.guessor = false;
player.clueFilterer = false;
player.removingOption = false;
player.clue = "";
}
}
startGame() {
this.reset();
if (this.players.length < 2) {
this.updateState();
return;
}
this.gamePhase = 1;
for (let i = 0; i < this.players.length; i++) this.cardOptions.push(sortedWordArray[Math.floor(Math.random() * sortedWordArray.length)]);
this.removingPlayers = [...this.players];
this.playerIndex++;
if (this.playerIndex >= this.players.length) this.playerIndex = 0;
this.guessor = this.removingPlayers.splice(this.playerIndex, 1)[0];
this.guessor.guessor = true;
if (this.playerIndex + 1 < this.players.length) this.players[this.playerIndex + 1].clueFilterer = true;
else this.players[0].clueFilterer = true;
this.removeOption();
}
startWriting() {
if (this.gamePhase !== 1) return;
this.currentCard = this.cardOptions[0];
this.gamePhase = 2;
for (let player of this.players) {
if (player.guessor) continue;
player.waiting = true;
}
console.log("writing phase")
this.updateState();
}
startRemoval() {
console.log("removal phase");
if (this.gamePhase !== 2) return;
this.gamePhase = 3;
this.hints = [];
for (let player of this.players) {
if (player.guessor) continue;
let addWord = true;
for (let i = 0; i < this.hints.length; i += 2) {
if (this.hints[i].substring(1) == player.clue.toLowerCase()) {
this.hints[i] = "!" + this.hints[i].substring(1);
addWord = false;
}
}
this.hints.push((addWord ? "-" : "!") + player.clue.toLowerCase(), player.name);
}
console.log(`sending out ${JSON.stringify(this.hints)}`);
this.updateState();
}
startGuessing() {
console.log("guessing phase");
if (this.gamePhase !== 3) return;
this.gamePhase = 4;
for (let i = this.hints.length - 1; i >= 0; i -= 2) {
if (this.hints[i - 1][0] === "!") this.hints[i - 1] = `-X${this.hints[i - 1].substring(1)}`;
}
console.log(this.hints);
this.updateState();
}
removeOption(removedWord) {
if (this.gamePhase !== 1) return;
console.log(`remaining options: ${this.cardOptions}`)
if (removedWord !== undefined) {
if (this.cardOptions.indexOf(removedWord) !== -1) this.cardOptions.splice(this.cardOptions.indexOf(removedWord), 1);
else {
console.log(`Attempted to remove a word (${removedWord}) which could not be found.`);
console.log("the game has been sent to a waiting state.");
this.reset();
this.updateState();
return;
}
}
if (this.cardOptions.length <= 1) {
this.startWriting();
return;
}
if (this.removingPlayers.length <= 0) {
console.log("expended all players, yet words remain, so something went wrong.");
console.log("the game has been sent to a waiting state.");
this.reset();
this.updateState();
return;
}
let remover = this.removingPlayers.splice(Math.floor(this.removingPlayers.length * Math.random()), 1)[0];
for (let i of this.players) i.removingOption = false;
remover.removingOption = true;
console.log(`${remover.name} is removing a word`)
let data = [protocol.client.removeCard];
data.push(this.cardOptions.length);
for (let word of this.cardOptions) data.push(word);
data.push(0);
remover.socket.talk(p.encodePacket(data, ["int8", "repeat", "string", "end"]));
this.updateState();
}
submitGuess(guess) {
console.log("guess submitted, the game is over");
this.gamePhase = 5;
if (guess === this.currentCard) {
this.points++;
} else this.correctGuess = false;
this.cardsRemaining++;
this.finalGuess = guess;
this.cardHistory.push(this.currentCard);
this.updateState();
}
submitClue(player, clue, takeback) {
if (this.gamePhase === 5 && player.host) this.startGame();
if (this.gamePhase === 4) this.submitGuess(clue.toLowerCase());
if (this.gamePhase !== 2) return;
for (let i of this.players) {
if (player !== i) continue;
if (takeback) {
i.waiting = true;
i.clue = "";
console.log(`${i.name} unsubmitted their clue`);
} else {
i.waiting = false;
i.clue = clue;
console.log(`${i.name} submitted the clue ${clue}`);
}
}
let stillWaiting = false;
for (let i of this.players) {
if (i.waiting) stillWaiting = true;
}
if (!stillWaiting) this.startRemoval();
else this.updateState();
}
toggleClue(clue) {
if (this.gamePhase !== 3) return;
for (let i = 0; i < this.hints.length; i += 2) {
if (clue !== this.hints[i]) continue;
this.hints[this.hints.indexOf(clue)] = this.hints[i][0] == "!" ? `-${this.hints[i].substring(1)}` : `!${this.hints[i].substring(1)}`;
console.log(`${this.hints[i].substring(1)} has been toggled`);
}
this.updateState();
}
updateState() {
for (let player of this.players.concat(this.waitingPlayers)) {
let data = [protocol.client.update];
data.push(this.players.length + this.waitingPlayers.length);
for (let i of this.players) {
data.push(i.name);
data.push(i.color);
data.push(i.spectator ? 1 : 0);
data.push(i.waiting ? 1 : 0);
data.push(i.guessor ? 1 : 0);
data.push(i.host ? 1 : 0);
data.push(i === player ? 1 : 0);
data.push(i.clueFilterer ? 1 : 0);
data.push(i.removingOption ? 1 : 0);
i.lobby = this;
}
for (let i of this.waitingPlayers) {
data.push(i.name);
data.push(i.color);
data.push(i.spectator ? 1 : 0);
data.push(i.waiting ? 1 : 0);
data.push(i.guessor ? 1 : 0);
data.push(i.host ? 1 : 0);
data.push(i === player ? 1 : 0);
data.push(i.clueFilterer ? 1 : 0);
data.push(i.removingOption ? 1 : 0);
i.lobby = this;
}
data.push(0);
data.push(this.points);
data.push(this.cardsRemaining);
data.push(this.currentCard);
data.push(this.gamePhase);
data.push(this.finalGuess);
data.push(this.cardHistory.length);
for (let card of this.cardHistory) data.push(card);
data.push(0);
data.push(this.hints.length);
for (let clue of this.hints) data.push(clue);
data.push(0);
player.socket.talk(p.encodePacket(
data,
[
"int8", // header
"repeat", "string", "string", "int8", "int8", "int8", "int8", "int8", "int8", "int8", "end", // player data
"int8", "int8", "string", "int8", "string", // lobby data
"repeat", "string", "end", // card history
"repeat", "string", "end", // hints
]
));
}
if (this.gamePhase === 99) {
this.reset();
for (let player of this.players) this.kickPlayer(player);
console.log("game ended, removed all players");
}
}
}
// defines our websocket which connects to clients to share data
const sockets = {
tally: 1,
clients: [],
class: class {
// when a new socket is created increase our socket counrt by 1, and assign it that unique id
constructor(socket, request) {
this.id = sockets.tally++;
this.socket = socket;
this.playerInstance = null;
this.request = request;
this.socket.binaryType = "arraybuffer";
socket.onerror = error => this.error(error);
socket.onclose = reason => this.close(reason);
socket.onmessage = data => this.message(data);
}
// when we recieve an encoded packet from the client, decode and handle it here
message(packet) {
let reader = new DataView(packet.data);
switch (reader.getInt8(0)) {
case protocol.server.createLobby: {
const d = p.decodePacket(reader, ["int8", "string", "string"]);
this.playerInstance = new Player(this, d[2]);
if (Lobby.findCode(d[1], this.playerInstance)) break;
this.playerInstance.host = true;
Lobby.lobbies.push(new Lobby(d[1], this.playerInstance));
break;
}
// starts the game in the applicable lobby
case protocol.server.startGame: {
if (!this.playerInstance) break;
if (!this.playerInstance.host) break;
this.playerInstance.lobby.startGame();
break;
}
// when a player submits their removal choice, we pass it to the next person in line
case protocol.server.cardRemoved: {
if (!this.playerInstance) break;
const d = p.decodePacket(reader, ["int8", "string"]);
this.playerInstance.lobby.removeOption(d[1]);
break;
}
// submitting a clue for the word
case protocol.server.submitClue: {
if (!this.playerInstance) break;
const d = p.decodePacket(reader, ["int8", "string", "int8"]);
this.playerInstance.lobby.submitClue(this.playerInstance, d[1], !!d[2]);
break;
}
// removing a duplicated word
case protocol.server.removeClue: {
if (!this.playerInstance) break;
if (!this.playerInstance.clueFilterer) break;
const d = p.decodePacket(reader, ["int8", "string"]);
this.playerInstance.lobby.toggleClue(d[1]);
break;
}
// begin the guessing phase
case protocol.server.sendClues: {
if (!this.playerInstance) break;
if (!this.playerInstance.clueFilterer) break;
this.playerInstance.lobby.startGuessing();
break;
}
// begin the guessing phase
case protocol.server.markCorrect: {
if (!this.playerInstance) break;
if (!this.playerInstance.host) break;
this.playerInstance.lobby.markCorrect();
break;
}
// kick player
case protocol.server.kickPlayer: {
if (!this.playerInstance) break;
if (!this.playerInstance.host) break;
const d = p.decodePacket(reader, ["int8", "int8"]);
if (!this.playerInstance.lobby.players[d[1]]) break;
this.playerInstance.lobby.players[d[1]].socket.close();
break;
}
// end the game
case protocol.server.endGame: {
if (!this.playerInstance) break;
if (!this.playerInstance.host) break;
if (!this.playerInstance.lobby || this.playerInstance.lobby.gamePhase !== 5) break;
this.playerInstance.lobby.gamePhase = 99;
this.playerInstance.lobby.updateState();
break;
}
// if an unknown packet header is found, log the header for potential troubleshooting
default: {
console.log(`An unknown code has been recieved: ${reader.getInt8(0)}`);
break;
}
}
}
// if the socket is closed or disconnected, find the player and remove it from the lobby
close() {
if (this.playerInstance) {
this.playerInstance.lobby.kickPlayer(this.playerInstance);
}
this.playerInstance = null;
let myIndex = sockets.clients.indexOf(this);
if (myIndex >= 0) sockets.clients.splice(myIndex, 1);
this.socket.close();
}
// sends an encoded binary packet to the client, where the information will be decoded
talk(data) {
if (this.socket.readyState === 1) this.socket.send(data, { binary: true });
}
// in the case of an error, throws the error for troubleshooting
error(error) {
throw error;
}
// kicks a socket, and provides a reason for the kick after closing the port
kick(reason) {
}
},
// whenever a new connection attempt is identified, we log and verify the connection, and create a socket for the player
connect(socket, request) {
console.log(`Socket ${sockets.tally} has connected. Active sockets: ${sockets.clients.length + 1}`);
let connectingSocket = new sockets.class(socket, request);
sockets.clients.push(connectingSocket);
connectingSocket.talk(p.encodePacket([protocol.client.connected], ["int8"]));
}
}
// uses our credentials to create an https server
const credentials = { key: privateKey, cert: certificate };
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "public/index.html");
});
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
WebSocket(app, httpsServer);
app.ws("/wss", sockets.connect);
httpServer.listen(8080);
httpsServer.listen(8443, () => {
console.log("Server running on port 8443")
});
const site = ((port, connect) => {
WebSocket(app);
app.ws("/ws", connect);
app.use(compression());
//app.use(minify());
app.use(cors());
app.use(express.static("public"));
app.use(express.json());
app.listen(port, () => console.log("Express is now active on port %s", port));
return (directory, callback) => app.get(directory, callback);
})(3000, sockets.connect);
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});