From 33ed7bfe2c52093a74cb6c65d4a3674da8d15e74 Mon Sep 17 00:00:00 2001
From: sladecraven <sylvain@lahiette.com>
Date: Sun, 25 Apr 2021 10:08:40 +0200
Subject: [PATCH] #185 - Reinit nombres astraux

---
 module/actor.js                  | 13 +++++++++++++
 module/rdd-astrologie-editeur.js | 13 +++++++++++--
 module/rdd-calendrier.js         |  6 ++++++
 module/rdd-tmr-dialog.js         |  8 ++++++++
 module/tmr-rencontres.js         |  4 ++++
 module/tmr-utility.js            |  3 +++
 template.json                    |  3 ++-
 7 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/module/actor.js b/module/actor.js
index e85ed5d7..897efaa1 100644
--- a/module/actor.js
+++ b/module/actor.js
@@ -1210,6 +1210,17 @@ export class RdDActor extends Actor {
     return queue;
   }
 
+  /* -------------------------------------------- */
+  async cacheTMR( ) {
+    await this.update({ 'data.reve.tmrpos.cache': true });
+  }
+  async montreTMR( ) {
+    await this.update({ 'data.reve.tmrpos.cache': false });
+  }
+  async isTMRCache( ) {
+    return this.data.data.reve.tmrpos.cache;
+  }
+
   /* -------------------------------------------- */
   async reinsertionAleatoire(raison) {
     ChatMessage.create({
@@ -1219,9 +1230,11 @@ export class RdDActor extends Actor {
     const innaccessible = this.buildTMRInnaccessible();
     let tmr = TMRUtility.getTMRAleatoire(tmr => !innaccessible.includes(tmr.coord));
     this.updateCoordTMR(tmr.coord);
+    this.cacheTMR();
     return tmr;
   }
 
+  /* -------------------------------------------- */
   buildTMRInnaccessible() {
     const tmrInnaccessibles = this.filterItemsData(it => Draconique.isCaseTMR(it) &&
       EffetsDraconiques.isInnaccessible(it));
diff --git a/module/rdd-astrologie-editeur.js b/module/rdd-astrologie-editeur.js
index b09ff7c4..a79f45c1 100644
--- a/module/rdd-astrologie-editeur.js
+++ b/module/rdd-astrologie-editeur.js
@@ -3,12 +3,13 @@
  * Extend the base Dialog entity by defining a custom window to perform roll.
  * @extends {Dialog}
  */
-export class RdDAstrologieEditeur extends Dialog {
+ export class RdDAstrologieEditeur extends Dialog {
 
   /* -------------------------------------------- */
   constructor(html, calendrier, calendrierData) {
 
     let myButtons = {
+        resetButton: { label: "Re-tirer les nombres astraux", callback: html => this.resetNombreAstraux() },
         saveButton: { label: "Fermer", callback: html => this.fillData() }
       };
 
@@ -21,7 +22,15 @@ export class RdDAstrologieEditeur extends Dialog {
     this.updateData( calendrierData );
   }
 
-  /* -------------------------------------------- */
+  /* -------------------------------------------- */  
+  resetNombreAstraux() {
+    game.system.rdd.calendrier.resetNombreAstral();
+    game.system.rdd.calendrier.rebuildListeNombreAstral();
+
+    game.system.rdd.calendrier.showAstrologieEditor();
+  }
+
+  /* -------------------------------------------- */  
   fillData( ) {
   }
 
diff --git a/module/rdd-calendrier.js b/module/rdd-calendrier.js
index 91380b69..b51107b6 100644
--- a/module/rdd-calendrier.js
+++ b/module/rdd-calendrier.js
@@ -136,6 +136,12 @@ export class RdDCalendrier extends Application {
     return this.getNombreAstral(indexDate);
   }
 
+  /* -------------------------------------------- */
+  resetNombreAstral( ) {
+    this.listeNombreAstral = [];
+    game.settings.set("foundryvtt-reve-de-dragon", "liste-nombre-astral", this.listeNombreAstral);
+  }
+
   /* -------------------------------------------- */
   getNombreAstral(indexDate) {
     const liste = this.listeNombreAstral ?? this._loadListNombreAstral();
diff --git a/module/rdd-tmr-dialog.js b/module/rdd-tmr-dialog.js
index cd0b9347..1d21c45b 100644
--- a/module/rdd-tmr-dialog.js
+++ b/module/rdd-tmr-dialog.js
@@ -44,6 +44,10 @@ export class RdDTMRDialog extends Dialog {
     }
     super(dialogConf, dialogOptions);
 
+    if ( actor.isTMRCache() ) {
+      console.log("TMR cachées !!!!!!");
+    }
+
     this.tmrdata = duplicate(tmrData);
     this.actor = actor;
     this.actor.tmrApp = this; // reference this app in the actor structure
@@ -669,6 +673,7 @@ export class RdDTMRDialog extends Dialog {
     dialog.render(true);
   }
 
+  /* -------------------------------------------- */
   async validerVisite(tmr) {
     await EffetsDraconiques.pelerinage.onVisiteSupprimer(this.actor, tmr, (casetmr) => this.removeToken(tmr, casetmr));
     await EffetsDraconiques.urgenceDraconique.onVisiteSupprimer(this.actor, tmr, (casetmr) => this.removeToken(tmr, casetmr));
@@ -783,6 +788,7 @@ export class RdDTMRDialog extends Dialog {
     await tmrObject._onClickTMRPos(eventPos); // Vérifier l'état des compteurs reve/fatigue/vie
   }
 
+  /* -------------------------------------------- */
   async _onClickTMRPos(eventPos) {
     let currentPos = TMRUtility.convertToCellPos(Misc.data(this.actor).data.reve.tmrpos.coord);
 
@@ -862,6 +868,8 @@ export class RdDTMRDialog extends Dialog {
     }
   }
 
+
+  /* -------------------------------------------- */
   async postRencontre(tmr) {
     if (!(this.viewOnly || this.currentRencontre)) {
       await this.manageCaseHumide(tmr);
diff --git a/module/tmr-rencontres.js b/module/tmr-rencontres.js
index 9fe59c92..0a98c6cf 100644
--- a/module/tmr-rencontres.js
+++ b/module/tmr-rencontres.js
@@ -445,6 +445,7 @@ export class TMRRencontres {
     }
   }
 
+  /* -------------------------------------------- */
   static async postEchecPasseurFou(tmrDialog, tmrData) {
     if (tmrData.sortReserve) {
       await tmrDialog.processSortReserve(tmrData.sortReserve);
@@ -468,6 +469,7 @@ export class TMRRencontres {
     await data.actor.santeIncDec("vie", -1); // Et -1 PV
   }
 
+  /* -------------------------------------------- */
   static async _toubillonner(tmrDialog, actor, cases) {
     let coord = Misc.templateData(actor).reve.tmrpos.coord;
     for (let i = 0; i < cases; i++) {
@@ -476,6 +478,7 @@ export class TMRRencontres {
     await tmrDialog.forceDemiRevePosition(coord)
   }
 
+  /* -------------------------------------------- */
   static async onPostSuccessReveDeDragon(tmrDialog, tmrData) {
     if (tmrData.rolled.isPart) {
       await tmrData.actor.appliquerExperience(tmrData.rolled, 'reve', tmrData.competence);
@@ -483,6 +486,7 @@ export class TMRRencontres {
     await tmrData.actor.resultCombatReveDeDragon(tmrData);
   }
 
+  /* -------------------------------------------- */
   static async onPostEchecReveDeDragon(tmrDialog, tmrData) {
     await tmrData.actor.resultCombatReveDeDragon(tmrData);
     tmrDialog.close();
diff --git a/module/tmr-utility.js b/module/tmr-utility.js
index 2426934d..454355fd 100644
--- a/module/tmr-utility.js
+++ b/module/tmr-utility.js
@@ -378,6 +378,7 @@ export class TMRUtility {
     return Misc.rollOneOf(tmrRandomMovePatten);
   }
 
+  /* -------------------------------------------- */
   static deplaceTMRAleatoire(actor, coord) {
     return TMRUtility.deplaceTMRSelonPattern(actor, coord, TMRUtility.getDirectionPattern(), 1);
   }
@@ -465,12 +466,14 @@ export class TMRUtility {
     return caseList;
   }
 
+  /* -------------------------------------------- */
   static distanceTMR(coord1, coord2) {
     let pos1 = this.convertToCellPos(coord1);
     let pos2 = this.convertToCellPos(coord2);
     return this.distancePosTMR(pos1, pos2);
   }
 
+  /* -------------------------------------------- */
   static distancePosTMR(pos1, pos2) {
     const dx = pos2.x - pos1.x;
     const dy = pos2.y - pos1.y;
diff --git a/template.json b/template.json
index 63b76488..6a610cc9 100644
--- a/template.json
+++ b/template.json
@@ -452,7 +452,8 @@
         },
         "tmrpos": {
           "coord": "A1",
-          "label": "Position TMR"
+          "label": "Position TMR",
+          "cache": false
         },
         "reserve": {
           "list": []