From 40ba46fc6bb0bdbca6129a278608d7909abb4c3d Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Thu, 20 Feb 2025 00:29:02 -0700 Subject: [PATCH] Get the DicePool to support edge/drag modifications --- langs/en-ca.json | 2 + module/Apps/DicePool.mjs | 72 +++++++++++++++++++++++++++++-- templates/Apps/DicePool/drag.hbs | 28 ++++++++++++ templates/Apps/DicePool/edge.hbs | 28 ++++++++++++ templates/Apps/DicePool/style.css | 1 - 5 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 templates/Apps/DicePool/drag.hbs create mode 100644 templates/Apps/DicePool/edge.hbs diff --git a/langs/en-ca.json b/langs/en-ca.json index 61a77e6..134593a 100644 --- a/langs/en-ca.json +++ b/langs/en-ca.json @@ -64,7 +64,9 @@ "tough": "Tough", "hard": "Hard" }, + "drag": "Drag", "edit": "Edit", + "edge": "Edge", "empty": "---", "equipped": "Equipped", "fate": "Fate", diff --git a/module/Apps/DicePool.mjs b/module/Apps/DicePool.mjs index 13b0449..7418a3c 100644 --- a/module/Apps/DicePool.mjs +++ b/module/Apps/DicePool.mjs @@ -26,6 +26,8 @@ export class DicePool extends GenericAppMixin(HandlebarsApplicationMixin(Applica actions: { diceCountDelta: this.#diceCountDelta, targetDelta: this.#targetDelta, + edgeDelta: this.#edgeDelta, + dragDelta: this.#dragDelta, roll: this.#roll, }, }; @@ -37,6 +39,12 @@ export class DicePool extends GenericAppMixin(HandlebarsApplicationMixin(Applica target: { template: filePath(`templates/Apps/DicePool/target.hbs`), }, + drag: { + template: filePath(`templates/Apps/DicePool/drag.hbs`), + }, + edge: { + template: filePath(`templates/Apps/DicePool/edge.hbs`), + }, buttons: { template: filePath(`templates/Apps/DicePool/buttons.hbs`), }, @@ -46,15 +54,20 @@ export class DicePool extends GenericAppMixin(HandlebarsApplicationMixin(Applica // #region Instance Data _diceCount; _target; + _drag; + _edge; constructor({ diceCount = 1, target, + drag = 0, edge = 0, flavor = ``, ...opts } = {}) { super(opts); + this._drag = drag; + this._edge = edge; this._flavor = flavor; this._diceCount = diceCount; this._target = target ?? game.settings.get(`ripcrypt`, `dc`) ?? 1; @@ -74,11 +87,19 @@ export class DicePool extends GenericAppMixin(HandlebarsApplicationMixin(Applica switch (partId) { case `numberOfDice`: { - this._prepareNumberOfDice(ctx); + await this._prepareNumberOfDice(ctx); break; }; case `target`: { - this._prepareTarget(ctx); + await this._prepareTarget(ctx); + break; + }; + case `edge`: { + await this._prepareEdge(ctx); + break; + }; + case `drag`: { + await this._prepareDrag(ctx); break; }; case `buttons`: { @@ -92,7 +113,7 @@ export class DicePool extends GenericAppMixin(HandlebarsApplicationMixin(Applica async _prepareNumberOfDice(ctx) { ctx.numberOfDice = this._diceCount; - ctx.decrementDisabled = this._diceCount <= 0; + ctx.decrementDisabled = this._diceCount <= 1; }; async _prepareTarget(ctx) { @@ -100,6 +121,18 @@ export class DicePool extends GenericAppMixin(HandlebarsApplicationMixin(Applica ctx.incrementDisabled = this._target >= 8; ctx.decrementDisabled = this._target <= 1; }; + + async _prepareEdge(ctx) { + ctx.edge = this._edge; + ctx.incrementDisabled = false; + ctx.decrementDisabled = this._edge <= 0; + }; + + async _prepareDrag(ctx) { + ctx.drag = this._drag; + ctx.incrementDisabled = false; + ctx.decrementDisabled = this._drag <= 0; + }; // #endregion // #region Actions @@ -137,8 +170,39 @@ export class DicePool extends GenericAppMixin(HandlebarsApplicationMixin(Applica this.render({ parts: [`target`] }); }; + static async #edgeDelta(_event, element) { + const delta = parseInt(element.dataset.delta); + if (Number.isNaN(delta)) { + ui.notifications.error( + localizer(`RipCrypt.notifs.error.invalid-delta`, { name: `@RipCrypt.common.edge` }), + ); + return; + }; + + this._edge += delta; + this.render({ parts: [`edge`] }); + }; + + static async #dragDelta(_event, element) { + const delta = parseInt(element.dataset.delta); + if (Number.isNaN(delta)) { + ui.notifications.error( + localizer(`RipCrypt.notifs.error.invalid-delta`, { name: `@RipCrypt.common.drag` }), + ); + return; + }; + + this._drag += delta; + this.render({ parts: [`drag`] }); + }; + static async #roll() { - const formula = `${this._diceCount}d8rc${this._target}`; + let target = this._target; + target -= this._edge; + target += this._drag; + target = Math.max(target, 1); + + const formula = `${this._diceCount}d8rc${target}`; Logger.debug(`Attempting to roll formula: ${formula}`); let flavor = this._flavor; diff --git a/templates/Apps/DicePool/drag.hbs b/templates/Apps/DicePool/drag.hbs new file mode 100644 index 0000000..0fb4a0f --- /dev/null +++ b/templates/Apps/DicePool/drag.hbs @@ -0,0 +1,28 @@ +
+ +
+ {{ rc-i18n "RipCrypt.common.drag" }} +
+
+ + {{drag}} + +
+
+
diff --git a/templates/Apps/DicePool/edge.hbs b/templates/Apps/DicePool/edge.hbs new file mode 100644 index 0000000..9d6c885 --- /dev/null +++ b/templates/Apps/DicePool/edge.hbs @@ -0,0 +1,28 @@ +
+ +
+ {{ rc-i18n "RipCrypt.common.edge" }} +
+
+ + {{edge}} + +
+
+
diff --git a/templates/Apps/DicePool/style.css b/templates/Apps/DicePool/style.css index 5b65207..a7778c4 100644 --- a/templates/Apps/DicePool/style.css +++ b/templates/Apps/DicePool/style.css @@ -7,7 +7,6 @@ padding: 8px; gap: 8px; width: 250px; - height: 185px; } --button-background: var(--alt-row-background);