mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
Added timer expiration end game scenario, cron job for clearing stale games
This commit is contained in:
26
server.js
26
server.js
@@ -6,8 +6,18 @@ const app = express();
|
||||
const server = http.Server(app);
|
||||
const io = socketIO(server);
|
||||
|
||||
// cron job for periodically clearing finished games
|
||||
const CronJob = require('cron').CronJob;
|
||||
|
||||
var activeGames = {};
|
||||
|
||||
const job = new CronJob('* * 2 * * *', function() {
|
||||
activeGames = activeGames.filter((game) => game.state !== "ended");
|
||||
console.log("Games pruned at: " + (new Date()).toDateString());
|
||||
});
|
||||
console.log("cron job created");
|
||||
job.start();
|
||||
|
||||
app.set('port', 5000);
|
||||
app.use('/static', express.static(__dirname + '/static')); // Routing
|
||||
app.use('/assets', express.static(__dirname + '/assets')); // Routing
|
||||
@@ -32,16 +42,6 @@ server.listen(process.env.PORT || 5000, function() {
|
||||
console.log('Starting server on port 5000');
|
||||
});
|
||||
|
||||
function didVillageWin(game) {
|
||||
let liveCount = 0;
|
||||
for (const player of game.players) {
|
||||
if (player.card.role === "Werewolf" && !player.dead) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function teamWon(game) {
|
||||
let wolvesAlive = 0;
|
||||
let villagersAlive = 0;
|
||||
@@ -124,6 +124,12 @@ io.on('connection', function(socket) {
|
||||
game.endTime = newDate.toJSON();
|
||||
io.to(code).emit('state', game);
|
||||
});
|
||||
socket.on("timerExpired", function(code) {
|
||||
let game = activeGames[Object.keys(activeGames).find((key) => key === code)];
|
||||
game.winningTeam = "wolf";
|
||||
game.state = "ended";
|
||||
io.to(code).emit('state', game);
|
||||
});
|
||||
socket.on('killPlayer', function(id, code) {
|
||||
let game = activeGames[Object.keys(activeGames).find((key) => key === code)];
|
||||
let player = game.players.find((player) => player.id === id);
|
||||
|
||||
Reference in New Issue
Block a user