From f7a0ff1be7cc91fbde2b020e9d928efece724a02 Mon Sep 17 00:00:00 2001
From: Tissevert
Date: Sun, 2 Feb 2020 19:16:27 +0100
Subject: [PATCH] Implement account saving / loading
---
index.html | 10 ++++-
js/GUI/Screen/Login.js | 78 +++++++++++++++++++++++++++++++--------
js/GUI/Screen/Settings.js | 8 +++-
js/Save.js | 2 +
js/Translations.js | 12 +++++-
5 files changed, 90 insertions(+), 20 deletions(-)
diff --git a/index.html b/index.html
index 5a53f27..d841c16 100644
--- a/index.html
+++ b/index.html
@@ -17,6 +17,14 @@
+
diff --git a/js/GUI/Screen/Login.js b/js/GUI/Screen/Login.js
index f49751e..f5763e6 100644
--- a/js/GUI/Screen/Login.js
+++ b/js/GUI/Screen/Login.js
@@ -1,12 +1,14 @@
import I18n;
import GUI.ConnectedForm;
-import {register, select} from GUI.Screen;
+import {dialog, register, select} from GUI.Screen;
import Messaging;
import Session;
import Save;
var login;
-var form;
+var loginForm;
+var accountLoader;
+var accountLoaderForm;
return {
init: init
@@ -14,25 +16,33 @@ return {
function init() {
register('login');
- login = GUI.ConnectedForm.get('loginForm');
- form = login.root;
- initDOM();
+ initLogin();
+ initLoader();
initMessageHandlers();
- var name = Save.get('player.name');
- if(name != undefined && name.length > 0) {
- form.you.value = name;
- login.enable();
- }
+ restoreName();
}
-function initDOM() {
- form.getElementsByTagName('label')[0].textContent = I18n.get('pickName');
- form.join.value = I18n.get('join');
- form.addEventListener('submit', function(e) {
+function initLogin() {
+ login = GUI.ConnectedForm.get('loginForm');
+ loginForm = login.root;
+ loginForm.getElementsByTagName('label')[0].textContent = I18n.get('pickName');
+ loginForm.join.value = I18n.get('join');
+ loginForm.addEventListener('submit', function(e) {
e.preventDefault();
- Session.start(form.you.value);
+ Session.start(loginForm.you.value);
+ });
+ loginForm.you.addEventListener("input", validate);
+}
+
+function initLoader() {
+ accountLoader = GUI.ConnectedForm.get('accountLoader');
+ accountLoaderForm = accountLoader.root;
+ accountLoaderForm.getElementsByTagName('label')[0].textContent = I18n.get('pickFile');
+ accountLoaderForm.doLoad.value = I18n.get('doLoad');
+ accountLoaderForm.addEventListener('submit', loadAccountConfirm);
+ accountLoaderForm.pickFile.addEventListener("input", function() {
+ accountLoader.enable();
});
- form.you.addEventListener("input", validate);
}
function initMessageHandlers() {
@@ -43,6 +53,42 @@ function initMessageHandlers() {
});
}
+function loadAccountConfirm(e) {
+ e.preventDefault();
+ if(Save.get('player') != undefined) {
+ dialog({
+ text: I18n.get('warnExistingAccount'),
+ answers: [
+ {label: 'confirmReplace', action: loadAccount},
+ {label: 'cancel', action: function() {}}
+ ]
+ });
+ } else {
+ loadAccount();
+ }
+}
+
+function loadAccount() {
+ var fileReader = new FileReader();
+ fileReader.addEventListener('load', function() {
+ Save.set(null, JSON.parse(fileReader.result));
+ var name = restoreName();
+ if(name != undefined) {
+ Session.start(name);
+ }
+ });
+ fileReader.readAsText(accountLoaderForm.pickFile.files[0]);
+}
+
+function restoreName() {
+ var name = Save.get('player.name');
+ if(name != undefined && name.length > 0) {
+ loginForm.you.value = name;
+ login.enable();
+ return name;
+ }
+}
+
function validate(e) {
login.enable(e.target.value != "");
}
diff --git a/js/GUI/Screen/Settings.js b/js/GUI/Screen/Settings.js
index 8aee6ac..342fba4 100644
--- a/js/GUI/Screen/Settings.js
+++ b/js/GUI/Screen/Settings.js
@@ -1,5 +1,8 @@
import I18n;
import register from GUI.Screen;
+import Save;
+
+var button;
return {
init: init
@@ -9,7 +12,7 @@ function init() {
register('settings');
var exportRoot = document.getElementById('export');
var label = exportRoot.getElementsByTagName('span')[0];
- var button = exportRoot.getElementsByTagName('button')[0];
+ button = exportRoot.getElementsByTagName('a')[0];
label.textContent = I18n.get('exportLabel');
button.textContent = I18n.get('doExport');
@@ -17,5 +20,6 @@ function init() {
}
function doExport() {
- console.log('export');
+ var data = encodeURIComponent(JSON.stringify(Save.get()));
+ button.href = 'data:application/json,' + data;
}
diff --git a/js/Save.js b/js/Save.js
index 49519d5..1149741 100644
--- a/js/Save.js
+++ b/js/Save.js
@@ -27,6 +27,8 @@ function get(key) {
} else {
return null;
}
+ } else {
+ return save;
}
}
diff --git a/js/Translations.js b/js/Translations.js
index 6663e9d..533b3ca 100644
--- a/js/Translations.js
+++ b/js/Translations.js
@@ -15,8 +15,11 @@ return {
accept: "Let's go !",
alone: "No one to play with yet ! Wait a little",
backToMain: "Back to main menu",
+ cancel: "Cancel",
+ confirmReplace: "Yes, do replace my existing account",
decline: "No thanks",
doExport: "Save",
+ doLoad: "Load",
endRound: "End the round",
endGame: "Return to main menu",
exportLabel: "Save your account data to load it somewhere else",
@@ -38,6 +41,7 @@ return {
var whose = yourTurn ? 'your' : name + "'s";
return 'Game vs. ' + name + ' (' + whose + ' turn)';
},
+ pickFile: "Load an existing account",
pickName: "Pick a name you like",
playing: function(name) {
return name + " is playing";
@@ -47,7 +51,7 @@ return {
if(yourTurn) {
return name + proposed;
} else {
- return "You" + proposed + " to " + name;
+ return "You" + proposed + " to " + name;
}
},
questionAccept: "Do you want to start the game ?",
@@ -58,6 +62,7 @@ return {
theyScored: function(name) {
return name + " scored";
},
+ warnExistingAccount: "Your current account will be erased and lost forever",
won: "You won !",
yourTurn: "Your turn",
youScored: "You scored ! Do you want to get your points and end the round or KoiKoi ?"
@@ -78,8 +83,11 @@ return {
accept: "C'est parti !",
alone: "Personne pour jouer pour l'instant ! Attendez un peu",
backToMain: "Retourner au menu principal",
+ cancel: "Annuler",
+ confirmReplace: "Oui, remplacer mon compte",
decline: "Non merci",
doExport: "Télécharger",
+ doLoad: "Charger",
endRound: "Finir la manche",
endGame: "Retourner au menu principal",
exportLabel: "Télécharger les données de votre compte pour les charger ailleurs",
@@ -101,6 +109,7 @@ return {
var whose = yourTurn ? 'vous' : name;
return 'Partie en cours contre ' + name + ' (à ' + whose + ')';
},
+ pickFile: "Charger un compte existant",
pickName: "Choisissez votre nom",
playing: function(name) {
return "C'est à " + name;
@@ -121,6 +130,7 @@ return {
theyScored: function(name) {
return name + " a marqué";
},
+ warnExistingAccount: "Le compte existant sera remplacé et perdu à tout jamais",
won: "Vous avez gagné !",
yourTurn: "À vous",
youScored: "Vous avez marqué ! Voulez-vous empocher vos gains et terminer la manche ou faire KoiKoi ?"