]> git.cworth.org Git - zombocom-ai/blobdiff - index.js
Add some convenience functions with implicit cairo context
[zombocom-ai] / index.js
index 91a74aeea4c23e6413717eaba94d266e562707f5..b0a115f79dd8d0e1393cde0600838203df5e19a7 100644 (file)
--- a/index.js
+++ b/index.js
@@ -1,8 +1,8 @@
 const fs = require('fs');
 
 const util = require('util');
-const execFile = util.promisify(require('child_process').execFile);
-
+const child_process = require('child_process');
+const execFile = util.promisify(child_process.execFile);
 const express = require('express');
 const app = express();
 const session = require('express-session');
@@ -15,6 +15,7 @@ const port = 2122;
 
 const python_path = '/usr/bin/python3'
 const generate_image_script = '/home/cworth/src/zombocom-ai/generate-image.py'
+const interpret_cairo_script = '/home/cworth/src/zombocom-ai/interpret-cairo-to-svg.py'
 const state_file = 'zombocom-state.json'
 const targets_dir = '/srv/cworth.org/zombocom/targets'
 const images_dir = '/srv/cworth.org/zombocom/images'
@@ -132,8 +133,112 @@ app.get('/index.html', (req, res) => {
     res.sendFile(__dirname + '/index.html');
 });
 
+function bus_app(req, res) {
+    res.sendFile(__dirname + '/bus.html');
+}
+
+app.get('/bus', bus_app);
+app.get('/bus/', bus_app);
+
+const io_bus = io.of("/bus");
+
+io_bus.use(wrap(session_middleware));
+
+var bus_interval = 0;
+
+function start_bus() {
+    const bus = state.bus;
+
+    bus.state = "program";
+
+    // Let all companions know the state of the game
+    io_bus.emit("state", bus.state);
+}
+
+function emit_bus_timer() {
+    const bus = state.bus;
+    io_bus.emit('timer', bus.timer);
+    bus.timer = bus.timer - 1;
+    if (bus.timer < 0) {
+       clearInterval(bus_interval);
+       bus.timer = 30;
+       setTimeout(start_bus, 3000);
+    }
+}
+
+function start_bus_timer() {
+    const bus = state.bus;
+    bus.timer = 3; // XXX: 30 in production
+    emit_bus_timer();
+    bus_interval = setInterval(emit_bus_timer, 1000);
+}
+
+io_bus.on("connection", (socket) => {
+    if (! socket.request.session.name) {
+       console.log("Error: Someone showed up at the Magic School Bus without a name.");
+       return;
+    }
+
+    const name = socket.request.session.name;
+    const bus = state.bus;
+
+    // Let the new user know the state of the bus
+    socket.emit("state", bus.state);
+
+    if (bus.students.count === 0) {
+       start_bus_timer();
+    }
+
+    if (! bus.students.names.includes(name)) {
+       bus.students.count = bus.students.count + 1;
+       io_bus.emit('students', bus.students.count);
+    }
+    bus.students.names.push(name);
+
+    socket.on('run', code => {
+       try {
+           output = child_process.execFileSync(python_path, [interpret_cairo_script, code], { input: code });
+           // Grab just first line of output
+           const nl = output.indexOf("\n");
+           if (nl === -1)
+               nl = undefined;
+           const filename = output.toString().substring(0, nl);
+           
+           // Give all clients the new image
+           io_bus.emit('output', filename);
+       } catch (e) {
+           console.log("Error executing turtle script: " + e);
+       }
+    });
+
+    socket.on('jumpstart', () => {
+       const bus = state.bus;
+
+       bus.state = "welcome";
+       io_bus.emit("state", bus.state);
+       io_bus.emit('students', bus.students.count);
+
+       start_bus_timer();
+    });
+
+    socket.on('disconnect', () => {
+       const names = bus.students.names;
+
+       names.splice(names.indexOf(name), 1);
+
+       if (! names.includes(name)) {
+           bus.students.count = bus.students.count - 1;
+           io_bus.emit('students', bus.students.count);
+       }
+    });
+});
+
 function tardis_app(req, res) {
-    res.sendFile(__dirname + '/tardis.html');
+    if (! req.session.name) {
+       res.sendFile(__dirname + '/tardis-error.html');
+    } else {
+       res.sendFile(__dirname + '/tardis.html');
+    }
 }
 
 app.get('/tardis', tardis_app);
@@ -250,6 +355,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 +374,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 +404,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 +428,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;