-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
568 lines (540 loc) · 25.6 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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const PORT = process.env.PORT || 8282;
app.use(express.static('public')); // Serve Static Assets
//Variables for the app and game start here, above are more environmental stuff
var connectionCounter = 0; //Monitor current connections
var connectionIDs = []; //array of all current connections
var joinedPlayers = {}; //players that have joined with a name, key is sID, with value player name
var readyPlayers = {}; //players that have joined and are ready to start, same format as above
//game related variables:
var gameRunning = false;
const words = [["Stuhl", "Bett"], ["Fahrrad", "Flugzeug"], ["Wassereis", "Kuchen"], ["Brokkoli", "Baum"], ["Marihuana", "Bier"], ["Hund", "Elefant"], ["Bahn", "Reise"]];
let game = {
players: [],
pnames: {},
ppoints: {},
imposters: [],
impostersCount: 0,
round: 0,
playersOrder: [],
usedWordSets: [],
setIndex: 0,
imposterIndex: 0,
talkTime: 0,
voteTime: 0,
canVote: false,
canVoteCorrectnes: false,
r1: {},
c1: {},
impScore1: {},
wronGuesses1: {},
noGuesses1: {},
};
let clientGame = { //this object will be sent to the client to sync game state
"players": ["ID1", "ID2", "ID3", "ID4"], //array with ids of players
"pnames": {}, //copy of readyplayers when startting game
"ppoints": {}, //object with sIds as keys and numbers representing the points of the player
"impostersCount": 1, //how many imposters are in this game?
"round": 1, //round counter, when = player count game ends.
"talkTime": 5, // time for players to talk in seconds
"voteTime": 10, //time for players to vote in seconds
};
io.on('connection', (socket) => {
const sID = socket.id //so that it remembers when disconnecting who is disconnecting
connectionIDs.push(sID); //array of connected user's ids
io.sockets.emit("connectionIDs", connectionIDs); //send the array of all connected ids to all clients
io.sockets.emit("joinPlayersNames", joinedPlayers); //send joinedPlayers to clients
io.sockets.emit("readyPlayers", readyPlayers); //send the player that are ready to the clients
console.log('User ' + sID + " connected.");
connectionCounter += 1; //count that there's a new connection
console.log(connectionCounter + ' connections are active.');
//Test
socket.on("test", (msg) => {
console.log(sID + ' says: ' + msg);
});
//player joins game
socket.on("playerJoin", (pname) => {
if (!gameRunning) {
//check if someone has the same name
var canJoin = true;
for (key in joinedPlayers) { //if there isn't a player with the same name already
if (joinedPlayers[key] == pname) {
canJoin = false;
io.to(sID).emit("gameRunningError", "Jemand hat bereits den gleichen Namen.")
}
}
if (pname == "") {
canJoin = false;
io.to(sID).emit("gameRunningError", "Geben Sie bitte einen Namen ein.")
}
if (canJoin) {
pname = pname.slice(0, 26) //don't allow long names.
//add player name the joinedPlayers object
joinedPlayers[sID] = pname; //add the name of the player to the joinedPlayers object with sID as the key
console.log(`${sID} joined the game as: ${joinedPlayers[sID]}`);
io.sockets.emit("joinPlayersNames", joinedPlayers); //send joinedPlayers to clients
io.to(sID).emit("joinSuccessful", "server: join succesful"); //this triggers the client to show start screen.
};
} else {
io.to(sID).emit("gameRunningError", "Spiel läuft, kann nicht beitreten.") //will trigger an alert on the client
}
});
//player says he is ready
socket.on("playerReady", () => {
if (joinedPlayers[sID]) { //cannot be ready if haven't joined, should always be true
if (!gameRunning) { //can only be ready if the game is not running.
readyPlayers[sID] = joinedPlayers[sID];
io.sockets.emit("readyPlayers", readyPlayers); //send the player that are ready to the clients
//calculate proportion:
var readyCount = Object.keys(readyPlayers).length;
var joinedCount = Object.keys(joinedPlayers).length;
console.log(readyCount + "/" + joinedCount);
if (readyCount >= 3 && readyCount / joinedCount == 1) { //if more then three people are connected and all of them are ready, start the game
startGame();
}
} else {
io.to(sID).emit("gameRunningError", "Spiel läuft.")
}
} else {
io.to(sID).emit("gameRunningError", "Bitte erst beitreten.")
}
//console.log(readyPlayers);
});
//player votes an imposter
socket.on("voteImposter", msg => { //msg is the id of who is being voted
if (gameRunning && game["canVote"] && game["imposters"].indexOf(sID) == -1 && msg != sID && game["players"].indexOf(msg) != -1) {
console.log(`${sID} voted ${msg}`);
if (game[`r${game["round"]}`][sID] == undefined) { //if the votes array in round object have not yet been created
game[`r${game["round"]}`][sID] = [];
};
if (game[`r${game["round"]}`][sID].length >= game["impostersCount"]) { //if the player voted more times than amount of imposters
game[`r${game["round"]}`][sID].push(msg); // register the vote in the game object
game[`r${game["round"]}`][sID].shift(); //remove the earliest vote
} else {
game[`r${game["round"]}`][sID].push(msg); // register the vote in the game object
}
} else {
console.log("invalid vote by " + sID)
}
});
//player evaluates an imposter
socket.on("voteCorrectnes", msg => { //data format is : ["imposter's id", number]
if (gameRunning && game["canVoteCorrectnes"] && game["imposters"].indexOf(sID) == -1 && game["imposters"].indexOf(msg[0]) != -1) {
console.log(`${sID} gave a score of ${msg[1]} to the imposter ${msg[0]}`);
game[`c${game["round"]}`][sID][msg[0]] = msg[1]; // register the vote in the game object
} else {
console.log("invalid correctnes vote by " + sID)
}
})
//vote endearly
socket.on("endEarly", msg => { //msg is boolean if want to end early
if (gameRunning) {
console.log(`${sID} voted endEarly: ${msg}`)
if (msg && game["endEarlyVotes"].indexOf(sID) == -1) { // this player wants to end early and didn't vote already
game["endEarlyVotes"].push(sID);
var votedN = game["endEarlyVotes"].length;
var requiredN = Math.round(game["connectedCount"] * 0.61);
for (key in game["players"]) { //for every player
io.to(game["players"][key]).emit("sMsg", `${game["pnames"][sID]} würde das Spiel gerne vorzeitig beenden. (${votedN}/${requiredN})`);
}
console.log(game['endEarlyVotes']);
if (votedN >= requiredN) { //if enough votes to end
game["totalRounds"] = game["round"];
gameSendGameState();
for (key in game["players"]) { //for every player
io.to(game["players"][key]).emit("sMsg", `Dies ist die letzte Runde.`);
}
}
} else if (!msg && game["endEarlyVotes"].indexOf(sID) != -1) { // cancel vote
const index = game['endEarlyVotes'].indexOf(sID);
game['endEarlyVotes'].splice(index, 1);
var votedN = game["endEarlyVotes"].length;
var requiredN = Math.round(game["connectedCount"] * 0.61);
for (key in game["players"]) { //for every player
io.to(game["players"][key]).emit("sMsg", `${game["pnames"][sID]} hat die Abstimmung zur vorzeitigen Beendigung des Spiels abgebrochen. (${votedN}/${requiredN})`);
}
console.log(game['endEarlyVotes']);
if (game["round"] == game["totalRounds"] && votedN < requiredN) { //cancel end early
game["totalRounds"] = game["players"].length;
gameSendGameState();
for (key in game["players"]) { //for every player
io.to(game["players"][key]).emit("sMsg", `Vorzeitiges Ende wird aufgehoben.`);
}
}
}
} else {
console.log('invalid endearly vote')
}
})
//When a connection goes down:
socket.on('disconnect', (socket) => {
connectionCounter -= 1;
connectionsIDs = connectionIDs.splice(connectionIDs.indexOf(sID), 1); //remove the id from the array of connected users
if (gameRunning && game["players"].indexOf(sID) != -1) { //if the game is running and this id is a player
console.log('player disconnected while in game')
io.sockets.emit("gameRunningError", `${game["pnames"][sID]} (${sID}) hat die Verbindung mitten im Spiel unterbrochen.`);
game["connectedCount"] -= 1;
console.log(`${game["connectedCount"]} players are connected`);
} else {
delete joinedPlayers[sID]; //remove from joinedPlayers object
delete readyPlayers[sID];
}
io.sockets.emit("connectionIDs", connectionIDs); //send the updated array to all users
io.sockets.emit("joinPlayersNames", joinedPlayers); //send joinedPlayers to clients
io.sockets.emit("readyPlayers", readyPlayers); //send the player that are ready to the clients
console.log('User ' + sID + " disconnected.");
console.log(joinedPlayers);
console.log(connectionCounter + ' connections are active.');
});
socket.on('reconnect', msg => {
if (gameRunning && connectionIDs.indexOf(msg) == -1) { //only reconnect if game is running and the requested id is not connected
if (game["players"].indexOf(msg) != -1) {//if the id is one of the players
//replace the id
console.log('reconnect approved: ' + sID + ' replaces ' + msg)
const regx = new RegExp(msg, 'g');
var modObject = JSON.stringify(game).replace(regx, sID)
game = JSON.parse(modObject);
//send the things for it to work fine
gameSendGameState();
io.to(sID).emit("startGame", "start");
io.to(sID).emit("sMsg", "Sie wurden automatisch neu verbunden.");
for (key in game["players"]) { //for every player
io.to(game["players"][key]).emit("sMsg", `${game["pnames"][sID]} (${sID}) wurde wieder verbunden.`);
}
game["connectedCount"] += 1;
console.log(`${game["connectedCount"]} players are connected`);
} else {
console.log('not player wanted to reconnect: ' + msg)
}
} else {
console.log('bad reconnect request: ' + msg)
}
});
});
//THE GAME FUNCTIONS:
function gameInit() { //initialize the game
game = {
"players": [], //array with ids of players
"pnames": {}, //copy of readyplayers when startting game
"ppoints": {}, //object with sIds as keys and numbers representing the points of the player
"imposters": [], //array of ids of imposters
"impostersCount": 1, //how many imposters are in this game?
"round": 1, //round counter, when = player count game ends.
"playersOrder": [], //players that stil haven't said anything.
"usedWordSets": [], //keys of sets used
"setIndex": 0, //current set
"imposterIndex": 0, // if the imposter gets the first or the second word of the word pair
"talkTime": 3, // time for players to talk in seconds
"voteTime": 10, //time for players to vote in seconds
"canVote": false, //when a client sends a imposter vote, it will only be registered if this is true
"canVoteCorrectnes": false, //when a client sends a correctnes vote, it will only be registered if this is true
"endEarlyVotes": [], //ids that want to end early
};
gameRunning = true;
console.log('start game says hi!')
for (key in readyPlayers) { // for every player that is ready
game["players"].push(key); //everybody is added to the players array
game["pnames"][key] = readyPlayers[key]; //add player's name to the pnames object in game object
game["ppoints"][key] = 0; //give everybody 0 points
};
joinedPlayers = {}; //clear joined players for potential next game
readyPlayers = {}; //clear ready players for potential next game
io.sockets.emit("joinPlayersNames", joinedPlayers); //send empty joinedPlayers to clients
io.sockets.emit("readyPlayers", readyPlayers); //send empty readyPlayers to the clients
game["totalRounds"] = game["players"].length;
game["impostersCount"] = Math.floor((game["players"].length + 4) / 5); //calculate number of imposters
game["connectedCount"] = game["players"].length;
//console.log(game)
gameSendGameState();
game["players"].forEach(e => { //send each player command to start game and client game state
io.to(e).emit("startGame", "start");
});
};
function gameSendGameState() {
clientGame = { //this object will be sent to the client to sync game state
"players": game["players"], //array with ids of players
"pnames": game["pnames"], //copy of readyplayers when startting game
"ppoints": game["ppoints"], //object with sIds as keys and numbers representing the points of the player
"impostersCount": game["impostersCount"], //how many imposters are in this game?
"round": game["round"], //round counter, when = player count game ends.
"talkTime": game["talkTime"], // time for players to talk in seconds
"voteTime": game["voteTime"], //time for players to vote in seconds
"totalRounds": game["totalRounds"], //num of total rounds
};
game["players"].forEach(e => { //send each player command to start game and client game state
io.to(e).emit("gameStateUpdate", clientGame);
});
};
function gameDecidePlayersOrder() {
game["playersOrder"] = [...game["players"]]; //make playersOrder a copy of players
for (i = game["playersOrder"].length; i > 0; i--) { //shuffle order of sIDs in game.playersOrder, this will determine who goes first
let index = Math.floor(Math.random() * game["playersOrder"].length); //random id with which to swap
let temp = game["playersOrder"][index];
game["playersOrder"][index] = game["playersOrder"][i - 1];
game["playersOrder"][i - 1] = temp;
};
};
function gameChoosoImposter() {
game["imposters"] = [];
var impCandidates = [];
for (key in game["players"]) { //copy the array
impCandidates[key] = game["players"][key];
}
for (key in impCandidates) { //shuffle the array
index = Math.floor(Math.random() * impCandidates.length);
temp = impCandidates[index];
impCandidates[index] = impCandidates[key];
impCandidates[key] = temp;
}
impCandidates.splice(game["impostersCount"]); //take the amount needed as imposters
game["imposters"] = impCandidates; //store the result in the game object
game[`r${game["round"]}`] = {}; //initialize round imposter voting register object
for (imp of game["imposters"]) {
game[`r${game["round"]}`][imp] = "imposter" //put imposter as the voting result
}
};
function gameChooseWordSet() {
if (words.length == game["usedWordSets"].length) { //if all word sets have already been used, this is an edge case
game["usedWordSets"] = [];
}
var impIndex = 0;
impIndex = Math.round(Math.random()); //the imposter gets word 0 or 1 of the array?
var haventFoundSet = true; //this is to avoid repetition
var questionIndex = 0;
while (haventFoundSet) {
questionIndex = Math.floor(Math.random() * words.length); //which set of words is used?
if (game["usedWordSets"].indexOf(questionIndex) == -1) {
haventFoundSet = false;
}
}
game["usedWordSets"].push(questionIndex);
game["setIndex"] = questionIndex;
game["imposterIndex"] = impIndex;
}
function gameSendWord(client) { //client is the sID to whom the word is sent
if (game["imposters"].indexOf(client) != -1) { // if imposter
game["players"].forEach(e => { // to every player
if (e != client) { //if not the destinatario of the word
io.to(e).emit("gamePlayerTalking", game["pnames"][client])//send who is talking
}
});
io.to(client).emit("gameImposterWord", words[game["setIndex"]][game["imposterIndex"]]);
} else { // if not imposter
game["players"].forEach(e => { // to every player
if (e != client) { //if not the destinatario of the word
io.to(e).emit("gamePlayerTalking", game["pnames"][client]) //send who is talking
}
});
io.to(client).emit("gameNormalWord", words[game["setIndex"]][Math.abs(game["imposterIndex"] - 1)]);
};
}
function gameVoteImposter() {
game["canVote"] = true;
game["players"].forEach(e => { //send each player command to vote
io.to(e).emit("gameVoteImposter", game["imposters"]);
});
}
function gameEndVoteImposter() { //when the voting ends
game["canVote"] = false; //can't vote anymore
game["players"].forEach(e => { //send each player command to vote
io.to(e).emit("gameEndVoteImposter", `Voting ends`);
});
};
function gameVoteCorrectnes() {
game["canVoteCorrectnes"] = true;
game[`c${game["round"]}`] = {} //initialize correctnes object
for (key in game['players']) {
if (game["imposters"].indexOf(game['players'][key]) == -1) { //if not imposter
game[`c${game["round"]}`][game['players'][key]] = {}; //initiaize player's voting object
}
}
game["players"].forEach(e => { //send each player command to vote
io.to(e).emit("gameVoteCorrectnes", [game["imposters"], words[game["setIndex"]][game["imposterIndex"]]]);
});
}
function gameEndVoteCorrectnes() { //when the voting ends
game["canVoteCorrectnes"] = false;
game["players"].forEach(e => { //send each player command to vote
io.to(e).emit("gameEndVoteCorrectnes", `Correctnes voting ends`);
});
}
function gameEvalPoints() {
//create array of nonimposters
var nonimposters = [...game["players"]];
for (key in game["imposters"]) {
nonimposters.splice(nonimposters.indexOf(game["imposters"][key]), 1)
}
//if guessed the imposter points/2
let points = 100;
for (key in nonimposters) { //for every nonimposter
try {
for (vote in game[`r${game["round"]}`][nonimposters[key]]) { //for every id whom this player voted
// game[`r${game["round"]}`][nonimposters[key]][vote] <- that is who was voted
if (game["imposters"].indexOf(game[`r${game["round"]}`][nonimposters[key]][vote]) != -1) { //if was indeed imposter
game["ppoints"][nonimposters[key]] += points / 2;
}
}
} catch (error) {
console.log('something went wrong with player: ' + nonimposters[key])
}
};
//calculate imposters' evaluation score
var impScores = {};
for (key in game["imposters"]) { //for every imposter
var score = 0;
var sum = 0;
var votes = 0;
for (player in game[`c${game["round"]}`]) { //for every player that voted
for (imp in game[`c${game["round"]}`][player]) { //for every imposter said player voted
if (game["imposters"][key] == imp) { //if this is the imposter we are looking at
sum += game[`c${game["round"]}`][player][imp];
votes += 1;
}
}
}
score = sum / votes;
if (isNaN(score)) { //if nobody voted give the best score
score = 1;
};
impScores[game["imposters"][key]] = score;
game[`impScore${game["round"]}`] = { ...impScores }; //store it to game object
};
//calculate imposter's points
game[`wronGuesses${game["round"]}`] = {}; //initialize object to count wrong guesses each imposter achieved
game[`noGuesses${game["round"]}`] = {}; //initialize object to count wrong guesses each imposter achieved
for (ik in game["imposters"]) { //for every imposter
var wrongGuesses = 0;
//calculate all the times a normal player guessed incorrectly
for (id in game[`r${game["round"]}`]) { //for everyone that voted
if (game["imposters"].indexOf(id) == -1) { //if wasn't imposter
var didntVotethisImposter = true; //assume the player didn't vote this imposter
for (key in game[`r${game["round"]}`][id]) { //for every vote
if (game["imposters"][ik] == game[`r${game["round"]}`][id][key]) {//if the vote was this imposter
didntVotethisImposter = false
}
}
if (didntVotethisImposter && game[`r${game["round"]}`][id].length == game["imposters"].length) { //if none of the votes were this imposter and voted as many times as there are imposters (if haven't voted all the times he could have, that is a noGuess, not wrongVote)
wrongGuesses += 1
};
}
}
game[`wronGuesses${game["round"]}`][game["imposters"][ik]] = wrongGuesses; //store wrong guesses
var noVote = 0;
//add all the empty votes, player that did not vote count as voting incorrectly
for (key in game["players"]) { //for every player
if (game["imposters"].indexOf(game["players"][key]) == -1) { //if wasn't imposter
if (game[`r${game["round"]}`][game["players"][key]] == undefined) { //if haven't voted at all
noVote += 1;
} else { //if have voted somebody already
if (game[`r${game["round"]}`][game["players"][key]].indexOf(game["imposters"][ik]) == -1) { //if haven't voted this imposter
if (game[`r${game["round"]}`][game["players"][key]].length < game["imposters"].length) { //if voted less players than there are imposters
noVote += 1;
}
}
}
}
}
game[`noGuesses${game["round"]}`][game["imposters"][ik]] = noVote; //store empty guesses
game["ppoints"][game["imposters"][ik]] += Math.round(points * (wrongGuesses + noVote) / game[`impScore${game["round"]}`][game["imposters"][ik]]); //give the points
};
};
function gameSendRoundStats() { //SEND r* c* impScore* wronGuesses* noGuesses* from game to client
const roundStats = {
'r': game[`r${game["round"]}`],
'c': game[`c${game["round"]}`],
'impScore': game[`impScore${game["round"]}`],
'wronGuesses': game[`wronGuesses${game["round"]}`],
'noGuesses': game[`noGuesses${game["round"]}`],
'imposters': game["imposters"]
};
game["players"].forEach(e => { // to every player
io.to(e).emit("gameRoundStats", roundStats); //round stats
});
}
function gameSendRoundStatsEnd() { //send command to remove round stats from screen
game["players"].forEach(e => { // to every player
io.to(e).emit("gameRoundStatsEnd", 'end'); //round stats
});
}
function gameEndOfRound() {
console.log("end of round:" + game["round"] + " , game state below:");
//console.log(game);
if (game["round"] >= game["totalRounds"]) { //if is the last round
gameEnds();
} else {
game["players"].forEach(e => { // to every player
// io.to(e).emit("sMsg", `end of round: ${game["round"]}`); //send end of round notice
});
game["round"] += 1; //move on to next round
}
}
function gameSendRemainingSecs(s) {
game["players"].forEach(e => { //send each player
io.to(e).emit("gameRemainingSecs", s); //how many secons remain
});
};
function gameSendNewPhaseNotice(phaseCode) { //phaseCode should be a single letter: 'j' join, 's' start, 't' talk, 'v' vote, 'e' evaluate, 'r' round stats, 'f' finished game (endScr)
game["players"].forEach(e => { //send each player
io.to(e).emit("gameNewPhase", phaseCode);
});
}
function gameEnds() {
gameRunning = false; //game finished
console.log('game ended')
game["players"].forEach(e => { //send each player command to end game
io.to(e).emit("endGame", game);
});
}
async function startGame() { //THE MAIN GAME FUNCTION //TODO: ABILITY TO PAUSE
gameInit();
while (gameRunning) { //GAME LOOP
gameDecidePlayersOrder(); //DECIDE PLAYERS ORDER
gameChoosoImposter(); //CHOOSE IMPOSTER
gameChooseWordSet(); //CHOOSE WORD SET
gameSendGameState(); //LET CLIENT UPDATE THE ROUNDS AND THINGS
gameSendNewPhaseNotice('t') //Tell player next phase is the talking phase
await new Promise(resolve => setTimeout(resolve, 3000)); //wait three seconds
for (const e of game["playersOrder"]) { //SEND EACH PLAYER ITS CORRESPONDING WORD
gameSendWord(e);
for (i = game["talkTime"]; i > 0; i--) { //wait talktime and notify clients
gameSendRemainingSecs(i); //send every player how many seconds remain
await new Promise(resolve => setTimeout(resolve, 1000)); //wait taktime seconds (for people to talk)
}
};
gameSendNewPhaseNotice('v') //Tell player next phase is the voting phase
await new Promise(resolve => setTimeout(resolve, 3000)); //wait three seconds
gameVoteImposter(); //START VOTING WHO IS THE IMPOSTER
for (i = game["voteTime"]; i > 0; i--) { //wait votetime and notify clients
gameSendRemainingSecs(i); //send every player how many seconds remain
await new Promise(resolve => setTimeout(resolve, 1000)); //wait taktime seconds (for people to talk)
}
gameEndVoteImposter(); //END IMPOSTER VOTING
gameSendNewPhaseNotice('e') //Tell player next phase is the evaluation phase
await new Promise(resolve => setTimeout(resolve, 3000)); //wait three seconds
gameVoteCorrectnes(); //VOTE WHO HAS DONE A TERRIBLE JOB //to discuss
for (i = game["voteTime"]; i > 0; i--) { //wait votetime and notify clients
gameSendRemainingSecs(i); //send every player how many seconds remain
await new Promise(resolve => setTimeout(resolve, 1000)); //wait taktime seconds (for people to talk)
}
gameEndVoteCorrectnes(); //END CORRECTNES VOTING
gameEvalPoints(); //COUNT POINTS USING r1 and c1
gameSendGameState(); //LET CLIENT UPDATE THE POINTS AND THINGS
gameSendNewPhaseNotice('r') //Tell player next phase is the round stats phase
await new Promise(resolve => setTimeout(resolve, 3000)); //wait three seconds
gameSendRoundStats(); //SEND r* c* impScore* wronGuesses* noGuesses* from game to client
for (i = 15; i > 0; i--) { //wait 15s and notify clients
gameSendRemainingSecs(i); //send every player how many seconds remain
await new Promise(resolve => setTimeout(resolve, 1000)); //wait taktime seconds (for people to talk)
}
gameSendRoundStatsEnd(); //TELL CLIENT TO STOP SHOWING THE STATS
gameEndOfRound(); //END OF ROUND
}
};
server.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});