diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index 218e816..44b9c1f 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -112,6 +112,25 @@ var addArrowsFromId = function(client, req, res, callback, id) { ); } +var addResetsFromId = function(client, req, res, callback, id) { + client.query( + "SELECT time FROM resetclicked WHERE user_id = $1", + [id], + function(err, result) { + res.locals.path = res.locals.path || []; + for (var i in result.rows) { + res.locals.path.push( + { + type: 'reset', + time: result.rows[i].time + } + ); + } + callback(); + } + ); +} + var getAllUsers = function(req, res, callback) { pg.connect(pgc.url, function(err, client, release) { client.query( @@ -180,15 +199,17 @@ module.exports.replay_info = function(req, res) { addCamerasFromId(client, req, res, function() { addCoinsFromId(client, req, res, function() { addArrowsFromId(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)); + addResetsFromId(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); diff --git a/posts/keyboard-event/index.js b/posts/keyboard-event/index.js index 1877bfa..6623cee 100644 --- a/posts/keyboard-event/index.js +++ b/posts/keyboard-event/index.js @@ -8,8 +8,8 @@ module.exports.index = function(req, res) { pg.connect(secret.url, function(err, client, release) { client.query( - "INSERT INTO keyboardevent(user_id, direction, camera, time)" + - "VALUES($1, NULL, ROW(ROW($2,$3,$4),ROW($5,$6,$7)), to_timestamp($8));" , + "INSERT INTO keyboardevent(user_id, camera, time)" + + "VALUES($1, ROW(ROW($2,$3,$4),ROW($5,$6,$7)), to_timestamp($8));" , [ user_id, camera.position.x, diff --git a/posts/reset-clicked/index.js b/posts/reset-clicked/index.js new file mode 100644 index 0000000..2d76a3f --- /dev/null +++ b/posts/reset-clicked/index.js @@ -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; + var camera = req.body.camera; + + pg.connect(secret.url, function(err, client, release) { + client.query( + "INSERT INTO resetclicked(user_id, time)" + + "VALUES($1, to_timestamp($2));" , + [ + user_id, + req.body.time + ], + function(err, result) { + console.log(err); + release(); + } + ); + }); + + res.setHeader('Content-Type', 'text/html'); + res.send("user_id = " + user_id); + +} diff --git a/posts/reset-clicked/urls.js b/posts/reset-clicked/urls.js new file mode 100644 index 0000000..c5a6d2d --- /dev/null +++ b/posts/reset-clicked/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/reset-clicked': 'index' +} diff --git a/sql/backup.pgsql b/sql/backup.pgsql index 19fce0a..0127872 100644 --- a/sql/backup.pgsql +++ b/sql/backup.pgsql @@ -2,6 +2,7 @@ DROP TABLE IF EXISTS users CASCADE; DROP TABLE IF EXISTS arrowclicked CASCADE; DROP TABLE IF EXISTS coinclicked CASCADE; DROP TABLE IF EXISTS keyboardevent CASCADE; +DROP TABLE IF EXISTS resetclicked CASCADE; DROP TYPE IF EXISTS VECTOR3 CASCADE; DROP TYPE IF EXISTS CAMERA CASCADE; @@ -42,3 +43,9 @@ CREATE TABLE keyboardevent( time TIMESTAMP DEFAULT NOW(), camera CAMERA ); + +CREATE TABLE resetclicked( + id SERIAL PRIMARY KEY, + user_id SERIAL REFERENCES users (id), + time TIMESTAMP DEFAULT NOW() +); diff --git a/static/js/Logger.js b/static/js/Logger.js index 9bcdb2f..ed18926 100644 --- a/static/js/Logger.js +++ b/static/js/Logger.js @@ -47,3 +47,10 @@ BD.Event.KeyboardEvent.prototype.send = function() { }; BD.Private.sendData(url, data); } + +BD.Event.ResetClicked = function() {}; +BD.Event.ResetClicked.prototype.send = function() { + var url = "/reset-clicked"; + var data = {}; + BD.Private.sendData(url, data); +} diff --git a/static/js/PointerCamera.js b/static/js/PointerCamera.js index 7a3099f..170ef52 100644 --- a/static/js/PointerCamera.js +++ b/static/js/PointerCamera.js @@ -155,6 +155,7 @@ PointerCamera.prototype.reset = function() { this.resetBobomb(); this.moving = false; this.movingHermite = false; + (new BD.Event.ResetClicked()).send(); // this.position.copy(new THREE.Vector3(-8.849933489419644, 9.050627639459208, 0.6192960680432451)); // this.target.copy(new THREE.Vector3(17.945323228767702, -15.156828589982375, -16.585740412769756)); // this.anglesFromVectors(); @@ -307,6 +308,7 @@ PointerCamera.prototype.onMouseDown = function(event) { this.mouse.y = - ( ( event.clientY - renderer.domElement.offsetTop ) / renderer.domElement.height ) * 2 + 1; this.dragging = true; + this.mouseMoved = false; } PointerCamera.prototype.onMouseMove = function(event) { @@ -317,11 +319,20 @@ PointerCamera.prototype.onMouseMove = function(event) { this.mouseMove.x = this.mouse.x - mouse.x; this.mouseMove.y = this.mouse.y - mouse.y; + this.mouseMoved = true; } } PointerCamera.prototype.onMouseUp = function(event) { this.onMouseMove(event); + + // Send log to DB + if (this.dragging && this.mouseMoved && !this.moving && !this.movingHermite) { + var event = new BD.Event.KeyboardEvent(); + event.camera = this; + event.send(); + } + this.dragging = false; } diff --git a/static/js/ReplayCamera.js b/static/js/ReplayCamera.js index d4c5781..bc84114 100644 --- a/static/js/ReplayCamera.js +++ b/static/js/ReplayCamera.js @@ -19,14 +19,7 @@ var ReplayCamera = function() { xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { self.path = JSON.parse(xhr.responseText); - self.position.x = self.path[0].position.x; - self.position.y = self.path[0].position.y; - self.position.z = self.path[0].position.z; - self.target = new THREE.Vector3( - self.path[0].target.x, - self.path[0].target.y, - self.path[0].target.z - ); + self.reset(); self.started = true; self.nextEvent(); } @@ -56,6 +49,7 @@ ReplayCamera.prototype.update = function(time) { this.hermiteMotion(time); } else if (this.event.type == 'coin') { // Nothing to do + } else if (this.event.type == 'reset') { } } } @@ -111,6 +105,13 @@ ReplayCamera.prototype.nextEvent = function() { })(this); } else if (this.event.type == 'arrow') { this.moveHermite(cameras.cameras[this.event.id]); + } else if (this.event.type == 'reset') { + this.reset(); + (function (self) { + setTimeout(function() { + self.nextEvent(); + },500); + })(this); } } diff --git a/static/js/prototype/ButtonManager.js b/static/js/prototype/ButtonManager.js index 4521c2d..c716832 100644 --- a/static/js/prototype/ButtonManager.js +++ b/static/js/prototype/ButtonManager.js @@ -33,7 +33,11 @@ var ButtonManager = function(cameras, previewer) { self.collisionElement.onchange = function() {self.cameras.mainCamera().collisions = self.collisionElement.checked;} self.showarrowsElement.onchange = function() {self.showArrows = self.showarrowsElement.checked;} - self.resetElement.onclick = function() {self.cameras.mainCamera().reset();} + + self.resetElement.onclick = function() { + // Reinit camera + self.cameras.mainCamera().reset(); + } self.recommendationElement.onchange = function() { previewer.fixedRecommendation(self.recommendationElement.checked); diff --git a/static/js/prototype/Coin.js b/static/js/prototype/Coin.js index 10ca378..45bb86b 100644 --- a/static/js/prototype/Coin.js +++ b/static/js/prototype/Coin.js @@ -8,6 +8,7 @@ var _toto = new Audio(); Coin.extension = _toto.canPlayType("audio/x-vorbis") === "" ? ".ogg" : ".mp3"; Coin.prototype.init = function(x,y,z) { + Coin.nextSound = new Audio(static_path + 'data/music/redcoins/1' + Coin.extension); if (Coin.BASIC_MESH !== null) { this.mesh = Coin.BASIC_MESH.clone(); this.mesh.position.x = x; @@ -19,7 +20,6 @@ Coin.prototype.init = function(x,y,z) { (function(self,x,y,z) { setTimeout(function() { self.init(x,y,z); - Coin.nextSound = new Audio(static_path + 'data/music/redcoins/1' + Coin.extension); },1000); })(this,x,y,z); } @@ -41,7 +41,9 @@ Coin.prototype.update = function() { Coin.prototype.get = function() { if (!this.got) { this.got = true; - this.mesh.visible = false; + if (this.mesh) { + this.mesh.visible = false; + } Coin.total ++; Coin.nextSound.play(); if (Coin.total === 9) {