Correction grosse erreur de synchro

This commit is contained in:
Bertrand BRUN 2011-04-28 11:35:57 +02:00
parent 001a45a1dd
commit 447573e593
2 changed files with 71 additions and 40 deletions

View File

@ -113,3 +113,7 @@ $.each({
var y = e.y; var y = e.y;
$.fn[i] = function(to, justCss) { return this.relativePos(x, y, to, justCss); }; $.fn[i] = function(to, justCss) { return this.relativePos(x, y, to, justCss); };
}); });
$.fn.clickOnce = function(fn) {
this.unbind("click",fn).click(fn);
};

View File

@ -15,20 +15,22 @@ State.prototype.set = function(key, value) {
return this; return this;
}; };
State.prototype.validate = function () { State.prototype.validate = function () {
if (oldScreen != state.screen) { state = this;
if (oldScreen != this.screen) {
if (window[oldScreen] && window[oldScreen].leave) window[oldScreen].leave(); if (window[oldScreen] && window[oldScreen].leave) window[oldScreen].leave();
oldScreen = state.screen; oldScreen = this.screen;
} }
if (window[state.screen] && window[state.screen].enter) window[state.screen].enter(); if (window[this.screen] && window[this.screen].enter) window[this.screen].enter();
return this;
}; };
var runstate = {};
var state; var state;
var oldScreen = ''; var oldScreen = '';
var ui = {}; var ui = {};
function hashchange() { function hashchange() {
var stateJSON = location.hash.substring(location.hash.indexOf("#") + 1); var stateJSON = location.hash.substring(location.hash.indexOf("#") + 1);
state = new State($.parseJSON(stateJSON)); state = new State($.parseJSON(stateJSON)).validate();
state.validate();
} }
// ==== JavaScript Style général // ==== JavaScript Style général
@ -59,8 +61,8 @@ function jss() {
var UI = { var UI = {
setPreference: function() {}, setPreference: function() {},
getPreference: function() {return "";}, getPreference: function() {return "";},
show: function() {}, show: function(title, text) {},// { if (typeof console != 'undefined') console.log(title, text);},
dismiss: function() {}, dismiss: function() {},//{if (typeof console != 'undefined') console.log('dismiss');},
exit: function() {} exit: function() {}
}; };
@ -108,14 +110,17 @@ frontpage.jss = function(w, h, iconSize) {
}; };
frontpage.enter = function () { frontpage.enter = function () {
state.commit(); if (location.hash != '') state.commit();
$("#frontpage .frontpage-button.game").click(function(){ $("#frontpage .frontpage-button.game").clickOnce(frontpage.click.game);
state.set('screen', 'game').validate();
});
jss(); jss();
UI.dismiss(); UI.dismiss();
}; };
frontpage.click = {};
frontpage.click.game = function(){
state.set('screen', 'game').validate();
};
// ==== Code métier pour le jeu // ==== Code métier pour le jeu
game = {}; game = {};
@ -166,23 +171,38 @@ game.jss = function(w, h, iconSize) {
game.enter = function () { game.enter = function () {
if (!state.game) { if (!state.game) {
var notAlreadyFetching = !runstate.gameFetched;
runstate.gameFetched = function(data) {
state.game = data;
state.currentWordNb = 0;
state.game.answers = [];
state.commit();
game.buildUi();
};
if (notAlreadyFetching) {
UI.show("PtiClic", "Récupération de la partie"); UI.show("PtiClic", "Récupération de la partie");
$.getJSON("getGame.php?callback=?", { $.getJSON("getGame.php?callback=?", {
user:"foo", user:"foo",
passwd:"bar", passwd:"bar",
nonce:Math.random() nonce:Math.random()
}, function(data) { }, function(data) {
state.game = data; var fn = runstate.gameFetched;
state.currentWordNb = 0; runstate.gameFetched = false;
state.game.answers = []; fn(data);
state.commit();
game.buildUi();
}).error(ajaxError); }).error(ajaxError);
}
} else { } else {
game.buildUi(); game.buildUi();
} }
jss(); jss();
}; };
game.leave = function () {
$("#game .relations").empty();
$('#game #mn-caption').stop().clearQueue();
if (runstate.gameFetched) runstate.gameFetched = function() {};
};
game.buildUi = function () { game.buildUi = function () {
$("#game .relations").empty(); $("#game .relations").empty();
$.each(state.game.relations, function(i, relation) { $.each(state.game.relations, function(i, relation) {
@ -201,18 +221,13 @@ game.buildUi = function () {
.appendTo("#game .relations"); .appendTo("#game .relations");
}); });
game.updateText(); game.updateText();
UI.dismiss();
} }
game.leave = function () {
$("#game .relations").empty();
$('#game #mn-caption').stop().clearQueue();
};
game.updateText = function() { game.updateText = function() {
$("#game .mn").text(state.game.cloud[state.currentWordNb].name); $("#game .mn").text(state.game.cloud[state.currentWordNb].name);
$("#game .mc").text(state.game.center.name); $("#game .mc").text(state.game.center.name);
jss(); jss();
UI.dismiss();
} }
game.animateNext = function (click, button) { game.animateNext = function (click, button) {
@ -261,6 +276,18 @@ score.jss = function(w, h, iconSize) {
score.enter = function () { score.enter = function () {
if (!state.hasScore) { if (!state.hasScore) {
var notAlreadyFetching = !runstate.scoreFetched;
runstate.scoreFetched = function(data) {
for (var i = 0; i < data.scores.length; ++i) {
state.game.cloud[i].score = data.scores[i];
}
delete data.score;
$.extend(state.game, data);
state.hasScore = true;
state.commit();
score.ui();
};
if (notAlreadyFetching) {
UI.show("PtiClic", "Calcul de votre score"); UI.show("PtiClic", "Calcul de votre score");
$.getJSON("server.php?callback=?", { $.getJSON("server.php?callback=?", {
user: "foo", user: "foo",
@ -270,22 +297,22 @@ score.enter = function () {
gid: state.game.gid, gid: state.game.gid,
answers: state.game.answers, answers: state.game.answers,
nonce: Math.random() nonce: Math.random()
}, function(data) { }, function(data){
for (var i = 0; i < data.scores.length; i++) { var fn = runstate.scoreFetched;
state.game.cloud[i].score = data.scores[i]; runstate.scoreFetched = false;
} fn(data);
delete data.score;
$.extend(state.game, data);
state.hasScore = true;
state.commit();
score.ui();
}).error(ajaxError); }).error(ajaxError);
}
} else { } else {
score.ui(); score.ui();
} }
jss(); jss();
} }
score.leave = function () {
if (runstate.scoreFetched) runstate.scoreFetched = function() {};
};
score.ui = function () { score.ui = function () {
$("#score .scores").empty(); $("#score .scores").empty();
$.each(state.game.cloud, function(i,e) { $.each(state.game.cloud, function(i,e) {
@ -302,10 +329,10 @@ score.ui = function () {
.css("color","rgb("+(255 - 255*percentScore).clip(0,255)+","+(191*percentScore).clip(0,255,true)+",0)") .css("color","rgb("+(255 - 255*percentScore).clip(0,255)+","+(191*percentScore).clip(0,255,true)+",0)")
.end() .end()
.appendTo("#score .scores"); .appendTo("#score .scores");
$("#score #jaivu").click(function() { });
$("#score #jaivu").clickOnce(function() {
state = new State().validate(); state = new State().validate();
}); });
jss(); jss();
});
UI.dismiss(); UI.dismiss();
} }