/
game.js
385 lines (320 loc) · 9.43 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
var Moniker = require("moniker");
/* Roles are:
* - villager
* - mafia
* - cop
* - doctor
* - jester
* - bodyguard
*
*
* Not added yet:
* - vigilante
*/
var canVote = {
"villager": false,
"mafia": true,
"cop": true,
"doctor": true,
"jester": false,
"bodyguard": true
}; // role does some sort of action at night
var deathQuotes = require("./deathQuotes");
function pluralize(name) {
if(name[name.length - 1].toLowerCase() === "s")
return name + "'";
else
return name + "'s";
}
function getDeathQuote(name) {
var quote = deathQuotes[Math.floor(Math.random() * deathQuotes.length)];
quote = quote.replace("%s", name).replace("%p", pluralize(name));
return quote;
}
function Player(id, socket, name) {
this.id = id;
this.socket = socket;
this.name = name;
this.role = null;
this.votingFor = null;
this.dead = false;
this.lynched = false; // Lynched is also dead, but if lynched, role is visible
this.leftGame = false;
}
function Game(io) {
this.io = io;
this.players = [];
this.room = Moniker.choose(); // essentially the game id
this.closed = false; // closed to new joins (so that the moderator knows how many people there are)
this.started = false; // game actually started (roles dealt out, etc)
this.dayNumber = 1; // increases at end of night
this.night = false; // whether it is night or not
this.lastAction = ""; // description of the last thing to happen (lynching, night death)
this.lastUpdate = new Date(); // used to garbage collect inactive games
this.settings = {
roles: {
"mafia": 1,
"cop": 0,
"doctor": 0,
"jester": 0,
"bodyguard": 0
}
}; // settings for the game
this.moderator = null; // First player to join
}
Game.prototype.addPlayer = function(id, socket, name) {
this.players.push(new Player(id, socket, name));
if(this.moderator === null)
this.moderator = id;
socket.emit("gameState", this.gameState());
this.sendPlayerListUpdate(null);
}
Game.prototype.reconnectPlayer = function(id, socket, name) {
for(var i = 0; i < this.players.length; i++) {
if(this.players[i].id === id) {
this.players[i].socket = socket;
this.players[i].name = name;
socket.emit("gameState", this.gameState());
this.sendPlayerListUpdate(null);
return true;
}
}
return false;
}
Game.prototype.leavePlayer = function(id) {
var p = this.playerById(id);
p.leftGame = true;
this.lastAction = p.name + " left the game.";
this.sendGameStateUpdate();
if(!p.dead && this.started) {
// Kill the player
p.dead = true;
this.sendPlayerListUpdate(null);
} else if(!this.started) {
// Straight up remove the player
for(var i = 0; i < this.players.length; i++) {
if(this.players[i].id == id) {
this.players.splice(i, 1);
break;
}
}
this.sendPlayerListUpdate(null);
}
p.id += "leave";
}
Game.prototype.playerById = function(id) {
for(var i = 0; i < this.players.length; i++) {
// Check for full id or shortened id
if(this.players[i].id === id || this.players[i].id.split("-")[0] === id) {
return this.players[i];
}
}
return null;
}
// Determines the number of votes for a specific player from a specific role
Game.prototype.votesForBy = function(id, role) {
var count = 0;
for(var i = 0; i < this.players.length; i++) {
if(this.players[i].votingFor === id && this.players[i].role === role)
count++;
}
return count;
}
// Makes a list of players for a specific player
Game.prototype.playerListFor = function(id) {
var p = this.playerById(id);
if(p === null) return [];
var list = [];
for(var i = 0; i < this.players.length; i++) {
var obj = {
id: this.players[i].id.split("-")[0], // shortened id for identification, but can't impersonate
name: this.players[i].name,
role: this.players[i].role === p.role || this.players[i].lynched ? this.players[i].role : null,
votes: this.votesForBy(this.players[i].id, p.role),
mod: this.players[i].id === this.moderator,
dead: this.players[i].dead,
lynched: this.players[i].lynched
};
list.push(obj);
}
return list;
}
// Gets the current game state, suitable for sending to all players
Game.prototype.gameState = function() {
var obj = {
room: this.room,
closed: this.closed,
started: this.started,
dayNumber: this.dayNumber,
night: this.night,
settings: this.settings,
lastAction: this.lastAction
};
return obj;
}
// Sends all players the current game state
Game.prototype.sendGameStateUpdate = function() {
_this = this;
this.players.forEach(function(p) {
if(p.leftGame) return;
p.socket.emit("gameState", _this.gameState());
});
}
// Sends all players of a specific role the updated player list
// If role is null, sends it to everyone
Game.prototype.sendPlayerListUpdate = function(role) {
_this = this;
this.players.forEach(function(p) {
if(p.role === role || role === null) {
if(p.leftGame) return;
p.socket.emit("playerList", _this.playerListFor(p.id));
}
});
}
Game.prototype.dealRoles = function() {
var roleList = [];
for(var role in this.settings.roles) {
if(this.settings.roles.hasOwnProperty(role)) {
for(var i = 0; i < this.settings.roles[role]; i++) {
roleList.push(role);
}
}
}
// Fill remaining roles with villager
while(roleList.length < this.players.length) {
roleList.push("villager");
}
// Shuffle the roles
for(var i = roleList.length - 1; i >= 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var t = roleList[i];
roleList[i] = roleList[j];
roleList[j] = t;
}
// Deal one role out to each player
this.players.forEach(function(p, i) {
p.role = roleList[i];
});
}
// Resets some values and starts the night
Game.prototype.startNight = function() {
this.night = true;
this.sendGameStateUpdate();
}
// Checks whether all multi-roles agree on what's happening
Game.prototype.isNightOver = function() {
var targets = {};
for(var i = 0; i < this.players.length; i++) {
var p = this.players[i];
if(p.dead)
continue;
if(p.votingFor === null && canVote[p.role])
return false;
if(targets[p.role] === undefined || targets[p.role] === p.votingFor) {
targets[p.role] = p.votingFor;
} else {
return false;
}
}
return targets;
}
// Figures out what happened and ends the night
Game.prototype.endNight = function() {
_this = this;
var targets = this.isNightOver();
var deadPeople = [];
if(targets["mafia"] === targets["bodyguard"]) {
// Kill off one of the mafia
for(var i = 0; i < this.players.length; i++) {
var p = this.players[i];
if(p.role === "mafia") {
p.dead = true;
deadPeople.push(p);
targets["mafia"] = null;
break;
}
};
}
if(targets["mafia"] === targets["doctor"]) {
targets["mafia"] = null;
}
if(targets["mafia"]) {
var p = this.playerById(targets["mafia"]);
p.dead = true;
deadPeople.push(p);
}
// Cop info
if(targets["cop"]) {
var copTarget = this.playerById(targets["cop"]);
var guilty = copTarget.role === "mafia" || copTarget.role === "doctor" || copTarget.role === "bodyguard";
this.players.forEach(function(p) {
if(p.role === "cop") {
p.socket.emit("copInfo", copTarget.name + " is " + (guilty ? "guilty" : "innocent") + ".");
}
});
}
if(deadPeople.length == 0) {
this.lastAction = "Nobody died last night.";
} else {
this.lastAction = "";
deadPeople.forEach(function(p) {
_this.lastAction += getDeathQuote(p.name) + " ";
});
}
this.players.forEach(function(p) {
p.votingFor = null;
});
this.night = false;
this.dayNumber += 1;
this.sendGameStateUpdate();
this.sendPlayerListUpdate(null);
}
Game.prototype.gameAction = function(id, action, data) {
this.lastUpdate = new Date();
if(action === "close") { // Closes the game
if(id === this.moderator && !this.closed) {
this.closed = true;
this.sendGameStateUpdate();
}
} else if(action === "settings") {
if(id === this.moderator && this.closed && !this.started) {
this.started = true;
this.settings = data;
// TODO: Make sure settings are valid; right now, trust that the client is a nice client
this.dealRoles();
this.sendGameStateUpdate();
this.sendPlayerListUpdate(null);
}
} else if(action === "night") {
if(id === this.moderator && this.started && !this.night) {
this.startNight();
}
} else if(action === "lynch") {
if(id === this.moderator && this.started && !this.night) {
var target = this.playerById(data);
if(target != null) {
target.dead = true;
target.lynched = true;
this.lastAction = target.name + " was lynched. They were a " + target.role + ".";
this.sendGameStateUpdate();
this.sendPlayerListUpdate(null);
}
}
} else if(action === "vote") {
if(this.started && this.night) {
var self = this.playerById(id);
var target = this.playerById(data);
// TODO: More validation
if(self && target && self.id !== target.id && self.role !== target.role && !self.dead && !target.dead && canVote[self.role]) {
self.votingFor = target.id;
this.sendPlayerListUpdate(self.role);
// Check if night is over
if(this.isNightOver()) {
// End the night!
this.endNight();
}
}
}
}
}
module.exports = Game;