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-07-29 11:04:38 +02:00
|
|
|
|
|
|
|
function randomReco() {
|
|
|
|
var recoStyles = [
|
|
|
|
'prototype_empty.jade',
|
|
|
|
'prototype_viewports.jade',
|
|
|
|
'prototype_arrows.jade'
|
|
|
|
];
|
|
|
|
|
|
|
|
return shuffle(recoStyles);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-07-29 11:04:38 +02:00
|
|
|
var generateRecommendationStyle = function(req, res) {
|
|
|
|
|
|
|
|
if (req.session.recos === undefined) {
|
|
|
|
req.session.recos = randomReco();
|
|
|
|
}
|
|
|
|
|
|
|
|
return req.session.recos.shift();
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-07-29 11:04:38 +02:00
|
|
|
|
2015-05-22 12:04:39 +02:00
|
|
|
return function(req, res) {
|
2015-07-29 11:04:38 +02:00
|
|
|
|
|
|
|
template = generateRecommendationStyle(req, res);
|
|
|
|
|
|
|
|
if (template === undefined) {
|
|
|
|
res.redirect('/feedback');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2015-07-29 11:04:38 +02:00
|
|
|
db.createExp(id, req.session.scenes[req.session.currentSceneIndex], template, 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-07-29 11:04:38 +02:00
|
|
|
module.exports.empty = protoHelper('prototype_empty.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) {
|
|
|
|
|
2015-07-22 14:48:14 +02:00
|
|
|
db.tryUser(req.session.user_id, function(id) {
|
|
|
|
req.session.user_id = id;
|
2015-05-26 11:49:24 +02:00
|
|
|
|
2015-07-22 14:48:14 +02:00
|
|
|
// 1 is the ID of peach scene
|
2015-07-29 11:04:38 +02:00
|
|
|
db.createExp(id, 1, null, function(id) {
|
2015-07-22 14:48:14 +02:00
|
|
|
req.session.exp_id = id;
|
|
|
|
req.session.save();
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
2015-07-23 18:16:44 +02:00
|
|
|
res.render('tutorial.jade', res.locals, function(err, result) {
|
2015-07-22 14:48:14 +02:00
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
});
|
2015-05-26 11:49:24 +02:00
|
|
|
});
|
2015-07-22 14:48:14 +02:00
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-07-17 11:32:57 +02:00
|
|
|
|
|
|
|
module.exports.clicker = function(req, res, next) {
|
|
|
|
|
|
|
|
var scene = req.params.scene;
|
|
|
|
|
|
|
|
switch (scene) {
|
|
|
|
|
|
|
|
case 'peach': res.locals.scene = "L3D.initPeach"; break;
|
|
|
|
case 'coolcoolmountain': res.locals.scene = "L3D.initMountain"; break;
|
|
|
|
case 'whomp': res.locals.scene = "L3D.initWhomp"; break;
|
|
|
|
case 'bobomb': res.locals.scene = "L3D.initBobomb"; break;
|
|
|
|
default:
|
|
|
|
// 404
|
|
|
|
var err = new Error('Incorrect scene');
|
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
res.render('prototype_clicker.jade', res.locals, function(err, result) {
|
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
|
2015-07-17 11:39:42 +02:00
|
|
|
};
|
2015-07-20 15:48:44 +02:00
|
|
|
|
|
|
|
module.exports.viewer = function(req, res, next) {
|
|
|
|
|
|
|
|
var scene = req.params.scene;
|
|
|
|
|
|
|
|
switch (scene) {
|
|
|
|
|
|
|
|
case 'peach': res.locals.scene = "L3D.initPeach"; break;
|
|
|
|
case 'coolcoolmountain': res.locals.scene = "L3D.initMountain"; break;
|
|
|
|
case 'whomp': res.locals.scene = "L3D.initWhomp"; break;
|
|
|
|
case 'bobomb': res.locals.scene = "L3D.initBobomb"; break;
|
|
|
|
default:
|
|
|
|
// 404
|
|
|
|
var err = new Error('Incorrect scene');
|
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
res.render('prototype_viewer.jade', res.locals, function(err, result) {
|
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
2015-07-23 17:19:53 +02:00
|
|
|
|
|
|
|
module.exports.userstudy = function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
|
2015-07-23 18:16:44 +02:00
|
|
|
res.locals.identificationFailed = req.session.identificationFailed;
|
|
|
|
req.session.identificationFailed = false;
|
|
|
|
req.session.save();
|
|
|
|
|
|
|
|
res.render('user_study.jade', res.locals, function(err, result) {
|
2015-07-23 17:19:53 +02:00
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|