diff --git a/ts/dictionnaire.ts b/ts/dictionnaire.ts index 2c3b2dc..0f7b8c4 100644 --- a/ts/dictionnaire.ts +++ b/ts/dictionnaire.ts @@ -3,8 +3,8 @@ import MotsATrouver from "./mots/motsATrouver"; export default class Dictionnaire { public constructor() {} - public getMot(): string { - let aujourdhui = new Date().getTime(); + public getMot(datePartie: Date): string { + let aujourdhui = datePartie.getTime(); let origine = new Date(2022, 0, 8).getTime(); let numeroGrille = Math.floor((aujourdhui - origine) / (24 * 3600 * 1000)); diff --git a/ts/finDePartiePanel.ts b/ts/finDePartiePanel.ts index 596e4ea..685da65 100644 --- a/ts/finDePartiePanel.ts +++ b/ts/finDePartiePanel.ts @@ -9,10 +9,11 @@ export default class FinDePartiePanel { private readonly _defaitePanelMot: HTMLElement; private readonly _resume: HTMLPreElement; private readonly _resumeBouton: HTMLElement; + private readonly _datePartie: Date; private _resumeTexte: string = ""; - public constructor() { + public constructor(datePartie: Date) { this._finDePartiePanel = document.getElementById("fin-de-partie-panel") as HTMLElement; this._victoirePanel = document.getElementById("victoire-panel") as HTMLElement; this._defaitePanel = document.getElementById("defaite-panel") as HTMLElement; @@ -20,6 +21,8 @@ export default class FinDePartiePanel { this._resume = document.getElementById("fin-de-partie-panel-resume") as HTMLPreElement; this._resumeBouton = document.getElementById("fin-de-partie-panel-resume-bouton") as HTMLElement; + this._datePartie = datePartie; + this._resumeBouton.addEventListener("click", (event) => { event.stopPropagation(); if (!navigator.clipboard) { @@ -32,7 +35,7 @@ export default class FinDePartiePanel { }); } - public genererResume(aBonneReponse: boolean, resultats: Array>): void { + public genererResume(estBonneReponse: boolean, resultats: Array>): void { let resultatsEmojis = resultats.map((mot) => mot .map((resultat) => resultat.statut) @@ -47,12 +50,12 @@ export default class FinDePartiePanel { } }, "") ); - let aujourdhui = new Date().getTime(); + let dateGrille = this._datePartie.getTime(); let origine = new Date(2022, 0, 8).getTime(); - let numeroGrille = Math.floor((aujourdhui - origine) / (24 * 3600 * 1000)) + 1; + let numeroGrille = Math.floor((dateGrille - origine) / (24 * 3600 * 1000)) + 1; - this._resumeTexte = "SUTOM #" + numeroGrille + " " + (aBonneReponse ? resultats.length : "-") + "/6\n\n" + resultatsEmojis.join("\n"); + this._resumeTexte = "SUTOM #" + numeroGrille + " " + (estBonneReponse ? resultats.length : "-") + "/6\n\n" + resultatsEmojis.join("\n"); this._resume.innerText = this._resumeTexte; } diff --git a/ts/gestionnaire.ts b/ts/gestionnaire.ts index 994581f..32a0fb2 100644 --- a/ts/gestionnaire.ts +++ b/ts/gestionnaire.ts @@ -8,6 +8,7 @@ import NotificationMessage from "./notificationMessage"; import SauvegardeStats from "./sauvegardeStats"; import Sauvegardeur from "./sauvegardeur"; import Configuration from "./configuration"; +import PartieEnCours from "./partieEnCours"; export default class Gestionnaire { private readonly _dictionnaire: Dictionnaire; @@ -20,33 +21,47 @@ export default class Gestionnaire { private _motATrouver: string; private _compositionMotATrouver: { [lettre: string]: number }; private _maxNbPropositions: number = 6; - private _datePartieEnCours: Date | undefined; + private _datePartieEnCours: Date; private _stats: SauvegardeStats = { partiesJouees: 0, partiesGagnees: 0 }; private _config: Configuration = Configuration.Default; public constructor() { this._config = Sauvegardeur.chargerConfig() ?? this._config; + + let partieEnCours = this.chargerPartieEnCours(); + + if (partieEnCours.datePartie) { + this._datePartieEnCours = partieEnCours.datePartie; + } else { + this._datePartieEnCours = new Date(); + } + this._dictionnaire = new Dictionnaire(); - this._motATrouver = this.choisirMot(); + this._motATrouver = this.choisirMot(this._datePartieEnCours); this._grille = new Grille(this._motATrouver.length, this._maxNbPropositions, this._motATrouver[0], this._config); this._input = new Input(this, this._motATrouver.length, this._motATrouver[0]); - this._victoirePanel = new FinDePartiePanel(); this._propositions = new Array(); this._resultats = new Array>(); this._compositionMotATrouver = this.decompose(this._motATrouver); + this._victoirePanel = new FinDePartiePanel(this._datePartieEnCours); - this.chargerSauvegarde(); + this.chargerPropositions(partieEnCours.propositions); } - private chargerSauvegarde(): void { - let sauvegardePartieEnCours = Sauvegardeur.chargerSauvegardePartieEnCours(); - if (sauvegardePartieEnCours) { - this._datePartieEnCours = sauvegardePartieEnCours.datePartie; - for (let mot of sauvegardePartieEnCours.propositions) { - this.verifierMot(mot, true); - } - } + private chargerPartieEnCours(): PartieEnCours { this._stats = Sauvegardeur.chargerSauvegardeStats() ?? { partiesJouees: 0, partiesGagnees: 0 }; + + let sauvegardePartieEnCours = Sauvegardeur.chargerSauvegardePartieEnCours(); + if (sauvegardePartieEnCours) return sauvegardePartieEnCours; + + return new PartieEnCours(); + } + + private chargerPropositions(propositions: Array | undefined): void { + if (!propositions || propositions.length === 0) return; + for (let mot of propositions) { + this.verifierMot(mot, true); + } } private enregistrerPartieDansStats(): void { @@ -58,13 +73,11 @@ export default class Gestionnaire { } private sauvegarderPartieEnCours(): void { - let datePartieEnCours = this._datePartieEnCours ?? new Date(); - - Sauvegardeur.sauvegarderPartieEnCours(this._propositions, datePartieEnCours); + Sauvegardeur.sauvegarderPartieEnCours(this._propositions, this._datePartieEnCours); } - private choisirMot(): string { - return this._dictionnaire.nettoyerMot(this._dictionnaire.getMot()); + private choisirMot(datePartie: Date): string { + return this._dictionnaire.nettoyerMot(this._dictionnaire.getMot(datePartie)); } private decompose(mot: string): { [lettre: string]: number } { diff --git a/ts/partieEnCours.ts b/ts/partieEnCours.ts new file mode 100644 index 0000000..9a3b6be --- /dev/null +++ b/ts/partieEnCours.ts @@ -0,0 +1,4 @@ +export default class PartieEnCours { + public propositions: Array | undefined; + public datePartie: Date | undefined; +} diff --git a/ts/sauvegardeur.ts b/ts/sauvegardeur.ts index 9bee45f..7f1f23a 100644 --- a/ts/sauvegardeur.ts +++ b/ts/sauvegardeur.ts @@ -1,4 +1,5 @@ import Configuration from "./configuration"; +import PartieEnCours from "./partieEnCours"; import SauvegardePartie from "./sauvegardePartie"; import SauvegardeStats from "./sauvegardeStats"; @@ -27,7 +28,7 @@ export default class Sauvegardeur { localStorage.setItem(this._clePartieEnCours, JSON.stringify(partieEnCours)); } - public static chargerSauvegardePartieEnCours(): { propositions: Array; datePartie: Date } | undefined { + public static chargerSauvegardePartieEnCours(): PartieEnCours | undefined { let dataPartieEnCours = localStorage.getItem(this._clePartieEnCours); if (!dataPartieEnCours) return;