]> git.cworth.org Git - zombocom-ai/blobdiff - index.js
Add a closing message and make the reboot button functional
[zombocom-ai] / index.js
index cc1bcdab4f9ea8fcd756c9feb6689ac8c47c0237..13ed7f6890a4bbd1a6d0fd5617b26c12aceaf705 100644 (file)
--- a/index.js
+++ b/index.js
@@ -179,7 +179,7 @@ const levels = [
     }
 ];
 
-var show_word_interval;
+var show_word_interval = 0;
 
 function show_word() {
     const tardis = state.tardis;
@@ -191,6 +191,40 @@ function show_word() {
        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;
 
@@ -201,7 +235,8 @@ function start_game() {
     // Let all companions know the state of the game
     io_tardis.emit("level", levels[tardis.level].title);
     io_tardis.emit("state", tardis.state);
-    show_word_interval = setInterval(show_word, 1200);
+
+    start_level();
 }
 
 function emit_tardis_timer() {
@@ -215,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.");
@@ -227,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':
@@ -252,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)) {
@@ -263,6 +308,36 @@ io_tardis.on("connection", (socket) => {
     }
     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;