Some commit

This commit is contained in:
Thomas FORGIONE 2015-09-09 15:18:18 +02:00
parent 464b3a47c0
commit 5e844538d2
7 changed files with 164 additions and 74 deletions

View File

@ -471,13 +471,19 @@ DBReq.Info.prototype.loadRedCoins = function() {
* @memberof DBReq * @memberof DBReq
* @constructor * @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 * Callback to call on the id when the user is created
* @type {function} * @type {function}
*/ */
this.finishAction = finishAction; this.finishAction = finishAction;
this.workerId = workerId;
this.age = age;
this.male = male;
this.rating = rating;
this.lastTime = lastTime;
// Connect to db // Connect to db
var self = this; var self = this;
pg.connect(pgc.url, function(err, client, release) { pg.connect(pgc.url, function(err, client, release) {
@ -492,25 +498,49 @@ DBReq.UserCreator = function(finishAction) {
*/ */
DBReq.UserCreator.prototype.execute = function() { DBReq.UserCreator.prototype.execute = function() {
var self = this; var self = this;
this.client.query( this.client.query("BEGIN; LOCK Users;", [], function() {
"INSERT INTO users DEFAULT VALUES; SELECT currval('users_id_seq');", self.client.query(
[], "INSERT INTO users(worker_id, age, male, rating, lasttime) VALUES($1, $2, $3, $4, $5);",
function(err, result) { [
self.finalResult = result.rows[0].currval; self.workerId,
self.finish(); 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 * Release the DB connection and call the callback
*/ */
DBReq.UserCreator.prototype.finish = function() { 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; self.release = release;
// Start transaction and lock table // Start transaction and lock table
self.client.query("BEGIN; LOCK CoinCombination; LOCK Experiment;"); self.client.query("BEGIN; LOCK CoinCombination; LOCK Experiment;", [], function() {
self.execute(); self.execute();
});
}); });
}; };
@ -591,6 +622,9 @@ DBReq.ExpCreator.prototype.execute = function() {
"LIMIT 1;", "LIMIT 1;",
[self.userId], [self.userId],
function(err, result) { function(err, result) {
if (err !== null) {
Log.dberror(err + ' in ExpCreator first request');
}
if (result.rows.length > 0) { if (result.rows.length > 0) {
// Set the result // Set the result
self.finalResult.coinCombinationId = result.rows[0].id; self.finalResult.coinCombinationId = result.rows[0].id;
@ -614,6 +648,9 @@ DBReq.ExpCreator.prototype.execute = function() {
"RETURNING id", "RETURNING id",
[self.userId, result.rows[0].id, result.rows[0].name], [self.userId, result.rows[0].id, result.rows[0].name],
function(err, result) { function(err, result) {
if (err !== null) {
Log.dberror(err + ' in ExpCreator second request (with suggested experiment)');
}
self.finalResult.expId = result.rows[0].id; self.finalResult.expId = result.rows[0].id;
self.finish(); self.finish();
} }
@ -645,6 +682,9 @@ DBReq.ExpCreator.prototype.execute = function() {
"LIMIT 1;", "LIMIT 1;",
[self.userId], [self.userId],
function(err, result) { function(err, result) {
if (err !== null) {
Log.dberror(err + ' in ExpCreator second request (without suggested experiment');
}
if (result.rows.length > 0) { if (result.rows.length > 0) {
self.finalResult.sceneId = result.rows[0].sceneId; self.finalResult.sceneId = result.rows[0].sceneId;
self.finalResult.recommendationStyle = result.rows[0].name; self.finalResult.recommendationStyle = result.rows[0].name;
@ -658,6 +698,9 @@ DBReq.ExpCreator.prototype.execute = function() {
"LIMIT 8;", "LIMIT 8;",
[self.finalResult.sceneId], [self.finalResult.sceneId],
function(err, result) { function(err, result) {
if (err !== null) {
Log.dberror(err + ' in ExpCreator third request (without suggested experiment');
}
self.finalResult.coins = []; self.finalResult.coins = [];
for (var i = 0; i < 8; i++) { for (var i = 0; i < 8; i++) {
self.finalResult.coins.push(result.rows[i].id); self.finalResult.coins.push(result.rows[i].id);
@ -680,6 +723,9 @@ DBReq.ExpCreator.prototype.execute = function() {
self.finalResult.coins[7], self.finalResult.coins[7],
], ],
function(err, result) { function(err, result) {
if (err !== null) {
Log.dberror(err + ' in ExpCreator fourth request (without suggested experiment');
}
self.finalResult.coinCombinationId = result.rows[0].id; self.finalResult.coinCombinationId = result.rows[0].id;
// And create the experiment // And create the experiment
@ -689,6 +735,9 @@ DBReq.ExpCreator.prototype.execute = function() {
"RETURNING id;", "RETURNING id;",
[self.userId, self.finalResult.coinCombinationId, self.finalResult.recommendationStyle], [self.userId, self.finalResult.coinCombinationId, self.finalResult.recommendationStyle],
function(err, result) { function(err, result) {
if (err !== null) {
Log.dberror(err + ' in ExpCreator fifth request (without suggested experiment');
}
self.finalResult.expId = result.rows[0].id; self.finalResult.expId = result.rows[0].id;
self.finish(); self.finish();
} }
@ -759,6 +808,10 @@ DBReq.UserIdChecker.prototype.execute = function() {
"SELECT count(id) > 0 AS answer FROM users WHERE id = $1;", "SELECT count(id) > 0 AS answer FROM users WHERE id = $1;",
[self.id], [self.id],
function(err, result) { function(err, result) {
if (err !== null) {
Log.dberror(err + ' in UserIdChecker');
return;
}
self.finalResult = result.rows[0].answer; self.finalResult = result.rows[0].answer;
self.finish(); self.finish();
} }
@ -797,8 +850,10 @@ DBReq.UserNameChecker.prototype.execute = function() {
"SELECT count(id) > 0 AS answer FROM users WHERE worker_id = $1", "SELECT count(id) > 0 AS answer FROM users WHERE worker_id = $1",
[self.name], [self.name],
function(err, result) { function(err, result) {
if (err !== null) if (err !== null) {
Log.dberror(err + ' in UserNameChecker'); Log.dberror(err + ' in UserNameChecker');
return;
}
self.finalResult = result.rows[0].answer; self.finalResult = result.rows[0].answer;
self.finish(); self.finish();
} }
@ -1022,8 +1077,8 @@ DBReq.getInfo = function(id, callback) {
* @memberof DBReq * @memberof DBReq
* @param callback {function} callback called on the new user id * @param callback {function} callback called on the new user id
*/ */
DBReq.createUser = function(callback) { DBReq.createUser = function(workerId, age, male, rating, lastTime, callback) {
new DBReq.UserCreator(callback); new DBReq.UserCreator(workerId, age, male, rating, lastTime, callback);
}; };
/** /**

View File

@ -26,31 +26,40 @@ var sceneToFunction = function(scene) {
module.exports.game = function(req, res) { 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'); res.redirect('/feedback');
return; return;
} }
req.session.expId = expId; req.session.expId = expId;
req.session.save(); req.session.save();
res.locals.scene = sceneToFunction(sceneId); res.locals.scene = sceneToFunction(sceneId);
res.locals.recommendationStyle = recommendationStyle; res.locals.recommendationStyle = recommendationStyle;
res.locals.coins = coins; 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) { module.exports.tutorial = function(req, res) {
db.tryUser(req.session.userId, function(id) { if (req.session.tutorialDone) {
req.session.userId = id;
// 1 is the ID of peach scene res.redirect('/before-begin');
db.createTutorial(id, function(id, coins) { return;
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); 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) { module.exports.userstudy = function(req, res) {
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
if (req.session.userId !== undefined) {
res.redirect('/prototype/tutorial');
return;
}
res.locals.identificationFailed = req.session.identificationFailed; res.locals.identificationFailed = req.session.identificationFailed;
req.session.identificationFailed = false; req.session.identificationFailed = false;
req.session.save(); req.session.save();

View File

@ -35,7 +35,7 @@ block content
return false; 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 h2 Please sign in
label(for='inputId').sr-only Id label(for='inputId').sr-only Id
input#inputId.form-control(name="inputId", type="text", placeholder='Id', required, autofocus) 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') 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();
}

View File

@ -28,6 +28,16 @@ Coin.onCoinGot = function(val) {
Coin.onLastCoin = function() { setNextButton(nextPage); }; 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() { function main() {
// Main container that holds everything // Main container that holds everything

View File

@ -7,7 +7,7 @@ module.exports.index = function(req, res) {
pg.connect(secret.url, function(err, client, release) { pg.connect(secret.url, function(err, client, release) {
client.query( client.query(
"INSERT INTO arrowclicked(exp_id, arrow_id, time) VALUES($1,$2, to_timestamp($3));", "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) { function(err, result) {
if (err !== null) if (err !== null)
Log.dberror(err + ' arrow-clicked'); Log.dberror(err + ' arrow-clicked');

View File

@ -7,32 +7,20 @@ module.exports.index = function(req, res) {
db.checkUserName(req.body.inputId, function(ok) { db.checkUserName(req.body.inputId, function(ok) {
if (!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) { db.createUser(
client.query( req.body.inputId,
"UPDATE Users SET worker_id = $1, age = $2, male = $3, rating = $5, lasttime = $6 WHERE id = $4;", req.body.inputAge,
[ req.body.inputGender === 'male',
req.body.inputId, req.body.input3dskills,
req.body.inputAge, req.body.inputLastTime,
req.body.inputGender === 'male', function(id) {
req.session.userId, req.session.userId = id;
req.body.input3dskills, req.session.save();
req.body.inputLastTime res.redirect('/prototype/tutorial');
], }
function(err, result) { );
if (err !== null)
Log.dberror(err + ' in identfication');
release();
}
);
});
res.redirect('/prototype/tutorial');
});
} else { } else {
req.session.identificationFailed = true; req.session.identificationFailed = true;
res.redirect('/user-study'); res.redirect('/user-study');

View File

@ -94,7 +94,7 @@ app.use(function(req, res) {
var serverPort, serverIpAddress; var serverPort, serverIpAddress;
if ( isDev ) { if ( isDev ) {
serverPort = 4000; serverPort = 4000;
serverIpAddress = 'localhost'; serverIpAddress = '147.127.121.42';
} else { } else {
// Openhift conf // Openhift conf
serverPort = process.env.OPENSHIFT_NODEJS_PORT || 8080; serverPort = process.env.OPENSHIFT_NODEJS_PORT || 8080;