From bfffd09f0f1cf9d2a7fffef8ddd99b3e5bb77a70 Mon Sep 17 00:00:00 2001
From: Vincent Vandemeulebrouck <vincent.vandemeulebrouck@itiviti.com>
Date: Thu, 31 Dec 2020 11:53:41 +0100
Subject: [PATCH] Ajout commande /ddr

---
 module/actor.js         |  3 +-
 module/de-draconique.js | 27 ++++++++++++++++
 module/rdd-commands.js  | 71 ++++++++++++++++++++++++++---------------
 module/rdd-dice.js      | 10 +-----
 module/tmr-utility.js   |  8 ++---
 5 files changed, 80 insertions(+), 39 deletions(-)
 create mode 100644 module/de-draconique.js

diff --git a/module/actor.js b/module/actor.js
index d7a87e57..5d0a7414 100644
--- a/module/actor.js
+++ b/module/actor.js
@@ -13,6 +13,7 @@ import { RdDItemSort } from "./item-sort.js";
 import { Grammar } from "./grammar.js";
 import { RdDEncaisser } from "./rdd-roll-encaisser.js";
 import { RdDCombat } from "./rdd-combat.js";
+import { DeDraconique } from "./de-draconique.js";
 
 /* -------------------------------------------- */
 /**
@@ -462,7 +463,7 @@ export class RdDActor extends Actor {
       message.content += "<br>Vous avez suffisament rêvé (seuil " + seuil + ", rêve actuel "+reveActuel+")";
     }
     else {
-      let deRecuperation = await RdDDice.deDraconique();
+      let deRecuperation = await DeDraconique.ddr("selfroll").total;
       console.log("recuperationReve", deRecuperation);
       if (deRecuperation>=7)
       {
diff --git a/module/de-draconique.js b/module/de-draconique.js
new file mode 100644
index 00000000..abfa4d45
--- /dev/null
+++ b/module/de-draconique.js
@@ -0,0 +1,27 @@
+import { RdDDice } from "./rdd-dice.js";
+
+export class DeDraconique extends Roll{
+
+  static async ddr(rollMode=undefined) {
+    let ddr = new DeDraconique().evaluate();
+    await RdDDice.show(ddr, rollMode);
+    return ddr;
+  }
+
+  constructor(){
+    super("1d8x8 - 0")
+  }
+
+  evaluate() {
+    super.evaluate();
+    const rerolls = Math.ceil(this.total / 8);
+    this.terms[this.terms.length - 1] = rerolls;
+    this.results[this.results.length - 1] = rerolls;
+    this._total -= rerolls;
+    return this;
+  }
+
+  async render(chatOptions) {
+    return super.render(chatOptions)
+  }
+}
\ No newline at end of file
diff --git a/module/rdd-commands.js b/module/rdd-commands.js
index 409ae4ee..f85ac974 100644
--- a/module/rdd-commands.js
+++ b/module/rdd-commands.js
@@ -1,7 +1,9 @@
 /* -------------------------------------------- */
 
 import { ChatUtility } from "./chat-utility.js";
+import { DeDraconique } from "./de-draconique.js";
 import { Misc } from "./misc.js";
+import { RdDDice } from "./rdd-dice.js";
 import { RdDResolutionTable } from "./rdd-resolution-table.js";
 import { RdDRollResolution } from "./rdd-roll-resolution.js";
 import { RdDRollTables } from "./rdd-rolltables.js";
