Fix #25 : Mauvais numéro de grille dans le résumé en cas de changement de jour

This commit is contained in:
JonathanMM 2022-01-23 10:43:57 +01:00
parent d976b6cabd
commit 72d451568e
5 changed files with 46 additions and 25 deletions

View file

@ -3,8 +3,8 @@ import MotsATrouver from "./mots/motsATrouver";
export default class Dictionnaire { export default class Dictionnaire {
public constructor() {} public constructor() {}
public getMot(): string { public getMot(datePartie: Date): string {
let aujourdhui = new Date().getTime(); let aujourdhui = datePartie.getTime();
let origine = new Date(2022, 0, 8).getTime(); let origine = new Date(2022, 0, 8).getTime();
let numeroGrille = Math.floor((aujourdhui - origine) / (24 * 3600 * 1000)); let numeroGrille = Math.floor((aujourdhui - origine) / (24 * 3600 * 1000));

View file

@ -9,10 +9,11 @@ export default class FinDePartiePanel {
private readonly _defaitePanelMot: HTMLElement; private readonly _defaitePanelMot: HTMLElement;
private readonly _resume: HTMLPreElement; private readonly _resume: HTMLPreElement;
private readonly _resumeBouton: HTMLElement; private readonly _resumeBouton: HTMLElement;
private readonly _datePartie: Date;
private _resumeTexte: string = ""; private _resumeTexte: string = "";
public constructor() { public constructor(datePartie: Date) {
this._finDePartiePanel = document.getElementById("fin-de-partie-panel") as HTMLElement; this._finDePartiePanel = document.getElementById("fin-de-partie-panel") as HTMLElement;
this._victoirePanel = document.getElementById("victoire-panel") as HTMLElement; this._victoirePanel = document.getElementById("victoire-panel") as HTMLElement;
this._defaitePanel = document.getElementById("defaite-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._resume = document.getElementById("fin-de-partie-panel-resume") as HTMLPreElement;
this._resumeBouton = document.getElementById("fin-de-partie-panel-resume-bouton") as HTMLElement; this._resumeBouton = document.getElementById("fin-de-partie-panel-resume-bouton") as HTMLElement;
this._datePartie = datePartie;
this._resumeBouton.addEventListener("click", (event) => { this._resumeBouton.addEventListener("click", (event) => {
event.stopPropagation(); event.stopPropagation();
if (!navigator.clipboard) { if (!navigator.clipboard) {
@ -32,7 +35,7 @@ export default class FinDePartiePanel {
}); });
} }
public genererResume(aBonneReponse: boolean, resultats: Array<Array<LettreResultat>>): void { public genererResume(estBonneReponse: boolean, resultats: Array<Array<LettreResultat>>): void {
let resultatsEmojis = resultats.map((mot) => let resultatsEmojis = resultats.map((mot) =>
mot mot
.map((resultat) => resultat.statut) .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 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; this._resume.innerText = this._resumeTexte;
} }

View file

@ -8,6 +8,7 @@ import NotificationMessage from "./notificationMessage";
import SauvegardeStats from "./sauvegardeStats"; import SauvegardeStats from "./sauvegardeStats";
import Sauvegardeur from "./sauvegardeur"; import Sauvegardeur from "./sauvegardeur";
import Configuration from "./configuration"; import Configuration from "./configuration";
import PartieEnCours from "./partieEnCours";
export default class Gestionnaire { export default class Gestionnaire {
private readonly _dictionnaire: Dictionnaire; private readonly _dictionnaire: Dictionnaire;
@ -20,33 +21,47 @@ export default class Gestionnaire {
private _motATrouver: string; private _motATrouver: string;
private _compositionMotATrouver: { [lettre: string]: number }; private _compositionMotATrouver: { [lettre: string]: number };
private _maxNbPropositions: number = 6; private _maxNbPropositions: number = 6;
private _datePartieEnCours: Date | undefined; private _datePartieEnCours: Date;
private _stats: SauvegardeStats = { partiesJouees: 0, partiesGagnees: 0 }; private _stats: SauvegardeStats = { partiesJouees: 0, partiesGagnees: 0 };
private _config: Configuration = Configuration.Default; private _config: Configuration = Configuration.Default;
public constructor() { public constructor() {
this._config = Sauvegardeur.chargerConfig() ?? this._config; 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._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._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._input = new Input(this, this._motATrouver.length, this._motATrouver[0]);
this._victoirePanel = new FinDePartiePanel();
this._propositions = new Array<string>(); this._propositions = new Array<string>();
this._resultats = new Array<Array<LettreResultat>>(); this._resultats = new Array<Array<LettreResultat>>();
this._compositionMotATrouver = this.decompose(this._motATrouver); this._compositionMotATrouver = this.decompose(this._motATrouver);
this._victoirePanel = new FinDePartiePanel(this._datePartieEnCours);
this.chargerSauvegarde(); this.chargerPropositions(partieEnCours.propositions);
} }
private chargerSauvegarde(): void { private chargerPartieEnCours(): PartieEnCours {
let sauvegardePartieEnCours = Sauvegardeur.chargerSauvegardePartieEnCours();
if (sauvegardePartieEnCours) {
this._datePartieEnCours = sauvegardePartieEnCours.datePartie;
for (let mot of sauvegardePartieEnCours.propositions) {
this.verifierMot(mot, true);
}
}
this._stats = Sauvegardeur.chargerSauvegardeStats() ?? { partiesJouees: 0, partiesGagnees: 0 }; this._stats = Sauvegardeur.chargerSauvegardeStats() ?? { partiesJouees: 0, partiesGagnees: 0 };
let sauvegardePartieEnCours = Sauvegardeur.chargerSauvegardePartieEnCours();
if (sauvegardePartieEnCours) return sauvegardePartieEnCours;
return new PartieEnCours();
}
private chargerPropositions(propositions: Array<string> | undefined): void {
if (!propositions || propositions.length === 0) return;
for (let mot of propositions) {
this.verifierMot(mot, true);
}
} }
private enregistrerPartieDansStats(): void { private enregistrerPartieDansStats(): void {
@ -58,13 +73,11 @@ export default class Gestionnaire {
} }
private sauvegarderPartieEnCours(): void { private sauvegarderPartieEnCours(): void {
let datePartieEnCours = this._datePartieEnCours ?? new Date(); Sauvegardeur.sauvegarderPartieEnCours(this._propositions, this._datePartieEnCours);
Sauvegardeur.sauvegarderPartieEnCours(this._propositions, datePartieEnCours);
} }
private choisirMot(): string { private choisirMot(datePartie: Date): string {
return this._dictionnaire.nettoyerMot(this._dictionnaire.getMot()); return this._dictionnaire.nettoyerMot(this._dictionnaire.getMot(datePartie));
} }
private decompose(mot: string): { [lettre: string]: number } { private decompose(mot: string): { [lettre: string]: number } {

4
ts/partieEnCours.ts Normal file
View file

@ -0,0 +1,4 @@
export default class PartieEnCours {
public propositions: Array<string> | undefined;
public datePartie: Date | undefined;
}

View file

@ -1,4 +1,5 @@
import Configuration from "./configuration"; import Configuration from "./configuration";
import PartieEnCours from "./partieEnCours";
import SauvegardePartie from "./sauvegardePartie"; import SauvegardePartie from "./sauvegardePartie";
import SauvegardeStats from "./sauvegardeStats"; import SauvegardeStats from "./sauvegardeStats";
@ -27,7 +28,7 @@ export default class Sauvegardeur {
localStorage.setItem(this._clePartieEnCours, JSON.stringify(partieEnCours)); localStorage.setItem(this._clePartieEnCours, JSON.stringify(partieEnCours));
} }
public static chargerSauvegardePartieEnCours(): { propositions: Array<string>; datePartie: Date } | undefined { public static chargerSauvegardePartieEnCours(): PartieEnCours | undefined {
let dataPartieEnCours = localStorage.getItem(this._clePartieEnCours); let dataPartieEnCours = localStorage.getItem(this._clePartieEnCours);
if (!dataPartieEnCours) return; if (!dataPartieEnCours) return;