diff --git a/controllers/prototype/dbrequests.js b/controllers/prototype/dbrequests.js index 811a7f3..1d557f2 100644 --- a/controllers/prototype/dbrequests.js +++ b/controllers/prototype/dbrequests.js @@ -471,13 +471,19 @@ DBReq.Info.prototype.loadRedCoins = function() { * @memberof DBReq * @constructor */ -DBReq.UserCreator = function(finishAction) { +DBReq.UserCreator = function(workerId, age, male, rating, lastTime, finishAction) { /** * Callback to call on the id when the user is created * @type {function} */ this.finishAction = finishAction; + this.workerId = workerId; + this.age = age; + this.male = male; + this.rating = rating; + this.lastTime = lastTime; + // Connect to db var self = this; pg.connect(pgc.url, function(err, client, release) { @@ -492,25 +498,49 @@ DBReq.UserCreator = function(finishAction) { */ DBReq.UserCreator.prototype.execute = function() { var self = this; - this.client.query( - "INSERT INTO users DEFAULT VALUES; SELECT currval('users_id_seq');", - [], - function(err, result) { - self.finalResult = result.rows[0].currval; - self.finish(); - } - ); + this.client.query("BEGIN; LOCK Users;", [], function() { + self.client.query( + "INSERT INTO users(worker_id, age, male, rating, lasttime) VALUES($1, $2, $3, $4, $5);", + [ + self.workerId, + self.age, + self.male, + self.rating, + self.lastTime + ], + function(err, result) { + if (err !== null) { + Log.dberror(err + ' in UserCreator INSERT INTO'); + } + self.client.query( + "SELECT max(id) FROM Users;", + [], + function(err, result) { + self.finalResult = result.rows[0].max; + self.finish(); + } + ); + } + ); + }); }; /** * Release the DB connection and call the callback */ DBReq.UserCreator.prototype.finish = function() { - this.release(); - this.client = null; - this.release = null; - this.finishAction(this.finalResult); + var self = this; + + this.client.query("COMMIT;", [], function() { + + self.release(); + self.client = null; + self.release = null; + + self.finishAction(self.finalResult); + + }); }; /** @@ -535,8 +565,9 @@ DBReq.ExpCreator = function(userId, finishAction) { self.release = release; // Start transaction and lock table - self.client.query("BEGIN; LOCK CoinCombination; LOCK Experiment;"); - self.execute(); + self.client.query("BEGIN; LOCK CoinCombination; LOCK Experiment;", [], function() { + self.execute(); + }); }); }; @@ -591,6 +622,9 @@ DBReq.ExpCreator.prototype.execute = function() { "LIMIT 1;", [self.userId], function(err, result) { + if (err !== null) { + Log.dberror(err + ' in ExpCreator first request'); + } if (result.rows.length > 0) { // Set the result self.finalResult.coinCombinationId = result.rows[0].id; @@ -614,6 +648,9 @@ DBReq.ExpCreator.prototype.execute = function() { "RETURNING id", [self.userId, result.rows[0].id, result.rows[0].name], function(err, result) { + if (err !== null) { + Log.dberror(err + ' in ExpCreator second request (with suggested experiment)'); + } self.finalResult.expId = result.rows[0].id; self.finish(); } @@ -645,6 +682,9 @@ DBReq.ExpCreator.prototype.execute = function() { "LIMIT 1;", [self.userId], function(err, result) { + if (err !== null) { + Log.dberror(err + ' in ExpCreator second request (without suggested experiment'); + } if (result.rows.length > 0) { self.finalResult.sceneId = result.rows[0].sceneId; self.finalResult.recommendationStyle = result.rows[0].name; @@ -658,6 +698,9 @@ DBReq.ExpCreator.prototype.execute = function() { "LIMIT 8;", [self.finalResult.sceneId], function(err, result) { + if (err !== null) { + Log.dberror(err + ' in ExpCreator third request (without suggested experiment'); + } self.finalResult.coins = []; for (var i = 0; i < 8; i++) { self.finalResult.coins.push(result.rows[i].id); @@ -680,6 +723,9 @@ DBReq.ExpCreator.prototype.execute = function() { self.finalResult.coins[7], ], function(err, result) { + if (err !== null) { + Log.dberror(err + ' in ExpCreator fourth request (without suggested experiment'); + } self.finalResult.coinCombinationId = result.rows[0].id; // And create the experiment @@ -689,6 +735,9 @@ DBReq.ExpCreator.prototype.execute = function() { "RETURNING id;", [self.userId, self.finalResult.coinCombinationId, self.finalResult.recommendationStyle], function(err, result) { + if (err !== null) { + Log.dberror(err + ' in ExpCreator fifth request (without suggested experiment'); + } self.finalResult.expId = result.rows[0].id; self.finish(); } @@ -759,6 +808,10 @@ DBReq.UserIdChecker.prototype.execute = function() { "SELECT count(id) > 0 AS answer FROM users WHERE id = $1;", [self.id], function(err, result) { + if (err !== null) { + Log.dberror(err + ' in UserIdChecker'); + return; + } self.finalResult = result.rows[0].answer; self.finish(); } @@ -797,8 +850,10 @@ DBReq.UserNameChecker.prototype.execute = function() { "SELECT count(id) > 0 AS answer FROM users WHERE worker_id = $1", [self.name], function(err, result) { - if (err !== null) + if (err !== null) { Log.dberror(err + ' in UserNameChecker'); + return; + } self.finalResult = result.rows[0].answer; self.finish(); } @@ -1022,8 +1077,8 @@ DBReq.getInfo = function(id, callback) { * @memberof DBReq * @param callback {function} callback called on the new user id */ -DBReq.createUser = function(callback) { - new DBReq.UserCreator(callback); +DBReq.createUser = function(workerId, age, male, rating, lastTime, callback) { + new DBReq.UserCreator(workerId, age, male, rating, lastTime, callback); }; /** diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index 461be29..7e20d37 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -26,31 +26,40 @@ var sceneToFunction = function(scene) { module.exports.game = function(req, res) { - db.tryUser(req.session.userId, function(id) { + db.checkUserId(req.session.userId, function(ok) { - req.session.userId = id; + if (ok) { - db.createExp(id, function(expId, coinCombinationId, sceneId, recommendationStyle, coins) { + db.createExp( + req.session.userId, + function(expId, coinCombinationId, sceneId, recommendationStyle, coins) { - if (expId === undefined) { + if (expId === undefined) { - res.redirect('/feedback'); - return; + res.redirect('/feedback'); + 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); + }); + }); + + } else { + + res.redirect('/'); + + } - res.setHeader('Content-Type','text/html'); - res.render('prototype_recommendation.jade', res.locals, function(err, result) { - res.send(result); - }); - }); }); }; @@ -105,20 +114,36 @@ module.exports.replayIndex = function(req, res, next) { module.exports.tutorial = function(req, res) { - db.tryUser(req.session.userId, function(id) { - req.session.userId = id; + if (req.session.tutorialDone) { - // 1 is the ID of peach scene - db.createTutorial(id, function(id, coins) { - req.session.expId = id; - res.locals.coins = coins; - req.session.save(); + res.redirect('/before-begin'); + return; - res.setHeader('Content-Type', 'text/html'); - res.render('tutorial.jade', res.locals, function(err, result) { - res.send(result); + } + + db.checkUserId(req.session.userId, function(ok) { + + if (ok) { + + // 1 is the ID of peach scene + db.createTutorial(req.session.userId, function(id, coins) { + 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); + }); }); - }); + + } else { + + res.redirect('/'); + + } + }); }; @@ -160,6 +185,13 @@ module.exports.checker = editorHelper('prototype_checker.jade'); module.exports.userstudy = function(req, res) { res.setHeader('Content-Type', 'text/html'); + if (req.session.userId !== undefined) { + + res.redirect('/prototype/tutorial'); + return; + + } + res.locals.identificationFailed = req.session.identificationFailed; req.session.identificationFailed = false; req.session.save(); diff --git a/controllers/prototype/views/user_study.jade b/controllers/prototype/views/user_study.jade index c064a02..823e312 100644 --- a/controllers/prototype/views/user_study.jade +++ b/controllers/prototype/views/user_study.jade @@ -35,7 +35,7 @@ block content return false; } } - form.form-signin(method="POST", action='/identification', onsubmit='return validateForm()') + form#form.form-signin(method="POST", action='/identification', onsubmit='return validateForm()') h2 Please sign in label(for='inputId').sr-only Id input#inputId.form-control(name="inputId", type="text", placeholder='Id', required, autofocus) @@ -90,4 +90,9 @@ block content input#3dgames(type='number', class='rating', min='0', max='5', step='1', default='3', name='input3dskills') - button.btn.btn-lg.btn-primary.btn-block(type='submit') Sign in + button#submitButton.btn.btn-lg.btn-primary.btn-block(type='submit') Sign in + script. + document.getElementById('submitButton').onclick = function() { + document.getElementById('submitButton').disabled = true; + document.getElementById('form').submit(); + } diff --git a/js/l3d/apps/prototype/tutorial/main.js b/js/l3d/apps/prototype/tutorial/main.js index e14e301..dc45eea 100644 --- a/js/l3d/apps/prototype/tutorial/main.js +++ b/js/l3d/apps/prototype/tutorial/main.js @@ -28,6 +28,16 @@ Coin.onCoinGot = function(val) { Coin.onLastCoin = function() { setNextButton(nextPage); }; +window.onbeforeunload = function() { + + if (!($('#next').is(":visible"))) { + + return "Warning : you are going to leave the tutorial, that's not good !"; + + } + +} + function main() { // Main container that holds everything diff --git a/posts/arrow-clicked/index.js b/posts/arrow-clicked/index.js index 0c6f037..2108a66 100644 --- a/posts/arrow-clicked/index.js +++ b/posts/arrow-clicked/index.js @@ -7,7 +7,7 @@ module.exports.index = function(req, res) { pg.connect(secret.url, function(err, client, release) { client.query( "INSERT INTO arrowclicked(exp_id, arrow_id, time) VALUES($1,$2, to_timestamp($3));", - [req.session.exp_id, req.body.arrow_id, req.body.time], + [req.session.expId, req.body.arrowId, req.body.time], function(err, result) { if (err !== null) Log.dberror(err + ' arrow-clicked'); diff --git a/posts/identification/index.js b/posts/identification/index.js index 6252331..ff78dbc 100644 --- a/posts/identification/index.js +++ b/posts/identification/index.js @@ -7,32 +7,20 @@ module.exports.index = function(req, res) { db.checkUserName(req.body.inputId, function(ok) { if (!ok) { - db.tryUser(req.session.userId, function(id) { - req.session.userId = id; - req.session.save(); - pg.connect(secret.url, function(err, client, release) { - client.query( - "UPDATE Users SET worker_id = $1, age = $2, male = $3, rating = $5, lasttime = $6 WHERE id = $4;", - [ - req.body.inputId, - req.body.inputAge, - req.body.inputGender === 'male', - req.session.userId, - req.body.input3dskills, - req.body.inputLastTime - ], - function(err, result) { - if (err !== null) - Log.dberror(err + ' in identfication'); - release(); - } - ); - }); + db.createUser( + req.body.inputId, + req.body.inputAge, + req.body.inputGender === 'male', + req.body.input3dskills, + req.body.inputLastTime, + function(id) { + req.session.userId = id; + req.session.save(); + res.redirect('/prototype/tutorial'); + } + ); - res.redirect('/prototype/tutorial'); - - }); } else { req.session.identificationFailed = true; res.redirect('/user-study'); diff --git a/server.js b/server.js index 878508a..394207e 100644 --- a/server.js +++ b/server.js @@ -94,7 +94,7 @@ app.use(function(req, res) { var serverPort, serverIpAddress; if ( isDev ) { serverPort = 4000; - serverIpAddress = 'localhost'; + serverIpAddress = '147.127.121.42'; } else { // Openhift conf serverPort = process.env.OPENSHIFT_NODEJS_PORT || 8080;