From e52bd0b0216895e2699d95f2ef0f9bc4ea09b9ea Mon Sep 17 00:00:00 2001 From: JonathanMM Date: Sun, 30 Jan 2022 10:24:39 +0100 Subject: [PATCH] =?UTF-8?q?Changement=20de=20la=20m=C3=A9thode=20de=20r?= =?UTF-8?q?=C3=A9cup=C3=A9ration=20du=20mot=20du=20jour?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + cron.sh | 2 -- ts/dictionnaire.ts | 7 +++--- ts/gestionnaire.ts | 43 ++++++++++++++++++++---------------- ts/mots/listeMotsATrouver.ts | 3 --- ts/server.ts | 16 ++++++++++++++ utils/majATrouver.js | 20 +++++++++++++++++ 7 files changed, 64 insertions(+), 28 deletions(-) mode change 100644 => 100755 cron.sh delete mode 100644 ts/mots/listeMotsATrouver.ts diff --git a/.gitignore b/.gitignore index 05019cf..af4080f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ config/ data/ js/ +mots/ node_modules/ ts/mots/listeMotsATrouver.*.ts cron.prod.sh diff --git a/cron.sh b/cron.sh old mode 100644 new mode 100755 index 4aed5b9..f3793c3 --- a/cron.sh +++ b/cron.sh @@ -4,5 +4,3 @@ npm install node utils/melangerATrouver.js node utils/majATrouver.js tsc -cp -r public/* /var/www/sutom/ -cp -r js/ /var/www/sutom/ diff --git a/ts/dictionnaire.ts b/ts/dictionnaire.ts index eaa48db..2c43e63 100644 --- a/ts/dictionnaire.ts +++ b/ts/dictionnaire.ts @@ -1,15 +1,14 @@ import ListeMotsProposables from "./mots/listeMotsProposables"; -import ListeMotsATrouver from "./mots/listeMotsATrouver"; export default class Dictionnaire { public constructor() {} - public getMot(datePartie: Date): string { + public async getMot(datePartie: Date): Promise { let aujourdhui = datePartie.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)) + 1; - return ListeMotsATrouver.Liste[numeroGrille % ListeMotsATrouver.Liste.length]; + return await fetch("mots/" + numeroGrille + ".txt").then((resultat) => resultat.text()); } public estMotValide(mot: string): boolean { diff --git a/ts/gestionnaire.ts b/ts/gestionnaire.ts index 6d1648e..94e65fd 100644 --- a/ts/gestionnaire.ts +++ b/ts/gestionnaire.ts @@ -15,8 +15,8 @@ import ConfigurationPanel from "./configurationPanel"; export default class Gestionnaire { private readonly _dictionnaire: Dictionnaire; - private readonly _grille: Grille; - private readonly _input: Input; + private _grille: Grille | null = null; + private _input: Input | null = null; private readonly _reglesPanel: ReglesPanel; private readonly _finDePartiePanel: FinDePartiePanel; private readonly _configurationPanel: ConfigurationPanel; @@ -24,8 +24,8 @@ export default class Gestionnaire { private readonly _resultats: Array>; private readonly _panelManager: PanelManager; - private _motATrouver: string; - private _compositionMotATrouver: { [lettre: string]: number }; + private _motATrouver: string = ""; + private _compositionMotATrouver: { [lettre: string]: number } = {}; private _maxNbPropositions: number = 6; private _datePartieEnCours: Date; private _stats: SauvegardeStats = { partiesJouees: 0, partiesGagnees: 0 }; @@ -43,18 +43,20 @@ export default class Gestionnaire { } this._dictionnaire = new Dictionnaire(); - 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._propositions = new Array(); this._resultats = new Array>(); - this._compositionMotATrouver = this.decompose(this._motATrouver); this._panelManager = new PanelManager(); this._reglesPanel = new ReglesPanel(this._panelManager); this._finDePartiePanel = new FinDePartiePanel(this._datePartieEnCours, this._panelManager); this._configurationPanel = new ConfigurationPanel(this._panelManager); - this.chargerPropositions(partieEnCours.propositions); + this.choisirMot(this._datePartieEnCours).then((mot) => { + this._motATrouver = mot; + 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._compositionMotATrouver = this.decompose(this._motATrouver); + this.chargerPropositions(partieEnCours.propositions); + }); this.afficherReglesSiNecessaire(); } @@ -87,8 +89,8 @@ export default class Gestionnaire { Sauvegardeur.sauvegarderPartieEnCours(this._propositions, this._datePartieEnCours); } - private choisirMot(datePartie: Date): string { - return this._dictionnaire.nettoyerMot(this._dictionnaire.getMot(datePartie)); + private async choisirMot(datePartie: Date): Promise { + return this._dictionnaire.nettoyerMot(await this._dictionnaire.getMot(datePartie)); } private decompose(mot: string): { [lettre: string]: number } { @@ -127,19 +129,22 @@ export default class Gestionnaire { this.enregistrerPartieDansStats(); } - this._grille.validerMot(mot, resultats, isBonneReponse, skipAnimation, () => { - this._input.updateClavier(resultats); - if (isBonneReponse || this._propositions.length === this._maxNbPropositions) { - this._input.bloquer(); - this._finDePartiePanel.afficher(); - } - }); + if (this._grille) + this._grille.validerMot(mot, resultats, isBonneReponse, skipAnimation, () => { + if (this._input) { + this._input.updateClavier(resultats); + if (isBonneReponse || this._propositions.length === this._maxNbPropositions) { + this._input.bloquer(); + this._finDePartiePanel.afficher(); + } + } + }); this.sauvegarderPartieEnCours(); } public actualiserAffichage(mot: string): void { - this._grille.actualiserAffichage(this._dictionnaire.nettoyerMot(mot)); + if (this._grille) this._grille.actualiserAffichage(this._dictionnaire.nettoyerMot(mot)); } private analyserMot(mot: string): Array { diff --git a/ts/mots/listeMotsATrouver.ts b/ts/mots/listeMotsATrouver.ts deleted file mode 100644 index bd3c376..0000000 --- a/ts/mots/listeMotsATrouver.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default class ListeMotsATrouver { - public static readonly Liste: Array = ["DIFFUSION", "NEGATIVE", "ABSENCE", "LISTES"]; -} diff --git a/ts/server.ts b/ts/server.ts index bcb8e47..b433699 100644 --- a/ts/server.ts +++ b/ts/server.ts @@ -1,5 +1,6 @@ import express from "express"; import http from "http"; +import fs from "fs"; const app = express(); const port = 4000; @@ -7,8 +8,23 @@ const port = 4000; app.use("/", express.static("public/")); app.use("/js", express.static("js/")); app.use("/ts", express.static("ts/")); + app.use("/mots", express.static("mots/")); app.use("/node_modules/requirejs/require.js", express.static("node_modules/requirejs/require.js")); + // Vu que le serveur node est prévu pour du test, on va créer un mot du jour s'il n'existe pas + let aujourdhui = new Date().getTime(); + let origine = new Date(2022, 0, 8).getTime(); + + let numeroGrille = Math.floor((aujourdhui - origine) / (24 * 3600 * 1000)) + 1; + + fs.access("mots/" + numeroGrille + ".txt", fs.constants.F_OK, (err) => { + if (err) { + fs.writeFile("mots/" + numeroGrille + ".txt", "DIFFUSION", (err) => { + if (err) console.error(err); + }); + } + }); + app.use(express.json()); const server = http.createServer(app); diff --git a/utils/majATrouver.js b/utils/majATrouver.js index ae97a90..ff96881 100644 --- a/utils/majATrouver.js +++ b/utils/majATrouver.js @@ -31,6 +31,26 @@ fs.readFile("data/motsATrouve.txt", "UTF8", function (erreur, contenu) { .join("\n"); contenu += "\n ]"; contenu += "\n}"; + + motsFiges + .map((mot) => + mot + .normalize("NFD") + .replace(/\p{Diacritic}/gu, "") + .toUpperCase() + .trim() + .replace(/^\s+|\s+$/g, "") + ) + .forEach((mot, numeroMot) => + fs.access("mots/" + (numeroMot + 1) + ".txt", fs.constants.F_OK, (err) => { + if (err) { + // Dans ce cas, le fichier n'existe pas + fs.writeFile("mots/" + (numeroMot + 1) + ".txt", mot, (err) => { + if (err) console.error(err); + }); + } + }) + ); fs.writeFile("ts/mots/listeMotsATrouver.ts", contenu, function (err) { if (err) { console.error(err);