]> git.cworth.org Git - zombocom-ai/commitdiff
Add a welcome stage to the TARDIS
authorCarl Worth <cworth@cworth.org>
Thu, 22 Dec 2022 01:03:29 +0000 (17:03 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 22 Dec 2022 03:54:34 +0000 (19:54 -0800)
This gives a 30 second countdown before entering the TARDIS along with
a display of how many companions are present. Finally, it ends with a
zoom into the TARDIS itself.

Next, we just need the puzzle itself.

index.js
tardis.html

index 04fe9bb0c65152eb1537d98a08e2315ca3430655..1fa214bdb28ef79e512980de989f12d40f75e94a 100644 (file)
--- a/index.js
+++ b/index.js
@@ -95,7 +95,16 @@ io.use(wrap(session_middleware));
 // Load comments at server startup
 fs.readFile(state_file, (err, data) => {
     if (err)
-        state = { images: [], targets: [] };
+        state = {
+           images: [],
+           targets: [],
+           tardis: {
+               companions: {
+                   names: [],
+                   count: 0
+               }
+           }
+       };
     else
         state = JSON.parse(data);
 });
@@ -128,6 +137,58 @@ function tardis_app(req, res) {
 app.get('/tardis', tardis_app);
 app.get('/tardis/', tardis_app);
 
+const io_tardis = io.of("/tardis");
+
+io_tardis.use(wrap(session_middleware));
+
+var tardis_interval;
+
+function emit_tardis_timer() {
+    const tardis = state.tardis;
+    console.log("Emitting timer at " + tardis.timer);
+    io_tardis.emit('timer', tardis.timer);
+    tardis.timer = tardis.timer - 1;
+    if (tardis.timer < 0) {
+       clearInterval(tardis_interval);
+       tardis.timer = 30;
+    }
+}
+
+io_tardis.on("connection", (socket) => {
+    console.log("In connection handler.");
+    if (! socket.request.session.name) {
+       console.log("Error: Someone showed up at the Tardis without a name.");
+       return;
+    }
+
+    const name = socket.request.session.name;
+    const tardis = state.tardis;
+
+    if (tardis.companions.count === 0) {
+       tardis.timer = 30;
+       emit_tardis_timer();
+       tardis_interval = setInterval(emit_tardis_timer, 1000);
+    }
+
+    if (! tardis.companions.names.includes(name)) {
+       tardis.companions.count = tardis.companions.count + 1;
+       console.log("Adding " + name + " for " + tardis.companions.count + " companions");
+       io_tardis.emit('companions', tardis.companions.count);
+    }
+    tardis.companions.names.push(name);
+
+    socket.on('disconnect', () => {
+       const names = tardis.companions.names;
+
+       names.splice(names.indexOf(name), 1);
+
+       if (! tardis.companions.includes(name)) {
+           tardis.companions.count = tardis.companions.count - 1;
+           io_tardis.emit('companions', tardis.companions.count);
+       }
+    });
+});
+
 io.on('connection', (socket) => {
 
     // First things first, tell the client their name (if any)
index ae80bc0acc714dec03f75835bd26c04cdb7d1917..7f5dd9f116668847b1e9ef23b9dca865ae42c38d 100644 (file)
@@ -16,7 +16,7 @@
            transform: scale(30);
         }
     }
-    .zoom-now {
+    .zoom-tardis {
         animation-name: zoom;
         animation-duration: 2s;
         animation-timing-function: ease-in;
        </p>
       </div>
 
-      <p id="welcome-message">
-       Waiting to enter Tardis.
+      <div id="welcome-message">
+       <div id="timer_div" style="visibility: hidden">
+         Entering TARDIS in <span id="timer"></span> seconds.
+       </div>
        <br>
        Companions present: <span id="companions">1</span>/4
-      </p>
+      </div>
     </div>
 
     <div id="game" style="display: none">
     </div>
 
   </div>
+
+  <script src="/socket.io/socket.io.js"></script>
+  <script>
+    const socket = io("/tardis");
+
+    const header = document.getElementById("header");
+    const companions = document.getElementById("companions");
+    const timer_div = document.getElementById("timer_div");
+    const timer = document.getElementById("timer");
+    const welcome_message = document.getElementById("welcome-message");
+
+    socket.on('companions', (count) => {
+       companions.textContent = count.toString();
+    });
+
+    socket.on('timer', (value) => {
+       console.log("Receiving timer value of " + value);
+       timer_div.style.visibility = "visible";
+       timer.textContent = value.toString();
+
+       if (value === 0) {
+           welcome_message.style.visibility = "hidden";
+           timer_div.style.visibility = "hidden";
+           header.className = "zoom-tardis";
+       }
+    });
+
+  </script>
 </body>
 </html>