From 327f921b9cd0e8a1d4155d224eddf276e4b56a9c Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sat, 27 Apr 2024 02:07:22 -0600 Subject: [PATCH] Implement the DiceField and required changes to get it working (closes #179) --- module/documents/Actor/GenericActor.mjs | 2 +- module/models/Actor/Player.mjs | 12 +-- module/models/fields/DiceField.mjs | 83 ++++++++++++------- module/sheets/Actors/PC/PlayerSheetV2.mjs | 2 +- .../char-sheet/v2/partials/stats.v2.pc.hbs | 3 + 5 files changed, 62 insertions(+), 40 deletions(-) diff --git a/module/documents/Actor/GenericActor.mjs b/module/documents/Actor/GenericActor.mjs index 171af63..feb3825 100644 --- a/module/documents/Actor/GenericActor.mjs +++ b/module/documents/Actor/GenericActor.mjs @@ -6,7 +6,7 @@ export class DotDungeonActor extends Actor { provide all that data to AE's without needing to disable any inputs. */ prepareEmbeddedDocuments() { - this.preAE = foundry.utils.deepClone(this.system); + this.preAE = foundry.utils.duplicate(this.system); super.prepareEmbeddedDocuments(); }; diff --git a/module/models/Actor/Player.mjs b/module/models/Actor/Player.mjs index 84af0a6..f7b5481 100644 --- a/module/models/Actor/Player.mjs +++ b/module/models/Actor/Player.mjs @@ -39,12 +39,12 @@ export class PlayerData extends foundry.abstract.TypeDataModel { integer: true, }), stats: new fields.SchemaField({ - build: diceChoiceField(), //new DiceField(), - meta: diceChoiceField(), - presence: diceChoiceField(), - hands: diceChoiceField(), - tilt: diceChoiceField(), - rng: diceChoiceField(), + build: new DiceField(), + meta: new DiceField(), + presence: new DiceField(), + hands: new DiceField(), + tilt: new DiceField(), + rng: new DiceField(), }), skills: new fields.SchemaField({ build: new fields.SchemaField({ diff --git a/module/models/fields/DiceField.mjs b/module/models/fields/DiceField.mjs index 7534101..e147fb2 100644 --- a/module/models/fields/DiceField.mjs +++ b/module/models/fields/DiceField.mjs @@ -1,10 +1,10 @@ import { statDice } from "../../config.mjs"; /** - * A subclass of StringField that allows ActiveEffects to integrate with dice + * A subclass of DataField that allows ActiveEffects to integrate with dice * values and increase/decrease the value step-wise according to the dice ladder. */ -export class DiceField extends foundry.data.fields.StringField { +export class DiceField extends foundry.data.fields.DataField { static get _defaults() { return foundry.utils.mergeObject(super._defaults, { trim: true, @@ -19,51 +19,70 @@ export class DiceField extends foundry.data.fields.StringField { // v- because for some reason Foundry doesn't respect the _defaults getter this.blank = true; - console.log(this.choices) }; - _castChangeDelta(delta) { - console.log(`DiceField._castChangeDelta(${delta})`) - return parseInt(delta) ?? 0; - }; + _cast(value) { return value }; + _castChangeDelta(delta) { return delta }; - /** @inheritdoc */ - _applyChangeAdd(value, delta, model, change) { - console.warn(`Cannot apply Add ActiveEffects to DiceFields. Not changing value.`); + /** + * @param {string} value The current value + * @param {string} delta The AE value + * @param {unknown} model + * @param {unknown} changes + */ + _applyChangeAdd(value, delta, model, changes) { + if (value === "") return value; + delta = parseInt(delta); + const dieIndex = statDice.findIndex(die => die === value); + const newIndex = Math.min(Math.max(0, dieIndex + delta), statDice.length - 1); + value = statDice[newIndex]; return value; }; - _applyChangeMultiply(value, delta, model, change) { - console.warn(`Cannot apply Multiply ActiveEffects to DiceFields. Not changing value.`); + /** + * @param {string} value The current value + * @param {string} delta The AE value + * @param {unknown} model + * @param {unknown} changes + */ + _applyChangeMultiply(value, delta, model, changes) { + console.warn(`.dungeon | Cannot apply Multiply ActiveEffects to DiceFields. Not changing value.`); return value; }; - _applyChangeOverride(value, delta, model, change) { + /** + * @param {string} value The current value + * @param {string} delta The AE value + * @param {unknown} model + * @param {unknown} changes + */ + _applyChangeOverride(value, delta, model, changes) { return delta; }; - _applyChangeUpgrade(value, delta, model, change) { - console.log(`.dungeon | Pre: value=${value}; delta=${delta}`); + /** + * @param {string} value The current value + * @param {string} delta The AE value + * @param {unknown} model + * @param {unknown} changes + */ + _applyChangeUpgrade(value, delta, model, changes) { if (value === "") return value; - const dieIndex = statDice.findIndex(value); - const newIndex = Math.min(Math.max(0, dieIndex - delta), statDice.length - 1); - value = statDice[newIndex]; - console.log(`.dungeon | Post: value=${value}; delta=${delta}`); - return value; + const currentIndex = statDice.findIndex(die => die === value); + const upgradedIndex = statDice.findIndex(die => die === delta); + return statDice[Math.max(currentIndex, upgradedIndex)]; }; - _applyChangeDowngrade(value, delta, model, change) { - console.log(`.dungeon | Pre: value=${value}; delta=${delta}`); + /** + * @param {string} value The current value + * @param {string} delta The AE value + * @param {unknown} model + * @param {unknown} changes + */ + _applyChangeDowngrade(value, delta, model, changes) { if (value === "") return value; - const dieIndex = statDice.findIndex(value); - const newIndex = Math.min(Math.max(0, dieIndex + delta), statDice.length - 1); - value = statDice[newIndex]; - console.log(`.dungeon | Post: value=${value}; delta=${delta}`); - return value + const currentIndex = statDice.findIndex(die => die === value); + const upgradedIndex = statDice.findIndex(die => die === delta); + return statDice[Math.min(currentIndex, upgradedIndex)]; }; - - _applyChangeCustom(...args) { - console.log(`.dungeon | Dicefield._applyChangeCustom`) - return super._applyChangeCustom(...args); - } }; diff --git a/module/sheets/Actors/PC/PlayerSheetV2.mjs b/module/sheets/Actors/PC/PlayerSheetV2.mjs index ac1a378..466e6c5 100644 --- a/module/sheets/Actors/PC/PlayerSheetV2.mjs +++ b/module/sheets/Actors/PC/PlayerSheetV2.mjs @@ -92,7 +92,7 @@ export class PlayerSheetv2 extends GenericActorSheet { get #statData() { const stats = []; - const usedDice = new Set(Object.values(this.actor.system.stats)); + const usedDice = new Set(Object.values(this.actor.preAE.stats)); for (const statName in this.actor.system.stats) { const stat = { key: statName, diff --git a/templates/actors/char-sheet/v2/partials/stats.v2.pc.hbs b/templates/actors/char-sheet/v2/partials/stats.v2.pc.hbs index 6353120..59e61ca 100644 --- a/templates/actors/char-sheet/v2/partials/stats.v2.pc.hbs +++ b/templates/actors/char-sheet/v2/partials/stats.v2.pc.hbs @@ -17,6 +17,9 @@ data-roll-label="Stat Roll : {{stat.name}}" > Roll + {{#if stat.value}} + 1{{stat.value}} + {{/if}} {{#if stat.skills}}