From 578080fdd7928921f8df8b398f717b0c141685ab Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 21 Dec 2022 19:39:45 -0800 Subject: [PATCH] Add a closing message and make the reboot button functional Also fix a bunch of state transitions, (joining the game in-progress, rebooting from anywhere, reloading from anywhere). With the sloppy DOM-manipulation approach I'm using it's pretty painful to get all of those right, (could have used React or something, but that has its own set of problems), so I won't promise that everything is perfect, but I think it's pretty close. --- index.js | 31 ++++++++++++++++++++++++++++--- tardis.html | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 91a74ae..13ed7f6 100644 --- a/index.js +++ b/index.js @@ -250,6 +250,13 @@ function emit_tardis_timer() { } } +function start_welcome_timer() { + const tardis = state.tardis; + tardis.timer = 30; + emit_tardis_timer(); + tardis_interval = setInterval(emit_tardis_timer, 1000); +} + io_tardis.on("connection", (socket) => { if (! socket.request.session.name) { console.log("Error: Someone showed up at the Tardis without a name."); @@ -262,6 +269,11 @@ io_tardis.on("connection", (socket) => { // Let the new user know the state of the game socket.emit("state", tardis.state); + // And the level if relevant + if (tardis.state === "game") { + socket.emit("level", levels[tardis.level].title); + } + // Put each of our boys into a different room switch (name[0]) { case 'C': @@ -287,9 +299,7 @@ io_tardis.on("connection", (socket) => { } if (tardis.companions.count === 0) { - tardis.timer = 30; - emit_tardis_timer(); - tardis_interval = setInterval(emit_tardis_timer, 1000); + start_welcome_timer(); } if (! tardis.companions.names.includes(name)) { @@ -313,6 +323,21 @@ io_tardis.on("connection", (socket) => { } }); + socket.on('reboot', () => { + const tardis = state.tardis; + + if (show_word_interval) { + clearInterval(show_word_interval); + show_word_interval = 0; + } + + tardis.state = "welcome"; + io_tardis.emit("state", tardis.state); + io_tardis.emit('companions', tardis.companions.count); + + start_welcome_timer(); + }); + socket.on('disconnect', () => { const names = tardis.companions.names; diff --git a/tardis.html b/tardis.html index 1e9e057..72a9d7d 100644 --- a/tardis.html +++ b/tardis.html @@ -11,6 +11,10 @@