Cleaning and coin events

This commit is contained in:
Thomas FORGIONE 2015-05-19 11:03:53 +02:00
parent d6e8686b3f
commit ee3bf01ab0
12 changed files with 124 additions and 49 deletions

View File

@ -26,7 +26,6 @@ module.exports.index = function(req, res) {
module.exports.arrows = function(req, res) { module.exports.arrows = function(req, res) {
createNewId(req, function() { createNewId(req, function() {
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.locals.cameraStyle = 'arrows'; res.locals.cameraStyle = 'arrows';
@ -34,11 +33,11 @@ module.exports.arrows = function(req, res) {
res.render('prototype.jade', res.locals, function(err, result) { res.render('prototype.jade', res.locals, function(err, result) {
res.send(result); res.send(result);
}); });
}); });
} }
module.exports.viewports = function(req, res) { module.exports.viewports = function(req, res) {
createNewId(req, function() {
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.locals.cameraStyle = 'viewports'; res.locals.cameraStyle = 'viewports';
@ -46,9 +45,11 @@ module.exports.viewports = function(req, res) {
res.render('prototype.jade', res.locals, function(err, result) { res.render('prototype.jade', res.locals, function(err, result) {
res.send(result); res.send(result);
}); });
});
} }
module.exports.reverse = function(req, res) { module.exports.reverse = function(req, res) {
createNewId(req, function() {
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.locals.cameraStyle = 'reverse'; res.locals.cameraStyle = 'reverse';
@ -56,4 +57,5 @@ module.exports.reverse = function(req, res) {
res.render('prototype.jade', res.locals, function(err, result) { res.render('prototype.jade', res.locals, function(err, result) {
res.send(result); res.send(result);
}); });
});
} }

View File

@ -34,4 +34,32 @@ module.exports = function(parent, options){
// mount the app // mount the app
parent.use(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);
});
}; };

View File

@ -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);
}

View File

@ -0,0 +1,3 @@
module.exports = {
'/arrow-clicked': 'index'
}

View File

@ -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);
}

View File

@ -0,0 +1,3 @@
module.exports = {
'/coin-clicked': 'index'
}

View File

@ -17,19 +17,12 @@ app.set('trust proxy', 1);
app.use(cookieParser(secret.secret)); app.use(cookieParser(secret.secret));
app.use(session({ app.use(session({
// express-session
// saveUninitialized: true,
// resave: true,
// secret: secret.secret
// cookie-session
keys: ['key1', 'key2'] keys: ['key1', 'key2']
})); }));
app.use(bodyParser.text()); app.use(bodyParser.text());
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); app.use(bodyParser.json());
// app.use(bodyParser.text({ type: 'text/html' }))
app.use(function(req, res, next) { app.use(function(req, res, next) {
res.locals.title = "3DUI"; res.locals.title = "3DUI";
@ -52,24 +45,6 @@ require('./lib/boot')(app, { verbose: !module.parent });
app.use('/static', express.static('static')); 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 // When error raised
app.use(function(err, req, res, next) { app.use(function(err, req, res, next) {
if (err.status === 404) { if (err.status === 404) {

4
sql/all_elements.pgsql Normal file
View File

@ -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;

View File

@ -1,5 +1,6 @@
DROP TABLE IF EXISTS users CASCADE; DROP TABLE IF EXISTS users CASCADE;
DROP TABLE IF EXISTS arrowclicked CASCADE; DROP TABLE IF EXISTS arrowclicked CASCADE;
DROP TABLE IF EXISTS coinclicked CASCADE;
CREATE TABLE users ( CREATE TABLE users (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
@ -13,3 +14,9 @@ CREATE TABLE arrowclicked(
arrow_id INTEGER arrow_id INTEGER
); );
CREATE TABLE coinclicked(
id SERIAL PRIMARY KEY,
user_id SERIAL REFERENCES users (id),
time TIMESTAMP DEFAULT NOW(),
coin_id INTEGER
);

View File

@ -1,19 +1,26 @@
var BD = {}; 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 = {};
BD.Event.ArrowClicked = function() {}; BD.Event.ArrowClicked = function() {};
BD.Event.ArrowClicked.prototype.send = 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 = {}; BD.Event.CoinClicked = function() {};
BD.Event.CoinClicked.prototype.send = function() {
data.arrow_id = this.arrow_id; var url = "/coin-clicked";
var data = {coin_id: this.coin_id};
var xhr = new XMLHttpRequest(); BD.Private.sendData(url, data);
xhr.open("POST", url, true); }
xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(data));
};

View File

@ -77,12 +77,15 @@ CameraSelecter.prototype.click = function(event) {
if (newCamera !== undefined && !(newCamera instanceof Coin)) { if (newCamera !== undefined && !(newCamera instanceof Coin)) {
var event = new BD.Event.ArrowClicked(); var event = new BD.Event.ArrowClicked();
event.arrow_id = cameras.cameras.indexOf(newCamera); event.arrow_id = cameras.cameras.indexOf(newCamera);
event.user_id = 1;
event.send(); event.send();
this.cameras.mainCamera().moveHermite(newCamera); this.cameras.mainCamera().moveHermite(newCamera);
buttonManager.updateElements(); buttonManager.updateElements();
} else if (newCamera instanceof Coin) { } 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(); newCamera.get();
} }
} }

View File

@ -59,8 +59,7 @@ html(lang='fr')
if alertCookie if alertCookie
.alert.alert-warning.alert-dismissible(role="alert", style={'margin-top':'20px'}) .alert.alert-warning.alert-dismissible(role="alert", style={'margin-top':'20px'})
button.close(type="button", data-dismiss="alert", aria-label="Close") button.close(type="button", data-dismiss="alert", aria-label="Close")
span(aria-hidden="true") span(aria-hidden="true") ×
×
<strong>Warning</strong> : this website use cookies ! <strong>Warning</strong> : this website use cookies !
block content block content