forked from AlecM33/Werewolf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-helper.js
More file actions
206 lines (188 loc) · 7.15 KB
/
server-helper.js
File metadata and controls
206 lines (188 loc) · 7.15 KB
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
const debugMode = Array.from(process.argv.map( (arg)=>arg.trim().toLowerCase() )).includes("debug");
const LOGGER = require("./javascript/modules/logger")(debugMode);
module.exports = class {
constructor(CronJob) {
// TODO: do better than a plain object
this.activeGames = {};
this.timers = {};
// cron job for periodically clearing finished games
const scope = this;
this.job = new CronJob('0 0 */2 * * *', function() {
console.log(scope.activeGames);
for (const key in scope.activeGames) {
if (scope.activeGames.hasOwnProperty(key) && (Math.abs((new Date()) - (new Date(scope.activeGames[key].startTime))) / 36e5) >= 2) {
delete scope.activeGames[key];
}
}
console.log("Games pruned at: " + (new Date().toDateString()) + " " + (new Date()).toTimeString());
});
console.log("cron job created");
this.job.start();
}
killPlayer(id, code) {
let game = this.findGame(code);
if (game) {
let player = game.players.find((player) => player.id === id);
if (player) {
player.dead = true;
player.deadAt = new Date().toJSON();
game.killedPlayer = player.name;
game.lastKilled = player.id;
game.killedRole = player.card.role;
game.message = game.reveals
? player.name + ", a " + player.card.role + ", was killed!"
: player.name + " has died!";
console.log(game.message);
if (player.card.isTypeOfWerewolf && game.hasDreamWolf) {
this.activateDreamWolvesIfNeeded(game);
}
const winCheck = module.exports.teamWon(game);
if (winCheck === "wolf") {
console.log("wolves won the game!");
game.winningTeam = "wolf";
game.status = "ended";
}
if (winCheck === "village") {
console.log("village won the game!");
game.winningTeam = "village";
game.status = "ended";
}
}
}
}
endGameDueToTimeExpired(code) {
if (this.timers[code]) {
clearInterval(this.timers[code]);
}
let game = this.findGame(code);
if (game) {
LOGGER.debug("Game " + code + " has ended due to expired timer.");
game.winningTeam = "wolf";
game.status = "ended";
}
}
clearGameTimer(code) {
if (this.timers[code]) {
clearInterval(this.timers[code]);
LOGGER.debug("game paused and timer cleared for " + code);
}
}
resumeGame(code) {
let game = this.findGame(code);
if (game) {
game.paused = false;
let newTime = new Date(game.endTime).getTime() + (new Date().getTime() - new Date(game.pauseTime).getTime());
let newDate = new Date(game.endTime);
newDate.setTime(newTime);
game.endTime = newDate.toJSON();
LOGGER.debug("Game " + code + " timer has been unpaused, starting clock anew...");
this.startGameClock(code, newDate - Date.now());
}
}
pauseGame(code) {
let game = this.findGame(code);
if (game) {
game.pauseTime = (new Date()).toJSON();
game.paused = true;
this.clearGameTimer(code);
}
}
startGameClock(code, time) {
LOGGER.debug("timer started for game " + code);
let moduleScope = this;
if (this.timers[code]) {
clearInterval(this.timers[code]);
}
this.timers[code] = setInterval(function() {
moduleScope.endGameDueToTimeExpired(code)
}, time);
}
startGame(gameData) {
let game = this.findGame(gameData.code);
if (game) {
LOGGER.debug("game " + gameData.code + " started");
game.status = "started";
game.players = gameData.players;
if (game.time) {
let d = new Date();
d.setMinutes(d.getMinutes() + parseInt(game.time));
game.endTime = d.toJSON();
this.startGameClock(gameData.code, game.time * 60 * 1000); // provided time is in minutes
}
}
}
requestState(data, socket) {
const game = this.findGame(data.code);
if (game && Object.keys(socket.rooms).includes(data.code) === false) {
socket.join(data.code, function() {
socket.emit('state', game);
});
} else {
if (game) {
socket.emit('state', game);
}
}
}
joinGame(playerInfo, socket) {
const game = this.findGame(playerInfo.code);
if (game && game.players.length < game.size && !game.players.find((player) => player.id === playerInfo.id)) {
game.players.push({name: playerInfo.name, id: playerInfo.id});
console.log(playerInfo.name + " joined the game!");
socket.emit('success');
} else {
if (game && game.players.length === game.size) {
socket.emit("joinError", "This game is full - sorry!");
} else {
socket.emit("joinError", "No game found");
}
}
}
newGame(game, onSuccess) {
this.activeGames[game.accessCode] = game;
this.activeGames[game.accessCode].startTime = (new Date()).toJSON();
console.log("Game created at " + (new Date().toDateString()) + " " + (new Date()).toTimeString());
onSuccess();
}
findGame(code) {
return this.activeGames[Object.keys(this.activeGames).find((key) => key === code)];
}
// If there are multiple dream wolves, convert them all.
activateDreamWolvesIfNeeded(game) {
game.players.forEach((player) => {
if (!player.dead && player.card.role === "Dream Wolf") {
player.card.isTypeOfWerewolf = true;
console.log("player " + player.name + " was converted to a wolf!");
}
})
}
static teamWon(game) {
let wolvesAlive = 0;
let villagersAlive = 0;
let totalAlive = 0;
let hunterAlive = false;
for (const player of game.players) {
if (!player.card.isTypeOfWerewolf && !player.dead) {
villagersAlive ++;
}
if (player.card.isTypeOfWerewolf && !player.dead) {
wolvesAlive ++;
}
if (player.card.role === "Hunter" && !player.dead) {
hunterAlive = true;
}
if (!player.dead) {
totalAlive ++;
}
}
if (wolvesAlive === 0) {
return "village"
}
if ((wolvesAlive >= villagersAlive) && (totalAlive !== 2)) {
return "wolf";
}
if (totalAlive === 2) {
return hunterAlive ? "village" : "wolf"
}
return false;
}
};