]> git.cworth.org Git - zombocom-ai/blobdiff - index.js
Shrink the font size a bit for the word prompts
[zombocom-ai] / index.js
index b149d09b58733ac1ab0b9da9fae802b2ff304446..13ed7f6890a4bbd1a6d0fd5617b26c12aceaf705 100644 (file)
--- a/index.js
+++ b/index.js
@@ -95,7 +95,18 @@ 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
+               },
+               state: "welcome",
+               level: 0
+           }
+       };
     else
         state = JSON.parse(data);
 });
@@ -121,16 +132,222 @@ app.get('/index.html', (req, res) => {
     res.sendFile(__dirname + '/index.html');
 });
 
-app.get('/tardis', (req, res) => {
+function tardis_app(req, res) {
     res.sendFile(__dirname + '/tardis.html');
-});
+}
 
-app.get('/tardis/', (req, res) => {
-    res.sendFile(__dirname + '/tardis.html');
-});
+app.get('/tardis', tardis_app);
+app.get('/tardis/', tardis_app);
 
-app.get('/tardis/index.html', (req, res) => {
-    res.sendFile(__dirname + '/tardis.html');
+const io_tardis = io.of("/tardis");
+
+io_tardis.use(wrap(session_middleware));
+
+var tardis_interval;
+var game_timer;
+
+const levels = [
+    {
+       title: "Calibrate the Trans-Dimensional Field Accelerator",
+       words: [
+           "What", "was", "the", "year", "of", "Coda's", "birth?"
+       ],
+       answer: 2098
+    },
+    {
+       title: "Reverse the Polarity of the Neutrino Flow Coil",
+       words: [
+           "How", "many", "years", "had", "Zombo.com", "been", "running",
+           "when", "Coda", "sent", "the", "message", "you", "first",
+           "received?"
+       ],
+       answer: 123
+    },
+    {
+       title: "Give a good whack to Hydro-chronometric Energy Feedback Retro-stabilizer",
+       words: [
+           "What", "is", "the", "sum", "of", "Cameron's", "age", "plus", "Andrew's", "age", "at", "Christmas", "time", "the", "year", "when", "Hyrum's", "age", "is", "25", "years", "older", "than", "Scott's", "age", "will", "have", "been", "when", "Scott's", "age", "will", "be", "half", "of", "Dad's", "age?"
+       ],
+       answer: 111
+    },
+    {
+       title: "Disable the Fragmentary Spatio-temporal Particle Detector",
+       words: [
+           "What", "is", "the", "product", "of", "the", "last", "three", "numbers?"
+       ],
+       answer: 28643994
+    }
+];
+
+var show_word_interval = 0;
+
+function show_word() {
+    const tardis = state.tardis;
+    const room = "room-" + (tardis.word % 4).toString();
+    const word = levels[tardis.level].words[tardis.word];
+    io_tardis.to(room).emit('show-word', word);
+    tardis.word = tardis.word + 1;
+    if (tardis.word >= levels[tardis.level].words.length)
+       tardis.word = 0;
+}
+
+function start_level() {
+    const tardis = state.tardis;
+
+    // Inform all players of the new level
+    io_tardis.emit("level", levels[tardis.level].title);
+
+    // Then start the timer that shows the words
+    show_word_interval = setInterval(show_word, 1200);
+}
+
+function level_up() {
+    const tardis = state.tardis;
+
+    if (show_word_interval) {
+       clearInterval(show_word_interval);
+       show_word_interval = 0;
+    }
+
+    if (tardis.state === "game") {
+       tardis.level = tardis.level + 1;
+       tardis.word = 0;
+
+       if (tardis.level >= levels.length) {
+           tardis.state = "over";
+           io_tardis.emit("state", tardis.state);
+       } else {
+           setTimeout(() => {
+               start_level();
+           }, 2000);
+       }
+
+    }
+}
+
+function start_game() {
+    const tardis = state.tardis;
+
+    tardis.state = "game";
+    tardis.level = 0;
+    tardis.word = 0;
+
+    // Let all companions know the state of the game
+    io_tardis.emit("level", levels[tardis.level].title);
+    io_tardis.emit("state", tardis.state);
+
+    start_level();
+}
+
+function emit_tardis_timer() {
+    const tardis = state.tardis;
+    io_tardis.emit('timer', tardis.timer);
+    tardis.timer = tardis.timer - 1;
+    if (tardis.timer < 0) {
+       clearInterval(tardis_interval);
+       tardis.timer = 30;
+       setTimeout(start_game, 3000);
+    }
+}
+
+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.");
+       return;
+    }
+
+    const name = socket.request.session.name;
+    const tardis = state.tardis;
+
+    // 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':
+    case 'c':
+       socket.join("room-0");
+       break;
+    case 'H':
+    case' h':
+       socket.join("room-1");
+       break;
+    case 'A':
+    case 'a':
+       socket.join("room-2");
+       break;
+    case 'S':
+    case 's':
+       socket.join("room-3");
+       break;
+    default:
+       const room = Math.floor(Math.random()*4);
+       socket.join("room-"+room.toString());
+       break;
+    }
+
+    if (tardis.companions.count === 0) {
+       start_welcome_timer();
+    }
+
+    if (! tardis.companions.names.includes(name)) {
+       tardis.companions.count = tardis.companions.count + 1;
+       io_tardis.emit('companions', tardis.companions.count);
+    }
+    tardis.companions.names.push(name);
+
+    socket.on('answer', answer => {
+       const tardis = state.tardis;
+
+       if (tardis.state != "game") {
+           return;
+       }
+
+       if (answer == levels[tardis.level].answer) {
+           io_tardis.emit('correct');
+           level_up();
+       } else {
+           io_tardis.emit('incorrect');
+       }
+    });
+
+    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;
+
+       names.splice(names.indexOf(name), 1);
+
+       if (! names.includes(name)) {
+           tardis.companions.count = tardis.companions.count - 1;
+           io_tardis.emit('companions', tardis.companions.count);
+       }
+    });
 });
 
 io.on('connection', (socket) => {