-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgame.js
507 lines (456 loc) · 15.2 KB
/
game.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 roles = require("./gameConsts.js").roles;
const descriptions = require("./gameConsts.js").descriptions;
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
// This is the master server copy of a game that is running
class Game {
constructor() {
// Sets up the game object with the same initial state as the client
this.players = {}; // The current players in the game with their roles
this.playersNight = {}; // The current players in the game with their roles
this.playerSockets = {}; // Map the players to their socket id's
this.centerCards = {
"Alpha": "",
"Beta": "",
"Gamma": ""
};
this.centerCardsNight = {}; // Center cards during the night
this.gamePhase = "Intro"; // Current state of the game, determines what view to show
this.currentRoles = []; // The roles that are in the current game
this.allRoles = roles; // All of the roles that are available in the game
this.currentDescriptions = {}; // Descriptions that will be used this game
this.allDescriptions = descriptions; // All the descriptions for the roles
this.majorityNum = 0; // Number used to confirm everyone
this.werewolves = []; // The players that are werewolves in this game
this.masons = []; // The playesr that are masons in this game
this.playerActions = {}; // Stores all of the actions players have sent to server
this.messageBack = {}; // Messages to send back to players
this.actionHappened = false; // Whether or not an action happened during the night
this.killSelect = {}; // Totaling KillSelect
this.guaranteeKill = null; // Guarantee kill for hunter
this.killActions = []; // Logs the kills
this.killed // List of people killed
}
// Print the status of the game
// For debugging purposes
printAll() {
console.log("===============================");
console.log("Players: ");
console.log(this.players);
console.log("PlayersNight: ");
console.log(this.playersNight);
console.log("Player Sockets");
console.log(this.playerSockets);
console.log("Center Cards:")
console.log(this.centerCards);
console.log("GamePhase");
console.log(this.gamePhase);
console.log("Current Roles");
console.log(this.currentRoles);
console.log("Current Descriptions");
console.log(this.currentDescriptions);
console.log("Majority Num");
console.log(this.majorityNum);
console.log("Werewolves");
console.log(this.werewolves);
console.log("Masons");
console.log(this.masons);
console.log("Player Actions");
console.log(this.playerActions);
console.log("Message Back");
console.log(this.messageBack);
console.log("KillSelect");
console.log(this.killSelect);
console.log("killActions");
console.log(this.killActions);
console.log("===============================");
}
// Gets the list of players
getPlayers() {
return this.players;
}
// Gets the list of players and socket id's
getPlayerSockets() {
return this.playerSockets;
}
// Gets the number of players
getNumPlayers() {
return Object.keys(this.players).length;
}
// Gets the current game phase
getGamePhase() {
return this.gamePhase;
}
// Gets the current roles
getCurrentRoles() {
return this.currentRoles;
}
// Gets all roles
getAllRoles() {
return this.allRoles;
}
// Gets the current number of clients confirmed for an action
getMajorityNum() {
return this.majorityNum;
}
getMessageBack() {
return this.messageBack;
}
getKilled() {
return this.killed;
}
clearPlayers() {
var playersToSend = Object.assign({}, this.players);
for (var player in playersToSend) {
playersToSend[player] = false;
}
return playersToSend;
}
// Get the role of a specific player
getPlayersRole(playerName) {
if (playerName in this.players) {
return this.players[playerName];
} else {
return this.centerCards[playerName];
}
}
getPlayersRoleNight(playerName) {
if (playerName in this.playersNight) {
return this.playersNight[playerName];
} else {
return this.centerCardsNight[playerName];
}
}
// Get the player for a role
getRolesPlayer(role) {
for (var player in this.players) {
if (this.players[player] === role) {
return player;
}
}
for (var centers in this.centerCards) {
if (this.centerCards[player] === role) {
return centers;
}
}
}
// Get the player for a role
getRolesPlayerNight(role) {
for (var player in this.playersNight) {
if (this.playersNight[player] === role) {
return player;
}
}
for (var centers in this.centerCardsNight) {
if (this.centerCardsNight[player] === role) {
return centers;
}
}
}
// Get the description of a specific player
getPlayersDescription(playerName) {
return this.currentDescriptions[playerName];
}
getNightSelectNum(role) {
let noActionRoles = [
"Villager 1",
"Villager 2",
"Minion",
"Mason 1",
"Mason 2",
"Werewolf 1",
"Werewolf 2",
"Tanner",
"Hunter",
"Insomniac"
];
if (role === "Troublemaker") {
return 2;
} else if (noActionRoles.indexOf(role) > -1) {
return 0;
} else {
return 1;
}
}
// Sets a player's Role
setPlayerRole(playerName, role) {
if (playerName in this.players) {
this.players = {
...this.players,
[playerName]: role
}
} else {
this.setCenterRole(playerName, role);
}
}
// Sets a player's Role
setPlayerRoleNight(playerName, role) {
if (playerName in this.playersNight) {
this.playersNight = {
...this.playersNight,
[playerName]: role
}
} else {
this.setCenterRoleNight(playerName, role);
}
}
// Sets a center card's Role
setCenterRole(playerName, role) {
this.centerCards = {
...this.centerCards,
[playerName]: role
}
}
// Sets a center card's Role
setCenterRoleNight(playerName, role) {
this.centerCardsNight = {
...this.centerCardsNight,
[playerName]: role
}
}
setMajorityNum(num) {
this.majorityNum = num;
}
// Adds a player to the players list
addPlayer(playerName, playerRole) {
this.players = {
...this.players,
[playerName]: playerRole
};
}
// Add a player and thier socket ID
addPlayerSocket(playerName, socketID) {
this.playerSockets = {
...this.playerSockets,
[playerName]: socketID
};
}
// Remove a player and their socket ID when they disconnect
removePlayerSocket(socketID) {
for (var player in this.playerSockets) {
if (this.playerSockets[player] == socketID) {
this.playerSockets = {
...this.playerSockets,
[player]: null
};
}
}
}
// Checks if there are still players left int the game
checkPlayersLeft() {
for (var player in this.playerSockets) {
if (this.playerSockets[player] != null) {
return true;
}
}
return false;
}
// Changes the gamePhase
changeGamePhase(newPhase) {
this.gamePhase = newPhase;
}
// Toggle if a role is selected for the game
roleToggle(payload) {
this.allRoles[payload.role] = !payload.select;
}
// Put the selected roles into Current Roles
setCurrentRoles() {
for (var role in this.allRoles) {
if (this.allRoles[role]) {
this.currentRoles.push(role);
}
}
}
// Shuffle the list of roles and assign them to players
setPlayersRoles() {
// Shuffle list
let currentIndex = this.currentRoles.length;
let tempValue, randIndex;
while (currentIndex != 0) {
randIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
tempValue = this.currentRoles[currentIndex];
this.currentRoles[currentIndex] = this.currentRoles[randIndex];
this.currentRoles[randIndex] = tempValue;
}
// Assign roles to players
let currentRoleCopy = this.currentRoles.slice(0)
for (var player in this.players) {
let role = currentRoleCopy.pop()
this.setPlayerRole(player, role);
// Check if role is werewolf or mason
if (role === "Werewolf 1" || role === "Werewolf 2") {
this.werewolves.push(player);
} else if (role === "Mason 1" || role === "Mason 2") {
this.masons.push(player);
}
}
// Werewolf checks
if (this.werewolves.length === 1) { // Check if there is a lone werewolf
this.setPlayerRole(this.werewolves[0], "Lone Werewolf");
} else { // Else edit the description of werewolf
for (var werewolf of this.werewolves) {
this.allDescriptions["Werewolf 1"] += werewolf + " ";
this.allDescriptions["Werewolf 2"] += werewolf + " ";
this.allDescriptions["Minion"] += werewolf + " ";
}
}
// Mason checks
for (var mason of this.masons) {
this.allDescriptions["Mason 1"] += mason + " ";
this.allDescriptions["Mason 2"] += mason + " ";
}
// Assign role descriptions for all players
for (var player in this.players) {
var role = this.getPlayersRole(player);
// Set current role Descriptions
this.currentDescriptions = {
...this.currentDescriptions,
[player]: this.allDescriptions[role]
}
}
// Assing roles to center cards
for (var card in this.centerCards) {
this.setCenterRole(card, currentRoleCopy.pop());
}
}
// Execute the player's action depending on the player
playerNightAction(player, action) {
// First logs the action
this.playerActions = {
...this.playerActions,
[player]: action,
};
//Check if everyone has submitted an action
if (Object.keys(this.playerActions).length === Object.keys(this.players).length) {
this.doBaseNightActions();
}
}
// Do all of the night actions
doBaseNightActions() {
this.playersNight = Object.assign({}, this.players);
this.centerCardsNight = Object.assign({}, this.centerCards);
//If doppleganger were implemented, it would go here
if (this.exists("Lone Werewolf")) {
this.actionHappened = true;
this.messageBack["Lone Werewolf"] = "The center card you discovered is " + this.playerActions["Lone Werewolf"][0] + ": " + this.getPlayersRoleNight(this.playerActions["Lone Werewolf"][0]);
}
if (this.exists("Seer")) {
this.actionHappened = true;
let messageIntro = "The card you see is "
this.playerActions.Seer.forEach(card => {
messageIntro += card + ": " + this.getPlayersRoleNight(card) + " ";
});
this.messageBack.Seer = messageIntro;
}
if (this.exists("Robber")) {
this.actionHappened = true;
this.swapPlayersRole(this.getRolesPlayer("Robber"), this.playerActions["Robber"][0]);
this.messageBack.Robber = "You have swapped your role with " + this.playerActions["Robber"][0] + ", you are now " + this.getPlayersRole(this.playerActions["Robber"][0]);
}
if (this.exists("Troublemaker")) {
this.actionHappened = true;
this.swapPlayersRole(this.playerActions["Troublemaker"][0], this.playerActions["Troublemaker"][1]);
this.messageBack.Troublemaker = "You have switched the roles of " + this.playerActions["Troublemaker"][0] + " and " + this.playerActions["Troublemaker"][1];
}
if (this.exists("Drunk")) {
this.actionHappened = true;
this.swapPlayersRole(this.getRolesPlayer("Drunk"), this.playerActions["Drunk"]);
this.messageBack.Drunk= "You have swapped your role with a center card";
}
if (this.exists("Insomniac")) {
if (this.getRolesPlayer("Insomniac") === this.getRolesPlayerNight("Insomniac")) {
this.messageBack.Insomniac = "You look at your card at the end of the night and you are still an Insomnaic";
} else {
this.messageBack.Insomniac = "You look at your card at the end of the night and you are now: " + this.getPlayersRoleNight(this.getRolesPlayer("Insomniac"));
}
}
// Set gamePhase to Day
this.changeGamePhase("Daytime")
}
getActionHappened() {
return this.actionHappened;
}
exists(role) {
return role in this.playerActions;
}
// Swaps the roles of two players
swapPlayersRole(player1, player2) {
let temp = this.getPlayersRoleNight(player1)
this.setPlayerRoleNight(player1, this.getPlayersRoleNight(player2));
this.setPlayerRoleNight(player2, temp);
}
// Select kills processing
killSelectAdd(selectedPlayer, user) {
// First logs the action
this.killActions.push(selectedPlayer);
// Process KillSelect
if (user !== "Hunter") {
if (selectedPlayer in this.killSelect) {
console.log("adding to existing");
this.killSelect[selectedPlayer]++;
} else {
console.log("adding new");
this.killSelect = {
...this.killSelect,
[selectedPlayer]: 1
};
}
} else {
this.guaranteeKill = [selectedPlayer, this.getPlayersRole(selectedPlayer)];
}
//Check if everyone has submitted an action
if (Object.keys(this.killActions).length === Object.keys(this.players).length) {
this.processKills();
}
}
// Process Kills, find the top kills
processKills() {
let killed = [];
let killedNum = 0;
for (var player in this.killSelect) {
if (this.killSelect[player] > killedNum) {
killed = [[player, "/", this.getPlayersRole(player)]];
killedNum = this.killSelect[player];
} else if (this.killSelect[player] === killedNum) {
killed.push([player, "/", this.getPlayersRole(player)]);
}
}
this.changeGamePhase("Finale")
if (this.guaranteeKill !== null) {
this.killed = {vote: killed, hunter: this.guaranteeKill};
} else {
this.killed = {vote: killed};
}
console.log(this.killed);
}
// Resets the game state
resetGame() {
this.players = {}; // The current players in the game with their roles
this.playersNight = {}; // The current players in the game with their roles
this.playerSockets = {}; // Map the players to their socket id's
this.centerCards = {
"Alpha": "",
"Beta": "",
"Gamma": ""
};
this.centerCardsNight = {}; // Center cards during the night
this.gamePhase = "Intro"; // Current state of the game, determines what view to show
this.currentRoles = []; // The roles that are in the current game
this.allRoles = roles; // All of the roles that are available in the game
this.currentDescriptions = {}; // Descriptions that will be used this game
this.allDescriptions = descriptions; // All the descriptions for the roles
this.majorityNum = 0; // Number used to confirm everyone
this.werewolves = []; // The players that are werewolves in this game
this.masons = []; // The playesr that are masons in this game
this.playerActions = {}; // Stores all of the actions players have sent to server
this.messageBack = {}; // Messages to send back to players
this.killSelect = {}; // Totaling KillSelect
this.guaranteeKill = null; // Guarantee kill for hunter
this.killActions = []; // Logs the kills
this.killed // List of people killed
}
}
module.exports = Game;