forked from cory/tildefriends
198 lines
4.3 KiB
JavaScript
198 lines
4.3 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
//! {"require": ["ui"]}
|
||
|
|
||
|
terminal.setEcho(false);
|
||
|
terminal.setTitle("Programming Contest Test");
|
||
|
|
||
|
let kProblem = {
|
||
|
description: "Write a function foo that returns a number.",
|
||
|
tests: [
|
||
|
[1, 1],
|
||
|
[2, 2],
|
||
|
[4, 4],
|
||
|
],
|
||
|
default: `// Problem 1
|
||
|
// ${core.user.name}
|
||
|
// ${new Date()}
|
||
|
|
||
|
function foo() {
|
||
|
print("hi cory");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
foo();`,
|
||
|
};
|
||
|
|
||
|
function back() {
|
||
|
terminal.split([{name: "terminal"}]);
|
||
|
if (gEditEvent) {
|
||
|
gEditEvent.back();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function runScript(script, input) {
|
||
|
var results = {};
|
||
|
try {
|
||
|
var output = [];
|
||
|
function print() {
|
||
|
for (var i in arguments) {
|
||
|
output.push(arguments[i].toString());
|
||
|
}
|
||
|
output.push("\n");
|
||
|
}
|
||
|
results.results = eval(script || "");
|
||
|
results.output = output.join("");
|
||
|
} catch (error) {
|
||
|
results.error = error;
|
||
|
}
|
||
|
return results;
|
||
|
}
|
||
|
|
||
|
function sendResults(document) {
|
||
|
var results = runScript(document);
|
||
|
var message = `${kProblem.description}
|
||
|
|
||
|
Return value:
|
||
|
${results.results}
|
||
|
|
||
|
Output:
|
||
|
${results.output}
|
||
|
|
||
|
`;
|
||
|
|
||
|
if (results.error) {
|
||
|
message += `Error:
|
||
|
${results.error}`;
|
||
|
}
|
||
|
|
||
|
terminal.postMessageToIframe("iframe", { results: message });
|
||
|
}
|
||
|
|
||
|
function loadScript() {
|
||
|
return database.get(core.user.name).then(function(script) {
|
||
|
return script || kProblem.default;
|
||
|
}).catch(function(error) {
|
||
|
return kProblem.default;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
core.register("onWindowMessage", function(event) {
|
||
|
if (event.message.ready) {
|
||
|
loadScript().then(function(script) {
|
||
|
terminal.postMessageToIframe("iframe", {contents: script});
|
||
|
sendResults(script);
|
||
|
});
|
||
|
} else if (event.message.contents) {
|
||
|
database.set(core.user.name, event.message.contents).then(function() {
|
||
|
sendResults(event.message.contents);
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
//if (!core.user.credentials.permissions || !core.user.credentials.permissions.authenticated) {
|
||
|
// terminal.print("Please authenticate.");
|
||
|
//} else {
|
||
|
showEditor();
|
||
|
//}
|
||
|
|
||
|
function showEditor() {
|
||
|
terminal.split([{name: "terminal", type: "vertical"}]);
|
||
|
terminal.clear();
|
||
|
terminal.print({iframe: `<html>
|
||
|
<head>
|
||
|
<script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.2/codemirror.min.js"></script>
|
||
|
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.2/codemirror.min.css"></link>
|
||
|
<style>
|
||
|
html {
|
||
|
height: 100%;
|
||
|
margin: 0;
|
||
|
padding: 0;
|
||
|
}
|
||
|
body {
|
||
|
display: flex;
|
||
|
flex-direction: column;
|
||
|
height: 100%;
|
||
|
margin: 0;
|
||
|
padding: 0;
|
||
|
}
|
||
|
#menu {
|
||
|
flex: 0 0 auto;
|
||
|
}
|
||
|
#container {
|
||
|
flex: 1 1 auto;
|
||
|
display: flex;
|
||
|
flex-direction: row;
|
||
|
width: 100%;
|
||
|
background-color: white;
|
||
|
}
|
||
|
.CodeMirror {
|
||
|
width: 100%;
|
||
|
height: 100%;
|
||
|
}
|
||
|
.CodeMirror-scroll {
|
||
|
}
|
||
|
#edit { background-color: white }
|
||
|
#preview {
|
||
|
background-color: #ccc;
|
||
|
font-family: monospace;
|
||
|
}
|
||
|
#edit, #preview {
|
||
|
display: flex;
|
||
|
overflow: auto;
|
||
|
flex: 0 0 50%;
|
||
|
}
|
||
|
</style>
|
||
|
<script>
|
||
|
var gEditor;
|
||
|
function index() {
|
||
|
parent.postMessage({index: true}, "*");
|
||
|
}
|
||
|
function submit() {
|
||
|
parent.postMessage({
|
||
|
contents: gEditor.getValue(),
|
||
|
}, "*");
|
||
|
}
|
||
|
function cursorActivity() {
|
||
|
var selection = gEditor.listSelections();
|
||
|
var key = "test";
|
||
|
var a = selection[0].anchor;
|
||
|
var b = selection[0].head;
|
||
|
if (b.line < a.line || a.line == b.line && b.ch < a.ch) {
|
||
|
[a, b] = [b, a];
|
||
|
}
|
||
|
parent.postMessage({cursor: {start: a, end: b}}, "*");
|
||
|
}
|
||
|
</script>
|
||
|
<script>
|
||
|
window.addEventListener("message", function(message) {
|
||
|
if (message.data.contents) {
|
||
|
gEditor.setValue(message.data.contents);
|
||
|
} else if (message.data.results) {
|
||
|
preview.innerText = message.data.results;
|
||
|
}
|
||
|
}, false);
|
||
|
|
||
|
window.addEventListener("load", function() {
|
||
|
gEditor = CodeMirror.fromTextArea(document.getElementById("contents"), {
|
||
|
lineNumbers: true
|
||
|
});
|
||
|
|
||
|
parent.postMessage({ready: true}, "*");
|
||
|
});
|
||
|
</script>
|
||
|
</head>
|
||
|
<body>
|
||
|
<div id="menu">
|
||
|
<input type="button" value="Back" onclick="index()">
|
||
|
` + (core.user.credentials.permissions && core.user.credentials.permissions.authenticated ? `
|
||
|
<input type="button" value="Save" onclick="submit()">
|
||
|
` : "") +
|
||
|
` </div>
|
||
|
<div id="container">
|
||
|
<div id="edit"><textarea id="contents"></textarea></div>
|
||
|
<div id="preview"></div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>`, name: "iframe", style: "flex: 1 1 auto; border: 0; width: 100%"});
|
||
|
}
|