www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 447573e5936d507747df8caa97092297865ff82c
parent 001a45a1dd4c662035d7b8f3034b747cfb5d7de7
Author: Bertrand BRUN <bertrand0brun@gmail.com>
Date:   Thu, 28 Apr 2011 11:35:57 +0200

Correction grosse erreur de synchro

Diffstat:
Mcode/serveur/php/ressources/my-extensions.js | 5+++++
Mcode/serveur/php/ressources/pticlic.js | 107+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
2 files changed, 72 insertions(+), 40 deletions(-)

diff --git a/code/serveur/php/ressources/my-extensions.js b/code/serveur/php/ressources/my-extensions.js @@ -113,3 +113,7 @@ $.each({ var y = e.y; $.fn[i] = function(to, justCss) { return this.relativePos(x, y, to, justCss); }; }); + +$.fn.clickOnce = function(fn) { + this.unbind("click",fn).click(fn); +}; +\ No newline at end of file diff --git a/code/serveur/php/ressources/pticlic.js b/code/serveur/php/ressources/pticlic.js @@ -15,20 +15,22 @@ State.prototype.set = function(key, value) { return this; }; State.prototype.validate = function () { - if (oldScreen != state.screen) { + state = this; + if (oldScreen != this.screen) { 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 oldScreen = ''; var ui = {}; function hashchange() { var stateJSON = location.hash.substring(location.hash.indexOf("#") + 1); - state = new State($.parseJSON(stateJSON)); - state.validate(); + state = new State($.parseJSON(stateJSON)).validate(); } // ==== JavaScript Style général @@ -59,8 +61,8 @@ function jss() { var UI = { setPreference: function() {}, getPreference: function() {return "";}, - show: function() {}, - dismiss: function() {}, + show: function(title, text) {},// { if (typeof console != 'undefined') console.log(title, text);}, + dismiss: function() {},//{if (typeof console != 'undefined') console.log('dismiss');}, exit: function() {} }; @@ -108,14 +110,17 @@ frontpage.jss = function(w, h, iconSize) { }; frontpage.enter = function () { - state.commit(); - $("#frontpage .frontpage-button.game").click(function(){ - state.set('screen', 'game').validate(); - }); + if (location.hash != '') state.commit(); + $("#frontpage .frontpage-button.game").clickOnce(frontpage.click.game); jss(); UI.dismiss(); }; +frontpage.click = {}; +frontpage.click.game = function(){ + state.set('screen', 'game').validate(); +}; + // ==== Code métier pour le jeu game = {}; @@ -166,23 +171,38 @@ game.jss = function(w, h, iconSize) { game.enter = function () { if (!state.game) { - UI.show("PtiClic", "Récupération de la partie"); - $.getJSON("getGame.php?callback=?", { - user:"foo", - passwd:"bar", - nonce:Math.random() - }, function(data) { + var notAlreadyFetching = !runstate.gameFetched; + runstate.gameFetched = function(data) { state.game = data; state.currentWordNb = 0; state.game.answers = []; state.commit(); game.buildUi(); - }).error(ajaxError); + }; + if (notAlreadyFetching) { + UI.show("PtiClic", "Récupération de la partie"); + $.getJSON("getGame.php?callback=?", { + user:"foo", + passwd:"bar", + nonce:Math.random() + }, function(data) { + var fn = runstate.gameFetched; + runstate.gameFetched = false; + fn(data); + }).error(ajaxError); + } } else { game.buildUi(); } jss(); }; + +game.leave = function () { + $("#game .relations").empty(); + $('#game #mn-caption').stop().clearQueue(); + if (runstate.gameFetched) runstate.gameFetched = function() {}; +}; + game.buildUi = function () { $("#game .relations").empty(); $.each(state.game.relations, function(i, relation) { @@ -201,18 +221,13 @@ game.buildUi = function () { .appendTo("#game .relations"); }); game.updateText(); - UI.dismiss(); } -game.leave = function () { - $("#game .relations").empty(); - $('#game #mn-caption').stop().clearQueue(); -}; - game.updateText = function() { $("#game .mn").text(state.game.cloud[state.currentWordNb].name); $("#game .mc").text(state.game.center.name); jss(); + UI.dismiss(); } game.animateNext = function (click, button) { @@ -261,17 +276,9 @@ score.jss = function(w, h, iconSize) { score.enter = function () { if (!state.hasScore) { - UI.show("PtiClic", "Calcul de votre score"); - $.getJSON("server.php?callback=?", { - user: "foo", - passwd: "bar", - action: 1, - pgid: state.game.pgid, - gid: state.game.gid, - answers: state.game.answers, - nonce: Math.random() - }, function(data) { - for (var i = 0; i < data.scores.length; i++) { + 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; @@ -279,13 +286,33 @@ score.enter = function () { state.hasScore = true; state.commit(); score.ui(); - }).error(ajaxError); + }; + if (notAlreadyFetching) { + UI.show("PtiClic", "Calcul de votre score"); + $.getJSON("server.php?callback=?", { + user: "foo", + passwd: "bar", + action: 1, + pgid: state.game.pgid, + gid: state.game.gid, + answers: state.game.answers, + nonce: Math.random() + }, function(data){ + var fn = runstate.scoreFetched; + runstate.scoreFetched = false; + fn(data); + }).error(ajaxError); + } } else { score.ui(); } jss(); } +score.leave = function () { + if (runstate.scoreFetched) runstate.scoreFetched = function() {}; +}; + score.ui = function () { $("#score .scores").empty(); $.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)") .end() .appendTo("#score .scores"); - $("#score #jaivu").click(function() { - state = new State().validate(); - }); - jss(); }); + $("#score #jaivu").clickOnce(function() { + state = new State().validate(); + }); + jss(); UI.dismiss(); }