Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added custom loading method for loading javascript code from user def…

…ined code nuggets stored in database
  • Loading branch information...
commit e2701d3a30896b79e58890a284cecfed35d38504 1 parent b9af3dd
@jonoxia authored
View
25 listnuggets.py
@@ -0,0 +1,25 @@
+#!/usr/bin/python
+from database_tables import UserCodeNugget
+
+import simplejson
+
+nuggets = UserCodeNugget.select()
+
+print "Content-type: text/html"
+print
+
+json = { "monsters": [],
+ "obstacles": [],
+ "powerups": [] }
+
+for nug in nuggets:
+ url = "nugget.py?id=%s" % nug.id
+ if nug.type == "monster":
+ json["monsters"].append(url)
+ if nug.type == "obstacle":
+ json["obstacles"].append(url)
+ if nug.type == "powerups":
+ json["obstacles"].append(url)
+
+print simplejson.dumps(json)
+
View
71 nugget-loader.js
@@ -0,0 +1,71 @@
+
+function testCode() {
+ var code = $("#code-input").val();
+ try {
+ var filename = "blarg";
+ var f = new Function("require", "exports", "module", code);
+ var context = {};
+ var exports = {};
+ var module = {id: "blarg", uri: filename};
+ f.call(context, require, exports, module);
+ var monster = exports.monster;
+
+ //DefineEnemy(testmodule.monster);*/
+ /*var monster = require("nugget.py?id=3").monster;*/
+ $("#debug").html( "Blarg: " + monster.name);
+ $("#img-preview").attr("src", monster.imageUrl);
+ }
+ catch(x) {
+ $("#debug").html("Error: " + x);
+ }
+}
+
+// Need to modify require.js so that it loads from URLs (done)
+// Then we can require urls like nugget.py?id=x
+// next, when you save a nugget it should extract name, type, and image from the code you put in
+// (if it can't extract those, then saving it is invalid!)
+
+// Then, in the level editor, generate options menu using the METADATA from every VALID code nugget.
+// When one of these things is used in the level, it saves a LevelObject using the FULLY QUALIFIED name, i.e.
+// URL#entity
+// When loading a level for play, hitting any LevelObject with a # in its type triggers
+// something like a
+// nugget = require(url);
+// if (nugget.monster) {
+// DefineEnemy(nugget.monster);
+// }
+// if (nugget.powerup) {
+// DefinePowerUp(nugget.powerup);
+// }
+// etc.
+
+
+// The level needs to have a list of URLs that it is gonna try to include?
+// Or we try to include ALL nuggets and just leave out the ones that raise exceptions?
+
+
+function saveCode() {
+ var url = "nugget-editor.py"
+ $.ajax({type: "POST",
+ url: url,
+ data: {"id": $("#nugget-id").attr("value"),
+ "nugget-name": $("#nugget-name").val(),
+ "nugget-type": $("#nugget-type").val(),
+ "code-input": $("#code-input").val()
+ },
+ success: function(data, textStatus, jqXHR) {
+ $("#debug").html(data);
+ },
+ error: function(data, textStatus, thing) {
+ $("#debug").html(thing);
+ },
+ dataType: "text"
+ });
+}
+
+function loadCode() {
+ var url = "nugget.py"
+ $.get(url, {id: nuggetId}, function(data, textStatus, jqXHR) {
+ $("#code-input").val(data);
+ }, "html");
+}
View
20 nugget.py
@@ -0,0 +1,20 @@
+#!/usr/bin/python
+from database_tables import UserCodeNugget
+
+import cgi
+import cgitb
+
+cgitb.enable()
+q = cgi.FieldStorage()
+nuggetId = q.getfirst("id", "")
+
+nugget = None
+if nuggetId != "":
+ nugget = UserCodeNugget.get(int(nuggetId))
+
+print "Content-type: text/javascript"
+print
+if nugget != None:
+ print nugget.code
+else:
+ print "No nugget here"
View
87 require.js
@@ -0,0 +1,87 @@
+/*
+ * An implementation of the CommonJS Modules 1.0
+ * Copyright (c) 2009 by David Flanagan
+ * http://www.davidflanagan.com/demos/require.js
+ */
+var require = function require(id) {
+ var origid = id, filename;
+
+ // If the module id is relative, convert it to a toplevel id
+ // The normalize function is below.
+ if (id.substring(0,2) == "./" || id.substring(0,3) == "../")
+ id = normalize(require._current_module_dir, id);
+
+ // Now resolve the toplevel id relative to require.dir
+ if (id.indexOf(".py") == -1) { // Jono hack - don't put ".js" on python urls
+ filename = require.dir + id + ".js";
+ } else {
+ filename = id;
+ }
+
+ // Only load the module if it is not already cached.
+ if (!require._cache.hasOwnProperty(filename)) {
+
+ // Remember the directory we're loading this module from
+ var olddir = require._current_module_dir;
+ require._current_module_dir = id.substring(0, id.lastIndexOf('/')+1);
+
+ try {
+ // Load the text of the module
+ var modtext = gettext(filename);
+ // Wrap it in a function
+ var f = new Function("require", "exports", "module", modtext);
+ // Prepare function arguments
+ var context = {}; // Invoke on empty obj
+ var exports = require._cache[filename] = {}; // API goes here
+ var module = { id: id, uri: filename }; // For Modules 1.1
+ f.call(context, require, exports, module); // Execute the module
+ }
+ catch(x) {
+ throw new Error("Can't load module " + origid + ": " + x);
+ }
+ finally { // Restore the directory we saved above
+ require._current_module_dir = olddir;
+ }
+ }
+ return require._cache[filename]; // Return the module API from the cache
+
+ /* Return the text of the specified url, script element or file */
+ function gettext(url) {
+ if (typeof XMLHttpRequest !== "undefined") { // Running in a browser
+ var req = new XMLHttpRequest();
+ req.open("GET", url, false); // Note synchronous get
+ req.send(null);
+ if (req.status && req.status != 200) throw req.statusText;
+ return req.responseText;
+ }
+ else if (typeof readUrl == "function") return readUrl("file:" + url);
+ else if (typeof snarf == "function") return snarf(url); // Tracemonkey
+ else if (typeof read == "function") return read(url); // V8
+ else throw "No mechanism to load module text";
+ }
+
+ function normalize(dir, file) {
+ for(;;) {
+ if (file.substring(0,2) == "./")
+ file = file.substring(2);
+ else if (file.substring(0,3) == "../") {
+ file = file.substring(3);
+ dir = up(dir);
+ }
+ else break;
+ }
+ return dir+file;
+
+ function up(dir) { // Return the parent directory of dir
+ if (dir == "") throw "Can't go up from ''";
+ if (dir.charAt(dir.length-1) != "/") throw "dir doesn't end in /";
+ return dir.substring(0, dir.lastIndexOf('/', dir.length-2)+1);
+ }
+ }
+};
+
+// Set require.dir to point to the directory from which modules should be
+// loaded. It must be an empty string or a string that ends with "/".
+require.dir = "";
+require._cache = {}; // So we only load modules once
+require._current_module_dir = ""; // For relative module names
View
16 strings.json
@@ -60,7 +60,13 @@
"_published_yes": "Yes",
"_published_no": "No",
"_other_player_score": "%(time)s by %(player)s with %(trinkets)d trinkets",
- "_your_score": "%(time)s with %(trinkets)d trinkets"
+ "_your_score": "%(time)s with %(trinkets)d trinkets",
+ "_code_editor": "code editor",
+ "_nugget_id": "nugget id",
+ "_nugget_name": "nugget name",
+ "_nugget_type": "nugget type",
+ "_save_code": "save code",
+ "_test_code": "test code"
},
"jp": {
"_runjumpbuild": "ラン・ジャンプ・ビルド",
@@ -123,7 +129,13 @@
"_published_yes": "公開中",
"_published_no": "未公開",
"_other_player_score": "%(player)s さんの成績:%(time)s お宝 %(trinkets)d 個",
- "_your_score": "%(time)s お宝 %(trinkets)d 個"
+ "_your_score": "%(time)s お宝 %(trinkets)d 個",
+ "_code_editor": "code editor",
+ "_nugget_id": "nugget id",
+ "_nugget_name": "nugget name",
+ "_nugget_type": "nugget type",
+ "_save_code": "save code",
+ "_test_code": "test code"
}
}
View
12 templates/nugget-editor.html
@@ -3,19 +3,21 @@
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>${_runjumpbuild} &mdash; ${_code_editor}</title>
<script src="jquery.js"></script>
+ <script src="require.js"></script>
<script src="nugget-loader.js"></script>
</head>
<body>
<form action="nugget-editor.py" method="POST">
- <input type="hidden" name="id" value="${nugget_id}"/>
- ${_nugget_name}: <input type="text" name="nugget-name"
+ <input type="hidden" name="id" id="nugget-id" value="${nugget_id}"/>
+ ${_nugget_name}: <input type="text" name="nugget-name" id="nugget-name"
value="${nugget_name}"/><br/>
- ${_nugget_type}: <input type="text" name="nugget-type"
+ ${_nugget_type}: <input type="text" name="nugget-type" id="nugget-type"
value="${nugget_type}"/><br/>
- <textarea rows="30" cols="80" name="code-input"/>${nugget_code}</textarea>
- <button id="test" onclick="testCode()">${_test_code}</button><br/>
+ <textarea rows="30" cols="80" name="code-input" id="code-input"/>${nugget_code}</textarea>
<input type="submit" value="${_save_code}"/><br/>
</form>
+ <button id="test" onclick="testCode()">${_test_code}</button><br/>
<span id="debug"></span>
+ <img id="img-preview"/>
</body>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.