3d-interface/controllers/prototype/index.js

148 lines
4.0 KiB
JavaScript
Raw Normal View History

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');
var pgc = require('../../private');
var db = require('./dbrequests');
2015-05-20 11:17:14 +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-05-05 11:56:35 +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];
};
var sceneToFunction = function(scene) {
switch (scene) {
case 2:
2015-07-01 16:31:43 +02:00
return 'L3D.initBobomb';
case 3:
2015-07-01 16:31:43 +02:00
return 'L3D.initMountain';
case 4:
2015-07-01 16:31:43 +02:00
return 'L3D.initWhomp';
default:
2015-07-01 16:31:43 +02:00
return 'L3D.initPeach';
}
};
var protoHelper = function(template) {
return function(req, res) {
db.tryUser(req.session.user_id, function(id) {
// 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) {
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-05 11:56:35 +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-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);
db.getInfo(id, function(results) {
res.send(JSON.stringify(results));
2015-05-20 10:47:27 +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) {
// Get id parameter
2015-05-19 15:43:09 +02:00
res.locals.id = tools.filterInt(req.params.id);
db.checkExpId(res.locals.id, function(scene_id) {
if (scene_id === null) {
var err = new Error("This replay does not exist");
err.status = 404;
next(err);
} else {
res.locals.initjs = sceneToFunction(scene_id);
res.setHeader('Content-Type', 'text/html');
res.render('prototype_replays.jade', res.locals, function(err, result) {
res.send(result);
});
}
});
};
2015-05-20 11:17:14 +02:00
module.exports.replay_index = function(req, res, next) {
db.getAllExps(function(result) {
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-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);
});
};