From 81ae15a6a29b9c82944530db74de7127eb230c7e Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Sat, 8 Oct 2022 17:36:11 +0200 Subject: [PATCH] =?UTF-8?q?Simuler=20les=20lancers=20de=20d=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lorsqu'on force le résultat des dés, on force l'affichage d'un lancer donnant ce résultat avec DiceSoNice --- module/rdd-dice.js | 109 ++++++++++++++++++++++++--------- module/rdd-resolution-table.js | 6 +- module/rdd-roll.js | 1 + 3 files changed, 82 insertions(+), 34 deletions(-) diff --git a/module/rdd-dice.js b/module/rdd-dice.js index 9b2f8ded..007abf8e 100644 --- a/module/rdd-dice.js +++ b/module/rdd-dice.js @@ -132,18 +132,15 @@ export class RdDDice { } } - static async roll(formula, options = { showDice: SHOW_DICE, rollMode: undefined }) { - const roll = new Roll(formula); - await roll.evaluate({ async: true }); - if (options.showDice != HIDE_DICE) { - await this.showDiceSoNice(roll, options.rollMode ?? game.settings.get("core", "rollMode")); - } - return roll; + static async rollTotal(formula, options = { showDice: HIDE_DICE }) { + return (await RdDDice.roll(formula, options)).total; } - static async rollTotal(formula, options = { showDice: HIDE_DICE}) { - const roll = await RdDDice.roll(formula, options); - return roll.total; + static async roll(formula, options = { showDice: SHOW_DICE, rollMode: undefined }) { + const roll = new Roll(RdDDice._formulaOrFake(formula, options)); + await roll.evaluate({ async: true }); + await this.showDiceSoNice(roll, options); + return roll; } static async rollOneOf(array) { @@ -160,27 +157,79 @@ export class RdDDice { } /* -------------------------------------------- */ - static async showDiceSoNice(roll, rollMode) { - if (game.modules.get("dice-so-nice")?.active) { - if (game.dice3d) { - let whisper = null; - let blind = false; - rollMode = rollMode ?? game.settings.get("core", "rollMode"); - switch (rollMode) { - case "blindroll": //GM only - blind = true; - case "gmroll": //GM + rolling player - whisper = ChatUtility.getUsers(user => user.isGM); - break; - case "roll": //everybody - whisper = ChatUtility.getUsers(user => user.active); - break; - case "selfroll": - whisper = [game.user.id]; - break; - } - await game.dice3d.showForRoll(roll, game.user, true, whisper, blind); + static async showDiceSoNice(roll, options) { + if (options.showDice == HIDE_DICE || !game.modules.get("dice-so-nice")?.active || !game.dice3d) { + return; + } + + let { whisper, blind } = RdDDice._getWhisperBlind(options); + if (options.forceDiceResult?.total) { + let terms = await RdDDice._getForcedTerms(options); + if (terms) { + await game.dice3d.show({ throws: [{ dice: terms }] }) + return; } } + await game.dice3d.showForRoll(roll, game.user, true, whisper, blind); + } + + static _formulaOrFake(formula, options) { + if (options?.forceDiceResult?.total) { + options.forceDiceResult.formula = formula; + return options.forceDiceResult.total.toString() + } + return formula; + } + + static async _getForcedTerms(options) { + const total = options.forceDiceResult.total; + switch (options.forceDiceResult.formula) { + case '1d100': + return terms1d100(total); + case "2d10": + // TODO + } + return undefined; + + function terms1d100(total) { + const unites = total % 10; + const dizaines = Math.floor(total / 10); + return [{ + resultLabel: dizaines * 10, + d100Result: total, + result: dizaines, + type: "d100", + vectors: [], + options: {} + }, + { + resultLabel: unites, + d100Result: total, + result: unites, + type: "d10", + vectors: [], + options: {} + }]; + } + } + + static _getWhisperBlind(options) { + let whisper = null; + let blind = false; + let rollMode = options.rollMode ?? game.settings.get("core", "rollMode"); + switch (rollMode) { + case "blindroll": //GM only + blind = true; + case "gmroll": //GM + rolling player + whisper = ChatUtility.getUsers(user => user.isGM); + break; + case "roll": //everybody + whisper = ChatUtility.getUsers(user => user.active); + break; + case "selfroll": + whisper = [game.user.id]; + break; + } + return { whisper, blind }; } } \ No newline at end of file diff --git a/module/rdd-resolution-table.js b/module/rdd-resolution-table.js index 88389a9c..37eac10a 100644 --- a/module/rdd-resolution-table.js +++ b/module/rdd-resolution-table.js @@ -142,10 +142,8 @@ export class RdDResolutionTable { /* -------------------------------------------- */ static async rollChances(chances, diviseur, forceDiceResult = -1) { - if (forceDiceResult <= 0 || forceDiceResult > 100) { - forceDiceResult = -1; - } - chances.roll = await RdDDice.rollTotal((forceDiceResult == -1) ? "1d100" : `${forceDiceResult}`, chances); + chances.forceDiceResult = forceDiceResult <= 0 || forceDiceResult > 100 ? undefined : {total: forceDiceResult}; + chances.roll = await RdDDice.rollTotal( "1d100", chances); mergeObject(chances, this.computeReussite(chances, chances.roll, diviseur), { overwrite: true }); return chances; } diff --git a/module/rdd-roll.js b/module/rdd-roll.js index 063a9c5a..fece5d69 100644 --- a/module/rdd-roll.js +++ b/module/rdd-roll.js @@ -144,6 +144,7 @@ export class RdDRoll extends Dialog { /* -------------------------------------------- */ async onAction(action, html) { + this.rollData.forceDiceResult = Number.parseInt($('#force-dice-result').val()) ?? -1; await RdDResolutionTable.rollData(this.rollData); console.log("RdDRoll -=>", this.rollData, this.rollData.rolled); this.actor.setRollWindowsOpened(false);