diff --git a/code/serveur/php/ressources/my-extensions.js b/code/serveur/php/ressources/my-extensions.js
index 5484687..a16ac0a 100644
--- a/code/serveur/php/ressources/my-extensions.js
+++ b/code/serveur/php/ressources/my-extensions.js
@@ -255,31 +255,3 @@ function Cache(resolver) {
return cache[k] = cache[k] || $.Deferred(function(dfd) { resolver(k, dfd, arg); }).fail(function() { cache[k] = false; }).promise();
};
}
-
-/* Enchaînement des écrans
-
-*** Utiliser un objet Deferred pour les fonctions qu'on ne veut apeller qu'une fois.
-
-***
-
-- Cache des parties récupérées & scores (key = pgid pour les deux, mais params supplémentaires pour scores)
-new Cache(queryFn(k, dfd, cache) { cache.set(k,v); dfd.resolve(data); });
-Cache.get(k) returns Promise; // Peut déclencher $.extend(Cache, queryFn(k)).
-
-- Récupérer une partie aléatoire, et la stocker dans le cache à son arrivée
-- Afficher $(#game) (et $(#score)) une fois la partie (score) récupéré(e) et le(la) consommer
-- Sauf si l'action a été annulée.
-$.when(getGame, goGame)
-if (runstate.nextScreen == 'game') …
-- Lorsqu'une requête échoue, on demande le login, on retente la requête avec ce login/mdp. Si ça marche avec ce login/mdp, on .resolve(), sinon on .fail().
-
-***
-
-Aller sur un écran donné (parfois sans changer l'URL, par ex. pour splash→frontpage, et lorsqu'on force le login).
-Recevoir des données avant d'entrer dans un écran.
-Envoyer des données avant de quiter un écran.
-Vérouiller l'écran courant pendant qu'on attend un transfert ou bien des écrans d'«attente».
-Lorsqu'un transfert a échoué car non logué, on va sur l'écran de connexion et on retente le transfert ensuite.
-Stocker uniquement les données importantes dans l'url (état, numéro de partie, réponses).
-Pouvoir basculer sur un écran et exécuter quelque chose une fois qu'il est chargé (exécuter le commit pour l'url).
-*/
\ No newline at end of file
diff --git a/code/serveur/php/ressources/pticlic.js b/code/serveur/php/ressources/pticlic.js
index 69738d3..8d7e364 100644
--- a/code/serveur/php/ressources/pticlic.js
+++ b/code/serveur/php/ressources/pticlic.js
@@ -1,29 +1,21 @@
-// ==== URL persistante
-var nullFunction = function(){};
-var futureHashChange = null;
-var runstate = {
- screen: 'none',
-};
-var state = decodeHash("");
-var oldstate = decodeHash("");
-
-$.screen = function (name) {
- return $(document.getElementById(name)).filter('.screen');
-}
-
-function hashchange() {
- oldstate = state;
- state = decodeHash(location.hash);
- $.screen(state.screen).trigger(state.screen != runstate.screen ? "goto" : "update");
-}
-
+// ==== Code métier général
function init(fn) {
$(window).queue('init', function(next) {fn(); next();});
}
-// ==== Code métier général
+$.ajaj = function(url, data, callback) {
+ var user = '' + UI().getPreference("user");
+ var passwd = '' + UI().getPreference("passwd");
+ user = runstate.user || user;
+ passwd = runstate.passwd || passwd;
+ if (user && passwd) {
+ if (!data.user) data.user = user;
+ if (!data.passwd) data.passwd = passwd;
+ }
+ return $.getJSON(url, data, callback);
+};
+
$(function() {
- loadPrefs();
var lastWinSize = $(window).wh();
$(window).dequeue('init');
$(window).resize($.debounce(function resizeJSS() {
@@ -37,9 +29,67 @@ $(function() {
runstate.loaded = true;
});
+// ==== URL persistantes et passage d'un écran à l'autre
+var runstate = { screen: 'none' };
+var state = decodeHash("");
+var oldstate = decodeHash("");
+
+init(function() {
+ $('.screen').live('goto', function() {
+ var screen = this.id;
+ if (screen == '') return;
+ // Afficher "Chargement…"
+ /* location.hash = "#" + screen; */
+ $.screen(runstate.screen).trigger('leave').hide();
+ runstate.screen = screen;
+ UI().setScreen(screen);
+ $(this).trigger('pre-enter');
+ });
+
+ $('.screen').live('pre-enter', function() {
+ $(this).trigger('enter');
+ });
+
+ $('.screen').live('enter', function() {
+ $(this).show();
+ $(this).trigger('update');
+ });
+
+ $('.screen').live('update', function() {
+ jss();
+ });
+
+ $('.screen').live('leave', function() {
+ $(this).hide();
+ });
+});
+
+$.screen = function (name) {
+ return $(document.getElementById(name)).filter('.screen');
+}
+
+function hashchange() {
+ oldstate = state;
+ state = decodeHash(location.hash);
+ $.screen(state.screen).trigger(state.screen != runstate.screen ? "goto" : "update");
+}
+
+// ==== Interface Android
+function UI () {
+ if (typeof(PtiClicAndroid) != "undefined") {
+ return PtiClicAndroid;
+ } else {
+ return {
+ isAndroid: function() { return false; },
+ setPreference: function() {},
+ getPreference: function() {return "";},
+ setScreen: function() {}
+ };
+ }
+}
+
// ==== Nouveau jss
function jss() {
- try {
if (jss.running) return;
jss.running = true;
$('body').removeClass().addClass(runstate.prefs.theme);
@@ -77,57 +127,26 @@ function jss() {
$('#game.screen').trigger('update');
}
jss.running = false;
- } catch(e) {alert("Error jss");alert(e);}
}
-// ==== Passage d'un écran à l'autre
-
-init(function() {
- $('.screen').live('goto', function() {
- var screen = this.id;
- if (screen == '') return;
- // Afficher "Chargement…"
- /* location.hash = "#" + screen; */
- $.screen(runstate.screen).trigger('leave').hide();
- runstate.screen = screen;
- $(this).trigger('pre-enter');
- });
-
- $('.screen').live('pre-enter', function() {
- $(this).trigger('enter');
- });
-
- $('.screen').live('enter', function() {
- $(this).show();
- $(this).trigger('update');
- });
-
- $('.screen').live('update', function() {
- jss();
- });
-
- $('.screen').live('leave', function() {
- $(this).hide();
- });
-});
-
// ==== Bulle pour les messages
init(function() {
$('#message').hide();
});
function message(title, msg) {
- try {
- $('#message')
- .qCss('opacity',0).qShow()
- .queue(function(next){ $('#message .text').text(msg); jss(); next(); })
- .fadeTo(700, 0.9).delay(5000).fadeOut(700);
- } catch(e) {alert("Error UI().info");alert(e);}
+ $('#message')
+ .qCss('opacity',0).qShow()
+ .queue(function(next){ $('#message .text').text(msg || 'Une erreur est survenue, veuillez nous en excuser.'); jss(); next(); })
+ .fadeTo(700, 0.9).delay(5000).fadeOut(700);
}
// ==== Écran splash
init(function() {
- $('#splash.screen').click(function(){ $('#frontpage').trigger('goto'); });
+ $('#splash.screen').click(function(){ $('#frontpage').trigger('goto'); return false; });
+ window.setTimeout(function() {
+ if (runstate.screen == 'splash') $('#frontpage').trigger('goto');
+ }, 5000);
$('#splash.screen').bind('goto', function(e){
if (runstate.loaded) {
$('#frontpage').trigger('goto');
@@ -139,20 +158,17 @@ init(function() {
// ==== Écran d'accueil
init(function() {
- $('.goFrontpage').click(function() { location.hash = "#frontpage"; });
$.screen('frontpage').bind('enter', function() { window.document.title = "PtiClic pre-alpha 0.2"; });
if (UI().isAndroid()) $('#back2site').hide();
});
// ==== Écran connexion
-runstate.pendingGetPrefs = function() {
- UI().log('Should execute pendingGetPrefs');
-};
-
init(function() {
$('#connection.screen form').submit(function() {
runstate.user = $('#user').val();
runstate.passwd = $('#passwd').val();
+ UI().setPreference('user', runstate.user);
+ UI().setPreference('passwd', runstate.passwd);
if (runstate.pendingSetPrefs) {
runstate.pendingSetPrefs();
} else {
@@ -162,6 +178,8 @@ init(function() {
$('#game').trigger('goto');
} else if (state.screen == 'score') {
$('#score').trigger('goto');
+ } else if (location.hash == "#frontpage") {
+ $.screen('frontpage').trigger('goto');
} else {
location.hash = "#frontpage";
}
@@ -175,16 +193,6 @@ init(function() {
});
// ==== Écran game
-$.ajaj = function(url, data, callback) {
- var user = runstate.user; /* '' + UI().getPreference("user"); */
- var passwd = runstate.passwd; /* '' + UI().getPreference("passwd"); */
- if (user && passwd) {
- if (!data.user) data.user = user;
- if (!data.passwd) data.passwd = passwd;
- }
- return $.getJSON(url, data, callback);
-};
-
runstate.gameCache = new Cache(function getGame(k, dfd) {
$.ajaj("getGame.php?callback=?", {pgid:k}, function(data) {
if (data.isError) {
@@ -317,7 +325,6 @@ init(function() {
var score = $.screen('score');
score.bind('pre-enter', function() {
runstate.scoreCache.get(state.pgid, state.answers).done(function(data) {
- console.log(data);
runstate.score = data;
score.trigger('enter');
});
@@ -330,32 +337,94 @@ init(function() {
.text(s.scoreTotal)
.goodBad(s.minScore*s.scores.length, s.maxScore*s.scores.length, {r:255,g:0,b:0}, {r:0,g:192,b:0});
$.each(s.scores, function(i,e) {
- try {
- $("#templates .scoreLine")
- .clone()
- .find(".word")
- .text(e.name)
- .end()
- .find(".score")
- .text(e.score)
- .goodBad(s.minScore, s.maxScore, {r:255,g:0,b:0}, {r:0,g:192,b:0})
- .end()
- .appendTo("#score .scores");
- } catch(e) {alert("Error anonymous 1 in score.ui");alert(e);}
+ $("#templates .scoreLine")
+ .clone()
+ .find(".word").text(e.name).end()
+ .find(".score").text(e.score).goodBad(s.minScore, s.maxScore, {r:255,g:0,b:0}, {r:0,g:192,b:0}).end()
+ .appendTo("#score .scores");
});
});
});
// ==== Écran Préférences
function loadPrefs(prefs) {
- runstate.prefs = (prefs && prefs.theme) ? prefs : {
- theme: "green"
- };
- if (runstate.loaded) jss();
+ var previousTheme = runstate.prefs ? runstate.prefs.theme : 'green';
+ runstate.prefs = (prefs && prefs.theme) ? prefs : { theme: "green" };
+ runstate.serverPrefs = $.extend({}, runstate.prefs);
+ if (runstate.loaded && previousTheme != runstate.prefs.theme) jss();
+}
+loadPrefs(); // initialize runstate.prefs.
+
+function setPrefs(prefs, callback) {
+ $.ajaj("server.php?callback=?", {
+ action: 8,
+ key: 'theme',
+ value: prefs.theme
+ }, function(data) {
+ if ((data.error == 10 || data.error == 3) && (state.screen == 'frontpage' || state.screen == 'prefs')) {
+ $.screen('connection').trigger('goto');
+ } else {
+ if (data.theme) {
+ runstate.pendingSetPrefs = false;
+ loadPrefs(data);
+ message("Préférences", "Les préférences ont été enregistrées.");
+ } else {
+ message("Erreur", data.msg);
+ message("Préférences", "Les préférences n'ont pas pu être enregistrées.");
+ }
+ }
+ });
}
+runstate.pendingGetPrefs = function() {
+ $.ajaj("server.php?callback=?", { action: 7 }, function(data) {
+ if (data.theme) { message("Succès", "Vous êtes connecté.", data.msg); loadPrefs(data); }
+ if (data.isError) message("Erreur", data.msg);
+ });
+};
+
init(function() {
+ $("#prefs").bind('enter', function() {
+ $("#prefs-form input:radio[name=theme]").attr('checked', function(i,val) {
+ return $(this).val() == runstate.prefs.theme;
+ });
+ });
+
+ var readPrefs = function() {
+ var newtheme = $("#prefs form input:radio[name=theme]:checked").eq(0).val();
+ if (runstate.prefs.theme != newtheme) {
+ runstate.prefs.theme = newtheme;
+ jss();
+ }
+ };
+
+ $("#prefs form").submit(function() {
+ readPrefs();
+ location.href = "#frontpage"
+ var p = $.extend({}, runstate.prefs);
+ runstate.pendingSetPrefs = function() { setPrefs(p); }
+ runstate.pendingSetPrefs();
+ return false;
+ });
+ $("#prefs form").bind('reset', function() {
+ runstate.prefs = runstate.serverPrefs;
+ location.hash = "#frontpage";
+ });
+ $("#prefs form input:radio[name=theme]").bind('change click', readPrefs);
});
+
+
+
+
+
+
+
+
+
+
+
+
+
/*prefs.enter = function() {
try {
$("#prefs-form").unbind('submit', prefs.apply).submit(prefs.apply);
@@ -408,14 +477,6 @@ prefs.loadPrefs = function(data) {
if (runstate.loaded) jss();
} catch(e) {alert("Error anonymous in prefs.loadPrefs");alert(e);}
};
-*/
-
-
-
-
-
-
-
game = {};
game.leave = function () {
@@ -532,6 +593,7 @@ State.prototype.validate = function () {
} catch(e) {alert("Error State.prototype.validate");alert(e);}
};*/
+/*
function _hashchange() {
try {
if (futureHashChange !== location.hash) {
@@ -541,35 +603,6 @@ function _hashchange() {
} catch(e) {alert("Error hashchange");alert(e);}
}
-// ==== Interface Android
-function UI () {
- try {
- if (typeof(PtiClicAndroid) != "undefined") {
- return PtiClicAndroid;
- } else {
- return {
- isAndroid: function() { return false; },
- setPreference: function() {},
- getPreference: function() {return "";},
- show: function(title, text) {},
- dismiss: function() {},
- exit: function() {},
- log: function(msg) {
- try {
- window.console && console.log(msg);
- } catch(e) {alert("Error UI().log");alert(e);}
- },
- info: function(title, msg) {
- try {
- alert(msg);
- } catch(e) {alert("Error UI().info");alert(e);}
- },
- setScreen: function() {}
- };
- }
- } catch(e) {alert("Error UI");alert(e);}
-}
-
// ==== Asynchronous Javascript And Json.
ajaj = {};
ajaj.request = function(url, data, okFunction, smallErrorFunction, bigErrorFunction) {
@@ -924,3 +957,5 @@ prefs.loadPrefs = function(data) {
if (runstate.loaded) jss();
} catch(e) {alert("Error anonymous in prefs.loadPrefs");alert(e);}
};
+
+*/
\ No newline at end of file