From 343ee4978bcd19794e2be221813b88edd1aab9e1 Mon Sep 17 00:00:00 2001 From: George Brocklehurst Date: Sun, 20 Jul 2025 10:35:06 +0100 Subject: [PATCH] Extract method for repeated calculation. In the downtime dialog, we need to calculate the number of selected moves in several places. This commit extracts a method to handle that, which reduces repetition and hopefully makes the code more readable. --- module/applications/dialogs/downtime.mjs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/module/applications/dialogs/downtime.mjs b/module/applications/dialogs/downtime.mjs index d046146a..163af03e 100644 --- a/module/applications/dialogs/downtime.mjs +++ b/module/applications/dialogs/downtime.mjs @@ -64,14 +64,8 @@ export default class DhpDowntime extends HandlebarsApplicationMixin(ApplicationV context.selectedActivity = this.selectedActivity; context.moveData = this.moveData; - const shortRestMovesSelected = Object.values(this.moveData.shortRest.moves).reduce( - (acc, x) => acc + (x.selected ?? 0), - 0 - ); - const longRestMovesSelected = Object.values(this.moveData.longRest.moves).reduce( - (acc, x) => acc + (x.selected ?? 0), - 0 - ); + const shortRestMovesSelected = this.#nrSelectedMoves('shortRest'); + const longRestMovesSelected = this.#nrSelectedMoves('longRest'); context.nrChoices = { ...this.nrChoices, shortRest: { @@ -95,7 +89,7 @@ export default class DhpDowntime extends HandlebarsApplicationMixin(ApplicationV static selectMove(_, target) { const { category, move } = target.dataset; - const nrSelected = Object.values(this.moveData[category].moves).reduce((acc, x) => acc + (x.selected ?? 0), 0); + const nrSelected = this.#nrSelectedMoves(category); if (nrSelected + this.nrChoices[category].taken >= this.nrChoices[category].max) { ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.noMoreMoves')); @@ -181,4 +175,8 @@ export default class DhpDowntime extends HandlebarsApplicationMixin(ApplicationV this.customActivity = foundry.utils.mergeObject(this.customActivity, formData.object); this.render(); } + + #nrSelectedMoves(category) { + return Object.values(this.moveData[category].moves).reduce((acc, x) => acc + (x.selected ?? 0), 0); + } }