Different scenes are now managed correctly

This commit is contained in:
Thomas FORGIONE 2015-06-05 10:35:20 +02:00
parent d0e3d4bc3e
commit baccd01111
7 changed files with 96 additions and 21 deletions

View File

@ -275,9 +275,10 @@ UserCreator.prototype.finish = function() {
this.finishAction(this.finalResult); this.finishAction(this.finalResult);
} }
var ExpCreator = function(user_id, finishAction) { var ExpCreator = function(user_id, scene_id, finishAction) {
this.finishAction = finishAction; this.finishAction = finishAction;
this.user_id = user_id; this.user_id = user_id;
this.scene_id = scene_id;
// Connect to db // Connect to db
var self = this; var self = this;
@ -291,13 +292,11 @@ var ExpCreator = function(user_id, finishAction) {
ExpCreator.prototype.execute = function() { ExpCreator.prototype.execute = function() {
var self = this; var self = this;
this.client.query( this.client.query(
// TODO this is ugly, we should not do that... "INSERT INTO experiment(user_id, scene_id) VALUES($1,$2);",
"INSERT INTO experiment(user_id, scene_id) VALUES($1,1);", [self.user_id, self.scene_id],
[self.user_id],
function(err, result) { function(err, result) {
self.client.query("SELECT MAX(id) AS id FROM experiment;", function(err, result) { self.client.query("SELECT MAX(id) AS id FROM experiment;", function(err, result) {
self.finalResult = result.rows[0].id; self.finalResult = result.rows[0].id;
console.log(self.finalResult);
self.finish(); self.finish();
}); });
} }
@ -359,10 +358,16 @@ var ExpIdChecker = function(id, finishAction) {
ExpIdChecker.prototype.execute = function() { ExpIdChecker.prototype.execute = function() {
var self = this; var self = this;
this.client.query( this.client.query(
"SELECT count(id) > 0 AS answer FROM experiment WHERE id = $1;", "SELECT scene_id FROM experiment WHERE id = $1;",
[self.id], [self.id],
function(err, result) { 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(); self.finish();
} }
); );
@ -428,10 +433,10 @@ var tryUser = function(id, callback) {
} }
} }
module.exports.getInfo = function(id, callback) { new Info(id, callback); }; module.exports.getInfo = function(id, callback) { new Info(id, callback); };
module.exports.createUser = function(callback) { new UserCreator(callback); }; module.exports.createUser = function(callback) { new UserCreator(callback); };
module.exports.createExp = function(id, callback) { new ExpCreator(id, 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.checkUserId = function(id, callback) { new UserIdChecker(id, callback); };
module.exports.checkExpId = function(id, callback) { new ExpIdChecker(id, callback); }; module.exports.checkExpId = function(id, callback) { new ExpIdChecker(id, callback); };
module.exports.getAllExps = function(callback) { new ExpGetter(callback); }; module.exports.getAllExps = function(callback) { new ExpGetter(callback); };
module.exports.tryUser = tryUser; module.exports.tryUser = tryUser;

View File

@ -3,6 +3,35 @@ var pg = require('pg');
var pgc = require('../../private'); var pgc = require('../../private');
var db = require('./dbrequests'); 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) { module.exports.index = function(req, res) {
res.setHeader('Content-Type', 'text/html'); 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) { var protoHelper = function(template) {
return function(req, res) { return function(req, res) {
db.tryUser(req.session.user_id, function(id) { db.tryUser(req.session.user_id, function(id) {
db.createExp(id, function(id) { // Get random scene number
req.session.exp_id = id; var scene = generateSceneNumber(req, res);
req.session.user_id = id; res.locals.scene = sceneToFunction(scene);
req.session.save(); 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.setHeader('Content-Type','text/html');
res.render(template, res.locals, function(err, result) { res.render(template, res.locals, function(err, result) {
res.send(result); res.send(result);
@ -47,12 +105,15 @@ module.exports.replay = function(req, res, next) {
// Get id parameter // Get id parameter
res.locals.id = tools.filterInt(req.params.id); res.locals.id = tools.filterInt(req.params.id);
db.checkExpId(res.locals.id, function(idExist) { db.checkExpId(res.locals.id, function(scene_id) {
if (!idExist) { console.log("Scene_id = " + scene_id);
if (scene_id === null) {
var err = new Error("This replay does not exist"); var err = new Error("This replay does not exist");
err.status = 404; err.status = 404;
next(err); next(err);
} else { } else {
res.locals.initjs = sceneToFunction(scene_id);
console.log(scene_id + " " + res.locals.initjs);
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.render('prototype_replays.jade', res.locals, function(err, result) { res.render('prototype_replays.jade', res.locals, function(err, result) {
res.send(result); res.send(result);

View File

@ -4,6 +4,8 @@ block title
title #{title} - Prototype title #{title} - Prototype
block mainjs block mainjs
script initMainScene = #{scene};
script(src="/static/js/prototypeinteractive.min.js") script(src="/static/js/prototypeinteractive.min.js")
block description block description

View File

@ -7,6 +7,7 @@ block extrajs
script(src="/static/js/prototypetools.min.js") script(src="/static/js/prototypetools.min.js")
script RecommendedCamera = FixedCamera; script RecommendedCamera = FixedCamera;
script var params = params || {}; params.get = params.get || {}; params.get.id = #{id}; script var params = params || {}; params.get = params.get || {}; params.get.id = #{id};
script initMainScene = #{initjs}
script(src="/static/js/replay.min.js") script(src="/static/js/replay.min.js")
block extrahead block extrahead

View File

@ -106,6 +106,10 @@ function initPeach(camera, scene, static_path, coins) {
scene.add(camera); scene.add(camera);
Coin.init(0.001); Coin.init(0.001);
var otherCams = [];
var cameras = new CameraContainer(camera, otherCams);
return cameras;
} }
function initZeldaScene(scene, collidableObjects, loader, static_path) { function initZeldaScene(scene, collidableObjects, loader, static_path) {

View File

@ -78,7 +78,9 @@ function init() {
// Initialize pointer camera // Initialize pointer camera
var camera1 = new PointerCamera(50, container_size.width() / container_size.height(), 0.1, 100000, renderer, container); 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 = initWhomp(camera1, scene, static_path, coins);
// cameras = initMountain(camera1, scene, static_path, coins); // cameras = initMountain(camera1, scene, static_path, coins);
// cameras = initSponza(camera1, scene, static_path, coins); // cameras = initSponza(camera1, scene, static_path, coins);

View File

@ -67,7 +67,7 @@ function init() {
// Initialize pointer camera // Initialize pointer camera
var camera1 = new ReplayCamera(50, container_size.width() / container_size.height(), 0.01, 100000, coins); 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; camera1.cameras = cameras;
// Add listeners // Add listeners