Some commit
This commit is contained in:
91
controllers/prototype/dbrequests.js
vendored
91
controllers/prototype/dbrequests.js
vendored
@@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
88
controllers/prototype/index.js
vendored
88
controllers/prototype/index.js
vendored
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user