diff --git a/index.js b/index.js index 86202d4..25bb282 100644 --- a/index.js +++ b/index.js @@ -224,7 +224,7 @@ io.on('connection', async (socket) => { let UTCTs = Math.floor((new Date()).getTime() / 1000 + 90); io.to(playerGame.id).emit('turn update', { turn: 0, phase: "preparation", timerToUTC: UTCTs }); - bships.timer(90, async () => { + GInfo.timer(playerGame.id, 15, async () => { const playerGame = await GInfo.getPlayerGameData(socket); for (let i = 0; i < playerGame.data.boards.length; i++) { const ships = playerGame.data.boards[i].ships; @@ -235,7 +235,7 @@ io.on('connection', async (socket) => { } GInfo.endPrepPhase(socket); - bships.timer(30, () => { + GInfo.timer(playerGame.id, 30, () => { AFKEnd(playerGame.id); }); }); @@ -316,15 +316,15 @@ io.on('connection', async (socket) => { } } - bships.resetTimers(); + GInfo.resetTimer(playerGame.id); endGame(playerGame.id); return; } } await GInfo.passTurn(socket); - bships.resetTimers(); - bships.timer(30, () => { + GInfo.resetTimer(playerGame.id); + GInfo.timer(playerGame.id, 30, () => { AFKEnd(playerGame.id); }); } diff --git a/utils/battleships.js b/utils/battleships.js index 1032c8d..5f4fbb9 100644 --- a/utils/battleships.js +++ b/utils/battleships.js @@ -4,6 +4,34 @@ export class GameInfo { this.io = io; } + async timer(tId, time, callback) { + await this.redis.set(`timer:${tId}`, new Date().getTime() / 1000); + let localLastUpdate = await this.redis.get(`timer:${tId}`); + + let timeout = setTimeout(callback, time * 1000); + + let interval = setInterval(async () => { + if (timeout._destroyed) { + // timer is finished, stop monitoring turn changes + clearInterval(interval); + return; + } + + let lastUpdate = await this.redis.get(`timer:${tId}`); + if (localLastUpdate != lastUpdate) { + // timer has been reset + clearTimeout(timeout); + clearInterval(interval); + return; + } + }, 200); + } + + async resetTimer(tId) { + let lastUpdate = await this.redis.get(`timer:${tId}`); + await this.redis.set(`timer:${tId}`, -lastUpdate); + } + async isPlayerInGame(socket) { const game = await this.redis.json.get(`game:${socket.session.activeGame}`); return game != null; @@ -125,28 +153,6 @@ export function isPlayerInRoom(socket) { var lastTimeChange = new Date().getTime(); -export function timer(time, callback) { - let localLastChange = lastTimeChange; - - let timeout = setTimeout(callback, time * 1000); - - let interval = setInterval(() => { - if (timeout._destroyed) { - // timer is finished, stop monitoring turn changes - clearInterval(interval); - } - if (localLastChange != lastTimeChange) { - // timer has been reset - clearTimeout(timeout); - clearInterval(interval); - } - }, 200); -} - -export function resetTimers() { - lastTimeChange = -lastTimeChange; -} - // export function getShipsLeft(data, playerIdx) { // let shipsLeft = [4, 3, 2, 1];