From f7c1ed0749f5ef9fc8325156c6439db462d062ab Mon Sep 17 00:00:00 2001
From: sladecraven <sylvain@lahiette.com>
Date: Sun, 4 Apr 2021 22:34:59 +0200
Subject: [PATCH] #171 - Gestion potion de soins (WIP)

---
 module/actor.js                  | 82 +++++++++++++++++++++++++++++++-
 system.json                      |  4 +-
 template.json                    |  1 +
 templates/item-potion-sheet.html | 22 ++++++++-
 4 files changed, 104 insertions(+), 5 deletions(-)

diff --git a/module/actor.js b/module/actor.js
index e5c399ab..ab6bf606 100644
--- a/module/actor.js
+++ b/module/actor.js
@@ -27,6 +27,7 @@ import { EffetsDraconiques } from "./tmr/effets-draconiques.js";
 import { Draconique } from "./tmr/draconique.js";
 import { RdDCarac } from "./rdd-carac.js";
 import { Monnaie } from "./item-monnaie.js";
+import { RdDHerbes } from "./rdd-herbes.js";
 
 
 /* -------------------------------------------- */
@@ -619,6 +620,7 @@ export class RdDActor extends Actor {
     dialog.render(true);
   }
 
+  /* -------------------------------------------- */
   async resultCombatReveDeDragon(rollData) {
     rollData.queues = [];
     if (rollData.rolled.isEchec) {
@@ -3051,9 +3053,85 @@ export class RdDActor extends Actor {
   }
 
   /* -------------------------------------------- */
-  consommerPotion( potion ) {
+  async buildPotionGuerisonList( pointsGuerison) {
+    let pointsGuerisonInitial = pointsGuerison;
+    let myData = Misc.templateData(this);
+    const blessures = duplicate(myData.blessures);
+    let guerisonData = { list: [], pointsConsommes: 0 }
+
+    console.log(blessures);
+    for (let critique of blessures.critiques.liste) {
+      if (critique.active && pointsGuerison >= 6 ) {
+        pointsGuerison -= 6;
+        critique.active = false;
+        guerisonData.list.push( "1 Blessure Critique (6 points)");
+      }
+    }
+    for (let grave of blessures.graves.liste) {
+      if (grave.active && pointsGuerison >= 4 ) {
+        pointsGuerison -= 4;
+        grave.active = false;
+        guerisonData.list.push( "1 Blessure Grave (4 points)");
+      }
+    }
+    for (let legere of blessures.legeres.liste) {
+      if (legere.active && pointsGuerison >= 2 ) {
+        pointsGuerison -= 2;
+        legere.active = false;
+        guerisonData.list.push( "1 Blessure Légère (2 points)");
+      }
+    }
+    await this.update({ "data.blessures": blessures });
+
+    let pvManquants = myData.sante.vie.max - myData.sante.vie.value;
+    let pvSoignees = Math.min(pvManquants, Math.floor(pointsGuerison / 2 ));
+    guerisonData.list.push( pvSoignees + " Points de Vie soignés");
+    await this.santeIncDec('vie', +pvSoignees, false);
+    guerisonData.pointsConsommes = pointsGuerisonInitial - pointsGuerison;
+
+    return guerisonData;
+  } 
+
+  /* -------------------------------------------- */
+  async consommerPotionSoin(potionData) {
+    if (potionData.data.categorie.includes('Enchante')) {
+      potionData.pointsGuerison = RdDHerbes.calculePointsGuerison( potionData.data);
+      potionData.alias = this.name;
+      potionData.enchanteTexte = "enchantée";
+      potionData.isEnchante = true;
+      ChatMessage.create({
+        whisper: ChatUtility.getWhisperRecipientsAndGMs(game.user.name),
+        content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-consommer-potion-soin.html`, potionData )
+      });
+      // Gestion de la résistance:
+      let rolled = await RdDResolutionTable.roll(this.getReveActuel(), -8 );
+      if (!rolled.isSuccess) {
+        await this.reveActuelIncDec(-1);
+        potionData.guerisonData = await this.buildPotionGuerisonList(potionData.pointsGuerison);
+        potionData.guerisonMinutes = potionData.guerisonData.pointsConsommes * 5;
+        ChatMessage.create({
+          whisper: ChatUtility.getWhisperRecipientsAndGMs(game.user.name),
+          content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-appliquer-potion-soin.html`, potionData )
+        });  
+      } else {
+
+      }
+    } else {
+
+    }
+  }
+
+  /* -------------------------------------------- */
+  async consommerPotion( potion ) {
     const potionData = Misc.data(potion);
-    console.log("Potion consommée", potionData);
+
+    if ( potionData.data.categorie.includes('Soin')) {
+      this.consommerPotionSoin( potionData);
+    } else if( potion.data.categorie.includes('Repos')) {
+      //TODO
+    } else {
+      //TODO
+    }
   }
 
   /* -------------------------------------------- */
diff --git a/system.json b/system.json
index c2818c66..6de0bc62 100644
--- a/system.json
+++ b/system.json
@@ -2,11 +2,11 @@
   "name": "foundryvtt-reve-de-dragon",
   "title": "Rêve de Dragon",
   "description": "Rêve de Dragon RPG for FoundryVTT",
-  "version": "1.4.1",
+  "version": "1.4.2",
   "manifestPlusVersion": "1.0.0",
   "minimumCoreVersion": "0.7.5",
   "compatibleCoreVersion": "0.7.9",
-  "templateVersion": 111,
+  "templateVersion": 112,
   "author": "LeRatierBretonnien",
   "authors": [
     { 
diff --git a/template.json b/template.json
index b8f084e6..98fbdca9 100644
--- a/template.json
+++ b/template.json
@@ -726,6 +726,7 @@
     "herbe": "",
     "herbebrins": 0,
     "herbebonus": 0,
+    "reposalchimique": false,
     "pr": 0,
     "prpermanent": false,
     "prdate": 0,
diff --git a/templates/item-potion-sheet.html b/templates/item-potion-sheet.html
index 7164df1e..0a0d78cf 100644
--- a/templates/item-potion-sheet.html
+++ b/templates/item-potion-sheet.html
@@ -5,7 +5,7 @@
       <h1 class="charname"><input name="name" type="text" value="{{name}}" placeholder="Name" /></h1>
       {{#if isOwned}}
       <div class="form-group">
-        <span for="xp"><a class="consommer-potion chat-card-button" data-actor-id="{{actorId}}">Consommer cette potion</a></span>
+        <span for="xp"><a class="consommer-potion chat-card-button" data-actor-id="{{actorId}}">Consommer cette potion et appliquer ses effets</a></span>
       </div>
       {{/if}}  
     </div>
@@ -57,6 +57,26 @@
       <input class="attribute-value" type="text" name="data.herbebonus" value="{{data.herbebonus}}" data-dtype="Number" />
     </div>
     {{/if}}
+    {{#if isRepos}}
+    <div class="form-group">
+      <label>Herbe</label>
+      <select name="data.herbe" class="herbe" data-dtype="String">
+        {{selectOptions herbesRepos selected=data.herbe localize=false}}
+      </select>
+    </div>
+    <div class="form-group">
+      <label>Nombre de brins</label>
+      <input class="attribute-value" type="text" name="data.herbebrins" value="{{data.herbebrins}}" data-dtype="Number" />
+    </div>
+    <div class="form-group">
+      <label>Bonus</label>
+      <input class="attribute-value" type="text" name="data.herbebonus" value="{{data.herbebonus}}" data-dtype="Number" />
+    </div>
+    <div class="form-group">
+      <label>Repos Alchimique ?</label>
+      <input class="attribute-value" type="checkbox" name="data.reposalchimique"  {{#if data.reposalchimique}}checked{{/if}}/>
+    </div>
+    {{/if}}
     {{#if isEnchante}}
     <div class="form-group">
       <label>Points de rêve</label>