Continuing on micro workers, and corrected some bugs

This commit is contained in:
Thomas FORGIONE 2015-09-29 17:22:48 +02:00
parent bee440668c
commit aa76fed43a
12 changed files with 149 additions and 13 deletions

View File

@ -1,4 +1,5 @@
module.exports.index = function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.render('index.jade', res.locals, function(err, result) {

View File

@ -1,4 +1,8 @@
module.exports.index = function(req, res) {
req.session.workerId = req.params.workerId;
req.session.save();
res.setHeader('Content-Type', 'text/html');
res.render('index.jade', res.locals, function(err, result) {

View File

@ -1,3 +1,4 @@
module.exports = {
'/intro': 'index',
'/intro/:workerId': 'index',
};

View File

@ -1,7 +1,7 @@
// Polyfill of find array
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this == null) {
if (this === null || this === undefined) {
throw new TypeError('Array.prototype.find a été appelé sur null ou undefined');
}
if (typeof predicate !== 'function') {
@ -22,9 +22,42 @@ if (!Array.prototype.find) {
};
}
// Production steps of ECMA-262, Edition 5, 15.4.4.21
// Reference: http://es5.github.io/#x15.4.4.21
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(callback /*, initialValue*/) {
'use strict';
if (this === null || this === undefined) {
throw new TypeError('Array.prototype.reduce called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var t = Object(this), len = t.length >>> 0, k = 0, value;
if (arguments.length == 2) {
value = arguments[1];
} else {
while (k < len && !(k in t)) {
k++;
}
if (k >= len) {
throw new TypeError('Reduce of empty array with no initial value');
}
value = t[k++];
}
for (; k < len; k++) {
if (k in t) {
value = callback(value, t[k], k, t);
}
}
return value;
};
}
var pg = require('pg');
var pgc = require('../../private.js');
var Log = require('../../lib/NodeLog.js');
var async = require('async');
/**
*
@ -605,7 +638,7 @@ function predicate(line) {
return (
(elt.recommendationStyle !== null && (elt.recommendationStyle.trim() === line.recommendationStyle.trim())) ||
line.id === elt.coinCombinationId
line.sceneId === elt.sceneId
);
};
@ -1182,6 +1215,61 @@ DBReq.TutorialCreator.prototype.finish = function() {
this.finishAction(this.finalResult.expId, this.finalResult.coins);
};
DBReq.UserVerifier = function(userId, finishAction) {
this.userId = userId;
this.finishAction = finishAction;
var self = this;
pg.connect(pgc.url, function(err, client, release) {
self.client = client;
self.release = release;
self.execute();
});
};
DBReq.UserVerifier.prototype.execute = function() {
var self = this;
this.client.query(
"SELECT id as \"expId\" FROM Experiment WHERE user_id = $1",
[self.userId],
function(err, result) {
if (result.rows.length !== 4) {
self.finish(false);
}
async.map(
result.rows,
function(elt, callback) {
self.client.query(
"SELECT count(*) > 5 AS ok FROM CoinClicked WHERE exp_id = $1",
[elt.expId],
function(err, result) {
callback(null, result.rows[0].ok === true);
}
);
},
function(err, result) {
var ok = result.reduce(function(prev, next) { return prev && next; });
self.finish(ok);
}
);
}
);
};
DBReq.UserVerifier.prototype.finish = function(finalResult) {
this.release();
this.client = null;
this.release = null;
this.finishAction(finalResult);
};
/**
* Try to get a user by id, and creates it if it doesn't exists
* @param id {Number} id to test
@ -1276,4 +1364,8 @@ DBReq.getLastExp = function(id, callback) {
new DBReq.LastExpGetter(id, callback);
};
DBReq.verifyUser = function(id, callback) {
new DBReq.UserVerifier(id, callback);
};
module.exports = DBReq;

View File

@ -82,7 +82,7 @@ module.exports.play = function(req, res) {
db.getLastExp(req.session.userId, function(expId, sceneId, coinId, recoStyle, coins) {
res.locals.scene = sceneToFunction(sceneId);
res.locals.recommendationStyle= recoStyle;
res.locals.recommendationStyle = recoStyle;
res.locals.coins = coins;
req.session.experiments.push({
@ -236,7 +236,6 @@ module.exports.viewer = editorHelper('prototype_viewer.jade');
module.exports.checker = editorHelper('prototype_checker.jade');
module.exports.userstudy = function(req, res) {
res.setHeader('Content-Type', 'text/html');
if (req.session.userId !== undefined) {
@ -249,6 +248,9 @@ module.exports.userstudy = function(req, res) {
req.session.identificationFailed = false;
req.session.save();
res.locals.workerId = req.session.workerId;
res.setHeader('Content-Type', 'text/html');
res.render('user_study.jade', res.locals, function(err, result) {
res.send(result);
});

View File

@ -39,7 +39,10 @@ block content
form#form.form-signin(method="POST", action='/identification')
h2 Please sign in
label(for='inputId').sr-only Id
input#inputId.form-control(name="inputId", type="text", placeholder='Id', required, autofocus)
if (workerId === undefined)
input#inputId.form-control(name="inputId", type="text", placeholder='Id', required, autofocus)
else
input#inputId.form-control(name="inputId", type="text", placeholder='Id', required, autofocus, disabled, value="#{workerId}")
.form-group
label Gender

View File

@ -1,12 +1,28 @@
var db = require('../prototype/dbrequests.js');
var vcode = require('../../lib/vcode.js');
module.exports.index = function(req, res) {
req.session = null;
res.locals.session = null;
db.verifyUser(req.session.userId, function(ok) {
res.setHeader('Content-Type', 'text/html');
if (ok) {
res.locals.vcode = vcode(req.session.workerId, req.session.campaignIp);
}
res.locals.workerId = req.session.workerId;
req.session = null;
res.locals.session = null;
res.setHeader('Content-Type', 'text/html');
res.render('index.jade', res.locals, function(err, result) {
console.log(err);
res.send(result);
});
res.render('index.jade', res.locals, function(err, result) {
res.send(result);
});
};

View File

@ -6,3 +6,8 @@ block content
source(src="/static/data/music/thankyou.ogg")
source(src="/static/data/music/thankyou.mp3")
if (vcode !== undefined)
p Your vcode is #{vcode}.
else if (workerId !== undefined)
p You have no vcode because you're bad

View File

@ -1,7 +1,13 @@
var hash = require('sha256');
var secretKey = require('../private.js').microSecretKey;
module.exports = function(campaignId, workerId) {
module.exports = function(workerId, campaignId) {
if (campaignId === undefined) {
return 'mw-dummyvcode';
}
return 'mw-' + hash(campaignId + workerId + secretKey);

5
npm-shrinkwrap.json generated
View File

@ -2,6 +2,11 @@
"name": "3d-interface",
"version": "2.0.0",
"dependencies": {
"async": {
"version": "1.4.2",
"from": "async@1.4.2",
"resolved": "https://registry.npmjs.org/async/-/async-1.4.2.tgz"
},
"body-parser": {
"version": "1.12.4",
"from": "https://registry.npmjs.org/body-parser/-/body-parser-1.12.4.tgz",

View File

@ -12,7 +12,8 @@
"serve-favicon": "2.3.0",
"emailjs":"0.3.16",
"three":"0.71.0",
"sha256":"0.2.0"
"sha256":"0.2.0",
"async":"1.4.2"
},
"repository" : {
"type" : "git",

View File

@ -9,7 +9,7 @@ module.exports.index = function(req, res) {
if (!ok) {
db.createUser(
req.body.inputId,
req.session.workerId || req.body.inputId,
req.body.inputAge,
req.body.inputGender === 'male',
req.body.input3dskills,