Skip to content

Instantly share code, notes, and snippets.

@bqqbarbhg
Created September 18, 2013 23:23
Show Gist options
  • Save bqqbarbhg/6617170 to your computer and use it in GitHub Desktop.
Save bqqbarbhg/6617170 to your computer and use it in GitHub Desktop.
var $http = require('http');
var $url = require('url');
var $fs = require('fs');
var tick = 0;
var dwarves = [];
function rand(min, max)
{
return Math.floor(Math.random() * (max - min) + min);
}
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
function Dwarf() {
var firsts = ['Efrin', 'Olef', 'Ufdur', 'Borgol', 'Reggel', 'Rhos'];
var lasts = ['Defren', 'Bolverk', 'Feystin', 'Folkbiorn', 'Frothi'];
this.fname = firsts[rand(0, firsts.length)];
this.lname = Math.random() < 0.2 ? firsts[rand(0, firsts.length)] + 'son' : lasts[rand(0, lasts.length)];
this.sleep = rand(0, 100);
this.statestring = "Idle";
}
for (var i = 0; i < 15; i++)
{
dwarves[i] = new Dwarf();
}
function debugLog(str) {
console.log(str);
}
function onRequest(request, response) {
var url = $url.parse(request.url, true);
debugLog('Request for "' + url.pathname + '" received from ' + request.socket.remoteAddress + '.');
if (url.pathname == "/apple-touch-icon.png") {
debugLog("Special case");
response.writeHead(200, {'Content-Type': 'image/png'});
$fs.readFile("dwarf.png", function (err, content) {
debugLog("Special case success");
response.end(content, "binary");
});
return;
}
response.writeHead(200, {'Content-Type': 'text/html'});
resp = '';
resp += '<html><head><title>Dorf Fort</title><style>';
resp += 'body { padding: 0px; margin: 0px; }';
resp += '#topbar { background-color: #000; color: #fff; font-family: monospace; margin: 0px; padding: 0px; }';
resp += '.dwarf { margin: 0px; padding: 5px 5px; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; }';
resp += '.dwarf:nth-child(even) { background-color: #DDD; }';
resp += '.dwarf:nth-child(odd) { background-color: #FFF; }';
resp += '#cont { margin: 0px; padding: 0px; overflow: scroll; -webkit-overflow-scrolling: touch; height: 400px; }';
resp += 'h3 { margin-bottom: 0px; margin-top: 0px; }';
resp += 'h4 { color: #333; margin-top: 0px; margin-bottom: 0px; margin-left: 10px; }';
resp += '</style>'
resp += '<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0" />\
<meta name="apple-mobile-web-app-capable" content="yes" />\
<meta name="apple-mobile-web-app-status-bar-style" content="black" />';
resp += '</head><body>';
resp += '<div id=topbar>Tick {0} </div>'.format(tick);
resp += '<div id="cont">';
for (var i = 0; i < dwarves.length; i++)
{
resp += '<div class="dwarf">';
resp += '<h3>{0} {1}</h3><h4>{2}</h4>'.format(dwarves[i].fname, dwarves[i].lname, dwarves[i].statestring);
/*
resp += '<ul>';
resp += '<li>Tiredness: {0}</li>'.format(dwarves[i].sleep);
resp += '</ul>';
*/
resp += '</div>';
}
resp += '</div>';
resp += '<script>document.body.ontouchstart = function(e){e.preventDefault(); }; document.getElementById("cont").ontouchstart = function(e) { e.stopPropagation(); };</script>';
resp += '</body></html>';
response.end(resp);
}
function update() {
debugLog("Updating tick " + tick);
tick++;
setTimeout(update, 1000);
}
$http.createServer(onRequest).listen(1337);
setTimeout(update, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment