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

281 lines
7.2 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
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 sceneToFunction = function(scene) {
switch (scene) {
case 2:
2016-01-08 16:05:09 +01:00
return 'BobombScene';
case 3:
2016-01-08 16:05:09 +01:00
return 'MountainScene';
case 4:
2016-01-08 16:05:09 +01:00
return 'WhompScene';
default:
2016-01-08 16:05:09 +01:00
return 'PeachScene';
}
};
2015-08-04 18:59:24 +02:00
module.exports.game = function(req, res) {
2015-07-29 11:04:38 +02:00
req.session.experiments = req.session.experiments || [];
req.session.save();
2015-09-09 15:18:18 +02:00
db.checkUserId(req.session.userId, function(ok) {
2015-07-29 11:04:38 +02:00
2015-09-09 15:18:18 +02:00
if (ok) {
2015-07-29 11:04:38 +02:00
2015-09-09 15:18:18 +02:00
db.createExp(
req.session.userId,
req.session.experiments,
2015-10-06 10:48:50 +02:00
function(expId, sceneId, coinCombinationId, recommendationStyle, coins) {
2015-08-04 18:59:24 +02:00
2015-09-28 11:00:52 +02:00
// if (expId === undefined) {
2015-08-04 18:59:24 +02:00
2015-09-28 11:00:52 +02:00
// req.session.finished = true;
// req.session.save();
// return;
2015-07-29 11:04:38 +02:00
2015-09-28 11:00:52 +02:00
// }
2015-08-04 18:59:24 +02:00
2015-09-28 11:00:52 +02:00
// req.session.expId = expId;
// req.session.save();
2015-08-04 18:59:24 +02:00
2015-09-28 11:00:52 +02:00
// res.locals.scene = sceneToFunction(sceneId);
// res.locals.recommendationStyle = recommendationStyle;
// res.locals.coins = coins;
2015-09-09 15:18:18 +02:00
2015-09-28 11:00:52 +02:00
// res.setHeader('Content-Type','text/html');
// res.send("Ok");
2015-09-09 15:18:18 +02:00
});
} else {
}
2015-08-04 18:59:24 +02:00
});
};
2015-05-05 11:56:35 +02:00
2015-09-28 11:00:52 +02:00
module.exports.play = function(req, res) {
req.session.counter = req.session.counter === undefined ? 0 : req.session.counter + 1;
req.session.save();
if (req.session.counter > 2) {
res.redirect('/feedback');
return;
}
req.session.experiments = req.session.experiments || [];
2015-09-29 10:46:44 +02:00
db.getLastExp(req.session.userId, function(expId, sceneId, coinId, recoStyle, coins) {
2015-09-28 11:00:52 +02:00
2015-10-01 16:25:02 +02:00
// if (coinId === undefined) {
// console.log("=== ERROR : COIN_ID IS UNDEFINED ===");
// process.exit(-1)
// }
2015-09-28 11:00:52 +02:00
res.locals.scene = sceneToFunction(sceneId);
res.locals.recommendationStyle = recoStyle;
2015-09-28 11:00:52 +02:00
res.locals.coins = coins;
2015-10-01 16:25:02 +02:00
// var elt = req.session.experiments.find(function(elt) {
// return elt.coinCombinationId === coinId;
// });
// if (elt !== undefined) {
// console.log("=== ERROR DETECTED === user " + req.session.userId);
// console.log(req.session.experiments);
// console.log(coinId);
// process.exit(-1)
// }
req.session.experiments.push({
sceneId: sceneId,
recommendationStyle: recoStyle,
coinCombinationId : coinId
});
2015-09-29 10:46:44 +02:00
req.session.expId = expId;
req.session.save();
2016-01-08 16:05:09 +01:00
res.locals.lowRes = true;
2015-09-28 11:00:52 +02:00
// Prepare next experiment
module.exports.game(req, null);
res.setHeader('Content-Type', 'text/html');
res.render('prototype_recommendation.jade', res.locals, function(err, result) {
res.send(result);
});
});
};
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
module.exports.replayInfo = function(req, res) {
2015-05-19 15:43:09 +02:00
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);
if (res.locals.id <= 0) {
var err = new Error("This replay does not exist");
err.status = 404;
next(err);
return;
}
db.checkExpId(res.locals.id, function(sceneId) {
if (sceneId === null) {
var err = new Error("This replay does not exist");
err.status = 404;
next(err);
} else {
res.locals.initjs = sceneToFunction(sceneId);
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.replayIndex = 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) {
2015-09-09 15:18:18 +02:00
if (req.session.tutorialDone) {
2015-05-26 11:49:24 +02:00
2015-09-09 15:18:18 +02:00
res.redirect('/before-begin');
return;
2015-09-09 15:18:18 +02:00
}
db.checkUserId(req.session.userId, function(ok) {
if (ok) {
// 1 is the ID of peach scene
db.createTutorial(req.session.userId, function(id, coins) {
2015-09-28 11:00:52 +02:00
// Generate next experiment
module.exports.game(req, null);
2015-09-09 15:18:18 +02:00
req.session.tutorialDone = true;
req.session.expId = id;
res.locals.coins = coins;
req.session.save();
res.setHeader('Content-Type', 'text/html');
res.render('tutorial.jade', res.locals, function(err, result) {
res.send(result);
});
});
2015-09-09 15:18:18 +02:00
} else {
res.redirect('/');
}
2015-05-26 11:49:24 +02:00
});
};
2015-07-17 11:32:57 +02:00
2015-08-25 14:03:01 +02:00
function editorHelper(templateName) {
2015-07-17 11:32:57 +02:00
2015-08-25 14:03:01 +02:00
return function(req, res, next) {
2015-07-20 15:48:44 +02:00
2015-08-25 14:03:01 +02:00
var scene = req.params.scene;
2015-07-20 15:48:44 +02:00
2015-08-25 14:03:01 +02:00
switch (scene) {
2015-07-20 15:48:44 +02:00
2016-01-08 16:05:09 +01:00
case 'peach': res.locals.scene = "PeachScene"; break;
case 'coolcoolmountain': res.locals.scene = "MountainScene"; break;
case 'whomp': res.locals.scene = "WhompScene"; break;
case 'bobomb': res.locals.scene = "BobombScene"; break;
2015-08-25 14:03:01 +02:00
default:
// 404
var err = new Error('Incorrect scene');
err.status = 404;
next(err);
break;
2015-07-20 15:48:44 +02:00
2015-08-25 14:03:01 +02:00
}
2015-07-20 15:48:44 +02:00
2015-08-25 14:03:01 +02:00
res.setHeader('Content-Type', 'text/html');
res.render(templateName, res.locals, function(err, result) {
res.send(result);
});
2015-07-20 15:48:44 +02:00
2015-08-25 14:03:01 +02:00
};
2015-07-20 15:48:44 +02:00
2015-08-26 14:27:22 +02:00
}
2015-07-23 17:19:53 +02:00
2015-08-25 14:03:01 +02:00
module.exports.clicker = editorHelper('prototype_clicker.jade');
module.exports.viewer = editorHelper('prototype_viewer.jade');
module.exports.checker = editorHelper('prototype_checker.jade');
2015-08-24 17:13:14 +02:00
2015-07-23 17:19:53 +02:00
module.exports.userstudy = function(req, res) {
2015-09-09 15:18:18 +02:00
if (req.session.userId !== undefined) {
res.redirect('/prototype/tutorial');
return;
}
res.locals.identificationFailed = req.session.identificationFailed;
req.session.identificationFailed = false;
req.session.save();
res.locals.workerId = req.session.workerId;
res.setHeader('Content-Type', 'text/html');
res.render('user_study.jade', res.locals, function(err, result) {
2015-07-23 17:19:53 +02:00
res.send(result);
});
};
2015-08-31 13:05:05 +02:00
module.exports.next = function(req, res) {
2015-09-01 09:51:57 +02:00
res.redirect('/prototype/game');
2015-08-31 13:05:05 +02:00
};