import LettreResultat from "./entites/lettreResultat"; import { LettreStatut } from "./entites/lettreStatut"; import NotificationMessage from "./notificationMessage"; import PanelManager from "./panelManager"; import Sauvegardeur from "./sauvegardeur"; export default class FinDePartiePanel { private readonly _datePartie: Date; private readonly _panelManager: PanelManager; private readonly _statsButton: HTMLElement; private _resumeTexte: string = ""; private _motATrouver: string = ""; private _estVictoire: boolean = false; private _partieEstFinie: boolean = false; public constructor(datePartie: Date, panelManager: PanelManager) { this._datePartie = datePartie; this._panelManager = panelManager; this._statsButton = document.getElementById("configuration-stats-bouton") as HTMLElement; this._statsButton.addEventListener( "click", (() => { this.afficher(); }).bind(this) ); } public genererResume(estBonneReponse: boolean, motATrouver: string, resultats: Array>): void { let resultatsEmojis = resultats.map((mot) => mot .map((resultat) => resultat.statut) .reduce((ligne, statut) => { switch (statut) { case LettreStatut.BienPlace: return ligne + "🟥"; case LettreStatut.MalPlace: return ligne + "🟡"; default: return ligne + "🟦"; } }, "") ); let dateGrille = this._datePartie.getTime(); let origine = new Date(2022, 0, 8).getTime(); this._motATrouver = motATrouver; this._estVictoire = estBonneReponse; this._partieEstFinie = true; let numeroGrille = Math.floor((dateGrille - origine) / (24 * 3600 * 1000)) + 1; this._resumeTexte = "SUTOM #" + numeroGrille + " " + (estBonneReponse ? resultats.length : "-") + "/6\n\n" + resultatsEmojis.join("\n"); } private attacherPartage(): void { let resumeBouton = document.getElementById("fin-de-partie-panel-resume-bouton") as HTMLElement; resumeBouton.addEventListener("click", (event) => { event.stopPropagation(); if (!navigator.clipboard) { NotificationMessage.ajouterNotificationPanel("Votre navigateur n'est pas compatible"); } navigator.clipboard .writeText(this._resumeTexte + "\n\nhttps://sutom.nocle.fr") .then(() => { NotificationMessage.ajouterNotificationPanel("Résumé copié dans le presse papier"); }) .catch((raison) => { NotificationMessage.ajouterNotificationPanel("Votre navigateur n'est pas compatible"); }); }); } public afficher(): void { let titre: string; let contenu: string = ""; if (!this._partieEstFinie) { titre = "Statistiques"; contenu += '

Vous n\'avez pas encore fini votre partie du jour.

'; } else { if (this._estVictoire) { titre = "Félicitations"; contenu += '

Bravo, tu as gagné. Reviens demain pour une nouvelle grille.

'; } else { titre = "Perdu"; contenu += '

\ Le mot a trouver était : ' + this._motATrouver + "
\ Peut être feras-tu mieux demain ? \

"; } contenu += '

Résumé de ta partie − Partager

\
' +
        this._resumeTexte +
        "
"; } let stats = Sauvegardeur.chargerSauvegardeStats(); if (stats) { contenu += '

Statistiques

Parties :
' + `
${stats.partiesGagnees}/${stats.partiesJouees}
` + "
" + `
1/6 :
${stats.repartition[1]}
` + `
2/6 :
${stats.repartition[2]}
` + `
3/6 :
${stats.repartition[3]}
` + `
4/6 :
${stats.repartition[4]}
` + `
5/6 :
${stats.repartition[5]}
` + `
6/6 :
${stats.repartition[6]}
` + `
-/6 :
${stats.repartition["-"]}
` + `
Moyenne :
${this.getMoyenne(stats.repartition)}
` + '
Lettres :
' + '
' + `${stats.lettresRepartitions.bienPlace} 🟥 ` + `${stats.lettresRepartitions.malPlace} 🟡 ` + `${stats.lettresRepartitions.nonTrouve} 🟦` + "
" + "
" + "
"; } this._panelManager.setContenu(titre, contenu); this._panelManager.setClasses(["fin-de-partie-panel"]); if (this._partieEstFinie) this.attacherPartage(); this._panelManager.afficherPanel(); } private getMoyenne(repartition: { 1: number; 2: number; 3: number; 4: number; 5: number; 6: number; "-": number }): string { return ( (repartition[1] * 1 + repartition[2] * 2 + repartition[3] * 3 + repartition[4] * 4 + repartition[5] * 5 + repartition[6] * 6 + repartition["-"] * 6) / (repartition[1] + repartition[2] + repartition[3] + repartition[4] + repartition[5] + repartition[6] + repartition["-"]) ).toLocaleString("fr-FR", { maximumFractionDigits: 2 }); } }