From bae70b96636dfd7459fbed3ceeff76ef3447a732 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Tue, 8 Jan 2019 11:46:21 -0500 Subject: [PATCH] Timers can be associated with rooms or ids now --- game.js | 34 ++++++++++++++++++++++++++++++++-- satiate.js | 6 ------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/game.js b/game.js index ff81b9f..6512822 100644 --- a/game.js +++ b/game.js @@ -6,6 +6,10 @@ function initGame(story, state) { state.player.stats = {}; state.player.stats.health = 100; + + state.timers = []; + + state.timers.global = new Set(); } // TODO: format string this lol @@ -38,20 +42,46 @@ function updatePlayerInfo(state) { /* { + id: an optional name; needed to manually kill a timer func: the function to invoke delay: how long to wait between invocations loop: false = no looping, true = loop forever + room: the room associated with the timer } +Returns the timeout id - but you still need to cancel it through stopTimer! + */ function startTimer(config, state) { if (config.loop) { const timeout = setTimeout(() => { config.func(); - state.timers.global.delete(timeout); + state.timers = state.timers.filter(x => x.timeout != timeout); startTimer(config, state); }, config.delay); - state.timers.global.add(timeout); + state.timers.push({id: config.id, timeout: timeout, room: config.room}); + + return timeout; } } + +function stopTimer(id, state) { + const matches = state.timers.filter(timer => timer.id == id); + matches.forEach(timer => clearTimeout(timer.timeout)); + + state.timers = state.timers.filter(timer => timer.id != id); +} + +function stopRoomTimers(room, state) { + const matches = state.timers.filter(timer => timer.room == room); + matches.forEach(timer => clearTimeout(timer.timeout)); + + state.timers = state.timers.filter(timer => timer.room != room); + +} + +function stopAllTimers(state) { + state.timers.forEach(x => clearTimeout(x.timeout)); + state.timers = []; +} diff --git a/satiate.js b/satiate.js index f9fe71d..afbcfba 100644 --- a/satiate.js +++ b/satiate.js @@ -69,12 +69,6 @@ function init(story) { } }, - timers: { - room: { - - }, - global: new Set() - } }; initWorld(story, state);