diff --git a/module/rdd-calendrier.js b/module/rdd-calendrier.js index 0e4b3f48..f99a53a1 100644 --- a/module/rdd-calendrier.js +++ b/module/rdd-calendrier.js @@ -37,13 +37,18 @@ const MAX_NOMBRE_ASTRAL = 12; /* -------------------------------------------- */ export class RdDCalendrier extends Application { - getCalendrier(index) { - let month = Math.floor(index / 28) % 12; + static createCalendrierPos() { + return { top: 200, left: 200 }; + } + + static getCalendrier(index) { let calendrier = { heureRdD: 0, // Index dans heuresList minutesRelative: 0, - moisRdD: month, - jour: (index - (month * 28)) + 1 + indexJour: index, + annee: Math.floor(index / (28 * 12)), + moisRdD: Math.floor(index / 28) % 12, + jour: (index % 28), } return calendrier; } @@ -52,9 +57,12 @@ 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; + //console.log("CALENDRIER", this.calendrier); if (this.calendrier == undefined || this.calendrier.moisRdD == undefined) { - this.calendrier = this.getCalendrier(0); + this.calendrier = RdDCalendrier.getCalendrier(0); if (game.user.isGM) { // Uniquement si GM game.settings.set("foundryvtt-reve-de-dragon", "calendrier", this.calendrier); } @@ -62,9 +70,7 @@ export class RdDCalendrier extends Application { // position this.calendrierPos = duplicate(game.settings.get("foundryvtt-reve-de-dragon", "calendrier-pos")); if (this.calendrierPos == undefined || this.calendrierPos.top == undefined) { - this.calendrierPos = {}; - this.calendrierPos.top = 200; - this.calendrierPos.left = 200; + this.calendrierPos = RdDCalendrier.createCalendrierPos(); if (game.user.isGM) { // Uniquement si GM game.settings.set("foundryvtt-reve-de-dragon", "calendrier-pos", this.calendrierPos); } @@ -94,19 +100,16 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ getDateFromIndex(index) { - index = index ?? this.getCurrentDayIndex(); - let month = Math.floor(index / 28); - let day = (index - (month * 28)) + 1; - return day + " " + heuresList[month]; + const date = this.getNumericDateFromIndex(index); + return date.day + ' ' + date.month; } /* -------------------------------------------- */ getNumericDateFromIndex(index = undefined) { - if (!index) index = this.getCurrentDayIndex(); - let month = Math.floor(index / 28) + const dateRdD = RdDCalendrier.getCalendrier(index ?? this.getCurrentDayIndex()); return { - month: heuresList[month], - day: (index - (month * 28)) + 1 + day: dateRdD.jour + 1, + month: heuresDef[heuresList[dateRdD.moisRdD]].label } } @@ -117,7 +120,7 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ getCurrentDayIndex() { - return (this.calendrier.moisRdD * 28) + this.calendrier.jour; + return (((this.calendrier.annee ?? 0) * 12 + (this.calendrier.moisRdD ?? 0)) * 28) + (this.calendrier.jour ?? 0); } /* -------------------------------------------- */ @@ -238,14 +241,8 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ incrementerJour() { - this.calendrier.jour += 1; - if (this.calendrier.jour >= RDD_JOUR_PAR_MOIS) { - this.calendrier.jour -= RDD_JOUR_PAR_MOIS; - if (this.calendrier.jour <= 0) - this.calendrier.jour = 0; - this.calendrier.moisRdD += 1; - // Reconstruire les nombres astraux - } + const index = this.getCurrentDayIndex() + 1; + this.calendrier = RdDCalendrier.getCalendrier(index); this.rebuildListeNombreAstral(); } @@ -258,7 +255,7 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ async positionnerHeure(indexHeure) { if (indexHeure <= this.calendrier.heureRdD) { - await this.incrementerJour(); + this.incrementerJour(); } this.calendrier.heureRdD = indexHeure; this.calendrier.minutesRelative = 0; @@ -332,8 +329,8 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ findHeure(heure) { - heure = Grammar.toLowerCaseNoAccent(heure); - let parHeureOuLabel = Object.values(heuresDef).filter(it => (it.heure+1) == heure || Grammar.toLowerCaseNoAccentNoSpace(it.label) == heure); + heure = Grammar.toLowerCaseNoAccentNoSpace(heure); + let parHeureOuLabel = Object.values(heuresDef).filter(it => (it.heure + 1) == heure || Grammar.toLowerCaseNoAccentNoSpace(it.label) == heure); if (parHeureOuLabel.length == 1) { return parHeureOuLabel[0]; } diff --git a/module/rdd-main.js b/module/rdd-main.js index 0afcccbd..9cc02aa1 100644 --- a/module/rdd-main.js +++ b/module/rdd-main.js @@ -72,6 +72,7 @@ Hooks.once("init", async function () { name: "calendrier", scope: "world", config: false, + default: RdDCalendrier.getCalendrier(0), type: Object }); @@ -80,14 +81,16 @@ Hooks.once("init", async function () { name: "liste-nombre-astral", scope: "world", config: false, + default: [], type: Object }); /* -------------------------------------------- */ game.settings.register("foundryvtt-reve-de-dragon", "calendrier-pos", { name: "calendrierPos", - scope: "world", + scope: "client", config: false, + default: RdDCalendrier.createCalendrierPos(), type: Object });