diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index f09e980..70287d4 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -26,7 +26,6 @@ module.exports.index = function(req, res) { module.exports.arrows = function(req, res) { createNewId(req, function() { - res.setHeader('Content-Type', 'text/html'); res.locals.cameraStyle = 'arrows'; @@ -34,26 +33,29 @@ module.exports.arrows = function(req, res) { res.render('prototype.jade', res.locals, function(err, result) { res.send(result); }); - }); } module.exports.viewports = function(req, res) { - res.setHeader('Content-Type', 'text/html'); + createNewId(req, function() { + res.setHeader('Content-Type', 'text/html'); - res.locals.cameraStyle = 'viewports'; + res.locals.cameraStyle = 'viewports'; - res.render('prototype.jade', res.locals, function(err, result) { - res.send(result); + res.render('prototype.jade', res.locals, function(err, result) { + res.send(result); + }); }); } module.exports.reverse = function(req, res) { - res.setHeader('Content-Type', 'text/html'); + createNewId(req, function() { + res.setHeader('Content-Type', 'text/html'); - res.locals.cameraStyle = 'reverse'; + res.locals.cameraStyle = 'reverse'; - res.render('prototype.jade', res.locals, function(err, result) { - res.send(result); + res.render('prototype.jade', res.locals, function(err, result) { + res.send(result); + }); }); } diff --git a/lib/boot.js b/lib/boot.js index d9b0642..81c59ff 100644 --- a/lib/boot.js +++ b/lib/boot.js @@ -34,4 +34,32 @@ module.exports = function(parent, options){ // mount the app parent.use(app); }); + + fs.readdirSync(__dirname + '/../posts').forEach(function(name){ + + // index.js in controller, with function as pages (views.py for django) + var obj = require('./../posts/' + name + '/index'); + + // urls.js, just like django urls.py + var urls = require('./../posts/' + name + '/urls'); + var name = obj.name || name; + var app = express(); + + // allow specifying the view engine + if (obj.engine) app.set('view engine', obj.engine); + app.set('views', __dirname + '/../posts/' + name + '/views'); + + // generate routes based + // on the exported methods + + verbose && console.log('\t' + name + ':'); + for (var key in urls) { + app.post(key, obj[urls[key]]); + console.log('\t\t' + key + ' -> ' + name + '.' + urls[key]); + } + console.log(); + + // mount the app + parent.use(app); + }); }; diff --git a/posts/arrow-clicked/index.js b/posts/arrow-clicked/index.js new file mode 100644 index 0000000..279c7e6 --- /dev/null +++ b/posts/arrow-clicked/index.js @@ -0,0 +1,22 @@ +var pg = require('pg'); +var secret = require('../../private'); + +module.exports.index = function(req, res) { + + var user_id = req.session.user_id; + var arrow_id = req.body.arrow_id; + + pg.connect(secret.url, function(err, client, release) { + client.query( + "INSERT INTO arrowclicked(user_id, arrow_id) VALUES($1,$2);", + [user_id, arrow_id], + function(err, result) { + release(); + } + ); + }); + + res.setHeader('Content-Type', 'text/html'); + res.send("user_id = " + user_id); + +} diff --git a/posts/arrow-clicked/urls.js b/posts/arrow-clicked/urls.js new file mode 100644 index 0000000..9e26ab8 --- /dev/null +++ b/posts/arrow-clicked/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/arrow-clicked': 'index' +} diff --git a/posts/coin-clicked/index.js b/posts/coin-clicked/index.js new file mode 100644 index 0000000..a6f9545 --- /dev/null +++ b/posts/coin-clicked/index.js @@ -0,0 +1,22 @@ +var pg = require('pg'); +var secret = require('../../private'); + +module.exports.index = function(req, res) { + + var user_id = req.session.user_id; + var coin_id = req.body.coin_id; + + pg.connect(secret.url, function(err, client, release) { + client.query( + "INSERT INTO coinclicked(user_id, coin_id) VALUES($1,$2);", + [user_id, coin_id], + function(err, result) { + release(); + } + ); + }); + + res.setHeader('Content-Type', 'text/html'); + res.send("user_id = " + user_id); + +} diff --git a/posts/coin-clicked/urls.js b/posts/coin-clicked/urls.js new file mode 100644 index 0000000..2b46584 --- /dev/null +++ b/posts/coin-clicked/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/coin-clicked': 'index' +} diff --git a/server.js b/server.js index 6d80a7f..12d5c9e 100644 --- a/server.js +++ b/server.js @@ -17,19 +17,12 @@ app.set('trust proxy', 1); app.use(cookieParser(secret.secret)); app.use(session({ - // express-session - // saveUninitialized: true, - // resave: true, - // secret: secret.secret - - // cookie-session keys: ['key1', 'key2'] })); app.use(bodyParser.text()); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); -// app.use(bodyParser.text({ type: 'text/html' })) app.use(function(req, res, next) { res.locals.title = "3DUI"; @@ -52,24 +45,6 @@ require('./lib/boot')(app, { verbose: !module.parent }); app.use('/static', express.static('static')); -app.post('/post', function(req, res) { - var user_id = req.session.user_id; - var arrow_id = req.body.arrow_id; - - pg.connect(secret.url, function(err, client, release) { - client.query( - "INSERT INTO arrowclicked(user_id, arrow_id) VALUES($1,$2);", - [user_id, arrow_id], - function(err, result) { - release(); - } - ); - }); - - res.setHeader('Content-Type', 'text/html'); - res.send("user_id = " + user_id); -}); - // When error raised app.use(function(err, req, res, next) { if (err.status === 404) { diff --git a/sql/all_elements.pgsql b/sql/all_elements.pgsql new file mode 100644 index 0000000..304dd61 --- /dev/null +++ b/sql/all_elements.pgsql @@ -0,0 +1,4 @@ +SELECT user_id, arrow_id as elt_id, time, 'arrow' as elt FROM arrowclicked +UNION +SELECT user_id, coin_id, time, 'coin' FROM coinclicked +ORDER BY time; diff --git a/sql/backup.pgsql b/sql/backup.pgsql index 354c123..9543059 100644 --- a/sql/backup.pgsql +++ b/sql/backup.pgsql @@ -1,5 +1,6 @@ DROP TABLE IF EXISTS users CASCADE; DROP TABLE IF EXISTS arrowclicked CASCADE; +DROP TABLE IF EXISTS coinclicked CASCADE; CREATE TABLE users ( id SERIAL PRIMARY KEY, @@ -13,3 +14,9 @@ CREATE TABLE arrowclicked( arrow_id INTEGER ); +CREATE TABLE coinclicked( + id SERIAL PRIMARY KEY, + user_id SERIAL REFERENCES users (id), + time TIMESTAMP DEFAULT NOW(), + coin_id INTEGER +); diff --git a/static/js/Logger.js b/static/js/Logger.js index 3c3d186..7d93d20 100644 --- a/static/js/Logger.js +++ b/static/js/Logger.js @@ -1,19 +1,26 @@ var BD = {}; +BD.Private = {}; +BD.Private.sendData = function(url, data) { + var xhr = new XMLHttpRequest(); + xhr.open("POST", url, true); + xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8"); + xhr.send(JSON.stringify(data)); +} + BD.Event = {}; BD.Event.ArrowClicked = function() {}; BD.Event.ArrowClicked.prototype.send = function() { - var url = "/post"; + var url = "/arrow-clicked"; + var data = {arrow_id: this.arrow_id}; + BD.Private.sendData(url, data); +} - var data = {}; - - data.arrow_id = this.arrow_id; - - var xhr = new XMLHttpRequest(); - xhr.open("POST", url, true); - xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8"); - xhr.send(JSON.stringify(data)); - -}; +BD.Event.CoinClicked = function() {}; +BD.Event.CoinClicked.prototype.send = function() { + var url = "/coin-clicked"; + var data = {coin_id: this.coin_id}; + BD.Private.sendData(url, data); +} diff --git a/static/js/prototype/raycasterTools.js b/static/js/prototype/raycasterTools.js index dbff63a..6c40f11 100644 --- a/static/js/prototype/raycasterTools.js +++ b/static/js/prototype/raycasterTools.js @@ -77,12 +77,15 @@ CameraSelecter.prototype.click = function(event) { if (newCamera !== undefined && !(newCamera instanceof Coin)) { var event = new BD.Event.ArrowClicked(); event.arrow_id = cameras.cameras.indexOf(newCamera); - event.user_id = 1; event.send(); this.cameras.mainCamera().moveHermite(newCamera); buttonManager.updateElements(); } else if (newCamera instanceof Coin) { + // Coin found, notify server + var event = new BD.Event.CoinClicked(); + event.coin_id = coins.indexOf(newCamera); + event.send(); newCamera.get(); } } diff --git a/views/main.jade b/views/main.jade index 6008cf1..5bf38ee 100644 --- a/views/main.jade +++ b/views/main.jade @@ -59,8 +59,7 @@ html(lang='fr') if alertCookie .alert.alert-warning.alert-dismissible(role="alert", style={'margin-top':'20px'}) button.close(type="button", data-dismiss="alert", aria-label="Close") - span(aria-hidden="true") - × + span(aria-hidden="true") × Warning : this website use cookies ! block content