Cleaning and coin events
This commit is contained in:
parent
d6e8686b3f
commit
ee3bf01ab0
|
@ -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,11 +33,11 @@ module.exports.arrows = function(req, res) {
|
|||
res.render('prototype.jade', res.locals, function(err, result) {
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
module.exports.viewports = function(req, res) {
|
||||
createNewId(req, function() {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
|
||||
res.locals.cameraStyle = 'viewports';
|
||||
|
@ -46,9 +45,11 @@ module.exports.viewports = function(req, res) {
|
|||
res.render('prototype.jade', res.locals, function(err, result) {
|
||||
res.send(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports.reverse = function(req, res) {
|
||||
createNewId(req, function() {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
|
||||
res.locals.cameraStyle = 'reverse';
|
||||
|
@ -56,4 +57,5 @@ module.exports.reverse = function(req, res) {
|
|||
res.render('prototype.jade', res.locals, function(err, result) {
|
||||
res.send(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
28
lib/boot.js
28
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);
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
module.exports = {
|
||||
'/arrow-clicked': 'index'
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
module.exports = {
|
||||
'/coin-clicked': 'index'
|
||||
}
|
25
server.js
25
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) {
|
||||
|
|
|
@ -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;
|
|
@ -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
|
||||
);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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") ×
|
||||
<strong>Warning</strong> : this website use cookies !
|
||||
|
||||
block content
|
||||
|
|
Loading…
Reference in New Issue