Added hovered events

This commit is contained in:
Thomas FORGIONE 2015-05-21 15:35:40 +02:00
parent 2a5ae4d985
commit 8e592c87b5
6 changed files with 95 additions and 9 deletions

View File

@ -168,6 +168,27 @@ var addPreviousNextFromId = function(client, req, res, callback, id) {
);
}
var addHoveredFromId = function(client, req, res, callback, id) {
client.query(
"SELECT start, time, arrow_id FROM hovered WHERE id = $1",
[id],
function(err, result) {
res.locals.path = res.locals.path || [];
for (var i in result.rows) {
res.locals.path.push(
{
type: "hovered",
time: result.rows[i].time,
start: result.rows[i].start,
id: result.rows[i].arrow_id
}
);
}
callback();
}
);
}
var getAllUsers = function(req, res, callback) {
pg.connect(pgc.url, function(err, client, release) {
client.query(
@ -238,15 +259,17 @@ module.exports.replay_info = function(req, res) {
addArrowsFromId(client, req, res, function() {
addResetsFromId(client, req, res, function() {
addPreviousNextFromId(client, req, res, function() {
res.locals.path.sort(function(elt1, elt2) {
// Dates as string can be compared
if (elt1.time < elt2.time)
return -1;
if (elt1.time > elt2.time)
return 1;
return 0;
});
res.send(JSON.stringify(res.locals.path));
addHoveredFromId(client, req, res, function() {
res.locals.path.sort(function(elt1, elt2) {
// Dates as string can be compared
if (elt1.time < elt2.time)
return -1;
if (elt1.time > elt2.time)
return 1;
return 0;
});
res.send(JSON.stringify(res.locals.path));
}, id);
}, id);
}, id);
}, id);

27
posts/hovered/index.js Normal file
View File

@ -0,0 +1,27 @@
var pg = require('pg');
var secret = require('../../private');
module.exports.index = function(req, res) {
var user_id = req.session.user_id;
pg.connect(secret.url, function(err, client, release) {
client.query(
"INSERT INTO hovered(user_id, time, start, arrow_id)" +
"VALUES($1, to_timestamp($2), $3, $4);" ,
[
user_id,
req.body.time,
req.body.start ? true : false,
req.body.arrow_id
],
function(err, result) {
release();
}
);
});
res.setHeader('Content-Type', 'text/html');
res.send("user_id = " + user_id);
}

3
posts/hovered/urls.js Normal file
View File

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

View File

@ -4,6 +4,7 @@ DROP TABLE IF EXISTS coinclicked CASCADE;
DROP TABLE IF EXISTS keyboardevent CASCADE;
DROP TABLE IF EXISTS resetclicked CASCADE;
DROP TABLE IF EXISTS previousnextclicked CASCADE;
DROP TABLE IF EXISTS hovered CASCADE;
DROP TYPE IF EXISTS VECTOR3 CASCADE;
DROP TYPE IF EXISTS CAMERA CASCADE;
@ -63,3 +64,11 @@ CREATE TABLE previousnextclicked(
time TIMESTAMP DEFAULT NOW(),
camera CAMERA
);
CREATE TABLE hovered(
id SERIAL PRIMARY KEY,
user_id SERIAL REFERENCES users (id),
start BOOLEAN NOT NULL,
time TIMESTAMP DEFAULT NOW(),
arrow_id INT
);

View File

@ -77,3 +77,14 @@ BD.Event.PreviousNextClicked.prototype.send = function() {
BD.Private.sendData(url, data);
}
BD.Event.Hovered = function() {};
BD.Event.Hovered.prototype.send = function() {
var url = "/hovered";
var data = {
start: this.start,
arrow_id: this.arrow_id
};
BD.Private.sendData(url, data);
}

View File

@ -66,12 +66,25 @@ CameraSelecter.prototype.update = function(event) {
if (hovered !== undefined && !(hovered instanceof Coin)) {
if (hovered !== previousCamera) {
// log it
var event = new BD.Event.Hovered();
event.start = true;
event.arrow_id = cameras.cameras.indexOf(this.currentPointedCamera);
event.send();
this.prev.x = this.mouse.x;
this.prev.y = this.mouse.y;
}
this.prev.camera = hovered;
this.prev.go = true;
} else {
if (this.prev.go) {
// Log if previous was not null
var event = new BD.Event.Hovered();
event.start = false;
event.arrow_id = null;
event.send();
}
this.prev.go = false;
}
}