"use strict"; terminal.setEcho(false); terminal.setPrompt("🍔"); terminal.split([ { type: "horizontal", children: [ { name: "form", }, { type: "vertical", basis: "240px", grow: "0", shrink: "0", children: [ { name: "image", basis: "8em", shrink: "0", }, { name: "fizzbuzz", grow: "1", }, ], }, ], }, ]); database.getAll().then(function(data) { for (var i = 0; i < data.length; i++) { database.remove(data[i]); } }); let kBurger = ` ...... /'.'.'.\\ &%@%@%@& (######) \\,,,,,,/`; terminal.select("image"); terminal.print(kBurger); terminal.select("form"); function countUp() { var current = 0; function countUpNext() { terminal.select("fizzbuzz"); ++current; var result = ""; if (current % 3 == 0) { result += "Fizz"; } if (current % 5 == 0) { result += "Buzz"; } if (current % 150 == 0) { result += "Grumble"; } if (result == "") { result = current.toString(); } terminal.print(result); terminal.select("form"); setTimeout(countUpNext, 1000); }; setTimeout(countUpNext, 1000); } countUp(); var user = core.user.name; let kIntroduction = [ {style: "font-size: xx-large; font-family: sans", value: "Summer of Cory"}, "Hi, it is warm outside again, and as is tradition, I want you to join me "+ "after work for some burgers on my deck. There are some quick questions "+ "below to gauge interest and availability. Please answer as best you can "+ "by clicking the options below, and I will get back to you with a plan.", [], ]; let kBottom = [ { iframe: ` `, style: "border: 0; width: 640px; height: 240px; margin: 0; padding: 0", }, ]; let kQuestions = [ {text: "Do you want to be a part of the Summer of Cory?", options: ["yes", "no"]}, {text: "Tuesday after work works best for me. Does that work for you?", options: ["yes", "no"]}, {text: "How often would you expect to join us?", options: ["weekly", "fortnightly", "monthly", "impossible to predict"]}, //{text: "Pick numbers", options: ["1", "2", "3"], allowMultiple: true}, //{text: "Pick a letter", options: ["a", "b", "c", "d"]}, //{text: "Pick whatever", options: ["1", "2", "3", "a", "b", "c", "d"], writeIn: true}, ]; let choices = []; let writeIn = []; let writingIn = undefined; function getOptions(index) { return kQuestions[index].options.concat(writeIn[index] || []); } function isChosen(question, choice) { let selected = false; if (kQuestions[question].allowMultiple) { selected = choices[question] && choices[question].indexOf(choice) != -1; } else { selected = choices[question] == choice; } return selected; } function readData() { return database.getAll().then(function(keys) { var promises = []; for (var i = 0; i < keys.length; i++) { promises.push(database.get(keys[i])); } return Promise.all(promises); }).then(function(allData) { writeIn = []; for (var i = 0; i < allData.length; i++) { if (allData[i]) { var entry = JSON.parse(allData[i]); if (entry && entry.writeIn) { for (var j = 0; j < entry.writeIn.length; j++) { if (writeIn.indexOf(entry.writeIn[j]) == -1) { writeIn.push(entry.writeIn[j]); } } } } } return database.get("data_" + user); }).then(function(data) { if (data) { choices = JSON.parse(data).choices; } }).catch(function(error) { terminal.select("image"); terminal.print(error); terminal.select("form"); }); } function writeData() { return database.set("data_" + user, JSON.stringify({choices: choices, writeIn: writeIn})).catch(function(error) { terminal.select("image"); terminal.print(error); terminal.select("form"); }); } function render() { return readData().then(function() { terminal.cork(); try { terminal.clear(); for (let line in kIntroduction) { terminal.print(kIntroduction[line]); } let allowFill = writingIn === undefined; for (let i in kQuestions) { let availableOptions = getOptions(i); let options = []; for (let j in availableOptions) { options.push(" "); let value = (isChosen(i, availableOptions[j]) ? "☒" : "☐") + availableOptions[j]; if (allowFill) { options.push({ command: JSON.stringify({action: "toggle", question: i, option: availableOptions[j]}), value: value, }); } else { options.push(value); } } if (kQuestions[i].writeIn) { options.push(" or "); if (writingIn !== undefined) { options.push("write in another option below") options.push(" "); options.push({command: JSON.stringify({action: "endWriteIn"}), value: "cancel"}); } else { options.push({command: JSON.stringify({action: "beginWriteIn", question: i}), value: "write in another option"}); } } terminal.print(kQuestions[i].text, ":", options); terminal.print(); } } finally { for (let line in kBottom) { terminal.print(kBottom[line]); } terminal.uncork(); } }); } core.register("onInput", function(input) { if (writingIn !== undefined && input != "cancel") { readData().then(function() { if (!writeIn[writingIn]) { writeIn[writingIn] = []; } if (input && writeIn[writingIn].indexOf(input) == -1) { writeIn[writingIn].push(input); } writingIn = undefined; terminal.setPrompt("> "); return writeData(); }).then(render); } else { let command = JSON.parse(input); if (command.action == "toggle") { readData().then(function() { if (kQuestions[command.question].allowMultiple) { if (choices[command.question] && choices[command.question].indexOf(command.option) != -1) { choices[command.question].splice(choices[command.question].indexOf(command.option), 1); } else { if (!choices[command.question]) { choices[command.question] = []; } choices[command.question].push(command.option); } } else { choices[command.question] = command.option; } return writeData(); }).then(render); } else if (command.action == "beginWriteIn") { writingIn = command.question; terminal.setPrompt('Enter new option for "' + kQuestions[command.question].text + '": '); render(); } else if (command.action == "endWriteIn") { writingIn = undefined; terminal.setPrompt("> "); render(); } } }); render().catch(function(error) { terminal.print(error); });