Added timer expiration end game scenario, cron job for clearing stale games

This commit is contained in:
Alec Maier
2019-09-02 20:44:06 -04:00
parent 2a2691bc91
commit 1eae5f63f6
7 changed files with 67 additions and 19 deletions

View File

@@ -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);