2015-05-22 12:04:39 +02:00
|
|
|
var pg = require('pg');
|
|
|
|
var pgc = require('../../private.js');
|
2015-07-09 16:02:01 +02:00
|
|
|
var Log = require('../../lib/NodeLog.js');
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
2015-08-04 18:59:24 +02:00
|
|
|
*
|
2015-07-20 17:30:32 +02:00
|
|
|
* @namespace
|
|
|
|
*/
|
|
|
|
var DBReq = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class that loads every information from an experiment
|
|
|
|
* @param id {Number} id of the experiment to load
|
|
|
|
* @param finishAction {function} callback on the result when loading is
|
|
|
|
* finished
|
|
|
|
* @memberof DBReq
|
|
|
|
* @constructor
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
DBReq.Info = function(id, finishAction) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Id of the experiment to log
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2015-05-22 12:04:39 +02:00
|
|
|
this.id = id;
|
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Map between each element to load and a boolean saying if the element has
|
|
|
|
* been already loaded
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
2015-05-22 12:04:39 +02:00
|
|
|
this.ready = {
|
|
|
|
cameras: false,
|
|
|
|
coins: false,
|
|
|
|
arrows: false,
|
|
|
|
resets : false,
|
|
|
|
previousNext: false,
|
2015-07-09 16:02:01 +02:00
|
|
|
hovered: false,
|
|
|
|
pointerLocked: false,
|
2015-07-20 14:41:59 +02:00
|
|
|
switchedLockOption: false,
|
|
|
|
redCoins: false
|
2015-05-22 12:04:39 +02:00
|
|
|
};
|
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* List of the ids of the coins involved in the experiment
|
|
|
|
* @type {Number[]}
|
|
|
|
*/
|
2015-07-20 14:41:59 +02:00
|
|
|
this.redCoins = [];
|
2015-07-20 17:30:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Container of the result
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
2015-05-22 12:04:39 +02:00
|
|
|
this.results = {};
|
2015-07-20 17:30:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to call on finalResult when the loading is complete
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2015-05-22 12:04:39 +02:00
|
|
|
this.finishAction = finishAction;
|
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Client of connection to database
|
|
|
|
*/
|
|
|
|
this.client = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that releases the client to database
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
this.release = null;
|
|
|
|
|
2015-05-22 12:04:39 +02:00
|
|
|
// Connect to db
|
|
|
|
var self = this;
|
|
|
|
pg.connect(pgc.url, function(err, client, release) {
|
|
|
|
self.client = client;
|
|
|
|
self.release = release;
|
2015-05-22 14:21:11 +02:00
|
|
|
self.execute();
|
2015-05-22 12:04:39 +02:00
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Loads everything async
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.execute = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
this.loadCameras();
|
|
|
|
this.loadCoins();
|
|
|
|
this.loadArrows();
|
|
|
|
this.loadResets();
|
|
|
|
this.loadPreviousNext();
|
|
|
|
this.loadHovered();
|
2015-07-09 16:02:01 +02:00
|
|
|
this.loadSwitchedLockOption();
|
|
|
|
this.loadPointerLocked();
|
2015-07-20 14:41:59 +02:00
|
|
|
this.loadRedCoins();
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Checks if everything is loaded, and if so merges the results and call the
|
|
|
|
* final callback
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.tryMerge = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
// If not ready, do nothing
|
|
|
|
for (var i in this.ready) {
|
|
|
|
if (!this.ready[i]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release db connection
|
|
|
|
this.release();
|
|
|
|
this.release = null;
|
|
|
|
this.client = null;
|
|
|
|
|
|
|
|
this.merge();
|
|
|
|
this.finishAction(this.finalResult);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Merges the results of the different SQL requests and prepare final result.
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.merge = function() {
|
2015-07-20 15:51:29 +02:00
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
|
2015-07-20 14:41:59 +02:00
|
|
|
this.finalResult = {redCoins : [], events : []};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Find next element
|
|
|
|
var nextIndex = null;
|
|
|
|
|
2015-07-20 15:51:29 +02:00
|
|
|
for (i in this.results) {
|
2015-05-22 12:04:39 +02:00
|
|
|
// The next element is placed at the index 0 (since the elements
|
|
|
|
// gotten from the database are sorted)
|
|
|
|
if (this.results[i].length !== 0 &&
|
2015-05-22 18:36:38 +02:00
|
|
|
(nextIndex === null || this.results[i][0].time < this.results[nextIndex][0].time)) {
|
2015-05-22 12:04:39 +02:00
|
|
|
nextIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no next element, we're done
|
2015-05-22 18:36:38 +02:00
|
|
|
if (nextIndex === null) {
|
2015-05-22 12:04:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the next element in results and shift its table
|
2015-07-20 14:41:59 +02:00
|
|
|
this.finalResult.events.push(this.results[nextIndex].shift());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set red coins
|
2015-07-20 15:51:29 +02:00
|
|
|
for (i = 0; i < this.redCoins.length; i++) {
|
2015-07-20 14:41:59 +02:00
|
|
|
this.finalResult.redCoins.push(this.redCoins[i]);
|
2015-05-22 12:04:39 +02:00
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the camera information from the DB and
|
|
|
|
* tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadCameras = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
|
|
|
"SELECT ((camera).position).x AS px, " +
|
|
|
|
"((camera).position).y AS py, " +
|
|
|
|
"((camera).position).z AS pz, " +
|
|
|
|
"((camera).target).x AS tx, " +
|
|
|
|
"((camera).target).y AS ty, " +
|
|
|
|
"((camera).target).z AS tz, " +
|
|
|
|
"time AS time " +
|
2015-06-04 16:52:58 +02:00
|
|
|
"FROM keyboardevent WHERE exp_id = $1 ORDER BY time;",
|
2015-05-22 12:04:39 +02:00
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
2015-07-09 16:02:01 +02:00
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadCameras');
|
|
|
|
} else {
|
|
|
|
self.results.cameras = [];
|
|
|
|
for (var i in result.rows) {
|
|
|
|
self.results.cameras.push(
|
|
|
|
{
|
|
|
|
type: 'camera',
|
|
|
|
position : {
|
|
|
|
x: result.rows[i].px,
|
|
|
|
y: result.rows[i].py,
|
|
|
|
z: result.rows[i].pz
|
|
|
|
},
|
|
|
|
target : {
|
|
|
|
x: result.rows[i].tx,
|
|
|
|
y: result.rows[i].ty,
|
|
|
|
z: result.rows[i].tz
|
|
|
|
},
|
|
|
|
time: result.rows[i].time
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-05-22 12:04:39 +02:00
|
|
|
}
|
|
|
|
self.ready.cameras = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the coin information from the DB and
|
|
|
|
* tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadCoins = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-08-28 21:34:29 +02:00
|
|
|
"SELECT coin_id AS \"coinId\", time FROM coinclicked WHERE exp_id = $1 ORDER BY time;",
|
2015-05-22 12:04:39 +02:00
|
|
|
[self.id],
|
|
|
|
function(err,result) {
|
2015-07-09 16:02:01 +02:00
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadCoins');
|
|
|
|
} else {
|
|
|
|
self.results.coins = [];
|
|
|
|
for (var i in result.rows) {
|
|
|
|
self.results.coins.push(
|
|
|
|
{
|
|
|
|
type: 'coin',
|
|
|
|
time: result.rows[i].time,
|
2015-08-28 21:34:29 +02:00
|
|
|
id: result.rows[i].coinId
|
2015-07-09 16:02:01 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-05-22 12:04:39 +02:00
|
|
|
}
|
|
|
|
self.ready.coins = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the recommendation information from the DB and
|
|
|
|
* tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadArrows = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-08-28 21:34:29 +02:00
|
|
|
"SELECT arrow_id AS \"arrowId\", time FROM arrowclicked WHERE exp_id = $1 ORDER BY time;",
|
2015-05-22 12:04:39 +02:00
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
2015-07-09 16:02:01 +02:00
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadArrows');
|
|
|
|
} else {
|
|
|
|
self.results.arrows = [];
|
|
|
|
for (var i in result.rows) {
|
|
|
|
self.results.arrows.push(
|
|
|
|
{
|
|
|
|
type: 'arrow',
|
|
|
|
time: result.rows[i].time,
|
2015-08-28 21:34:29 +02:00
|
|
|
id: result.rows[i].arrowId
|
2015-07-09 16:02:01 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-05-22 12:04:39 +02:00
|
|
|
}
|
|
|
|
self.ready.arrows = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the reset information from the DB and
|
|
|
|
* tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadResets = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-06-04 16:52:58 +02:00
|
|
|
"SELECT time FROM resetclicked WHERE exp_id = $1 ORDER BY time;",
|
2015-05-22 12:04:39 +02:00
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
2015-07-09 16:02:01 +02:00
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadResets');
|
|
|
|
} else {
|
|
|
|
self.results.resets = [];
|
|
|
|
for (var i in result.rows) {
|
|
|
|
self.results.resets.push(
|
|
|
|
{
|
|
|
|
type: 'reset',
|
|
|
|
time: result.rows[i].time
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-05-22 12:04:39 +02:00
|
|
|
}
|
|
|
|
self.ready.resets = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the previous / next information from the DB and
|
|
|
|
* tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadPreviousNext = function () {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
|
|
|
"SELECT ((camera).position).x AS px, " +
|
|
|
|
"((camera).position).y AS py, " +
|
|
|
|
"((camera).position).z AS pz, " +
|
|
|
|
"((camera).target).x AS tx, " +
|
|
|
|
"((camera).target).y AS ty, " +
|
|
|
|
"((camera).target).z AS tz, " +
|
|
|
|
"time AS time " +
|
2015-06-04 16:52:58 +02:00
|
|
|
"FROM previousnextclicked WHERE exp_id = $1 ORDER BY time;",
|
2015-05-22 12:04:39 +02:00
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
2015-07-09 16:02:01 +02:00
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadPreviousNext');
|
|
|
|
} else {
|
|
|
|
self.results.previousNext = [];
|
|
|
|
for (var i in result.rows) {
|
|
|
|
self.results.previousNext.push(
|
|
|
|
{
|
|
|
|
type: 'previousnext',
|
|
|
|
time: result.rows[i].time,
|
|
|
|
previous: result.rows[i].previousnext == 'p',
|
|
|
|
position : {
|
|
|
|
x: result.rows[i].px,
|
|
|
|
y: result.rows[i].py,
|
|
|
|
z: result.rows[i].pz
|
|
|
|
},
|
|
|
|
target : {
|
|
|
|
x: result.rows[i].tx,
|
|
|
|
y: result.rows[i].ty,
|
|
|
|
z: result.rows[i].tz
|
|
|
|
}
|
2015-05-22 12:04:39 +02:00
|
|
|
}
|
2015-07-09 16:02:01 +02:00
|
|
|
);
|
|
|
|
}
|
2015-05-22 12:04:39 +02:00
|
|
|
}
|
|
|
|
self.ready.previousNext = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the hovered information from the DB and
|
|
|
|
* tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadHovered = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-08-28 21:34:29 +02:00
|
|
|
"SELECT start, time, arrow_id AS \"arrowId\" FROM hovered WHERE exp_id = $1 ORDER BY time;",
|
2015-05-22 12:04:39 +02:00
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
2015-07-09 16:02:01 +02:00
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadHovered');
|
|
|
|
} else {
|
|
|
|
self.results.hovered = [];
|
|
|
|
for (var i in result.rows) {
|
|
|
|
self.results.hovered.push(
|
|
|
|
{
|
|
|
|
type: "hovered",
|
|
|
|
time: result.rows[i].time,
|
|
|
|
start: result.rows[i].start,
|
2015-08-28 21:34:29 +02:00
|
|
|
id: result.rows[i].arrowId
|
2015-07-09 16:02:01 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-05-22 12:04:39 +02:00
|
|
|
}
|
|
|
|
self.ready.hovered = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the pointer lock information from the DB and
|
|
|
|
* tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadPointerLocked = function() {
|
2015-07-09 16:02:01 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
|
|
|
"SELECT time, locked FROM pointerlocked WHERE exp_id = $1 ORDER BY time;",
|
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadPointerLocked');
|
|
|
|
} else {
|
|
|
|
self.results.pointerlocked = [];
|
|
|
|
for (var i in result.rows) {
|
|
|
|
self.results.pointerlocked.push(
|
|
|
|
{
|
|
|
|
type: "pointerlocked",
|
|
|
|
locked: result.rows[i].locked,
|
|
|
|
time: result.rows[i].time
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.ready.pointerLocked = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-09 17:48:46 +02:00
|
|
|
};
|
2015-07-09 16:02:01 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the switch pointer lock option information
|
|
|
|
* from the DB and tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadSwitchedLockOption = function() {
|
2015-07-09 16:02:01 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
|
|
|
"SELECT time, locked FROM switchedlockoption WHERE exp_id = $1 ORDER BY time;",
|
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadSwitchedLockOption');
|
|
|
|
} else {
|
|
|
|
self.results.switchedlockoption = [];
|
|
|
|
for (var i in result.rows) {
|
|
|
|
self.results.switchedlockoption.push(
|
|
|
|
{
|
|
|
|
type: "switchedlockoption",
|
|
|
|
locked: result.rows[i].locked,
|
|
|
|
time: result.rows[i].time
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.ready.switchedLockOption = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-09 17:48:46 +02:00
|
|
|
};
|
2015-07-09 16:02:01 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Launches the SQL request to load the coins used in the expermient from the
|
|
|
|
* DB and tries to merge when finished
|
|
|
|
*/
|
|
|
|
DBReq.Info.prototype.loadRedCoins = function() {
|
2015-07-20 14:41:59 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-08-28 21:34:29 +02:00
|
|
|
"SELECT coin_1 AS coin1, \n" +
|
|
|
|
" coin_2 AS coin2, \n" +
|
|
|
|
" coin_3 AS coin3, \n" +
|
|
|
|
" coin_4 AS coin4, \n" +
|
|
|
|
" coin_5 AS coin5, \n" +
|
|
|
|
" coin_6 AS coin6, \n" +
|
|
|
|
" coin_7 AS coin7, \n" +
|
|
|
|
" coin_8 AS coin8 \n" +
|
2015-08-25 09:25:45 +02:00
|
|
|
"FROM CoinCombination, Experiment \n" +
|
|
|
|
"WHERE CoinCombination.id = Experiment.coin_combination_id AND \n" +
|
|
|
|
" Experiment.id = $1;",
|
2015-07-20 14:41:59 +02:00
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
|
|
|
if (err !== null) {
|
|
|
|
Log.dberror(err + ' in loadRedCoins');
|
|
|
|
} else {
|
2015-08-28 21:34:29 +02:00
|
|
|
self.redCoins.push(result.rows[0].coin1);
|
|
|
|
self.redCoins.push(result.rows[0].coin2);
|
|
|
|
self.redCoins.push(result.rows[0].coin3);
|
|
|
|
self.redCoins.push(result.rows[0].coin4);
|
|
|
|
self.redCoins.push(result.rows[0].coin5);
|
|
|
|
self.redCoins.push(result.rows[0].coin6);
|
|
|
|
self.redCoins.push(result.rows[0].coin7);
|
|
|
|
self.redCoins.push(result.rows[0].coin8);
|
2015-07-20 14:41:59 +02:00
|
|
|
}
|
|
|
|
self.ready.redCoins = true;
|
|
|
|
self.tryMerge();
|
|
|
|
}
|
|
|
|
);
|
2015-07-20 15:51:29 +02:00
|
|
|
};
|
2015-07-20 14:41:59 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Class that creates a user
|
|
|
|
* @param finishAction {function} callback that has as a parameter the id of
|
|
|
|
* the new user
|
|
|
|
* @memberof DBReq
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
DBReq.UserCreator = function(finishAction) {
|
|
|
|
/**
|
|
|
|
* Callback to call on the id when the user is created
|
|
|
|
* @type {function}
|
|
|
|
*/
|
2015-05-22 12:04:39 +02:00
|
|
|
this.finishAction = finishAction;
|
|
|
|
|
|
|
|
// Connect to db
|
|
|
|
var self = this;
|
|
|
|
pg.connect(pgc.url, function(err, client, release) {
|
|
|
|
self.client = client;
|
|
|
|
self.release = release;
|
|
|
|
self.execute();
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Executes the SQL request and calls the callback
|
|
|
|
*/
|
|
|
|
DBReq.UserCreator.prototype.execute = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-07-23 17:19:53 +02:00
|
|
|
"INSERT INTO users DEFAULT VALUES; SELECT currval('users_id_seq');",
|
2015-05-22 12:04:39 +02:00
|
|
|
[],
|
|
|
|
function(err, result) {
|
|
|
|
self.finalResult = result.rows[0].currval;
|
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Release the DB connection and call the callback
|
|
|
|
*/
|
|
|
|
DBReq.UserCreator.prototype.finish = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
this.release();
|
|
|
|
this.client = null;
|
|
|
|
this.release = null;
|
|
|
|
|
|
|
|
this.finishAction(this.finalResult);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Class that creates an experiment
|
2015-08-28 21:34:29 +02:00
|
|
|
* @param userId {Number} id of the user that does the experiment
|
|
|
|
* @param sceneId {Number} id of the scene the experiment is based on
|
2015-07-20 17:30:32 +02:00
|
|
|
* @param finishAction {function} callback that has as a parameter the id of
|
|
|
|
* the new experiment
|
|
|
|
* @memberof DBReq
|
|
|
|
* @constructor
|
|
|
|
*/
|
2015-08-28 21:34:29 +02:00
|
|
|
DBReq.ExpCreator = function(userId, finishAction) {
|
2015-06-04 16:52:58 +02:00
|
|
|
this.finishAction = finishAction;
|
2015-08-28 21:34:29 +02:00
|
|
|
this.userId = userId;
|
2015-08-04 18:59:24 +02:00
|
|
|
this.finalResult = {};
|
2015-06-04 16:52:58 +02:00
|
|
|
|
|
|
|
// Connect to db
|
|
|
|
var self = this;
|
|
|
|
pg.connect(pgc.url, function(err, client, release) {
|
2015-08-20 10:42:30 +02:00
|
|
|
|
2015-06-04 16:52:58 +02:00
|
|
|
self.client = client;
|
|
|
|
self.release = release;
|
2015-08-20 10:42:30 +02:00
|
|
|
|
|
|
|
// Start transaction and lock table
|
|
|
|
self.client.query("BEGIN; LOCK CoinCombination; LOCK Experiment;");
|
2015-06-04 16:52:58 +02:00
|
|
|
self.execute();
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-04 16:52:58 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Executes the SQL request and calls the callback
|
|
|
|
*/
|
2015-08-04 18:59:24 +02:00
|
|
|
// Abandon all hope ye who enter here
|
2015-07-20 17:30:32 +02:00
|
|
|
DBReq.ExpCreator.prototype.execute = function() {
|
2015-06-04 16:52:58 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-08-28 21:34:29 +02:00
|
|
|
"SELECT DISTINCT \n" +
|
|
|
|
" RecommendationStyle.name, \n" +
|
|
|
|
" CoinCombination.scene_id AS \"sceneId\", \n" +
|
|
|
|
" CoinCombination.id, \n" +
|
|
|
|
" coin_1 AS coin1, \n" +
|
|
|
|
" coin_2 AS coin2, \n" +
|
|
|
|
" coin_3 AS coin3, \n" +
|
|
|
|
" coin_4 AS coin4, \n" +
|
|
|
|
" coin_5 AS coin5, \n" +
|
|
|
|
" coin_6 AS coin6, \n" +
|
|
|
|
" coin_7 AS coin7, \n" +
|
|
|
|
" coin_8 AS coin8\n" +
|
2015-08-04 18:59:24 +02:00
|
|
|
"FROM CoinCombination, Experiment, Users U, Users Other, RecommendationStyle, Scene\n" +
|
|
|
|
"WHERE\n" +
|
|
|
|
" Scene.id = CoinCombination.scene_id AND\n" +
|
|
|
|
" Scene.name != 'peachcastle' AND\n" +
|
|
|
|
" CoinCombination.id = Experiment.coin_combination_id AND\n" +
|
|
|
|
" Other.id = Experiment.user_id AND\n" +
|
|
|
|
" Other.rating = U.rating AND\n" +
|
|
|
|
" Other.id != U.id AND\n" +
|
|
|
|
" U.id = $1 AND\n" +
|
|
|
|
" RecommendationStyle.name NOT IN (\n" +
|
|
|
|
" SELECT DISTINCT Experiment.recommendation_style\n" +
|
|
|
|
" FROM CoinCombination, Experiment, Users U, Users Other\n" +
|
|
|
|
" WHERE\n" +
|
|
|
|
" CoinCombination.id = Experiment.coin_combination_id AND\n" +
|
|
|
|
" Other.id = Experiment.user_id AND\n" +
|
|
|
|
" Other.rating = U.rating AND\n" +
|
|
|
|
" Other.id != U.id AND\n" +
|
|
|
|
" CoinCombination.scene_id = Scene.id\n" +
|
|
|
|
" ) AND\n" +
|
|
|
|
" RecommendationStyle.name NOT IN (\n" +
|
|
|
|
" SELECT DISTINCT Experiment.recommendation_style\n" +
|
|
|
|
" FROM Experiment\n" +
|
|
|
|
" WHERE Experiment.user_id = $1 AND Experiment.recommendation_style != ''\n" +
|
|
|
|
" ) AND\n" +
|
|
|
|
" CoinCombination.scene_id NOT IN (\n" +
|
|
|
|
" SELECT DISTINCT scene_id\n" +
|
|
|
|
" FROM Experiment, CoinCombination\n" +
|
|
|
|
" WHERE Experiment.coin_combination_id = CoinCombination.id AND Experiment.user_id = $1\n" +
|
|
|
|
" )\n" +
|
|
|
|
"LIMIT 1;",
|
2015-08-28 21:34:29 +02:00
|
|
|
[self.userId],
|
2015-06-04 16:52:58 +02:00
|
|
|
function(err, result) {
|
2015-08-04 18:59:24 +02:00
|
|
|
if (result.rows.length > 0) {
|
|
|
|
// Set the result
|
2015-08-28 21:34:29 +02:00
|
|
|
self.finalResult.coinCombinationId = result.rows[0].id;
|
|
|
|
self.finalResult.sceneId = result.rows[0].sceneId;
|
|
|
|
self.finalResult.recommendationStyle = result.rows[0].name;
|
2015-08-04 18:59:24 +02:00
|
|
|
self.finalResult.coins = [
|
2015-08-28 21:34:29 +02:00
|
|
|
result.rows[0].coin1,
|
|
|
|
result.rows[0].coin2,
|
|
|
|
result.rows[0].coin3,
|
|
|
|
result.rows[0].coin4,
|
|
|
|
result.rows[0].coin5,
|
|
|
|
result.rows[0].coin6,
|
|
|
|
result.rows[0].coin7,
|
|
|
|
result.rows[0].coin8
|
2015-08-04 18:59:24 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// There is a suggested experiment : create it
|
|
|
|
self.client.query(
|
|
|
|
"INSERT INTO Experiment(user_id, coin_combination_id, recommendation_style)\n" +
|
|
|
|
"VALUES($1,$2,$3)\n" +
|
|
|
|
"RETURNING id",
|
2015-08-28 21:34:29 +02:00
|
|
|
[self.userId, result.rows[0].id, result.rows[0].name],
|
2015-08-04 18:59:24 +02:00
|
|
|
function(err, result) {
|
2015-08-28 21:34:29 +02:00
|
|
|
self.finalResult.expId = result.rows[0].id;
|
2015-08-04 18:59:24 +02:00
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Find the scene / recommendation style for the new experiment
|
|
|
|
self.client.query(
|
2015-08-28 21:34:29 +02:00
|
|
|
"SELECT RecommendationStyle.name, Scene.id AS \"sceneId\"\n" +
|
2015-08-04 18:59:24 +02:00
|
|
|
"FROM RecommendationStyle, Scene\n" +
|
|
|
|
"WHERE\n" +
|
|
|
|
" RecommendationStyle.name NOT IN(\n" +
|
|
|
|
" SELECT Experiment.recommendation_style AS name\n" +
|
|
|
|
" FROM Experiment\n" +
|
|
|
|
" WHERE Experiment.user_id = $1\n" +
|
|
|
|
" AND Experiment.recommendation_style != ''\n" +
|
|
|
|
" ) AND\n" +
|
|
|
|
" Scene.id NOT IN(\n" +
|
|
|
|
" SELECT DISTINCT CoinCombination.scene_id AS id\n" +
|
|
|
|
" FROM Experiment, CoinCombination\n" +
|
|
|
|
" WHERE\n" +
|
|
|
|
" Experiment.coin_combination_id = CoinCombination.id AND\n" +
|
|
|
|
" user_id = $1\n" +
|
|
|
|
" ) AND\n" +
|
|
|
|
" Scene.name != 'peachcastle'\n" +
|
|
|
|
"\n" +
|
|
|
|
"ORDER BY RANDOM()\n" +
|
|
|
|
"LIMIT 1;",
|
2015-08-28 21:34:29 +02:00
|
|
|
[self.userId],
|
2015-08-04 18:59:24 +02:00
|
|
|
function(err, result) {
|
|
|
|
if (result.rows.length > 0) {
|
2015-08-28 21:34:29 +02:00
|
|
|
self.finalResult.sceneId = result.rows[0].sceneId;
|
|
|
|
self.finalResult.recommendationStyle = result.rows[0].name;
|
2015-08-04 18:59:24 +02:00
|
|
|
|
|
|
|
// Generate random coins
|
|
|
|
self.client.query(
|
|
|
|
"SELECT generate_series AS id\n" +
|
|
|
|
"FROM Scene, generate_series(0,Scene.coin_number-1)\n" +
|
|
|
|
"WHERE Scene.id = $1\n" +
|
|
|
|
"ORDER BY RANDOM()\n" +
|
|
|
|
"LIMIT 8;",
|
2015-08-28 21:34:29 +02:00
|
|
|
[self.finalResult.sceneId],
|
2015-08-04 18:59:24 +02:00
|
|
|
function(err, result) {
|
|
|
|
self.finalResult.coins = [];
|
|
|
|
for (var i = 0; i < 8; i++) {
|
|
|
|
self.finalResult.coins.push(result.rows[i].id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And then, create the CoinCombination
|
|
|
|
self.client.query(
|
|
|
|
"INSERT INTO CoinCombination(scene_id, coin_1, coin_2, coin_3, coin_4, coin_5, coin_6, coin_7, coin_8)\n" +
|
|
|
|
"VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)\n" +
|
|
|
|
"RETURNING id;",
|
|
|
|
[
|
2015-08-28 21:34:29 +02:00
|
|
|
self.finalResult.sceneId,
|
2015-08-04 18:59:24 +02:00
|
|
|
self.finalResult.coins[0],
|
|
|
|
self.finalResult.coins[1],
|
|
|
|
self.finalResult.coins[2],
|
|
|
|
self.finalResult.coins[3],
|
|
|
|
self.finalResult.coins[4],
|
|
|
|
self.finalResult.coins[5],
|
|
|
|
self.finalResult.coins[6],
|
|
|
|
self.finalResult.coins[7],
|
|
|
|
],
|
|
|
|
function(err, result) {
|
2015-08-28 21:34:29 +02:00
|
|
|
self.finalResult.coinCombinationId = result.rows[0].id;
|
2015-08-04 18:59:24 +02:00
|
|
|
|
|
|
|
// And create the experiment
|
|
|
|
self.client.query(
|
|
|
|
"INSERT INTO Experiment(user_id, coin_combination_id, recommendation_style)\n" +
|
|
|
|
"VALUES($1,$2,$3)\n" +
|
|
|
|
"RETURNING id;",
|
2015-08-28 21:34:29 +02:00
|
|
|
[self.userId, self.finalResult.coinCombinationId, self.finalResult.recommendationStyle],
|
2015-08-04 18:59:24 +02:00
|
|
|
function(err, result) {
|
2015-08-28 21:34:29 +02:00
|
|
|
self.finalResult.expId = result.rows[0].id;
|
2015-08-04 18:59:24 +02:00
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-06-04 16:52:58 +02:00
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-04 16:52:58 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Release the DB connection and call the callback
|
|
|
|
*/
|
|
|
|
DBReq.ExpCreator.prototype.finish = function() {
|
2015-08-20 10:42:30 +02:00
|
|
|
var self = this;
|
2015-06-04 16:52:58 +02:00
|
|
|
|
2015-08-20 10:42:30 +02:00
|
|
|
// Commit, and then release
|
|
|
|
this.client.query("COMMIT;", function() {
|
|
|
|
|
|
|
|
self.release();
|
|
|
|
self.client = null;
|
|
|
|
self.release = null;
|
|
|
|
|
|
|
|
self.finishAction(
|
2015-08-28 21:34:29 +02:00
|
|
|
self.finalResult.expId,
|
|
|
|
self.finalResult.coinCombinationId,
|
|
|
|
self.finalResult.sceneId,
|
|
|
|
self.finalResult.recommendationStyle,
|
2015-08-20 10:42:30 +02:00
|
|
|
self.finalResult.coins
|
|
|
|
);
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-04 16:52:58 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Class that creates an experiment
|
|
|
|
* @param id {Number} id of the user that does the experiment
|
|
|
|
* @param finishAction {function} callback that has as a parameter which is a
|
|
|
|
* boolean indicating wether the user id exists or not
|
|
|
|
* @memberof DBReq
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
DBReq.UserIdChecker = function(id, finishAction) {
|
2015-05-22 12:04:39 +02:00
|
|
|
this.id = id;
|
|
|
|
this.finishAction = finishAction;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
pg.connect(pgc.url, function(err, client, release) {
|
|
|
|
self.client = client;
|
|
|
|
self.release = release;
|
|
|
|
self.execute();
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Executes the SQL request and calls the callback
|
|
|
|
*/
|
|
|
|
DBReq.UserIdChecker.prototype.execute = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
|
|
|
"SELECT count(id) > 0 AS answer FROM users WHERE id = $1;",
|
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
|
|
|
self.finalResult = result.rows[0].answer;
|
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Release the DB connection and call the callback
|
|
|
|
*/
|
|
|
|
DBReq.UserIdChecker.prototype.finish = function() {
|
2015-06-04 16:52:58 +02:00
|
|
|
this.release();
|
|
|
|
this.client = null;
|
|
|
|
this.release = null;
|
|
|
|
|
|
|
|
this.finishAction(this.finalResult);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-04 16:52:58 +02:00
|
|
|
|
2015-07-23 18:16:44 +02:00
|
|
|
DBReq.UserNameChecker = function(name, finishAction) {
|
|
|
|
this.name = name;
|
|
|
|
this.finishAction = finishAction;
|
|
|
|
var self = this;
|
|
|
|
pg.connect(pgc.url, function(err, client, release) {
|
2015-08-04 23:08:50 +02:00
|
|
|
if (err) {
|
|
|
|
Log.dberror(err + ' in UserNameChcker connection');
|
|
|
|
return;
|
|
|
|
}
|
2015-07-23 18:16:44 +02:00
|
|
|
self.client = client;
|
|
|
|
self.release = release;
|
|
|
|
self.execute();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
DBReq.UserNameChecker.prototype.execute = function() {
|
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
|
|
|
"SELECT count(id) > 0 AS answer FROM users WHERE worker_id = $1",
|
|
|
|
[self.name],
|
|
|
|
function(err, result) {
|
|
|
|
if (err !== null)
|
|
|
|
Log.dberror(err + ' in UserNameChecker');
|
|
|
|
self.finalResult = result.rows[0].answer;
|
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
DBReq.UserNameChecker.prototype.finish = function() {
|
|
|
|
this.release();
|
|
|
|
this.client = null;
|
|
|
|
this.release = null;
|
|
|
|
|
|
|
|
this.finishAction(this.finalResult);
|
|
|
|
};
|
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Class that creates an experiment
|
|
|
|
* @param id {Number} id of the experiment to check
|
|
|
|
* @param finishAction {function} callback that has as a parameter which is the
|
|
|
|
* id of the scene if the experiment exists, or null otherwise
|
|
|
|
* @memberof DBReq
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
DBReq.ExpIdChecker = function(id, finishAction) {
|
2015-06-04 16:52:58 +02:00
|
|
|
this.id = id;
|
|
|
|
this.finishAction = finishAction;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
pg.connect(pgc.url, function(err, client, release) {
|
|
|
|
self.client = client;
|
|
|
|
self.release = release;
|
|
|
|
self.execute();
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-04 16:52:58 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Executes the SQL request and calls the callback
|
|
|
|
*/
|
|
|
|
DBReq.ExpIdChecker.prototype.execute = function() {
|
2015-06-04 16:52:58 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-08-28 21:34:29 +02:00
|
|
|
"SELECT scene_id AS \"sceneId\" FROM experiment, CoinCombination WHERE CoinCombination.id = Experiment.coin_combination_id AND Experiment.id = $1;",
|
2015-06-04 16:52:58 +02:00
|
|
|
[self.id],
|
|
|
|
function(err, result) {
|
2015-06-22 14:48:44 +02:00
|
|
|
if (result === undefined || result.rows.length === 0) {
|
2015-06-05 10:35:20 +02:00
|
|
|
self.finalResult = null;
|
|
|
|
} else {
|
2015-08-28 21:34:29 +02:00
|
|
|
self.finalResult = result.rows[0].sceneId;
|
2015-06-05 10:35:20 +02:00
|
|
|
}
|
2015-06-04 16:52:58 +02:00
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-04 16:52:58 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Release the DB connection and call the callback
|
|
|
|
*/
|
|
|
|
DBReq.ExpIdChecker.prototype.finish = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
this.release();
|
|
|
|
this.client = null;
|
|
|
|
this.release = null;
|
|
|
|
|
|
|
|
this.finishAction(this.finalResult);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Class that gets the info from all experiment
|
|
|
|
* @param finishAction {function} callback that has as a parameter which is an
|
|
|
|
* array of objects containing the id, the username, the name of the scene and
|
|
|
|
* the id of the user.
|
|
|
|
* @memberof DBReq
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
DBReq.ExpGetter = function(finishAction) {
|
2015-05-22 12:04:39 +02:00
|
|
|
this.finishAction = finishAction;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
pg.connect(pgc.url, function(err, client, release) {
|
|
|
|
self.client = client;
|
|
|
|
self.release = release;
|
|
|
|
self.execute();
|
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Executes the SQL request and calls the callback
|
|
|
|
*/
|
|
|
|
DBReq.ExpGetter.prototype.execute = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
2015-06-04 16:52:58 +02:00
|
|
|
"SELECT " +
|
2015-08-28 21:34:29 +02:00
|
|
|
"experiment.id AS \"expId\", " +
|
|
|
|
"users.worker_id AS username, " +
|
|
|
|
"scene.name AS scenename, " +
|
|
|
|
"users.id AS \"userId\" " +
|
2015-08-24 17:13:14 +02:00
|
|
|
"FROM experiment, users, scene, CoinCombination " +
|
|
|
|
"WHERE experiment.user_id = users.id and scene.id = CoinCombination.scene_id AND " +
|
|
|
|
" Experiment.coin_combination_id = CoinCombination.id " +
|
2015-06-04 16:52:58 +02:00
|
|
|
"ORDER BY experiment.id;",
|
2015-05-22 12:04:39 +02:00
|
|
|
[],
|
|
|
|
function(err, result) {
|
|
|
|
self.finalResult = result.rows;
|
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Release the DB connection and call the callback
|
|
|
|
*/
|
|
|
|
DBReq.ExpGetter.prototype.finish = function() {
|
2015-05-22 12:04:39 +02:00
|
|
|
this.release();
|
|
|
|
this.client = null;
|
|
|
|
this.release = null;
|
|
|
|
|
|
|
|
this.finishAction(this.finalResult);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-05-22 12:04:39 +02:00
|
|
|
|
2015-08-04 18:59:24 +02:00
|
|
|
DBReq.TutorialCreator = function(id, finishAction) {
|
|
|
|
this.id = id;
|
|
|
|
this.finishAction = finishAction;
|
2015-08-05 14:47:54 +02:00
|
|
|
this.finalResult = {};
|
2015-08-04 18:59:24 +02:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
pg.connect(pgc.url, function(err, client, release) {
|
|
|
|
self.client = client;
|
|
|
|
self.release = release;
|
|
|
|
self.execute();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
DBReq.TutorialCreator.prototype.execute = function() {
|
|
|
|
var self = this;
|
|
|
|
this.client.query(
|
|
|
|
// Generate random coins
|
2015-08-28 21:34:29 +02:00
|
|
|
"SELECT Scene.id AS \"sceneId\", generate_series AS id\n" +
|
2015-08-04 22:18:37 +02:00
|
|
|
"FROM Scene, generate_series(0,Scene.coin_number-1)\n" +
|
2015-08-04 18:59:24 +02:00
|
|
|
"WHERE Scene.name = 'peachcastle'\n" +
|
|
|
|
"ORDER BY RANDOM()\n" +
|
|
|
|
"LIMIT 8;",
|
|
|
|
[],
|
|
|
|
function(err, result) {
|
2015-08-05 14:47:54 +02:00
|
|
|
self.finalResult.coins = [];
|
2015-08-04 18:59:24 +02:00
|
|
|
for (var i = 0; i < 8; i++) {
|
2015-08-05 14:47:54 +02:00
|
|
|
self.finalResult.coins.push(result.rows[i].id);
|
2015-08-04 18:59:24 +02:00
|
|
|
}
|
|
|
|
// Create CoinCombination
|
|
|
|
self.client.query(
|
|
|
|
"INSERT INTO CoinCombination(scene_id, coin_1, coin_2, coin_3, coin_4, coin_5, coin_6, coin_7, coin_8)\n" +
|
|
|
|
"VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)\n" +
|
|
|
|
"RETURNING id;",
|
|
|
|
[
|
2015-08-28 21:34:29 +02:00
|
|
|
result.rows[0].sceneId,
|
2015-08-04 18:59:24 +02:00
|
|
|
result.rows[0].id,
|
|
|
|
result.rows[1].id,
|
|
|
|
result.rows[2].id,
|
|
|
|
result.rows[3].id,
|
|
|
|
result.rows[4].id,
|
|
|
|
result.rows[5].id,
|
|
|
|
result.rows[6].id,
|
|
|
|
result.rows[7].id
|
|
|
|
],
|
|
|
|
function(err, result) {
|
|
|
|
// Create experiment
|
|
|
|
self.client.query(
|
|
|
|
"INSERT INTO Experiment(user_id, coin_combination_id)\n" +
|
|
|
|
"VALUES($1,$2)\n" +
|
|
|
|
"RETURNING id;",
|
|
|
|
[self.id, result.rows[0].id],
|
|
|
|
function(err, result) {
|
2015-08-05 14:47:54 +02:00
|
|
|
self.finalResult.expId = result.rows[0].id;
|
2015-08-04 18:59:24 +02:00
|
|
|
self.finish();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DBReq.TutorialCreator.prototype.finish = function() {
|
|
|
|
this.release();
|
|
|
|
this.release = null;
|
|
|
|
this.client = null;
|
|
|
|
|
2015-08-05 14:47:54 +02:00
|
|
|
this.finishAction(this.finalResult.expId, this.finalResult.coins);
|
2015-08-25 14:03:01 +02:00
|
|
|
};
|
2015-08-04 18:59:24 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Try to get a user by id, and creates it if it doesn't exists
|
|
|
|
* @param id {Number} id to test
|
|
|
|
* @param callback {function} callback to call on the id
|
|
|
|
* @memberof DBReq
|
|
|
|
*/
|
2015-07-20 17:50:39 +02:00
|
|
|
DBReq.tryUser = function(id, callback) {
|
2015-06-04 16:52:58 +02:00
|
|
|
if (id !== undefined && id !== null) {
|
2015-07-20 17:30:32 +02:00
|
|
|
new DBReq.UserIdChecker(id, function(clear) {
|
2015-06-04 16:52:58 +02:00
|
|
|
if (clear) {
|
|
|
|
callback(id);
|
|
|
|
} else {
|
2015-07-20 17:30:32 +02:00
|
|
|
new DBReq.UserCreator(callback);
|
2015-06-04 16:52:58 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2015-07-20 17:30:32 +02:00
|
|
|
new DBReq.UserCreator(callback);
|
2015-06-04 16:52:58 +02:00
|
|
|
}
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-04 16:52:58 +02:00
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Get all the info from an experiment
|
|
|
|
* @memberof DBReq
|
|
|
|
* @param id {Number} id of the experiment to get the info
|
|
|
|
* @param callback {function} callback called on the result of all the SQL requests
|
|
|
|
*/
|
2015-07-20 17:50:39 +02:00
|
|
|
DBReq.getInfo = function(id, callback) {
|
2015-07-20 17:30:32 +02:00
|
|
|
new DBReq.Info(id, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a user
|
|
|
|
* @memberof DBReq
|
|
|
|
* @param callback {function} callback called on the new user id
|
|
|
|
*/
|
2015-07-20 17:50:39 +02:00
|
|
|
DBReq.createUser = function(callback) {
|
2015-07-20 17:30:32 +02:00
|
|
|
new DBReq.UserCreator(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an experiment
|
|
|
|
* @memberof DBReq
|
|
|
|
* @param id {Number} id of the user doing the experiment
|
2015-08-28 21:34:29 +02:00
|
|
|
* @param sceneId {Number} id of the scene on which the experiment is
|
2015-07-20 17:30:32 +02:00
|
|
|
* @param callback {function} callback called on the new experiment id
|
|
|
|
*/
|
2015-08-04 18:59:24 +02:00
|
|
|
DBReq.createExp = function(id, callback) {
|
|
|
|
new DBReq.ExpCreator(id, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
DBReq.createTutorial = function(id, callback) {
|
|
|
|
new DBReq.TutorialCreator(id, callback);
|
2015-07-20 17:30:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the user id
|
|
|
|
* @memberof DBReq
|
|
|
|
* @param id {Number} id to check
|
|
|
|
* @param callback {function} callback called on a boolean (true if the user id
|
|
|
|
* exists, false otherwise)
|
|
|
|
*/
|
2015-07-20 17:50:39 +02:00
|
|
|
DBReq.checkUserId = function(id, callback) {
|
2015-07-20 17:30:32 +02:00
|
|
|
new DBReq.UserIdChecker(id, callback);
|
|
|
|
};
|
|
|
|
|
2015-07-23 18:16:44 +02:00
|
|
|
DBReq.checkUserName = function(name, callback) {
|
|
|
|
new DBReq.UserNameChecker(name, callback);
|
|
|
|
};
|
|
|
|
|
2015-07-20 17:30:32 +02:00
|
|
|
/**
|
|
|
|
* Checks if an experiment id exists
|
|
|
|
* @memberof DBReq
|
|
|
|
* @param id {Number} id of the experiment to check
|
|
|
|
* @param callback {function} callback called on an object (null if the
|
2015-08-28 21:34:29 +02:00
|
|
|
* experiment doesn't exist, an object containing username, sceneId,
|
|
|
|
* scenename, and expId if it exists
|
2015-07-20 17:30:32 +02:00
|
|
|
*/
|
2015-07-20 17:50:39 +02:00
|
|
|
DBReq.checkExpId = function(id, callback) {
|
2015-07-20 17:30:32 +02:00
|
|
|
new DBReq.ExpIdChecker(id, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list of all experiments
|
|
|
|
* @memberof DBReq
|
|
|
|
* @param callback {function} callback called on an array containing all experiments
|
|
|
|
*/
|
2015-07-20 17:50:39 +02:00
|
|
|
DBReq.getAllExps = function(callback) {
|
2015-07-20 17:30:32 +02:00
|
|
|
new DBReq.ExpGetter(callback);
|
|
|
|
};
|
|
|
|
|
2015-07-20 17:50:39 +02:00
|
|
|
module.exports = DBReq;
|