sutom/ts/finDePartiePanel.ts
2023-05-19 13:15:54 +02:00

155 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import CopieHelper from "./copieHelper";
import Configuration from "./entites/configuration";
import LettreResultat from "./entites/lettreResultat";
import { LettreStatut } from "./entites/lettreStatut";
import InstanceConfiguration from "./instanceConfiguration";
import PanelManager from "./panelManager";
import Sauvegardeur from "./sauvegardeur";
import StatistiquesDisplayer from "./statistiquesDisplayer";
export default class FinDePartiePanel {
private readonly _datePartie: Date;
private readonly _panelManager: PanelManager;
private readonly _statsButton: HTMLElement;
private _resumeTexte: string = "";
private _resumeTexteLegacy: string = "";
private _motATrouver: string = "";
private _estVictoire: boolean = false;
private _partieEstFinie: boolean = false;
public constructor(datePartie: Date, panelManager: PanelManager) {
this._datePartie = new Date(datePartie);
this._datePartie.setHours(0, 0, 0);
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<Array<LettreResultat>>, dureeMs: number): 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 resultatsEmojisLegacy = resultats.map((mot) =>
mot
.map((resultat) => resultat.statut)
.reduce((ligne, statut) => {
switch (statut) {
case LettreStatut.BienPlace:
return ligne + '<span class="emoji-carre-rouge">🟥</span>';
case LettreStatut.MalPlace:
return ligne + '<span class="emoji-cercle-jaune">🟡</span>';
default:
return ligne + '<span class="emoji-carre-bleu">🟦</span>';
}
}, "")
);
let dateGrille = this._datePartie.getTime();
let origine = InstanceConfiguration.dateOrigine.getTime();
this._motATrouver = motATrouver;
this._estVictoire = estBonneReponse;
this._partieEstFinie = true;
let numeroGrille = Math.round((dateGrille - origine) / (24 * 3600 * 1000)) + 1;
let afficherChrono = (Sauvegardeur.chargerConfig() ?? Configuration.Default).afficherChrono;
const entete =
"#SUTOM #" +
numeroGrille +
" " +
(estBonneReponse ? resultats.length : "-") +
"/6" +
(afficherChrono ? " " + this.genererTempsHumain(dureeMs) : "") +
"\n\n";
this._resumeTexte = entete + resultatsEmojis.join("\n");
this._resumeTexteLegacy = entete + resultatsEmojisLegacy.join("\n");
}
private genererTempsHumain(dureeMs: number): string {
// Note : Durée est en millisecondes.
let duree = Math.floor(dureeMs / 1000);
let retour = "";
if (duree >= 3600) {
retour += Math.floor(duree / 3600) + "h";
}
retour +=
Math.floor((duree / 60) % 60)
.toString()
.padStart(2, "0") + ":";
retour += Math.floor(duree % 60)
.toString()
.padStart(2, "0");
return retour;
}
private attacherPartage(): void {
const resumeBouton = document.getElementById("fin-de-partie-panel-resume-bouton") as HTMLElement;
CopieHelper.attacheBoutonCopieLien(resumeBouton, this._resumeTexte + "\n\nhttps://sutom.nocle.fr", "Résumé copié dans le presse papier.");
}
public afficher(): void {
let titre: string;
let contenu: string = "";
if (!this._partieEstFinie) {
titre = "Statistiques";
contenu += '<p class="fin-de-partie-panel-phrase">Vous n\'avez pas encore fini votre partie du jour.</p>';
} else {
if (this._estVictoire) {
titre = "Félicitations";
contenu += '<p class="fin-de-partie-panel-phrase">Bravo, tu as gagné. Reviens demain pour une nouvelle grille.</p>';
} else {
titre = "Perdu";
contenu +=
'<p class="fin-de-partie-panel-phrase"> \
Le mot à trouver était : ' +
this._motATrouver +
"<br /> \
Peut-être feras-tu mieux demain ? \
</p>";
}
contenu += "<p>Résumé de ta partie ";
contenu += CopieHelper.creerBoutonPartage("fin-de-partie-panel-resume-bouton", "Partager").outerHTML;
contenu +=
'</p> \
<pre id="fin-de-partie-panel-resume">' +
this._resumeTexteLegacy +
"</pre>";
}
let stats = Sauvegardeur.chargerSauvegardeStats();
if (stats) {
const displayer = new StatistiquesDisplayer(stats);
contenu += displayer.genererHtmlStats().outerHTML;
}
this._panelManager.setContenu(titre, contenu);
this._panelManager.setClasses(["fin-de-partie-panel"]);
if (this._partieEstFinie) this.attacherPartage();
this._panelManager.afficherPanel();
}
}