-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
423 lines (372 loc) · 12 KB
/
index.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
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
var express = require("express");
var app = express();
var http = require("http").createServer(app);
var io = require("socket.io")(http);
const { v4: uuidv4 } = require("uuid");
var games = [];
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/lobby", (req, res) => {
res.sendFile(__dirname + "/lobby.html");
});
function joinGame(socket, game, name) {
game.sockets.push(socket);
socket.join(game.gameID, () => {
socket.gameID = game.gameID;
socket.name = name;
console.log(
name + " with socket ID " + socket.id + " joined " + game.gameID
);
});
}
function getPlayersInGame(game) {
names = [];
for (let i = 0; i < game.sockets.length; i++) {
names.push(game.sockets[i].name);
}
return names;
}
function sendToAllPlayersInGame(game, msg, msgType) {
for (let i = 0; i < game.sockets.length; i++) {
let socket = game.sockets[i];
socket.emit(msgType, msg);
}
}
function sendToGameOwner(game, msg, msgType) {
game.owner.emit(msgType, msg);
}
function sendToNthPlayer(game, n, msg, msgType) {
game.sockets[n].emit(msgType, msg);
}
function getRandom(arr, n) {
var result = new Array(n),
len = arr.length,
taken = new Array(len);
if (n > len)
throw new RangeError("getRandom: more elements taken than available");
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
}
// gets most frequent elements in arr, including ties
function getMostFrequent(arr) {
var result = [];
var tmp = {};
// count the amount of each
for (let i = 0; i < arr.length; i++) {
let panelNum = arr[i];
if (panelNum.toString(10) in tmp) {
tmp[arr[i]]++;
} else {
tmp[arr[i]] = 1;
}
}
var max = Object.keys(tmp).reduce((a, b) => (tmp[a] > tmp[b] ? a : b));
for (var i in tmp) {
if (tmp[i] === tmp[max]) {
result.push(i);
}
}
return result;
}
Array.prototype.remove = function () {
var what,
a = arguments,
L = a.length,
ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
// welcome screen
io.on("connection", (socket) => {
// when someone disconnects, remove them from the game they are in and update lobby
socket.on("disconnect", () => {
console.log("Disconnection from " + socket.name + " with ID " + socket.id);
if (socket.gameID in games && games[socket.gameID].sockets.length !== 0) {
games[socket.gameID].sockets.remove(socket);
playerNames = getPlayersInGame(games[socket.gameID]);
sendToAllPlayersInGame(games[socket.gameID], playerNames, "lobby update");
if (games[socket.gameID].sockets.length === 0) {
// remove the game
delete games[socket.gameID];
} else if (games[socket.gameID].sockets.length % 2 !== 0) {
console.log("not ready to start");
sendToGameOwner(
games[socket.gameID],
"not ready to start",
"not ready to start"
);
} else {
sendToGameOwner(
games[socket.gameID],
"ready to start",
"ready to start"
);
}
}
});
// handle start game request
socket.on("start request", (data) => {
cards = [];
for (let i = 1; i <= 40; i++) {
cards.push(i);
}
const game = {
gameID: uuidv4(),
name: "game" + parseInt(games.length + 1),
owner: socket,
sockets: [],
started: false,
currentPlayer: 0,
deck: cards,
storyText: "<br>",
cardsOnBoard: [],
votes: {},
};
games[game.gameID] = game;
joinGame(socket, game, data.name);
// emit confirmation back to client
socket.emit("start success", { gameID: game.gameID });
});
// handle join game request
socket.on("join request", (data) => {
var gameID = data.gameID;
if (gameID in games) {
if (games[gameID].sockets.length < 4 && games[gameID].started === false) {
const game = games[gameID];
joinGame(socket, game, data.name);
// emit confirmation back to client
socket.emit("join success", {
gameID: data.gameID,
});
} else if (games[gameID].sockets.length === 4) {
socket.emit(
"join failure",
"Sorry, the game you're trying to join is full."
);
} else if (games[gameID].started === true) {
socket.emit(
"join failure",
"Sorry, the game you are trying to join has already started."
);
}
} else {
// emit no game with that gameID exists back to client
socket.emit("join failure", "Sorry, that game does not exist.");
}
});
// send updated player list to people in the lobby
socket.on("entered lobby", (data) => {
// let everyone in the game room know about the updated list
playerNames = getPlayersInGame(games[data.gameID]);
sendToAllPlayersInGame(games[socket.gameID], playerNames, "lobby update");
// if game has 4 players, give the owner the option to start the game
if (games[socket.gameID].sockets.length % 2 === 0) {
console.log("Game with ID " + socket.gameID + " ready to start");
sendToGameOwner(games[socket.gameID], "ready to start", "ready to start");
} else if (games[socket.gameID].sockets.length % 2 !== 0) {
console.log("Game with ID " + socket.gameID + " not ready to start");
sendToGameOwner(
games[socket.gameID],
"not ready to start",
"not ready to start"
);
}
});
// start a game
socket.on("start game", (data) => {
games[socket.gameID].started = true;
var deal = getRandom(games[socket.gameID].deck, 16);
games[socket.gameID].deck = games[socket.gameID].deck.filter(
(value) => !deal.includes(value)
);
for (let i = 0; i < games[socket.gameID].sockets.length; i++) {
startingCards = deal.slice(4 * i, 4 * (i + 1));
sendToNthPlayer(
games[socket.gameID],
i,
{
startingCards: startingCards,
currentPlayerName: games[socket.gameID].sockets[0].name,
playerNumber: i,
},
"game started"
);
}
sendToNthPlayer(
games[socket.gameID],
games[socket.gameID].currentPlayer,
"your turn",
"your turn"
);
});
// handle player submitting their turn
socket.on("submit turn", (data) => {
var name = data.name;
var cardNum = data.cardNum;
var text = data.text;
console.log(
name +
" submitted their turn with card number " +
cardNum +
" and text '" +
text +
"'"
);
// zero-based
var classNumForHighlights =
"cardMatch" + games[socket.gameID].cardsOnBoard.length;
// update game data (text, cards on board, currentPlayer)
games[socket.gameID].storyText +=
"<span class='voteText color" +
games[socket.gameID].currentPlayer +
" " +
classNumForHighlights +
"'>" +
text +
"</span> ";
games[socket.gameID].cardsOnBoard.push(parseInt(cardNum));
games[socket.gameID].currentPlayer =
(games[socket.gameID].currentPlayer + 1) %
games[socket.gameID].sockets.length;
// deal a new random card to the person who just submitted their turn
var newCard = getRandom(games[socket.gameID].deck, 1);
games[socket.gameID].deck.remove(parseInt(newCard));
socket.emit("dealt card", {
oldCard: cardNum,
newCard: newCard,
});
// TMP: FORCES GAME TO END AFTER FIRST TURN (TODO: REMOVE AFTER) ----------------------------------------------------------------------------------------------------
// while (games[socket.gameID].cardsOnBoard.length < 16) {
// games[socket.gameID].cardsOnBoard.push(1);
// }
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------
// update everyone in the game about the updated cards on board and game text
sendToAllPlayersInGame(
games[socket.gameID],
{
cardsOnBoard: games[socket.gameID].cardsOnBoard,
text: games[socket.gameID].storyText,
currentPlayerName:
games[socket.gameID].sockets[games[socket.gameID].currentPlayer].name,
},
"update board"
);
// signal the next player that it is their turn
sendToNthPlayer(
games[socket.gameID],
games[socket.gameID].currentPlayer,
"your turn",
"your turn"
);
});
// signal all players in game to move on to the voting section
socket.on("owner clicked proceed to voting", (data) => {
sendToAllPlayersInGame(
games[socket.gameID],
"proceed to voting",
"proceed to voting"
);
});
socket.on("vote", (data) => {
var playerNumber = games[socket.gameID].sockets.findIndex(
(element) => element === socket
);
console.log("Player # " + playerNumber + " just cast their vote.");
sendToAllPlayersInGame(
games[socket.gameID],
{
category: data.category,
panelNum: data.panelNum,
playerNumber: playerNumber,
},
"vote update"
);
// store the vote as well
if (!(socket.id in games[socket.gameID].votes)) {
games[socket.gameID].votes[socket.id] = [
{
panelNum: data.panelNum,
category: data.category,
voterName: socket.name,
},
];
} else {
games[socket.gameID].votes[socket.id].push({
panelNum: data.panelNum,
category: data.category,
voterName: socket.name,
});
}
var numPlayersSubmittedVotes = Object.keys(games[socket.gameID].votes)
.length;
// if we have votes from all players
if (numPlayersSubmittedVotes === games[socket.gameID].sockets.length) {
// if the last vote was just submitted, redirect all players to a summary screen
var lastVoteSubmitted = true;
for (var id in games[socket.gameID].votes) {
if (games[socket.gameID].votes[id].length !== 3) {
lastVoteSubmitted = false;
break;
}
}
if (lastVoteSubmitted === true) {
console.log("All votes submitted!");
// calculate the voting statistics to display in the summary screen
var allFunniestVotes = [];
var allBestSaveVotes = [];
var allPivotalVotes = [];
for (var id in games[socket.gameID].votes) {
for (let i = 0; i < 3; i++) {
let category = games[socket.gameID].votes[id][i].category;
let panelNum = games[socket.gameID].votes[id][i].panelNum;
if (category === "funniest") {
allFunniestVotes.push(parseInt(panelNum));
} else if (category === "pivotal") {
allPivotalVotes.push(parseInt(panelNum));
} else if (category === "best save") {
allBestSaveVotes.push(parseInt(panelNum));
}
}
}
// find the top voted panels
var funniestTop = getMostFrequent(allFunniestVotes);
var pivotalTop = getMostFrequent(allPivotalVotes);
var bestSaveTop = getMostFrequent(allBestSaveVotes);
var playerNames = [];
for (let i = 0; i < games[socket.gameID].sockets.length; i++) {
playerNames.push(games[socket.gameID].sockets[i].name);
}
console.log("Funniest winners: " + funniestTop);
console.log("Pivotal winners: " + pivotalTop);
console.log("Best Save winners: " + bestSaveTop);
// wait 500 ms for last player's vote to show up on their screen
setTimeout(function () {
sendToAllPlayersInGame(
games[socket.gameID],
{
funniestTop: funniestTop,
pivotalTop: pivotalTop,
bestSaveTop: bestSaveTop,
playerNames: playerNames,
},
"all votes submitted"
);
}, 500);
}
}
});
});
http.listen(process.env.PORT || 3000, () => {
console.log("listening on *:3000");
});