diff --git a/controllers/before-begin/views/index.jade b/controllers/before-begin/views/index.jade index 6451dd9..2ef82f0 100644 --- a/controllers/before-begin/views/index.jade +++ b/controllers/before-begin/views/index.jade @@ -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'; } diff --git a/controllers/prototype/dbrequests.js b/controllers/prototype/dbrequests.js index fc44c77..53b1f17 100644 --- a/controllers/prototype/dbrequests.js +++ b/controllers/prototype/dbrequests.js @@ -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; diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index 7e20d37..42de5ee 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -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; diff --git a/controllers/prototype/urls.js b/controllers/prototype/urls.js index dd8da62..c8016d8 100644 --- a/controllers/prototype/urls.js +++ b/controllers/prototype/urls.js @@ -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' }; diff --git a/js/l3d/apps/prototype/interactive/main.js b/js/l3d/apps/prototype/interactive/main.js index 9c546ff..e45bf5d 100644 --- a/js/l3d/apps/prototype/interactive/main.js +++ b/js/l3d/apps/prototype/interactive/main.js @@ -27,7 +27,7 @@ window.onbeforeunload = function() { }; -var nextPage = '/prototype/game'; +var nextPage = '/prototype/play'; Coin.onCoinGot = function(coin) { if (coin === 6) { diff --git a/lib/NodeLog.js b/lib/NodeLog.js index a117abb..6185e24 100644 --- a/lib/NodeLog.js +++ b/lib/NodeLog.js @@ -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 { diff --git a/server.js b/server.js index 6b0311c..b10ed1d 100644 --- a/server.js +++ b/server.js @@ -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(); });