Preloading of the experiments

This commit is contained in:
Thomas FORGIONE 2015-09-28 11:00:52 +02:00
parent 322fe99ea9
commit 96cb58b6f6
7 changed files with 121 additions and 20 deletions

View File

@ -15,6 +15,6 @@ block content
script.
document.getElementById('next').onclick = function() {
document.getElementById('next').disabled = true;
window.location = '/prototype/game';
window.location = '/prototype/play';
}

View File

@ -920,6 +920,65 @@ DBReq.ExpIdChecker.prototype.finish = function() {
this.finishAction(this.finalResult);
};
DBReq.LastExpGetter = function(userId, finishAction) {
var self = this;
this.userId = userId;
this.finishAction = finishAction;
this.finalResult = {};
pg.connect(pgc.url, function(err, client, release) {
self.client = client;
self.release = release;
self.execute();
});
};
DBReq.LastExpGetter.prototype.execute = function() {
var self = this;
this.client.query(
'SELECT scene_id AS "sceneId", \n' +
' coin_1, \n' +
' coin_2, \n' +
' coin_3, \n' +
' coin_4, \n' +
' coin_5, \n' +
' coin_6, \n' +
' coin_7, \n' +
' coin_8, \n' +
' Experiment.recommendation_style AS "recommendationStyle" \n' +
'FROM Experiment, CoinCombination \n' +
'WHERE Experiment.coin_combination_id = CoinCombination.id \n' +
' AND Experiment.user_id = $1 \n' +
'ORDER BY Experiment.id DESC \n' +
'LIMIT 1;',
[self.userId],
function (err, result) {
self.finalResult.sceneId = result.rows[0].sceneId;
self.finalResult.recommendationStyle = result.rows[0].recommendationStyle;
self.finalResult.coins = [
result.rows[0].coin_1,
result.rows[0].coin_2,
result.rows[0].coin_3,
result.rows[0].coin_4,
result.rows[0].coin_5,
result.rows[0].coin_6,
result.rows[0].coin_7,
result.rows[0].coin_8
];
self.finish();
}
);
};
DBReq.LastExpGetter.prototype.finish = function() {
this.release();
this.client = null;
this.release = null;
this.finishAction(this.finalResult.sceneId, this.finalResult.recommendationStyle, this.finalResult.coins);
}
/**
* Class that gets the info from all experiment
* @param finishAction {function} callback that has as a parameter which is an
@ -1133,4 +1192,8 @@ DBReq.getAllExps = function(callback) {
new DBReq.ExpGetter(callback);
};
DBReq.getLastExp = function(id, callback) {
new DBReq.LastExpGetter(id, callback);
};
module.exports = DBReq;

View File

@ -34,35 +34,62 @@ module.exports.game = function(req, res) {
req.session.userId,
function(expId, coinCombinationId, sceneId, recommendationStyle, coins) {
if (expId === undefined) {
// if (expId === undefined) {
res.redirect('/feedback');
return;
// req.session.finished = true;
// req.session.save();
// return;
}
// }
req.session.expId = expId;
req.session.save();
// req.session.expId = expId;
// req.session.save();
res.locals.scene = sceneToFunction(sceneId);
res.locals.recommendationStyle = recommendationStyle;
res.locals.coins = coins;
// res.locals.scene = sceneToFunction(sceneId);
// res.locals.recommendationStyle = recommendationStyle;
// res.locals.coins = coins;
res.setHeader('Content-Type','text/html');
res.render('prototype_recommendation.jade', res.locals, function(err, result) {
res.send(result);
});
// res.setHeader('Content-Type','text/html');
// res.send("Ok");
});
} else {
res.redirect('/');
}
});
};
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;
}
db.getLastExp(req.session.userId, function(sceneId, recoStyle, coins) {
res.locals.scene = sceneToFunction(sceneId);
res.locals.recommendationStyle= recoStyle;
res.locals.coins = coins;
// 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);
});
});
};
module.exports.sponza = function(req, res) {
res.setHeader('Content-Type', 'text/html');
@ -127,6 +154,10 @@ module.exports.tutorial = function(req, res) {
// 1 is the ID of peach scene
db.createTutorial(req.session.userId, function(id, coins) {
// Generate next experiment
module.exports.game(req, null);
req.session.tutorialDone = true;
req.session.expId = id;
res.locals.coins = coins;

View File

@ -10,5 +10,6 @@ module.exports = {
'/prototype/coin-viewer/:scene': 'viewer',
'/prototype/coin-checker/:scene': 'checker',
'/user-study': 'userstudy',
'/prototype/next': 'next'
'/prototype/next': 'next',
'/prototype/play': 'play'
};

View File

@ -27,7 +27,7 @@ window.onbeforeunload = function() {
};
var nextPage = '/prototype/game';
var nextPage = '/prototype/play';
Coin.onCoinGot = function(coin) {
if (coin === 6) {

View File

@ -9,6 +9,7 @@ var Colors = Object.freeze({
BLUE: '\033[34m',
MAGENTA: '\033[35m',
CYAN: '\033[36m',
ORANGE: '\033[38;5;202m',
});
var isDev = require('express')().get('env') === 'development';
@ -35,8 +36,8 @@ Log.request = function(req, res, time) {
'[REQ] ' + new Date() + ' ' +
(req.headers['x-forwarded-for'] || req.connection.remoteAddress) +
(time !== undefined ? (' in ' + (" " + time).slice(-6) + ' ms') : '') +
' : ' + req.url ,
Colors.CYAN
' : ' + (req.static && req.url !== '/favicon.ico' ? '/static' + req.url : req.url),
req.static ? Colors.YELLOW : Colors.CYAN
);
}
};
@ -76,7 +77,7 @@ if (isDev) {
Log.debug = function(info) {
log(
'[DBG] ' + (info !== undefined ? info : ''),
Colors.YELLOW
Colors.ORANGE
);
};
} else {

View File

@ -60,6 +60,11 @@ app.use(function(req, res, next) {
res.locals.alertCookie = true;
res.cookie('alreadyCame', true, {maxAge: 604800000}); // One week in ms
}
if (req.url.substr(0, 7) === '/static' || req.url === '/favicon.ico') {
req.static = true;
}
next();
});