From baccd011116b24122628f8c313556a64400ea03d Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Fri, 5 Jun 2015 10:35:20 +0200 Subject: [PATCH] Different scenes are now managed correctly --- controllers/prototype/dbrequests.js | 31 ++++---- controllers/prototype/index.js | 73 +++++++++++++++++-- .../views/prototype_interactive.jade | 2 + .../prototype/views/prototype_replays.jade | 1 + js/prototype/initScene.js | 4 + js/prototype/main.js | 4 +- js/prototype/replay.js | 2 +- 7 files changed, 96 insertions(+), 21 deletions(-) diff --git a/controllers/prototype/dbrequests.js b/controllers/prototype/dbrequests.js index a9b2d00..a19edff 100644 --- a/controllers/prototype/dbrequests.js +++ b/controllers/prototype/dbrequests.js @@ -275,9 +275,10 @@ UserCreator.prototype.finish = function() { this.finishAction(this.finalResult); } -var ExpCreator = function(user_id, finishAction) { +var ExpCreator = function(user_id, scene_id, finishAction) { this.finishAction = finishAction; this.user_id = user_id; + this.scene_id = scene_id; // Connect to db var self = this; @@ -291,13 +292,11 @@ var ExpCreator = function(user_id, finishAction) { ExpCreator.prototype.execute = function() { var self = this; this.client.query( - // TODO this is ugly, we should not do that... - "INSERT INTO experiment(user_id, scene_id) VALUES($1,1);", - [self.user_id], + "INSERT INTO experiment(user_id, scene_id) VALUES($1,$2);", + [self.user_id, self.scene_id], function(err, result) { self.client.query("SELECT MAX(id) AS id FROM experiment;", function(err, result) { self.finalResult = result.rows[0].id; - console.log(self.finalResult); self.finish(); }); } @@ -359,10 +358,16 @@ var ExpIdChecker = function(id, finishAction) { ExpIdChecker.prototype.execute = function() { var self = this; this.client.query( - "SELECT count(id) > 0 AS answer FROM experiment WHERE id = $1;", + "SELECT scene_id FROM experiment WHERE id = $1;", [self.id], function(err, result) { - self.finalResult = result.rows[0].answer; + console.log(err); + console.log(result); + if (result === undefined) { + self.finalResult = null; + } else { + self.finalResult = result.rows[0].scene_id; + } self.finish(); } ); @@ -428,10 +433,10 @@ var tryUser = function(id, callback) { } } -module.exports.getInfo = function(id, callback) { new Info(id, callback); }; -module.exports.createUser = function(callback) { new UserCreator(callback); }; -module.exports.createExp = function(id, callback) { new ExpCreator(id, callback); }; -module.exports.checkUserId = function(id, callback) { new UserIdChecker(id, callback); }; -module.exports.checkExpId = function(id, callback) { new ExpIdChecker(id, callback); }; -module.exports.getAllExps = function(callback) { new ExpGetter(callback); }; +module.exports.getInfo = function(id, callback) { new Info(id, callback); }; +module.exports.createUser = function(callback) { new UserCreator(callback); }; +module.exports.createExp = function(id, scene_id, callback) { new ExpCreator(id, scene_id, callback); }; +module.exports.checkUserId = function(id, callback) { new UserIdChecker(id, callback); }; +module.exports.checkExpId = function(id, callback) { new ExpIdChecker(id, callback); }; +module.exports.getAllExps = function(callback) { new ExpGetter(callback); }; module.exports.tryUser = tryUser; diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index 2b604ae..b43526c 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -3,6 +3,35 @@ var pg = require('pg'); var pgc = require('../../private'); var db = require('./dbrequests'); +// Shuffle array +function shuffle(array) { + var currentIndex = array.length, temporaryValue, randomIndex ; + + // While there remain elements to shuffle... + while (0 !== currentIndex) { + + // Pick a remaining element... + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + + // And swap it with the current element. + temporaryValue = array[currentIndex]; + array[currentIndex] = array[randomIndex]; + array[randomIndex] = temporaryValue; + } + + return array; +} + +function randomArray() { + var arr = []; + for (var i = 2; i < 5; i++) { + arr.push(i); + } + arr = shuffle(arr); + return arr; +} + module.exports.index = function(req, res) { res.setHeader('Content-Type', 'text/html'); @@ -11,14 +40,43 @@ module.exports.index = function(req, res) { }); } +var generateSceneNumber = function(req, res) { + if (req.session.scenes !== undefined) { + req.session.currentSceneIndex++; + } else { + req.session.scenes = randomArray(); + req.session.currentSceneIndex = 0; + console.log("Init : " + req.session.scenes); + } + + console.log("SceneIndex : " + req.session.currentSceneIndex); + return req.session.scenes[req.session.currentSceneIndex]; +} + +var sceneToFunction = function(scene) { + switch (scene) { + case 2: + return 'initBobomb'; + case 3: + return 'initMountain'; + case 4: + return 'initWhomp'; + default: + return 'initPeach'; + } +} + var protoHelper = function(template) { return function(req, res) { db.tryUser(req.session.user_id, function(id) { - db.createExp(id, function(id) { - req.session.exp_id = id; - req.session.user_id = id; - req.session.save(); + // Get random scene number + var scene = generateSceneNumber(req, res); + res.locals.scene = sceneToFunction(scene); + req.session.user_id = id; + db.createExp(id, req.session.scenes[req.session.currentSceneIndex], function(id) { + req.session.exp_id = id; + req.session.save(); res.setHeader('Content-Type','text/html'); res.render(template, res.locals, function(err, result) { res.send(result); @@ -47,12 +105,15 @@ module.exports.replay = function(req, res, next) { // Get id parameter res.locals.id = tools.filterInt(req.params.id); - db.checkExpId(res.locals.id, function(idExist) { - if (!idExist) { + db.checkExpId(res.locals.id, function(scene_id) { + console.log("Scene_id = " + scene_id); + if (scene_id === null) { var err = new Error("This replay does not exist"); err.status = 404; next(err); } else { + res.locals.initjs = sceneToFunction(scene_id); + console.log(scene_id + " " + res.locals.initjs); res.setHeader('Content-Type', 'text/html'); res.render('prototype_replays.jade', res.locals, function(err, result) { res.send(result); diff --git a/controllers/prototype/views/prototype_interactive.jade b/controllers/prototype/views/prototype_interactive.jade index 24fd196..8c12dce 100644 --- a/controllers/prototype/views/prototype_interactive.jade +++ b/controllers/prototype/views/prototype_interactive.jade @@ -4,6 +4,8 @@ block title title #{title} - Prototype block mainjs + script initMainScene = #{scene}; + script(src="/static/js/prototypeinteractive.min.js") block description diff --git a/controllers/prototype/views/prototype_replays.jade b/controllers/prototype/views/prototype_replays.jade index 9f1f4dc..93e99cb 100644 --- a/controllers/prototype/views/prototype_replays.jade +++ b/controllers/prototype/views/prototype_replays.jade @@ -7,6 +7,7 @@ block extrajs script(src="/static/js/prototypetools.min.js") script RecommendedCamera = FixedCamera; script var params = params || {}; params.get = params.get || {}; params.get.id = #{id}; + script initMainScene = #{initjs} script(src="/static/js/replay.min.js") block extrahead diff --git a/js/prototype/initScene.js b/js/prototype/initScene.js index a02c8e6..dcfc6ff 100644 --- a/js/prototype/initScene.js +++ b/js/prototype/initScene.js @@ -106,6 +106,10 @@ function initPeach(camera, scene, static_path, coins) { scene.add(camera); Coin.init(0.001); + var otherCams = []; + var cameras = new CameraContainer(camera, otherCams); + + return cameras; } function initZeldaScene(scene, collidableObjects, loader, static_path) { diff --git a/js/prototype/main.js b/js/prototype/main.js index e3f49ad..30045b4 100644 --- a/js/prototype/main.js +++ b/js/prototype/main.js @@ -78,7 +78,9 @@ function init() { // Initialize pointer camera var camera1 = new PointerCamera(50, container_size.width() / container_size.height(), 0.1, 100000, renderer, container); - cameras = initBobomb(camera1, scene, static_path, coins); + cameras = initMainScene(camera1, scene, static_path, coins); + // cameras = initPeach(camera1, scene, static_path, coins); + // cameras = initBobomb(camera1, scene, static_path, coins); // cameras = initWhomp(camera1, scene, static_path, coins); // cameras = initMountain(camera1, scene, static_path, coins); // cameras = initSponza(camera1, scene, static_path, coins); diff --git a/js/prototype/replay.js b/js/prototype/replay.js index 14cf824..2c5033a 100644 --- a/js/prototype/replay.js +++ b/js/prototype/replay.js @@ -67,7 +67,7 @@ function init() { // Initialize pointer camera var camera1 = new ReplayCamera(50, container_size.width() / container_size.height(), 0.01, 100000, coins); - cameras = initBobomb(camera1, scene, static_path, coins); + cameras = initMainScene(camera1, scene, static_path, coins); camera1.cameras = cameras; // Add listeners