diff --git a/module/rdd-calendrier.js b/module/rdd-calendrier.js index f99a53a1..63306295 100644 --- a/module/rdd-calendrier.js +++ b/module/rdd-calendrier.js @@ -5,8 +5,8 @@ import { HtmlUtility } from "./html-utility.js"; import { RdDResolutionTable } from "./rdd-resolution-table.js"; import { RdDUtility } from "./rdd-utility.js"; import { Grammar } from "./grammar.js"; -import { Misc } from "./misc.js"; import { RdDDice } from "./rdd-dice.js"; +import { Misc } from "./misc.js"; /* -------------------------------------------- */ const dossierIconesHeures = 'systems/foundryvtt-reve-de-dragon/icons/heures/' @@ -31,7 +31,11 @@ const saisonsDef = { "automne": { label: "Automne" }, "hiver": { label: "Hiver" } }; -const RDD_JOUR_PAR_MOIS = 28; + +const RDD_MOIS_PAR_AN = 12; +export const RDD_JOUR_PAR_MOIS = 28; +const RDD_HEURES_PAR_JOUR = 12; +const RDD_MINUTES_PAR_HEURES = 120; const MAX_NOMBRE_ASTRAL = 12; /* -------------------------------------------- */ @@ -41,15 +45,21 @@ export class RdDCalendrier extends Application { return { top: 200, left: 200 }; } + static getDefSigne(moisRdD) { + moisRdD = moisRdD % RDD_MOIS_PAR_AN; + return Object.values(heuresDef).find(h => h.heure == moisRdD); + } + static getCalendrier(index) { let calendrier = { - heureRdD: 0, // Index dans heuresList + heureRdD: 0, // Index dans heuresList / heuresDef[x].heure minutesRelative: 0, indexJour: index, - annee: Math.floor(index / (28 * 12)), - moisRdD: Math.floor(index / 28) % 12, - jour: (index % 28), + annee: Math.floor(index / (RDD_JOUR_PAR_MOIS * RDD_MOIS_PAR_AN)), + moisRdD: Math.floor(index / RDD_JOUR_PAR_MOIS) % RDD_MOIS_PAR_AN, + jour: (index % RDD_JOUR_PAR_MOIS) // Le calendrier stocke le jour en 0-27, mais en 1-28 à l'affichage } + calendrier.moisLabel = RdDCalendrier.getDefSigne(calendrier.moisRdD).label; return calendrier; } @@ -57,8 +67,8 @@ export class RdDCalendrier extends Application { async initCalendrier() { // Calendrier this.calendrier = duplicate(game.settings.get("foundryvtt-reve-de-dragon", "calendrier")); - this.calendrier.annee = this.calendrier.annee ?? (this.calendrier.moisRdD == 12 ? 1 : 0); - this.calendrier.moisRdD = (this.calendrier.moisRdD ?? 0) % 12; + this.calendrier.annee = this.calendrier.annee ?? (this.calendrier.moisRdD == RDD_MOIS_PAR_AN ? 1 : 0); + this.calendrier.moisRdD = (this.calendrier.moisRdD ?? 0) % RDD_MOIS_PAR_AN; //console.log("CALENDRIER", this.calendrier); if (this.calendrier == undefined || this.calendrier.moisRdD == undefined) { @@ -120,20 +130,19 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ getCurrentDayIndex() { - return (((this.calendrier.annee ?? 0) * 12 + (this.calendrier.moisRdD ?? 0)) * 28) + (this.calendrier.jour ?? 0); + return (((this.calendrier.annee ?? 0) * RDD_MOIS_PAR_AN + (this.calendrier.moisRdD ?? 0)) * RDD_JOUR_PAR_MOIS) + (this.calendrier.jour ?? 0); } /* -------------------------------------------- */ getIndexFromDate(jour, mois) { - return (heuresDef[mois].heure * 28) + (jour - 1); + return (heuresDef[mois].heure * RDD_JOUR_PAR_MOIS) + jour - 1; } /* -------------------------------------------- */ getJoursSuivants(num) { let jours = []; let index = this.getCurrentDayIndex(); for (let i = 0; i < num; i++) { - jours[i] = { label: this.getDateFromIndex(index), index: index }; - index += 1; + jours[i] = { label: this.getDateFromIndex(index + i), index: index + i }; } return jours; } @@ -170,14 +179,14 @@ export class RdDCalendrier extends Application { }); } - /* -------------------------------------------- */ + /* -------------------------------------------- */ getNombreAstral(indexDate) { - if ( this.listeNombreAstral == undefined ) { + if (this.listeNombreAstral == undefined) { this.listeNombreAstral = this._loadListNombreAstral(); } let liste = this.listeNombreAstral; - if ( game.user.isGM ) { - if ( typeof(liste) != 'Array' || liste.length == 0 ) { + if (game.user.isGM) { + if (typeof (liste) != 'Array' || liste.length == 0) { this.rebuildListeNombreAstral(); liste = this.listeNombreAstral; } @@ -192,10 +201,10 @@ export class RdDCalendrier extends Application { let jourCourant = this.getCurrentDayIndex(); let newList = []; - for (let i = 0; i < 12; i++) { + for (let i = 0; i < MAX_NOMBRE_ASTRAL; i++) { let dayIndex = jourCourant + i; - let na = this.listeNombreAstral.find( n => n.index == dayIndex); - if ( na ) { + let na = this.listeNombreAstral.find(n => n.index == dayIndex); + if (na) { newList[i] = duplicate(na); } else { newList[i] = await this.ajouterNombreAstral(dayIndex); @@ -223,12 +232,12 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ async incrementTime(minutes = 0) { this.calendrier.minutesRelative += minutes; - if (this.calendrier.minutesRelative >= 120) { - this.calendrier.minutesRelative -= 120; + if (this.calendrier.minutesRelative >= RDD_MINUTES_PAR_HEURES) { + this.calendrier.minutesRelative -= RDD_MINUTES_PAR_HEURES; this.calendrier.heureRdD += 1; } - if (this.calendrier.heureRdD > 11) { - this.calendrier.heureRdD -= 12; + if (this.calendrier.heureRdD >= RDD_HEURES_PAR_JOUR) { + this.calendrier.heureRdD -= RDD_HEURES_PAR_JOUR; this.incrementerJour(); } game.settings.set("foundryvtt-reve-de-dragon", "calendrier", duplicate(this.calendrier)); @@ -330,14 +339,13 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ findHeure(heure) { heure = Grammar.toLowerCaseNoAccentNoSpace(heure); - let parHeureOuLabel = Object.values(heuresDef).filter(it => (it.heure + 1) == heure || Grammar.toLowerCaseNoAccentNoSpace(it.label) == heure); + let parHeureOuLabel = Object.values(heuresDef).filter(it => (it.heure + 1) == parseInt(heure) || Grammar.toLowerCaseNoAccentNoSpace(it.label) == heure); if (parHeureOuLabel.length == 1) { return parHeureOuLabel[0]; } let parLabelPartiel = Object.values(heuresDef).filter(it => Grammar.toLowerCaseNoAccentNoSpace(it.label).includes(heure)); - const matchLength = heure.length; - if(parLabelPartiel.length > 0) { - parLabelPartiel.sort((a,b)=> (a.label.length - matchLength)^2 - (b.label.length- matchLength)^2); + if (parLabelPartiel.length > 0) { + parLabelPartiel.sort(Misc.ascending(h => h.label.length)); return parLabelPartiel[0]; } return undefined; @@ -350,7 +358,7 @@ export class RdDCalendrier extends Application { let hn = defHeure.heure; let chiffreAstral = this.getCurrentNombreAstral() ?? 0; let heureCourante = this.calendrier.heureRdD; - let ecartChance = (hn + chiffreAstral - heureCourante) % 12; + let ecartChance = (hn + chiffreAstral - heureCourante) % RDD_HEURES_PAR_JOUR; switch (ecartChance) { case 0: return 4; case 4: case 8: return 2; @@ -361,8 +369,8 @@ export class RdDCalendrier extends Application { else if (name) { ui.notifications.warn(name + " n'a pas d'heure de naissance, ou elle est incorrecte : " + heureNaissance); } - else{ - ui.notifications.warn(heureNaissance+" ne correspond pas à une heure de naissance"); + else { + ui.notifications.warn(heureNaissance + " ne correspond pas à une heure de naissance"); } return 0; } @@ -423,12 +431,12 @@ export class RdDCalendrier extends Application { async saveEditeur(calendrierData) { this.calendrier.minutesRelative = Number(calendrierData.minutesRelative); this.calendrier.jour = Number(calendrierData.jourMois) - 1; - this.calendrier.moisRdD = heuresList.findIndex(mois => mois === calendrierData.moisKey); - this.calendrier.heureRdD = heuresList.findIndex(heure => heure === calendrierData.heureKey);; // Index dans heuresList + this.calendrier.moisRdD = RdDCalendrier.getDefSigne(calendrierData.moisKey); + this.calendrier.heureRdD = RdDCalendrier.getDefSigne(calendrierData.heureKey); // Index dans heuresList game.settings.set("foundryvtt-reve-de-dragon", "calendrier", duplicate(this.calendrier)); await this.rebuildListeNombreAstral(); - + game.socket.emit("system.foundryvtt-reve-de-dragon", { msg: "msg_sync_time", data: duplicate(this.calendrier) @@ -441,9 +449,9 @@ export class RdDCalendrier extends Application { async showCalendarEditor() { let calendrierData = duplicate(this.fillCalendrierData()); if (this.editeur == undefined) { - calendrierData.jourMoisOptions = Array(28).fill().map((item, index) => 1 + index); + calendrierData.jourMoisOptions = this.buildJoursMois(); calendrierData.heuresOptions = [0, 1]; - calendrierData.minutesOptions = Array(120).fill().map((item, index) => 0 + index); + calendrierData.minutesOptions = Array(RDD_MINUTES_PAR_HEURES).fill().map((item, index) => 0 + index); let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/calendar-editor-template.html', calendrierData); this.editeur = new RdDCalendrierEditeur(html, this, calendrierData) } @@ -451,6 +459,10 @@ export class RdDCalendrier extends Application { this.editeur.render(true); } + static buildJoursMois() { + return Array(RDD_JOUR_PAR_MOIS).fill().map((item, index) => 1 + index); + } + /* -------------------------------------------- */ async showAstrologieEditor() { let calendrierData = duplicate(this.fillCalendrierData()); diff --git a/module/rdd-herbes.js b/module/rdd-herbes.js index e1b31911..fa67527e 100644 --- a/module/rdd-herbes.js +++ b/module/rdd-herbes.js @@ -37,7 +37,7 @@ export class RdDHerbes extends Item { static updatePotionData( formData ) { formData.herbesSoins = this.buildHerbesList(this.herbesSoins, 12); formData.herbesRepos = this.buildHerbesList(this.herbesRepos, 7); - formData.jourMoisOptions = Array(28).fill().map((item, index) => 1 + index); + formData.jourMoisOptions = RdDCalendrier.buildJoursMois(); formData.dateActuelle = game.system.rdd.calendrier.getDateFromIndex(); formData.splitDate = game.system.rdd.calendrier.getNumericDateFromIndex(formData.data.prdate);