From 7db79125f078fcaf0e4e73b19161400002aaa857 Mon Sep 17 00:00:00 2001 From: JonathanMM Date: Mon, 21 Feb 2022 22:09:55 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20possibilit=C3=A9=20de=20charg?= =?UTF-8?q?er=20une=20partie=20dans=20l'URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ts/dictionnaire.ts | 10 ++++++++-- ts/gestionnaire.ts | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/ts/dictionnaire.ts b/ts/dictionnaire.ts index 8a76d8a..379877d 100644 --- a/ts/dictionnaire.ts +++ b/ts/dictionnaire.ts @@ -1,4 +1,3 @@ -import InstanceConfiguration from "./instanceConfiguration"; import ListeMotsProposables from "./mots/listeMotsProposables"; export default class Dictionnaire { public constructor() {} @@ -6,7 +5,14 @@ export default class Dictionnaire { public static async getMot(idPartie: string, datePartie: Date): Promise { return await this.getNomFichier(idPartie, datePartie) .then((nom) => fetch("mots/" + nom + ".txt")) - .then((resultat) => resultat.text()); + .then( + (resultat) => + new Promise((resolve, reject) => { + if (!resultat.ok) return reject("Mot non trouvé"); + + return resolve(resultat.text()); + }) + ); } private static async getNomFichier(idPartie: string, datePartie: Date): Promise { diff --git a/ts/gestionnaire.ts b/ts/gestionnaire.ts index 54966d7..8968fde 100644 --- a/ts/gestionnaire.ts +++ b/ts/gestionnaire.ts @@ -42,14 +42,18 @@ export default class Gestionnaire { let partieEnCours = this.chargerPartieEnCours(); + this._idPartieEnCours = this.getIdPartie(partieEnCours); + + if (this._idPartieEnCours !== partieEnCours.idPartie) { + partieEnCours = new PartieEnCours(); + } + if (partieEnCours.datePartie) { this._datePartieEnCours = partieEnCours.datePartie; } else { this._datePartieEnCours = new Date(); } - this._idPartieEnCours = this.getIdPartie(partieEnCours); - if (partieEnCours.dateFinPartie) { this._dateFinPartie = partieEnCours.dateFinPartie; } @@ -63,19 +67,35 @@ export default class Gestionnaire { this._finDePartiePanel = new FinDePartiePanel(this._datePartieEnCours, this._panelManager); this._configurationPanel = new ConfigurationPanel(this._panelManager, this._audioPanel, this._themeManager); - this.choisirMot(this._idPartieEnCours, this._datePartieEnCours).then((mot) => { - this._motATrouver = mot; - this._input = new Input(this, this._config, this._motATrouver.length, this._motATrouver[0]); - this._grille = new Grille(this._motATrouver.length, this._maxNbPropositions, this._motATrouver[0], this._audioPanel); - this._configurationPanel.setInput(this._input); - this._compositionMotATrouver = this.decompose(this._motATrouver); - this.chargerPropositions(partieEnCours.propositions); - }); + this.choisirMot(this._idPartieEnCours, this._datePartieEnCours) + .then((mot) => { + this._motATrouver = mot; + this._input = new Input(this, this._config, this._motATrouver.length, this._motATrouver[0]); + this._grille = new Grille(this._motATrouver.length, this._maxNbPropositions, this._motATrouver[0], this._audioPanel); + this._configurationPanel.setInput(this._input); + this._compositionMotATrouver = this.decompose(this._motATrouver); + this.chargerPropositions(partieEnCours.propositions); + }) + .catch((raison) => NotificationMessage.ajouterNotification("Aucun mot n'a été trouvé pour aujourd'hui")); this.afficherReglesSiNecessaire(); } private getIdPartie(partieEnCours: PartieEnCours) { + if (window.location.hash !== "" && window.location.hash !== "#") { + let hashPart = atob(window.location.hash.substring(1)).split("/"); + for (let infoPos in hashPart) { + let info = hashPart[infoPos]; + if (!info.includes("=")) continue; + let infoPart = info.split("="); + let infoKey = infoPart[0]; + + if (infoKey !== "p") continue; + + return infoPart[1]; + } + } + if (partieEnCours.idPartie !== undefined) return partieEnCours.idPartie; return InstanceConfiguration.idPartieParDefaut; @@ -131,7 +151,7 @@ export default class Gestionnaire { } private async choisirMot(idPartie: string, datePartie: Date): Promise { - return Dictionnaire.nettoyerMot(await Dictionnaire.getMot(idPartie, datePartie)); + return Dictionnaire.getMot(idPartie, datePartie).then((mot) => Dictionnaire.nettoyerMot(mot)); } private decompose(mot: string): { [lettre: string]: number } {