<commit_before>require.paths.unshift(__dirname + "/lib");

var Connect = require('connect');
var Postgres = require('postgres-pure'),
    Tmpl = require('tmpl'),
    sys = require('sys'),
    fs = require('fs');

var conn = new Postgres.Connection("postgres://tim@127.0.0.1/sousaball");
conn.addListener('error', function (err) {
    sys.error(err.stack);
});

var template_cache = {};
function load_template(template, callback) {
    if (template_cache[template]) {
        callback(null, template_cache[template]);
        return;
    }
    fs.readFile(__dirname + "/templates/" + template + ".tmpl", function (err, buffer) {
      if (err) {
        callback(err);
        return;
      }
      try {
           template_cache[template] = Tmpl(buffer.toString('utf8'));
      } catch (err) {
          callback(err);
          return;
      }
      callback(null, template_cache[template]);
      setTimeout(function () {
          delete template_cache[template];
      });
      
    });
}


function render_template(template, user, level, callback) {
 load_map(user, level, function (level_data) {
   var data = {
     "title": "SousaBall " + user + " - " + level,
     "user": user,
     "name": level,
     "level": level_data
   };
   load_template(template, function (err, tmpl) {
     if (err) {
       callback("Missing template: " + template);
       return;
     }
     callback(tmpl(data));
   });
 });
}

function load_map(user, level, callback) {
  conn.query("SELECT data FROM levels WHERE owner=:owner AND name=:name", {owner: user, name: level}, function (rows) {
    if (!rows.length) { 
      callback({error: "Missing level file", user: user, level: level});
      return;
    }
    var level_data = JSON.parse(rows[0].data);
    conn.query("SELECT data FROM tilesets WHERE owner=:owner AND name=:name", {owner: user, name: level_data.blockset}, function (rows) {
      if (!rows.length) { 
        callback({error: "Missing tileset", user: user, tileset: level_data.blockset});
        return;
      }
      level_data.blocks = JSON.parse(rows[0].data);
      callback(level_data);
    });
  });
}


function save_map(user, level, data, callback) {
  
  conn.query("UPDATE levels SET data=:data WHERE owner=:owner AND name=:name", {
    data: JSON.stringify(data),
    owner: user,
    name: level
  }, function () {
    callback();
  });
}


function play(req, res, user, level) {
    render_template('play', user, level, function (html) {
        res.simpleBody(200, html, "text/html");
    });
}

function edit(req, res, user, level) {
    render_template('edit', user, level, function (html) {
        res.simpleBody(200, html, "text/html");
    });
}

function save(req, res, user, level) {
    var data = req.body;
    save_map(user, level, data, function () {
        res.simpleBody(200, "Save " + user + "/" + level + ".level\n" + JSON.stringify(data));
    });
}

// Serve the App
new Connect.Server([
    {filter: "log"},
    {filter: "response-time"},
    {filter: "conditional-get"},
    {filter: "cache", param: {ramCache: 100}},
    {filter: "gzip"},
    // First serve static files
    {provider: "static", param: __dirname + "/public"},
    // Then the game logic as a router endpoint
    {filter: "body-decoder"},
    {provider: "router", param: function (server) {
        server.get(new RegExp('^/([^/]+)/([^/]+);edit$'), edit);
        server.get(new RegExp('^/([^/]+)/([^/]+)$'), play);
        server.post(new RegExp('^/([^/]+)/([^/]+)$'), save);
    }},
    {filter: "error-handler"}
]).listen();<commit_msg>Update sousaball example to latest syntax.<commit_after>require.paths.unshift(__dirname + "/lib");

var Connect = require('connect');
var Postgres = require('postgres-pure'),
    Tmpl = require('tmpl'),
    sys = require('sys'),
    fs = require('fs');

var conn = new Postgres.Connection("postgres://tim@127.0.0.1/sousaball");
conn.addListener('error', function (err) {
    sys.error(err.stack);
});

var template_cache = {};
function load_template(template, callback) {
    if (template_cache[template]) {
        callback(null, template_cache[template]);
        return;
    }
    fs.readFile(__dirname + "/templates/" + template + ".tmpl", function (err, buffer) {
      if (err) {
        callback(err);
        return;
      }
      try {
           template_cache[template] = Tmpl(buffer.toString('utf8'));
      } catch (err) {
          callback(err);
          return;
      }
      callback(null, template_cache[template]);
      setTimeout(function () {
          delete template_cache[template];
      });
      
    });
}


function render_template(template, user, level, callback) {
 load_map(user, level, function (level_data) {
   var data = {
     "title": "SousaBall " + user + " - " + level,
     "user": user,
     "name": level,
     "level": level_data
   };
   load_template(template, function (err, tmpl) {
     if (err) {
       callback("Missing template: " + template);
       return;
     }
     callback(tmpl(data));
   });
 });
}

function load_map(user, level, callback) {
  conn.query("SELECT data FROM levels WHERE owner=:owner AND name=:name", {owner: user, name: level}, function (rows) {
    if (!rows.length) { 
      callback({error: "Missing level file", user: user, level: level});
      return;
    }
    var level_data = JSON.parse(rows[0].data);
    conn.query("SELECT data FROM tilesets WHERE owner=:owner AND name=:name", {owner: user, name: level_data.blockset}, function (rows) {
      if (!rows.length) { 
        callback({error: "Missing tileset", user: user, tileset: level_data.blockset});
        return;
      }
      level_data.blocks = JSON.parse(rows[0].data);
      callback(level_data);
    });
  });
}


function save_map(user, level, data, callback) {
  
  conn.query("UPDATE levels SET data=:data WHERE owner=:owner AND name=:name", {
    data: JSON.stringify(data),
    owner: user,
    name: level
  }, function () {
    callback();
  });
}


function play(req, res, user, level) {
    render_template('play', user, level, function (html) {
        res.simpleBody(200, html, "text/html");
    });
}

function edit(req, res, user, level) {
    render_template('edit', user, level, function (html) {
        res.simpleBody(200, html, "text/html");
    });
}

function save(req, res, user, level) {
    var data = req.body;
    save_map(user, level, data, function () {
        res.simpleBody(200, "Save " + user + "/" + level + ".level\n" + JSON.stringify(data));
    });
}

// Serve the App
module.exports = new Connect.Server([
    {filter: "log"},
    {filter: "response-time"},
    {filter: "conditional-get"},
    {filter: "cache", param: {ramCache: 100}},
    {filter: "gzip"},
    // First serve static files
    {provider: "static", root: __dirname + "/public"},
    // Then the game logic as a router endpoint
    {filter: "body-decoder"},
    {provider: "router", param: function (server) {
        server.get(new RegExp('^/([^/]+)/([^/]+);edit$'), edit);
        server.get(new RegExp('^/([^/]+)/([^/]+)$'), play);
        server.post(new RegExp('^/([^/]+)/([^/]+)$'), save);
    }},
    {filter: "error-handler"}
]);
{'fraction_non_alphanumeric': 0.18052057094878254, 'fraction_numerical': 0.005877413937867338, 'mean_word_length': 2.9771841958820255, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4790980', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2261, 'n_words': 534}