"use strict";
//! {"description": "Massively multiplayer online Turtle Graphics"}
// This script runs server-side, once for each client session.
if (imports.terminal) {
terminal.setEcho(false);
terminal.split([
{
type: "horizontal",
children: [
{name: "graphics", basis: "700px", shrink: "0", grow: "0"},
{name: "text"},
],
},
]);
// Request a callback every time the user hits enter at the terminal prompt.
core.register("onInput", function(input) {
// Ask a persistent service session to broadcast our message. We'll also get a copy back.
return core.getService("turtle").then(function(service) {
return service.postMessage(input);
});
});
// Request a callback for every message that is broadcast.
core.register("onMessage", function(sender, message) {
if (message.history) {
for (var i = 0; i < message.history.length; i++) {
// Pass the message on to the iframe in the client.
terminal.postMessageToIframe("turtle", message.history[i]);
}
} else {
// Pass the message on to the iframe in the client.
terminal.postMessageToIframe("turtle", message);
}
});
core.register("onWindowMessage", function(data) {
if (data.message.ready) {
core.getService("turtle").then(function(service) {
return service.postMessage("sync");
});
} else {
terminal.print(data.message);
}
});
terminal.select("graphics");
terminal.print("MMO Turtle Graphics using ", {href: "http://codeheartjs.com/turtle/"}, ".");
// Add an iframe to the terminal. This is how we sandbox code running on the client.
var contents = `
`
terminal.print({iframe: contents, width: 640, height: 480, name: "turtle"});
terminal.select("text");
terminal.print("Supported commands: ", ["fd ", "bk ", "rt ", "lt ", "pu", "pd", "home", "reset", "clear"].join(", "));
} else {
var gHistory = null;
function ensureHistoryLoaded() {
if (!gHistory) {
return database.get("history").then(function(data) {
gHistory = JSON.parse(data);
return gHistory;
}).catch(function(error) {
gHistory = [];
return gHistory;
});
} else {
return new Promise(function(resolve, reject) { resolve(gHistory); });
}
}
core.register("onMessage", function(sender, message) {
return ensureHistoryLoaded().then(function(history) {
if (message == "reset") {
history.length = 0;
database.set("history", JSON.stringify(history));
return core.broadcast(message);
} else if (message == "sync") {
sender.postMessage({history: history});
} else {
history.push(message);
database.set("history", JSON.stringify(history));
return core.broadcast(message);
}
});
});
}