2015-06-30 15:13:49 +02:00
|
|
|
var tools = require('../../lib/filterInt');
|
2015-05-18 17:51:20 +02:00
|
|
|
var pg = require('pg');
|
2015-05-22 12:04:39 +02:00
|
|
|
var pgc = require('../../private');
|
|
|
|
var db = require('./dbrequests');
|
2015-05-20 11:17:14 +02:00
|
|
|
|
2015-06-05 10:35:20 +02:00
|
|
|
// Shuffle array
|
|
|
|
function shuffle(array) {
|
|
|
|
var currentIndex = array.length, temporaryValue, randomIndex ;
|
|
|
|
|
|
|
|
// While there remain elements to shuffle...
|
|
|
|
while (0 !== currentIndex) {
|
|
|
|
|
|
|
|
// Pick a remaining element...
|
|
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
|
|
currentIndex -= 1;
|
|
|
|
|
|
|
|
// And swap it with the current element.
|
|
|
|
temporaryValue = array[currentIndex];
|
|
|
|
array[currentIndex] = array[randomIndex];
|
|
|
|
array[randomIndex] = temporaryValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomArray() {
|
|
|
|
var arr = [];
|
|
|
|
for (var i = 2; i < 5; i++) {
|
|
|
|
arr.push(i);
|
|
|
|
}
|
|
|
|
arr = shuffle(arr);
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2015-05-05 11:56:35 +02:00
|
|
|
module.exports.index = function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
|
2015-05-05 16:30:51 +02:00
|
|
|
res.render('index.jade', res.locals, function(err, result) {
|
2015-05-05 11:56:35 +02:00
|
|
|
res.send(result);
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-05 11:56:35 +02:00
|
|
|
|
2015-06-05 10:35:20 +02:00
|
|
|
var generateSceneNumber = function(req, res) {
|
|
|
|
if (req.session.scenes !== undefined) {
|
|
|
|
req.session.currentSceneIndex++;
|
|
|
|
} else {
|
|
|
|
req.session.scenes = randomArray();
|
|
|
|
req.session.currentSceneIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return req.session.scenes[req.session.currentSceneIndex];
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-05 10:35:20 +02:00
|
|
|
|
|
|
|
var sceneToFunction = function(scene) {
|
|
|
|
switch (scene) {
|
|
|
|
case 2:
|
2015-07-01 16:31:43 +02:00
|
|
|
return 'L3D.initBobomb';
|
2015-06-05 10:35:20 +02:00
|
|
|
case 3:
|
2015-07-01 16:31:43 +02:00
|
|
|
return 'L3D.initMountain';
|
2015-06-05 10:35:20 +02:00
|
|
|
case 4:
|
2015-07-01 16:31:43 +02:00
|
|
|
return 'L3D.initWhomp';
|
2015-06-05 10:35:20 +02:00
|
|
|
default:
|
2015-07-01 16:31:43 +02:00
|
|
|
return 'L3D.initPeach';
|
2015-06-05 10:35:20 +02:00
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-05 10:35:20 +02:00
|
|
|
|
2015-05-22 12:04:39 +02:00
|
|
|
var protoHelper = function(template) {
|
|
|
|
return function(req, res) {
|
2015-06-04 16:52:58 +02:00
|
|
|
db.tryUser(req.session.user_id, function(id) {
|
2015-06-05 10:35:20 +02:00
|
|
|
// Get random scene number
|
|
|
|
var scene = generateSceneNumber(req, res);
|
|
|
|
res.locals.scene = sceneToFunction(scene);
|
|
|
|
req.session.user_id = id;
|
|
|
|
|
|
|
|
db.createExp(id, req.session.scenes[req.session.currentSceneIndex], function(id) {
|
2015-06-04 16:52:58 +02:00
|
|
|
req.session.exp_id = id;
|
|
|
|
req.session.save();
|
|
|
|
res.setHeader('Content-Type','text/html');
|
|
|
|
res.render(template, res.locals, function(err, result) {
|
|
|
|
res.send(result);
|
|
|
|
});
|
2015-05-22 12:04:39 +02:00
|
|
|
});
|
2015-05-19 10:14:06 +02:00
|
|
|
});
|
2015-05-22 12:04:39 +02:00
|
|
|
};
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-05 11:56:35 +02:00
|
|
|
|
2015-05-22 12:04:39 +02:00
|
|
|
module.exports.arrows = protoHelper('prototype_arrows.jade');
|
|
|
|
module.exports.viewports = protoHelper('prototype_viewports.jade');
|
|
|
|
module.exports.reverse = protoHelper('prototype_reverse.jade');
|
2015-05-19 15:43:09 +02:00
|
|
|
|
2015-06-11 11:47:44 +02:00
|
|
|
module.exports.sponza = function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
|
|
|
|
res.render('sponza.jade', res.locals, function(err, result) {
|
|
|
|
res.send(result);
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-11 11:47:44 +02:00
|
|
|
|
2015-05-19 15:43:09 +02:00
|
|
|
module.exports.replay_info = function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
|
|
|
|
// Parse id
|
|
|
|
var id = tools.filterInt(req.params.id);
|
|
|
|
|
2015-05-22 12:04:39 +02:00
|
|
|
db.getInfo(id, function(results) {
|
|
|
|
res.send(JSON.stringify(results));
|
2015-05-20 10:47:27 +02:00
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-19 15:43:09 +02:00
|
|
|
|
2015-05-19 15:55:00 +02:00
|
|
|
module.exports.replay = function(req, res, next) {
|
2015-05-22 12:04:39 +02:00
|
|
|
// Get id parameter
|
2015-05-19 15:43:09 +02:00
|
|
|
res.locals.id = tools.filterInt(req.params.id);
|
|
|
|
|
2015-06-05 10:35:20 +02:00
|
|
|
db.checkExpId(res.locals.id, function(scene_id) {
|
|
|
|
if (scene_id === null) {
|
2015-05-22 12:04:39 +02:00
|
|
|
var err = new Error("This replay does not exist");
|
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
} else {
|
2015-06-05 10:35:20 +02:00
|
|
|
res.locals.initjs = sceneToFunction(scene_id);
|
2015-05-22 12:04:39 +02:00
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
res.render('prototype_replays.jade', res.locals, function(err, result) {
|
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-20 11:17:14 +02:00
|
|
|
|
|
|
|
module.exports.replay_index = function(req, res, next) {
|
2015-06-04 16:52:58 +02:00
|
|
|
db.getAllExps(function(result) {
|
2015-05-22 12:04:39 +02:00
|
|
|
res.locals.users = result;
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
2015-05-20 11:17:14 +02:00
|
|
|
res.render("replay_index.jade", res.locals, function(err, result) {
|
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-26 11:49:24 +02:00
|
|
|
|
|
|
|
module.exports.tutorial = function(req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
res.render('tutorial.jade', res.lcals, function(err, result) {
|
|
|
|
res.send(result);
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|