Fixed parsing of resource.max

This commit is contained in:
WBHarry 2025-07-13 16:34:55 +02:00
parent 9fc1005caa
commit de4267ff3c
6 changed files with 36 additions and 51 deletions

View file

@ -500,9 +500,8 @@ export default class CharacterSheet extends DHBaseActorSheet {
const item = this.getItem(event.currentTarget);
if (!item) return;
const value = item.system.resource.max
? Math.min(Number(event.currentTarget.value), item.system.resource.max)
: event.currentTarget.value;
const max = item.system.resource.max ? Roll.replaceFormulaData(item.system.resource.max, this.document) : null;
const value = max ? Math.min(Number(event.currentTarget.value), max) : event.currentTarget.value;
await item.update({ 'system.resource.value': value });
this.render();
}

View file

@ -44,7 +44,7 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
initial: CONFIG.DH.ITEM.itemResourceTypes.simple
}),
value: new fields.NumberField({ integer: true, min: 0, initial: 0 }),
max: new fields.NumberField({ nullable: true, initial: null }),
max: new fields.StringField({ nullable: true, initial: null }),
icon: new fields.StringField(),
recovery: new fields.StringField({
choices: CONFIG.DH.GENERAL.refreshTypes,
@ -94,25 +94,25 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
async _preCreate(data, options, user) {
// Skip if no initial action is required or actions already exist
if (!this.metadata.hasInitialAction || !foundry.utils.isEmpty(this.actions)) return;
if (this.metadata.hasInitialAction && foundry.utils.isEmpty(this.actions)) {
const metadataType = this.metadata.type;
const actionType = { weapon: 'attack' }[metadataType];
const ActionClass = game.system.api.models.actions.actionsTypes[actionType];
const metadataType = this.metadata.type;
const actionType = { weapon: 'attack' }[metadataType];
const ActionClass = game.system.api.models.actions.actionsTypes[actionType];
const action = new ActionClass(
{
_id: foundry.utils.randomID(),
type: actionType,
name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name),
...ActionClass.getSourceConfig(this.parent)
},
{
parent: this.parent
}
);
const action = new ActionClass(
{
_id: foundry.utils.randomID(),
type: actionType,
name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name),
...ActionClass.getSourceConfig(this.parent)
},
{
parent: this.parent
}
);
this.updateSource({ actions: [action] });
this.updateSource({ actions: [action] });
}
}
_onCreate(data) {
@ -132,23 +132,6 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
);
}
async _preUpdate(data) {
if (data.system?.resource?.max) {
const diceStatesKeys = Object.keys(this.resource.diceStates);
const resourceDiff = Math.abs(data.system.resource.max - diceStatesKeys.length);
if (!resourceDiff) return;
const diceStates = {};
const deleting = data.system.resource.max < diceStatesKeys.length;
[...Array(resourceDiff).keys()].forEach(nr => {
const key = deleting ? diceStatesKeys.length - 1 - nr : diceStatesKeys.length + nr;
diceStates[`${deleting ? '-=' : ''}${key}`] = deleting ? null : { value: null };
});
foundry.utils.setProperty(data, 'system.resource.diceStates', diceStates);
}
}
async _preDelete() {
if (!this.actor || this.actor.type !== 'character') return;

View file

@ -6,7 +6,7 @@ export default class RegisterHandlebarsHelpers {
times: this.times,
damageFormula: this.damageFormula,
damageSymbols: this.damageSymbols,
tertiary: this.tertiary
rollParsed: this.rollParsed
});
}
@ -42,7 +42,7 @@ export default class RegisterHandlebarsHelpers {
return new Handlebars.SafeString(Array.from(symbols).map(symbol => `<i class="fa-solid ${symbol}"></i>`));
}
static tertiary(a, b) {
return a ?? b;
static rollParsed(value, actor) {
return Roll.replaceFormulaData(value, actor);
}
}

View file

@ -216,6 +216,7 @@
filter: drop-shadow(0 0 1px @golden);
z-index: 2;
font-size: 18px;
cursor: pointer;
}
img {

View file

@ -5,15 +5,17 @@
</div>
{{else}}
<div class="item-resources">
{{#each item.system.resource.diceStates as | state key |}}
<a class="item-resource" data-action="toggleResourceDice" data-dice="{{key}}">
<div class="item-dice-resource">
<label>{{tertiary state.value '?'}}</label>
<img src="{{concat "systems/daggerheart/assets/icons/dice/hope/d" ../item.system.resource.dieFaces ".svg"}}" />
{{#if state.used}}<i class="fa-solid fa-x"></i>{{/if}}
</div>
</a>
{{/each}}
{{#times (rollParsed item.system.resource.max item.parent)}}
{{#with (ifThen (lookup ../item.system.resource.diceStates this) (lookup ../item.system.resource.diceStates this) this) as | state |}}
<a class="item-resource" data-action="toggleResourceDice" data-dice="{{#if ../../this}}{{../this}}{{else}}{{state}}{{/if}}">
<div class="item-dice-resource">
<label>{{ifThen state.value state.value '?'}}</label>
<img src="{{concat "systems/daggerheart/assets/icons/dice/hope/d" (ifThen ../../item.system.resource.dieFaces ../../item.system.resource.dieFaces ../item.system.resource.dieFaces) ".svg"}}" />
{{#if state.used}}<i class="fa-solid fa-x"></i>{{/if}}
</div>
</a>
{{/with}}
{{/times}}
<a data-action="handleResourceDice" data-tooltip="DAGGERHEART.APPLICATIONS.ResourceDice.rerollDice"><i class="fa-solid fa-dice resource-edit"></i></a>
</div>
{{/if}}

View file

@ -6,7 +6,7 @@
<div class="tooltip-tag-label">{{localize feature.name}}</div>
{{#if feature.img}}<img class="tooltip-tag-image" src="{{feature.img}}" />{{/if}}
</div>
<div class="tooltip-tag-description">{{{localize (tertiary feature.description feature.system.description)}}}</div>
<div class="tooltip-tag-description">{{{localize (ifThen feature.description feature.description feature.system.description)}}}</div>
</div>
{{/each}}
</div>