@@ -16,57 +18,67 @@ export class RdDCommands {
   static init() {
     if (!game.system.rdd.commands) {
       const rddCommands = new RdDCommands();
-      rddCommands.registerCommand({ path: ["/aide"], descr: "Affiche l'aide pour toutes les commandes", func: (content, msg, params) => rddCommands.help(msg) });
-      rddCommands.registerCommand({ path: ["/help"], descr: "Affiche l'aide pour toutes les commandes", func: (content, msg, params) => rddCommands.help(msg) });
-      rddCommands.registerCommand({ path: ["/table", "queues"], descr: "Tire une Queue de Dragon", func: (content, msg, params) => RdDRollTables.getQueue() });
-      rddCommands.registerCommand({ path: ["/table", "ombre"], descr: "Tire une Ombre de Dragon", func: (content, msg, params) => RdDRollTables.getOmbre() });
-      rddCommands.registerCommand({ path: ["/table", "tetehr"], descr: "Tire une Tête de Dragon pour Hauts Revants", func: (content, msg, params) => RdDRollTables.getTeteHR() });
-      rddCommands.registerCommand({ path: ["/table", "tete"], descr: "Tire une Tête de Dragon", func: (content, msg, params) => RdDRollTables.getTete() });
-      rddCommands.registerCommand({ path: ["/table", "souffle"], descr: " Tire un Souffle de Dragon", func: (content, msg, params) => RdDRollTables.getSouffle() });
-      rddCommands.registerCommand({ path: ["/table", "tarot"], descr: "Tire une carte du Tarot Draconique", func: (content, msg, params) => RdDRollTables.getTarot() });
-      rddCommands.registerCommand({ path: ["/table", "tmr"], descr: "Tire une case aléatoire des Terre médiane", func: (content, msg, params) => TMRUtility.getTMRAleatoire() });
+      rddCommands.registerCommand({ path: ["/aide"], func: (content, msg, params) => rddCommands.help(msg), descr: "Affiche l'aide pour toutes les commandes" });
+      rddCommands.registerCommand({ path: ["/help"], func: (content, msg, params) => rddCommands.help(msg), descr: "Affiche l'aide pour toutes les commandes" });
+      rddCommands.registerCommand({ path: ["/table", "queues"], func: (content, msg, params) => RdDRollTables.getQueue(), descr: "Tire une Queue de Dragon" });
+      rddCommands.registerCommand({ path: ["/table", "ombre"], func: (content, msg, params) => RdDRollTables.getOmbre(), descr: "Tire une Ombre de Dragon" });
+      rddCommands.registerCommand({ path: ["/table", "tetehr"], func: (content, msg, params) => RdDRollTables.getTeteHR(), descr: "Tire une Tête de Dragon pour Hauts Revants" });
+      rddCommands.registerCommand({ path: ["/table", "tete"], func: (content, msg, params) => RdDRollTables.getTete(), descr: "Tire une Tête de Dragon" });
+      rddCommands.registerCommand({ path: ["/table", "souffle"], func: (content, msg, params) => RdDRollTables.getSouffle(), descr: " Tire un Souffle de Dragon" });
+      rddCommands.registerCommand({ path: ["/table", "tarot"], func: (content, msg, params) => RdDRollTables.getTarot(), descr: "Tire une carte du Tarot Draconique" });
+      rddCommands.registerCommand({ path: ["/table", "tmr"], func: (content, msg, params) => TMRUtility.getTMRAleatoire(), descr: "Tire une case aléatoire des Terre médiane" });
 
-      rddCommands.registerCommand({ path: ["/tmra"], descr: "Tire une case aléatoire des Terre médiane", func: (content, msg, params) => TMRUtility.getTMRAleatoire() });
-      rddCommands.registerCommand({ path: ["/tmrr"], descr: "Syntaxe: <strong>/tmrr case jet</strong><br>Détermine quelle est la rencontre dans la case pour le jet<br>Example: <strong>/tmrr forêt 50</strong>", func: (content, msg, params) => rddCommands.getRencontreTMR(params) });
+      rddCommands.registerCommand({ path: ["/tmra"], func: (content, msg, params) => TMRUtility.getTMRAleatoire(), descr: "Tire une case aléatoire des Terre médiane" });
       rddCommands.registerCommand({
-        path: ["/rdd"], descr: `Effectue un jet de dés dans la table de résolution. Examples:
+        path: ["/tmrr"], func: (content, msg, params) => rddCommands.getRencontreTMR(params),
+        descr: "Syntaxe: <strong>/tmrr case jet</strong><br>Détermine quelle est la rencontre dans la case pour le jet<br>Example: <strong>/tmrr forêt 50</strong>"
+      });
+
+      rddCommands.registerCommand({
+        path: ["/rdd"], func: (content, msg, params) => rddCommands.rollRdd(msg, params),
+        descr: `Effectue un jet de dés dans la table de résolution. Examples:
       <br><strong>/rdd</strong> ouvre la table de résolution
       <br><strong>/rdd 10 3</strong> effectue un jet 10 à +3
       <br><strong>/rdd 10 +2</strong> effectue un jet 10 à +2
       <br><strong>/rdd 15 -2</strong> effectue un jet 15 à -2
       <br><strong>/rdd 15 0 s</strong> effectue un jet 15 à 0, avec significative requise
-      `, func: (content, msg, params) => rddCommands.rollRdd(msg, params)
+      `
       });
+      rddCommands.registerCommand({ path: ["/ddr"], func: (content, msg, params) => rddCommands.rollDeDraconique(msg), descr: "Lance un Dé Draconique" });
 
-      rddCommands.registerCommand({ path: ["/payer"], descr: `Permet de payer un montant. Exemples:
+      rddCommands.registerCommand({
+        path: ["/payer"], func: (content, msg, params) => RdDUtility.afficherDemandePayer(params[0], params[1]),
+        descr: `Permet de payer un montant. Exemples:
       <br><strong>/payer 5s 10d</strong> permet d'envoyer un message pour payer 5 sols et 10 deniers
       <br><strong>/payer 10d</strong> permet d'envoyer un message pour payer 10 deniers
-      `, func: (content, msg, params) => RdDUtility.afficherDemandePayer(params[0], params[1])});
+      `
+      });
       game.system.rdd.commands = rddCommands;
     }
   }
-
   constructor() {
     this.commandsTable = {};
   }
 
   registerCommand(command) {
-    this._addCommand(this.commandsTable, command.path, command);
+    this._addCommand(this.commandsTable, command.path, '', command);
   }
 
-  _addCommand(targetTable, path, command) {
+  _addCommand(targetTable, path, fullPath, command) {
     if (!this._validateCommand(targetTable, path, command)) {
       return;
     }
     const term = path[0];
+    fullPath = fullPath+term+' '
     if (path.length == 1) {
+      command.descr = `<strong>${fullPath}</strong>: ${command.descr}`;
       targetTable[term] = command;
     }
     else {
       if (!targetTable[term]) {
         targetTable[term] = { subTable: {} };
       }
-      this._addCommand(targetTable[term].subTable, path.slice(1), command)
+      this._addCommand(targetTable[term].subTable, path.slice(1), fullPath, command)
     }
   }
 
@@ -122,24 +134,25 @@ export class RdDCommands {
   /* -------------------------------------------- */
   help(msg, table = undefined) {
     let list = []
-    this._buildSubTableHelp(list, '', table || this.commandsTable);
+    this._buildSubTableHelp(list, table || this.commandsTable);
+    const messageAide = list.reduce((a, b) => a + '</li><li class="list-item">' + b);
     msg.whisper = [game.user._id];
-    msg.content = 'Commandes disponibles<ul class="alterne-list"><li class="list-item">' + list.reduce((a, b) => a + '</li><li class="list-item">' + b) + '</li></ul>';
+    msg.content = `Commandes disponibles<ul class="alterne-list"><li class="list-item">${messageAide}</li></ul>`;
     ChatMessage.create(msg);
   }
 
   /* -------------------------------------------- */
-  _buildSubTableHelp(list, path, table) {
+  _buildSubTableHelp(list, table) {
     for (let [name, command] of Object.entries(table)) {
       if (command) {
         if (command.subTable) {
-          this._buildSubTableHelp(list, path + name + " ", command.subTable);
+          this._buildSubTableHelp(list, command.subTable);
         } else {
-          list.push(`<strong>${path}${name}</strong>: ${command.descr}`);
+          list.push(command.descr);
         }
       }
     }
-    return list;
+    return list.sort();
   }
 
 
@@ -181,5 +194,13 @@ export class RdDCommands {
     msg.content = await RdDResolutionTable.explainRollDataV2(rollData);
     ChatUtility.chatWithRollMode(msg, game.user.name);
   }
+
+  async rollDeDraconique(msg) {
+    let rollMode = game.settings.get("core", "rollMode");
+    let ddr = new DeDraconique().evaluate();
+    await RdDDice.show(ddr, rollMode);
+    msg.content = `Lancer d'un Dé draconique: ${ddr.total}`;
+    ChatUtility.createChatMessage(msg, rollMode, game.user.name);
+  }
 }
 
diff --git a/module/rdd-dice.js b/module/rdd-dice.js
index 7a08ee2d..e98e7241 100644
--- a/module/rdd-dice.js
+++ b/module/rdd-dice.js
@@ -2,14 +2,6 @@ import { ChatUtility } from "./chat-utility.js";
 
 export class RdDDice {
 
-  /* -------------------------------------------- */
-  static async deDraconique(rollMode="selfroll") {
-    let roll = new Roll("1d8x8").evaluate();
-    await this.show(roll, rollMode);
-    return roll.total - Math.ceil(roll.total / 8);
-  }
-
-
   /* -------------------------------------------- */
   static async show(roll, rollMode = undefined) {
     if (roll.showDice || game.settings.get("foundryvtt-reve-de-dragon", "dice-so-nice") == true) {
@@ -23,7 +15,7 @@ export class RdDDice {
     if (game.modules.get("dice-so-nice") && game.modules.get("dice-so-nice").active) {
       let whisper = null;
       let blind = false;
-      rollMode = rollMode == undefined ? game.settings.get("core", "rollMode") : rollMode;
+      rollMode = rollMode || game.settings.get("core", "rollMode");
       switch (rollMode) {
         case "blindroll": //GM only
           blind = true;
diff --git a/module/tmr-utility.js b/module/tmr-utility.js
index 1573392e..22778d15 100644
--- a/module/tmr-utility.js
+++ b/module/tmr-utility.js
@@ -1,4 +1,4 @@
-import { RdDDice } from "./rdd-dice.js";
+import { DeDraconique } from "./de-draconique.js";
 
 /* -------------------------------------------- */
 const TMRMapping = { 
@@ -439,9 +439,9 @@ export class TMRUtility  {
   /* -------------------------------------------- */
   static async evaluerForceRencontre(rencontre) {
     if (this.isReveDeDragon(rencontre)) {
-      let ddr = await RdDDice.deDraconique();
-      return ddr + 7;
-    }
+      let ddr = await DeDraconique.ddr("selfroll");
+      return ddr.total + 7;
+    } 
     else {
       const roll = new Roll(rencontre.data.force).evaluate();
       return roll.total;