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.
This commit is contained in:
George Brocklehurst 2025-07-20 10:35:06 +01:00
parent 26d412815a
commit 343ee4978b
No known key found for this signature in database
GPG key ID: 0C643A97B51FFCFB

View file

@ -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);
}
}