From c2a619cd32255ff072e527e3bb01908e628d3dd7 Mon Sep 17 00:00:00 2001 From: Zhell Date: Fri, 6 Dec 2024 22:30:24 +0100 Subject: [PATCH] Track advantage modes in new number field class --- module/data/fields/_module.mjs | 3 +- module/data/fields/advantage-mode-field.mjs | 59 +++++++++++++++++++++ module/data/shared/roll-config-field.mjs | 4 +- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 module/data/fields/advantage-mode-field.mjs diff --git a/module/data/fields/_module.mjs b/module/data/fields/_module.mjs index 84c717be47..16bde5ba18 100644 --- a/module/data/fields/_module.mjs +++ b/module/data/fields/_module.mjs @@ -1,6 +1,7 @@ export * from "./activities-field.mjs"; -export {default as AdvancementField} from "./advancement-field.mjs"; export {default as AdvancementDataField} from "./advancement-data-field.mjs"; +export {default as AdvancementField} from "./advancement-field.mjs"; +export {default as AdvantageModeField} from "./advantage-mode-field.mjs"; export {default as FormulaField} from "./formula-field.mjs"; export {default as IdentifierField} from "./identifier-field.mjs"; export {default as LocalDocumentField} from "./local-document-field.mjs"; diff --git a/module/data/fields/advantage-mode-field.mjs b/module/data/fields/advantage-mode-field.mjs new file mode 100644 index 0000000000..d06016dcff --- /dev/null +++ b/module/data/fields/advantage-mode-field.mjs @@ -0,0 +1,59 @@ +/** + * Subclass of NumberField that tracks the number of changes made to a roll mode. + */ +export default class AdvantageModeField extends foundry.data.fields.NumberField { + /** @inheritDoc */ + static get _defaults() { + return foundry.utils.mergeObject(super._defaults, { + choices: [-1, 0, 1], + initial: 0, + label: "DND5E.AdvantageMode" + }); + } + + /* -------------------------------------------- */ + + /** + * Number of advantage modifications. + * @type {number} + */ + #advantage; + + /* -------------------------------------------- */ + + /** + * Number of disadvantage modifications. + * @type {number} + */ + #disadvantage; + + /* -------------------------------------------- */ + + /** @inheritDoc */ + initialize(value, model, options={}) { + this.#advantage = Number(value === 1); + this.#disadvantage = Number(value === -1); + return value; + } + + /* -------------------------------------------- */ + /* Active Effect Integration */ + /* -------------------------------------------- */ + + /** @override */ + applyChange(value, model, change) { + const delta = this._castChangeDelta(change.value); + if ( change.mode === CONST.ACTIVE_EFFECT_MODES.CUSTOM ) { + return this._applyChangeCustom(value, delta, model, change); + } + switch (delta) { + case 1: + this.#advantage++; + break; + case -1: + this.#disadvantage++; + break; + } + return Math.sign(this.#advantage) - Math.sign(this.#disadvantage); + } +} diff --git a/module/data/shared/roll-config-field.mjs b/module/data/shared/roll-config-field.mjs index c292a99b34..6d9a699c99 100644 --- a/module/data/shared/roll-config-field.mjs +++ b/module/data/shared/roll-config-field.mjs @@ -1,3 +1,5 @@ +import AdvantageModeField from "../fields/advantage-mode-field.mjs"; + const { StringField, NumberField, SchemaField } = foundry.data.fields; /** @@ -20,7 +22,7 @@ export default class RollConfigField extends foundry.data.fields.SchemaField { roll: new SchemaField({ min: new NumberField({...opts, label: "DND5E.ROLL.Range.Minimum"}), max: new NumberField({...opts, label: "DND5E.ROLL.Range.Maximum"}), - mode: new NumberField({choices: [-1, 0, 1], initial: 0, label: "DND5E.AdvantageMode"}), + mode: new AdvantageModeField(), ...roll }), ...fields