diff --git a/module/rdd-calendrier.js b/module/rdd-calendrier.js index 18facb1e..d0f7c47b 100644 --- a/module/rdd-calendrier.js +++ b/module/rdd-calendrier.js @@ -117,45 +117,33 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ getCurrentNombreAstral() { - let index = this.getCurrentDayIndex(); - return this.getNombreAstral(index); + let indexDate = this.getCurrentDayIndex(); + return this.getNombreAstral(indexDate); } /* -------------------------------------------- */ - getNombreAstral( index ) { - const liste = this.listeNombreAstral || this._loadListNombreAstral(); - let astralData = liste.find( (nombreAstral, i) => nombreAstral.index == index ); - if ( astralData == undefined ) { + getNombreAstral( indexDate ) { + const liste = this.listeNombreAstral ?? this._loadListNombreAstral(); + let astralData = liste.find( (nombreAstral, i) => nombreAstral.index == indexDate ); + if (! astralData?.nombreAstral ) { this.rebuildListeNombreAstral(); - astralData = liste.find( (nombreAstral, i) => nombreAstral.index == index ); + astralData = liste.find( (nombreAstral, i) => nombreAstral.index == indexDate ); } - return astralData.nombreAstral || "N/A"; + return astralData?.nombreAstral ?? "N/A"; } - + /* -------------------------------------------- */ rebuildListeNombreAstral() { - // Auto-create if needed - if ( this.listeNombreAstral == undefined) - this.listeNombreAstral = []; - - // Nettoyage des nombres astraux anciens let jourCourant = this.getCurrentDayIndex(); let jourFin = jourCourant + 12; - let newList = this.listeNombreAstral.filter( (nombreAstral, i) => nombreAstral && nombreAstral.index >= jourCourant && nombreAstral.index < jourFin); - //console.log("LSTES", this.listeNombreAstral, newList ); - - let lastDay = jourCourant; - for (let i=0; i < MAX_NOMBRE_ASTRAL; i++) { - let nombreAstral = newList[i]; - if ( nombreAstral ) { - lastDay = nombreAstral.index + 1; - } else { - newList.push( this.ajouterNombreAstral( lastDay) ); - lastDay += 1; + let newList = [0,1,2,3,4,5,6,7,8,9,10,11].map( i => this.ajouterNombreAstral(jourCourant + i)); + if (this.listeNombreAstral) { + for (const na of this.listeNombreAstral) { + if (na && na.index >= jourCourant && na.index < jourFin) { + newList[na.index - jourCourant] = na; + } } } - this.listeNombreAstral = newList; - game.settings.set("foundryvtt-reve-de-dragon", "liste-nombre-astral", this.listeNombreAstral ); }