Fix: calcul de date au delà de 12 mois
Gestion de mois 12 (par modulo) Ajout de l'année incrémenter le jour se fait sur l'index (et calcul des infos de calendrier) + sécurité pour les calendriers existants
This commit is contained in:
		| @@ -37,13 +37,15 @@ const MAX_NOMBRE_ASTRAL = 12; | |||||||
| /* -------------------------------------------- */ | /* -------------------------------------------- */ | ||||||
| export class RdDCalendrier extends Application { | export class RdDCalendrier extends Application { | ||||||
|  |  | ||||||
|   getCalendrier(index) { |  | ||||||
|     let month =  Math.floor(index / 28) % 12; |   static getCalendrier(index) { | ||||||
|     let calendrier = { |     let calendrier = { | ||||||
|       heureRdD: 0, // Index dans heuresList |       heureRdD: 0, // Index dans heuresList | ||||||
|       minutesRelative: 0, |       minutesRelative: 0, | ||||||
|       moisRdD: month, |       indexJour: index, | ||||||
|       jour: (index - (month * 28)) + 1 |       annee: Math.floor(index / (28 * 12)), | ||||||
|  |       moisRdD: Math.floor(index / 28) % 12, | ||||||
|  |       jour: (index % 28), | ||||||
|     } |     } | ||||||
|     return calendrier; |     return calendrier; | ||||||
|   } |   } | ||||||
| @@ -52,9 +54,12 @@ export class RdDCalendrier extends Application { | |||||||
|   async initCalendrier() { |   async initCalendrier() { | ||||||
|     // Calendrier |     // Calendrier | ||||||
|     this.calendrier = duplicate(game.settings.get("foundryvtt-reve-de-dragon", "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); |     //console.log("CALENDRIER", this.calendrier); | ||||||
|     if (this.calendrier == undefined || this.calendrier.moisRdD == undefined) { |     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 |       if (game.user.isGM) { // Uniquement si GM | ||||||
|         game.settings.set("foundryvtt-reve-de-dragon", "calendrier", this.calendrier); |         game.settings.set("foundryvtt-reve-de-dragon", "calendrier", this.calendrier); | ||||||
|       } |       } | ||||||
| @@ -94,19 +99,16 @@ export class RdDCalendrier extends Application { | |||||||
|  |  | ||||||
|   /* -------------------------------------------- */ |   /* -------------------------------------------- */ | ||||||
|   getDateFromIndex(index) { |   getDateFromIndex(index) { | ||||||
|     index = index ?? this.getCurrentDayIndex(); |     const date = this.getNumericDateFromIndex(index); | ||||||
|     let month = Math.floor(index / 28); |     return date.day + ' ' + date.month; | ||||||
|     let day = (index - (month * 28)) + 1; |  | ||||||
|     return day + " " + heuresList[month]; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /* -------------------------------------------- */ |   /* -------------------------------------------- */ | ||||||
|   getNumericDateFromIndex(index = undefined) { |   getNumericDateFromIndex(index = undefined) { | ||||||
|     if (!index) index = this.getCurrentDayIndex(); |     const dateRdD = RdDCalendrier.getCalendrier(index ?? this.getCurrentDayIndex()); | ||||||
|     let month = Math.floor(index / 28) |  | ||||||
|     return { |     return { | ||||||
|       month: heuresList[month], |       day: dateRdD.jour + 1, | ||||||
|       day: (index - (month * 28)) + 1 |       month: heuresDef[heuresList[dateRdD.moisRdD]].label | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -117,7 +119,7 @@ export class RdDCalendrier extends Application { | |||||||
|  |  | ||||||
|   /* -------------------------------------------- */ |   /* -------------------------------------------- */ | ||||||
|   getCurrentDayIndex() { |   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 +240,8 @@ export class RdDCalendrier extends Application { | |||||||
|  |  | ||||||
|   /* -------------------------------------------- */ |   /* -------------------------------------------- */ | ||||||
|   incrementerJour() { |   incrementerJour() { | ||||||
|     this.calendrier.jour += 1; |     const index = this.getCurrentDayIndex() + 1; | ||||||
|     if (this.calendrier.jour >= RDD_JOUR_PAR_MOIS) { |     this.calendrier = RdDCalendrier.getCalendrier(index); | ||||||
|       this.calendrier.jour -= RDD_JOUR_PAR_MOIS; |  | ||||||
|       if (this.calendrier.jour <= 0) |  | ||||||
|         this.calendrier.jour = 0; |  | ||||||
|       this.calendrier.moisRdD += 1; |  | ||||||
|       // Reconstruire les nombres astraux |  | ||||||
|     } |  | ||||||
|     this.rebuildListeNombreAstral(); |     this.rebuildListeNombreAstral(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -258,7 +254,7 @@ export class RdDCalendrier extends Application { | |||||||
|   /* -------------------------------------------- */ |   /* -------------------------------------------- */ | ||||||
|   async positionnerHeure(indexHeure) { |   async positionnerHeure(indexHeure) { | ||||||
|     if (indexHeure <= this.calendrier.heureRdD) { |     if (indexHeure <= this.calendrier.heureRdD) { | ||||||
|       await this.incrementerJour(); |       this.incrementerJour(); | ||||||
|     } |     } | ||||||
|     this.calendrier.heureRdD = indexHeure; |     this.calendrier.heureRdD = indexHeure; | ||||||
|     this.calendrier.minutesRelative = 0; |     this.calendrier.minutesRelative = 0; | ||||||
|   | |||||||
| @@ -72,6 +72,7 @@ Hooks.once("init", async function () { | |||||||
|     name: "calendrier", |     name: "calendrier", | ||||||
|     scope: "world", |     scope: "world", | ||||||
|     config: false, |     config: false, | ||||||
|  |     default: RdDCalendrier.getCalendrier(0), | ||||||
|     type: Object |     type: Object | ||||||
|   }); |   }); | ||||||
|  |  | ||||||
| @@ -80,6 +81,7 @@ Hooks.once("init", async function () { | |||||||
|     name: "liste-nombre-astral", |     name: "liste-nombre-astral", | ||||||
|     scope: "world", |     scope: "world", | ||||||
|     config: false, |     config: false, | ||||||
|  |     default: [], | ||||||
|     type: Object |     type: Object | ||||||
|   }); |   }); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user