diff --git a/controllers/before-begin/index.js b/controllers/before-begin/index.js index a6a2159..6b870d0 100644 --- a/controllers/before-begin/index.js +++ b/controllers/before-begin/index.js @@ -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) { diff --git a/controllers/intro/index.js b/controllers/intro/index.js index a6a2159..18b37a2 100644 --- a/controllers/intro/index.js +++ b/controllers/intro/index.js @@ -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) { diff --git a/controllers/intro/urls.js b/controllers/intro/urls.js index 8bbe18c..873b090 100644 --- a/controllers/intro/urls.js +++ b/controllers/intro/urls.js @@ -1,3 +1,4 @@ module.exports = { '/intro': 'index', + '/intro/:workerId': 'index', }; diff --git a/controllers/prototype/dbrequests.js b/controllers/prototype/dbrequests.js index b7076dd..5fcf474 100644 --- a/controllers/prototype/dbrequests.js +++ b/controllers/prototype/dbrequests.js @@ -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; diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index 3b325dd..582e179 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -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); }); diff --git a/controllers/prototype/views/user_study.jade b/controllers/prototype/views/user_study.jade index 654dd5d..03a1c6b 100644 --- a/controllers/prototype/views/user_study.jade +++ b/controllers/prototype/views/user_study.jade @@ -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 diff --git a/controllers/thankyou/index.js b/controllers/thankyou/index.js index 4cb75df..d83310c 100644 --- a/controllers/thankyou/index.js +++ b/controllers/thankyou/index.js @@ -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); }); + }; diff --git a/controllers/thankyou/views/index.jade b/controllers/thankyou/views/index.jade index 821cda0..53a86d8 100644 --- a/controllers/thankyou/views/index.jade +++ b/controllers/thankyou/views/index.jade @@ -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 + diff --git a/lib/vcode.js b/lib/vcode.js index d920a74..be22910 100644 --- a/lib/vcode.js +++ b/lib/vcode.js @@ -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); diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 9f0914d..b1ba03c 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -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", diff --git a/package.json b/package.json index c688c5e..abe4eb4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/posts/identification/index.js b/posts/identification/index.js index ff78dbc..436e7ca 100644 --- a/posts/identification/index.js +++ b/posts/identification/index.js @@ -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,