-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
507 lines (435 loc) · 15.3 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
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
500
501
502
503
504
505
506
507
const express = require("express");
const app = express();
// const uuid = require("uuid");
// for socket.io:
const server = require("http").Server(app);
const io = require("socket.io")(server, {
origins:
"localhost:8080 http://192.168.0.15:8080:* http://192.168.2.112:8080:* allthatstuff.herokuapp.com:*"
});
// cards:
const cardsEN = require("./cards_enUS.json");
const cardsDE = require("./cards_de.json");
let cards = cardsEN;
let chosenLanguage = "english";
app.use(express.static("./public"));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/public/main.html");
});
server.listen(process.env.PORT || 8080, () =>
console.log("port 8080 listening! - AllThatStuff")
);
// §§ GAME STATE: ********************************
let gameStarted = false;
let joinedPlayers = {}; // { socketId: selectedPieceId, ... }
let selectedPieces = []; // [ pieceId, .... ]
let gameMaster;
let currentPlayer;
// number of rounds, depending on number of players:
let numberOfTurnsForThisGame;
let numberOfTurnsLeft;
// card deck: ----------------------
let stuffCards = [];
let discardPile = [];
let firstCard;
let newPile = false;
// guessing & points: --------------
let correctAnswer;
let guessedAnswers = {}; // { pieceId: <guessed item number> }
let answeringOrder = []; // [ pieceId, ... ]
let playerPointsTotal = {}; // { pieceId: <points> }
let playerNames = {};
let doneBtnPressed = false;
let cardPointsHTML;
let guessingOrDiscussionTime = false;
// active and queued objects:
let activeObjects;
let queuedObjects;
let dataForNextTurn = {};
// TODO: IN THE FUTURE: replace above selectedPieces, guessedAnswers & playerPointsTotal with playersObj:
let playersObj = {};
// playersObj[data.selectedPieceId].name = data.playerName;
// https://hackernoon.com/accessing-nested-objects-in-javascript-f02f1bd6387f
// (playersObj[data.selectedPieceId] || {}).name = data.playerName;
// console.log('playersObj', playersObj);
// players will later be an object like this:
// let playersObj = {
// "pieceId": {
// name: "Bob",
// totalPoints: 8,
// guessedAnswer: 2
// }
// };
// OR.. use a "game state" object for all information:
const gameState = {
players: {}
};
// will later look like this:
// const gameState = {
// players: {
// "socket.id": {
// pieceId: "green",
// name: "Bob",
// totalPonts: 8,
// guessedAnswer: 2
// }
// }
// };
// §§ FUNCTIONS ********************************
function rainbowSort(a, b) {
let rainbow = ['grey', 'purple', 'blue', 'green', 'yellow', 'orange', 'red', 'pink'];
return rainbow.indexOf(a) - rainbow.indexOf(b);
}
//modern version of the Fisher–Yates shuffle algorithm:
function shuffleCards(cards) {
//shuffles array in place
let j, x, i;
for (i = cards.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = cards[i];
cards[i] = cards[j];
cards[j] = x;
}
stuffCards = discardPile;
discardPile = [];
return cards;
}
function discardCard() {
if (newPile === false) {
discardPile.push(firstCard);
}
}
function replaceCard() {
discardCard();
if (stuffCards.length > 0) {
newPile = false;
// drawCard(stuffCards);
firstCard = stuffCards.shift();
} else {
shuffleCards(discardPile);
newPile = true;
}
}
function nextPlayersTurn(data) {
let currentPlayerIndex = selectedPieces.indexOf(data.activePlayer);
let nextPlayer;
if (selectedPieces[currentPlayerIndex + 1]) {
nextPlayer = selectedPieces[currentPlayerIndex + 1];
} else {
nextPlayer = selectedPieces[0];
}
currentPlayer = nextPlayer;
replaceCard();
correctAnswer = randomNumber(1, 7);
guessedAnswers = {};
answeringOrder = [];
activeObjects = data.activeObjects;
queuedObjects = data.queuedObjects;
doneBtnPressed = false;
guessingOrDiscussionTime = false;
io.sockets.emit("next turn", {
activePlayer: data.activePlayer,
nextPlayer: nextPlayer,
activeObjects: data.activeObjects,
queuedObjects: data.queuedObjects,
newCard: firstCard,
correctAnswer: correctAnswer,
numberOfTurnsLeft: numberOfTurnsLeft
});
}
// Function to generate random number, min incl, max excl.
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function collectGuesses(data) {
guessedAnswers[data.guessingPlayer] = data.guessedItem;
answeringOrder.push(data.guessingPlayer);
let guessedAnswersLength = Object.keys(guessedAnswers).length;
let joinedPlayersLength = selectedPieces.length;
io.sockets.emit("someone guessed", {
guessingPlayer: data.guessingPlayer,
guessedItem: data.guessedItem
});
// when everyone guessed: add points:
if (guessedAnswersLength == joinedPlayersLength - 1) {
let playerPointsIfCorrect = {};
let actualPlayerPoints = {};
let numberOfCorrectGuesses = 0;
if (joinedPlayersLength <= 6) {
// points for up to 6 players (5 guessers):
let pointsCounter = answeringOrder.length;
for (let i = 0; i < answeringOrder.length; i++) {
playerPointsIfCorrect[answeringOrder[i]] = pointsCounter;
if (guessedAnswers[answeringOrder[i]] == correctAnswer) {
actualPlayerPoints[answeringOrder[i]] = pointsCounter;
playerPointsTotal[answeringOrder[i]] += pointsCounter;
numberOfCorrectGuesses++;
} else {
actualPlayerPoints[answeringOrder[i]] = 0;
}
pointsCounter--;
}
} else if (joinedPlayersLength > 6) {
// for more than 6 players (max 8): (6-7 guessers):
// maximum points: 5
let pointsCounter = 0;
for (let i = answeringOrder.length; i > 0; i--) {
playerPointsIfCorrect[answeringOrder[i]] = pointsCounter;
if (guessedAnswers[answeringOrder[i]] == correctAnswer) {
actualPlayerPoints[answeringOrder[i]] = pointsCounter;
playerPointsTotal[answeringOrder[i]] += pointsCounter;
numberOfCorrectGuesses++;
} else {
actualPlayerPoints[answeringOrder[i]] = 0;
}
if (pointsCounter < 5) {
pointsCounter++;
}
}
}
// building player gets 1 point for each correct guess:
playerPointsTotal[currentPlayer] += numberOfCorrectGuesses;
dataForNextTurn = {
activePlayer: currentPlayer,
correctAnswer: correctAnswer,
guessedAnswers: guessedAnswers,
playerPointsIfCorrect: playerPointsIfCorrect,
actualPlayerPoints: actualPlayerPoints,
playerPointsTotal: playerPointsTotal
};
io.sockets.emit("everyone guessed", dataForNextTurn);
}
}
function getWinner() {
let ranking = [];
for (let player in playerPointsTotal) {
let name = playerNames[player];
let playerPointsObj = {
player: player,
name: name,
points: playerPointsTotal[player]
};
ranking.push(playerPointsObj);
}
// sort array in place by points, descending:
ranking.sort(function(a, b) {
return b.points - a.points;
});
let winner = ranking[0].player;
console.log(winner, "wins!");
// reset selectedPieces for next game:
selectedPieces = [];
gameStarted = false;
io.sockets.emit("game ends", {
rankingArray: ranking,
winner: winner
});
}
function addPlayerMidGame(data) {
console.log(
`${data.playerName} rejoined the game with the color ${data.selectedPieceId}`
);
io.sockets.emit("add player midgame", {
selectedPieceId: data.selectedPieceId,
playerName: data.playerName,
gameMaster: gameMaster,
activePlayer: currentPlayer,
playerPointsTotal: playerPointsTotal,
numberOfTurnsForThisGame: numberOfTurnsForThisGame,
numberOfTurnsLeft: numberOfTurnsLeft,
firstCard: firstCard,
correctAnswer: correctAnswer,
guessedAnswers: guessedAnswers,
activeObjects: activeObjects,
queuedObjects: queuedObjects,
doneBtnPressed: doneBtnPressed,
cardPointsHTML: cardPointsHTML,
guessingOrDiscussionTime: guessingOrDiscussionTime,
dataForNextTurn: dataForNextTurn
});
}
// §§ SOCKET.IO ********************************
io.on("connection", socket => {
console.log(`socket with the id ${socket.id} is now connected`);
// Generate a v1 (time-based) id:
// socket.userId = uuid.v1();
// tell the player they connected, giving them their socket id and the list with players that joined so far:
socket.emit("welcome", {
// userId: socket.userId,
socketId: socket.id,
selectedPieces: selectedPieces,
playerNames: playerNames,
chosenLanguage: chosenLanguage,
gameStarted: gameStarted,
gameMaster: gameMaster
});
socket.on("selected piece", data => {
if (data.selectedPieceId) {
console.log(
`${data.playerName} joined the game with the color ${data.selectedPieceId}`
);
selectedPieces.push(data.selectedPieceId);
joinedPlayers[socket.id] = data.selectedPieceId;
playerNames[data.selectedPieceId] = data.playerName;
// first player that selects a piece becomes "game master":
if (selectedPieces.length == 1) {
gameMaster = data.selectedPieceId;
}
io.sockets.emit("add selected piece", {
socketId: data.socketId,
selectedPieceId: data.selectedPieceId,
playerName: data.playerName,
gameMaster: gameMaster
});
}
});
socket.on("change language", data => {
if (data.newLanguage == "german") {
cards = cardsDE;
chosenLanguage = "german";
} else if (data.newLanguage == "english") {
cards = cardsEN;
chosenLanguage = "english";
}
io.sockets.emit("language has been changed", data);
});
socket.on("game started", data => {
currentPlayer = data.startPlayer;
// this line makes sure, that selectedPieces (joined players) is in the correct order, like the player pieces are rendered (in a beautiful rainbow order):
// selectedPieces = data.joinedPlayerIds;
selectedPieces.sort(rainbowSort);
console.log("joined players at game start: ", selectedPieces);
// set number of turns:
if (selectedPieces.length == 3 || selectedPieces.length == 5) {
numberOfTurnsLeft = 15;
} else if (selectedPieces.length == 4) {
numberOfTurnsLeft = 12;
} else if (selectedPieces.length == 6) {
numberOfTurnsLeft = 18;
} else if (selectedPieces.length == 7) {
numberOfTurnsLeft = 14;
} else if (selectedPieces.length == 8) {
numberOfTurnsLeft = 16;
}
numberOfTurnsForThisGame = numberOfTurnsLeft;
console.log(
`${selectedPieces.length} players joined the game.
Each player will be the builder ${numberOfTurnsLeft / selectedPieces.length} times!`
);
discardPile = cards;
// discard pile gets shuffled and builds the new stuffCards pile:
shuffleCards(discardPile);
// drawCard(stuffCards);
firstCard = stuffCards.shift();
playerPointsTotal = {};
for (let i = 0; i < selectedPieces.length; i++) {
playerPointsTotal[selectedPieces[i]] = 0;
}
correctAnswer = randomNumber(1, 7);
guessedAnswers = {};
answeringOrder = [];
gameStarted = true;
doneBtnPressed = false;
guessingOrDiscussionTime = false;
let msg = `"${data.startPlayer}" started the game and starts with building!`;
// console.log(msg);
activeObjects = data.activeObjects;
queuedObjects = data.queuedObjects;
io.sockets.emit("game has been started", {
message: msg,
startPlayer: data.startPlayer,
activeObjects: data.activeObjects,
queuedObjects: data.queuedObjects,
firstCard: firstCard,
correctAnswer: correctAnswer,
numberOfTurnsLeft: numberOfTurnsLeft
});
});
socket.on("ready for next turn", () => {
io.sockets.emit("ready for next turn");
});
socket.on("objects for next turn", data => {
numberOfTurnsLeft--;
if (numberOfTurnsLeft == 0) {
getWinner();
} else {
nextPlayersTurn(data);
}
});
socket.on("done building", data => {
activeObjects = data.movedObjects;
let msg = `player "${data.activePlayer}" finished building! Guess what it is!`;
doneBtnPressed = true;
io.sockets.emit("building is done", {
message: msg,
activePlayer: data.activePlayer,
movedObjects: data.movedObjects
});
});
socket.on("moving objects", data => {
activeObjects = data.movedObjects;
io.sockets.emit("objects are moving", {
activePlayer: data.activePlayer,
movedObjects: data.movedObjects
});
});
socket.on("made a guess", data => {
collectGuesses(data);
});
socket.on("end game", () => {
getWinner();
});
socket.on("let me rejoin the game", data => {
console.log('rejoining socketId:', socket.id);
if (data.selectedPieceId && data.playerName) {
selectedPieces.push(data.selectedPieceId);
console.log('selectedPieces after rejoining:', selectedPieces);
// NOTE: depending on the disconnection (intenet failure / page refresh) there might be double entries in the selected pieces array (because they don't get deleted on "disconnect").. so:
// NOTE: update: the selected piece WILL get deleted on internet disconnection, BUT: in this case it happens a long time after the same player rejoined the game already.. how do I solve this problem? the rejoining player shouldn't get deleted..
selectedPieces = [...new Set(selectedPieces)];
console.log('selectedPieces after filtering double entries:', selectedPieces);
// now the player piece order is destroyed.. so after someone disconnects/reconnets, resort in rainbow pattern:
selectedPieces.sort(rainbowSort);
console.log('selectedPieces after rainbowSort:', selectedPieces);
joinedPlayers[socket.id] = data.selectedPieceId;
playerNames[data.selectedPieceId] = data.playerName;
playerPointsTotal[data.selectedPieceId] = data.myTotalPoints;
addPlayerMidGame(data);
}
});
socket.on("guesses backup", data => {
cardPointsHTML = data.cardPointsHTML;
guessingOrDiscussionTime = true;
});
socket.on("disconnect", () => {
console.log(`socket with the id ${socket.id} is now disconnected`);
// NOTE: for some reason, this event only fires, when the browser is refreshed; not if it just lost internet connection? --> seems to be delayed, so players will be removed after disconnecting after they actually rejoined :(
let piece = joinedPlayers[socket.id];
selectedPieces = selectedPieces.filter(item => item !== piece);
if (selectedPieces.length == 0) {
gameStarted = false;
}
if (piece) {
console.log(`player piece "${piece}" is now free again`);
io.sockets.emit("remove selected piece", piece);
delete joinedPlayers[socket.id];
delete playerNames[piece];
delete playerPointsTotal[piece];
// TODO: what happens if disconnected player is the game master?
}
});
// send a message to all connected sockets:
// io.sockets.emit("achtung", {
// warning: "This site will go offline for maintenance in one hour."
// });
// send messages to specific sockets:
// io.sockets.sockets[recipientSocketId].emit("request", {
// message: "You have a new friend request!"
// });
// send a message to every socket except a particular one:
// io.sockets.sockets[recipientSocketId].broadcast.emit("brag", {
// message: "Hey everybody, I just received a new friend request!"
// });
// OR: to everyone except for the emmiting socket:
// socket.broadcast.emit('hi everyone else')
});