-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
98 lines (86 loc) · 2.71 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require.paths.unshift(__dirname + "/lib");
var Http = require('http'),
Stack = require('stack'),
Creationix = require('creationix'),
Corn = require('./corn'),
FS = require('fs');
// Loads a level from the database including the tileset
function loadMap(user, level, callback) {
FS.readFile(__dirname + "/data/" + user + "." + level + ".json", 'utf8', function (err, json) {
if (err) return callback(err);
try {
var levelData = JSON.parse(json);
levelData.blocks = require('./tilesets/' + levelData.blockset);
} catch (err) {
return callback(err);
}
callback(null, levelData);
});
}
// Save a map to the database
function saveMap(user, level, data, callback) {
FS.writeFile(__dirname + "/data/" + user + "." + level + ".json", data, callback);
}
// Load a template from disk and inject level data
function renderTemplate(template, user, level, callback) {
FS.readFile(__dirname + "/templates/" + template + ".tmpl", 'utf8', function (err, template) {
if (err) return callback(err);
Corn(template, {
"title": "SousaBall " + user + " - " + level,
"user": JSON.stringify(user),
"name": JSON.stringify(level),
"level": function (callback) {
loadMap(user, level, function (err, data) {
if (err) return callback(err);
callback(null, JSON.stringify(data));
});
}
}, callback);
});
}
// Http route handlers
function play(req, res, params, next) {
renderTemplate('play', params.user, params.level, function (err, html) {
if (err) { next(err); return; }
res.writeHead(200, {
"Content-Type": "text/html",
"Content-Length": Buffer.byteLength(html)
});
res.end(html);
});
}
function edit(req, res, params, next) {
renderTemplate('edit', params.user, params.level, function (err, html) {
if (err) { next(err); return; }
res.writeHead(200, {
"Content-Type": "text/html",
"Content-Length": Buffer.byteLength(html)
});
res.end(html);
});
}
function save(req, res, params, next) {
var json = "";
req.setEncoding('utf8');
req.on('data', function (chunk) {
json += chunk;
});
req.on('end', function () {
saveMap(params.user, params.level, json, function (err, result) {
if (err) { next(err); return; }
res.writeHead(200, {
"Content-Length": 0
});
res.end();
});
});
}
// Serve the App
Http.createServer(Stack(
Creationix.log(),
Creationix.route("POST", "/:user/:level", save),
Creationix.static("/", __dirname + "/public", "index.html"),
Creationix.route("GET", "/:user/:level;edit", edit),
Creationix.route("GET", "/:user/:level", play)
)).listen(3000);
console.log("Server listening at http://localhost:3000/");