Feature: add methods for generate tags and labels for documents and actions (#499)

* FEAT: getTags and getLabels for weapons items

* FEAT: add _gettags and _getLabels to armor, domainCard, weapons and ActiveEffect

* define tags for actions

---------

Co-authored-by: Joaquin Pereyra <joaquinpereyra98@users.noreply.github.com>
This commit is contained in:
joaquinpereyra98 2025-07-31 23:33:26 -03:00 committed by GitHub
parent 9d025bf105
commit a27ee1578e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 237 additions and 165 deletions

View file

@ -345,4 +345,17 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
}); });
} }
} }
/**
* Generates a list of localized tags for this action.
* @returns {string[]} An array of localized tag strings.
*/
_getTags() {
const tags = [
game.i18n.localize(`DAGGERHEART.ACTIONS.TYPES.${this.type}.name`),
game.i18n.localize(`DAGGERHEART.CONFIG.ActionType.${this.actionType}`)
];
return tags;
}
} }

View file

@ -117,4 +117,26 @@ export default class DHArmor extends AttachableItem {
} }
} }
} }
/**
* Generates a list of localized tags based on this item's type-specific properties.
* @returns {string[]} An array of localized tag strings.
*/
_getTags() {
const tags = [
`${game.i18n.localize('DAGGERHEART.ITEMS.Armor.baseScore')}: ${this.baseScore}`,
`${game.i18n.localize('DAGGERHEART.ITEMS.Armor.baseThresholds.base')}: ${this.baseThresholds.major} / ${this.baseThresholds.severe}`
];
return tags;
}
/**
* Generate a localized label array for this item subtype.
* @returns {(string | { value: string, icons: string[] })[]} An array of localized strings and damage label objects.
*/
_getLabels() {
const labels = [`${game.i18n.localize('DAGGERHEART.ITEMS.Armor.baseScore')}: ${this.baseScore}`];
return labels;
}
} }

View file

@ -1,5 +1,4 @@
import BaseDataItem from './base.mjs'; import BaseDataItem from './base.mjs';
import { ActionField } from '../fields/actionField.mjs';
export default class DHDomainCard extends BaseDataItem { export default class DHDomainCard extends BaseDataItem {
/** @inheritDoc */ /** @inheritDoc */
@ -34,6 +33,7 @@ export default class DHDomainCard extends BaseDataItem {
}; };
} }
/**@inheritdoc */
async _preCreate(data, options, user) { async _preCreate(data, options, user) {
const allowed = await super._preCreate(data, options, user); const allowed = await super._preCreate(data, options, user);
if (allowed === false) return; if (allowed === false) return;
@ -55,4 +55,35 @@ export default class DHDomainCard extends BaseDataItem {
} }
} }
} }
/**
* Generates a list of localized tags based on this item's type-specific properties.
* @returns {string[]} An array of localized tag strings.
*/
_getTags() {
const tags = [
game.i18n.localize(`DAGGERHEART.CONFIG.DomainCardTypes.${this.type}`),
game.i18n.localize(`DAGGERHEART.GENERAL.Domain.${this.domain}.label`),
`${game.i18n.localize('DAGGERHEART.ITEMS.DomainCard.recallCost')}: ${this.recallCost}`
];
return tags;
}
/**
* Generate a localized label array for this item subtype.
* @returns {(string | { value: string, icons: string[] })[]} An array of localized strings and damage label objects.
*/
_getLabels() {
const labels = [
game.i18n.localize(`DAGGERHEART.CONFIG.DomainCardTypes.${this.type}`),
game.i18n.localize(`DAGGERHEART.GENERAL.Domain.${this.domain}.label`),
{
value: `${this.recallCost}`, //converts the number to a string
icons: ['fa-bolt']
}
];
return labels;
}
} }

View file

@ -167,4 +167,64 @@ export default class DHWeapon extends AttachableItem {
} }
} }
} }
/**
* Generates a list of localized tags based on this item's type-specific properties.
* @returns {string[]} An array of localized tag strings.
*/
_getTags() {
const { attack, burden } = this;
const tags = [
game.i18n.localize(`DAGGERHEART.CONFIG.Traits.${attack.roll.trait}.name`),
game.i18n.localize(`DAGGERHEART.CONFIG.Range.${attack.range}.name`),
game.i18n.localize(`DAGGERHEART.CONFIG.Burden.${burden}`)
];
for (const { value, type } of attack.damage.parts) {
const parts = [value.dice];
if (value.bonus) parts.push(value.bonus.signedString());
if (type.size > 0) {
const typeTags = Array.from(type)
.map(t => game.i18n.localize(`DAGGERHEART.CONFIG.DamageType.${t}.abbreviation`))
.join(' | ');
parts.push(` (${typeTags})`); // Add a space in front and put it inside a ().
}
tags.push(parts.join(''));
}
return tags;
}
/**
* Generate a localized label array for this item subtype.
* @returns {(string | { value: string, icons: string[] })[]} An array of localized strings and damage label objects.
*/
_getLabels() {
const { roll, range, damage } = this.attack;
const labels = [
game.i18n.localize(`DAGGERHEART.CONFIG.Traits.${roll.trait}.short`),
game.i18n.localize(`DAGGERHEART.CONFIG.Range.${range}.short`)
];
for (const { value, type } of damage.parts) {
const str = [value.dice];
if (value.bonus) str.push(value.bonus.signedString());
const icons = Array.from(type)
.map(t => CONFIG.DH.GENERAL.damageTypes[t]?.icon)
.filter(Boolean);
const labelValue = str.join('');
if (icons.length === 0) {
labels.push(labelValue);
} else {
labels.push({ value: labelValue, icons });
}
}
return labels;
}
} }

View file

@ -1,6 +1,12 @@
import { itemAbleRollParse } from '../helpers/utils.mjs'; import { itemAbleRollParse } from '../helpers/utils.mjs';
export default class DhActiveEffect extends ActiveEffect { export default class DhActiveEffect extends foundry.documents.ActiveEffect {
/* -------------------------------------------- */
/* Properties */
/* -------------------------------------------- */
/**@override */
get isSuppressed() { get isSuppressed() {
// If this is a copied effect from an attachment, never suppress it // If this is a copied effect from an attachment, never suppress it
// (These effects have attachmentSource metadata) // (These effects have attachmentSource metadata)
@ -41,14 +47,11 @@ export default class DhActiveEffect extends ActiveEffect {
}); });
} }
get localizedStatuses() { /* -------------------------------------------- */
const statusMap = new Map(foundry.CONFIG.statusEffects.map(status => [status.id, status.name])); /* Event Handlers */
return this.statuses.map(x => ({ /* -------------------------------------------- */
key: x,
name: game.i18n.localize(statusMap.get(x))
}));
}
/**@inheritdoc*/
async _preCreate(data, options, user) { async _preCreate(data, options, user) {
const update = {}; const update = {};
if (!data.img) { if (!data.img) {
@ -62,13 +65,22 @@ export default class DhActiveEffect extends ActiveEffect {
await super._preCreate(data, options, user); await super._preCreate(data, options, user);
} }
/* -------------------------------------------- */
/* Methods */
/* -------------------------------------------- */
/**@inheritdoc*/
static applyField(model, change, field) { static applyField(model, change, field) {
const evalValue = this.effectSafeEval(itemAbleRollParse(change.value, model, change.effect.parent)); const evalValue = this.effectSafeEval(itemAbleRollParse(change.value, model, change.effect.parent));
change.value = evalValue ?? change.value; change.value = evalValue ?? change.value;
super.applyField(model, change, field); super.applyField(model, change, field);
} }
/* Altered Foundry safeEval to allow non-numeric returns */ /**
* Altered Foundry safeEval to allow non-numeric return
* @param {string} expression
* @returns
*/
static effectSafeEval(expression) { static effectSafeEval(expression) {
let result; let result;
try { try {
@ -82,6 +94,24 @@ export default class DhActiveEffect extends ActiveEffect {
return result; return result;
} }
/**
* Generates a list of localized tags based on this item's type-specific properties.
* @returns {string[]} An array of localized tag strings.
*/
_getTags() {
const tags = [
`${game.i18n.localize(this.parent.system.metadata.label)}: ${this.parent.name}`,
game.i18n.localize(this.isTemporary ? 'DAGGERHEART.EFFECTS.Duration.temporary' : 'DAGGERHEART.EFFECTS.Duration.passive')
];
for (const statusId of this.statuses) {
const status = CONFIG.statusEffects.find(s => s.id === statusId);
tags.push(game.i18n.localize(status.name));
}
return tags;
}
async toChat(origin) { async toChat(origin) {
const cls = getDocumentClass('ChatMessage'); const cls = getDocumentClass('ChatMessage');
const systemData = { const systemData = {

View file

@ -74,8 +74,8 @@ export default class DHItem extends foundry.documents.Item {
isInventoryItem === true isInventoryItem === true
? 'Inventory Items' //TODO localize ? 'Inventory Items' //TODO localize
: isInventoryItem === false : isInventoryItem === false
? 'Character Items' //TODO localize ? 'Character Items' //TODO localize
: 'Other'; //TODO localize : 'Other'; //TODO localize
return { value: type, label, group }; return { value: type, label, group };
} }
@ -96,6 +96,28 @@ export default class DHItem extends foundry.documents.Item {
}); });
} }
/* -------------------------------------------- */
/**
* Generate an array of localized tag.
* @returns {string[]} An array of localized tag strings.
*/
getTags() {
const tags = [];
if (this.system.getTags) tags.push(...this.system.getTags());
return tags;
}
/**
* Generate a localized label array for this item.
* @returns {(string | { value: string, icons: string[] })[]} An array of localized strings and damage label objects.
*/
getLabels() {
const labels = [];
if (this.system.getLabels) labels.push(...this.system.getLabels());
return labels;
}
async use(event) { async use(event) {
const actions = new Set(this.system.actionsList); const actions = new Set(this.system.actionsList);
if (actions?.size) { if (actions?.size) {
@ -115,10 +137,10 @@ export default class DHItem extends foundry.documents.Item {
this.type === 'ancestry' this.type === 'ancestry'
? game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.ancestryTitle') ? game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.ancestryTitle')
: this.type === 'community' : this.type === 'community'
? game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.communityTitle') ? game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.communityTitle')
: this.type === 'feature' : this.type === 'feature'
? game.i18n.localize('TYPES.Item.feature') ? game.i18n.localize('TYPES.Item.feature')
: game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.subclassFeatureTitle'), : game.i18n.localize('DAGGERHEART.UI.Chat.foundationCard.subclassFeatureTitle'),
origin: origin, origin: origin,
img: this.img, img: this.img,
name: this.name, name: this.name,

View file

@ -240,7 +240,7 @@ export const updateActorTokens = async (actor, update) => {
* Retrieves a Foundry document associated with the nearest ancestor element * Retrieves a Foundry document associated with the nearest ancestor element
* that has a `data-item-uuid` attribute. * that has a `data-item-uuid` attribute.
* @param {HTMLElement} element - The DOM element to start the search from. * @param {HTMLElement} element - The DOM element to start the search from.
* @returns {foundry.abstract.Document|null} The resolved document, or null if not found or invalid. * @returns {Promise<foundry.abstract.Document|null>} The resolved document, or null if not found or invalid.
*/ */
export async function getDocFromElement(element) { export async function getDocFromElement(element) {
const target = element.closest('[data-item-uuid]'); const target = element.closest('[data-item-uuid]');

View file

@ -15,169 +15,63 @@ Parameters:
- showActions {boolean} : If true show feature's actions. - showActions {boolean} : If true show feature's actions.
--}} --}}
<li class="inventory-item" data-item-id="{{item.id}}" {{#if (or (eq type 'action' ) (eq type 'attack'))}} data-action-id="{{item.id}}" {{/if}} <li class="inventory-item" data-item-id="{{item.id}}" {{#if (or (eq type 'action' ) (eq type 'attack' ))}}
data-item-uuid="{{item.uuid}}" data-type="{{type}}" draggable="true"> data-action-id="{{item.id}}" {{/if}} data-item-uuid="{{item.uuid}}" data-type="{{type}}" draggable="true">
<div class="inventory-item-header" {{#unless noExtensible}}data-action="toggleExtended" {{/unless}}> <div class="inventory-item-header" {{#unless noExtensible}}data-action="toggleExtended" {{/unless}}>
{{!-- Image --}} {{!-- Image --}}
<div class="img-portait" <div class="img-portait" data-action='{{ifThen (or (hasProperty item "use") (eq type ' attack')) "useItem" (ifThen
data-action='{{ifThen (or (hasProperty item "use") (eq type 'attack')) "useItem" (ifThen (hasProperty item "toChat") "toChat" "editDoc") }}' (hasProperty item "toChat" ) "toChat" "editDoc" ) }}' {{#unless hideTooltip}} {{#if (eq type 'attack' )}}
{{#unless hideTooltip}} data-tooltip="#attack#{{item.actor.uuid}}" {{else}} data-tooltip="#item#{{item.uuid}}" {{/if}} {{/unless}}>
{{#if (eq type 'attack')}}
data-tooltip="#attack#{{item.actor.uuid}}"
{{else}}
data-tooltip="#item#{{item.uuid}}"
{{/if}}
{{/unless}}
>
<img src="{{item.img}}" class="item-img {{#if isActor}}actor-img{{/if}}" /> <img src="{{item.img}}" class="item-img {{#if isActor}}actor-img{{/if}}" />
<img class="roll-img" src="systems/daggerheart/assets/icons/dice/default/d20.svg" alt="d20"> <img class="roll-img" src="systems/daggerheart/assets/icons/dice/default/d20.svg" alt="d20">
</div> </div>
{{!-- Name & Tags --}} {{!-- Name & Tags --}}
<div class="item-label {{#if hideResources}}fullWidth{{/if}}"> <div class="item-label">
{{!-- Item Name --}} {{!-- Item Name --}}
<div class="item-name">{{localize item.name}}</div> <div class="item-name">{{localize item.name}}</div>
{{!-- Attack Block Start --}} {{!-- Attack Tags Start --}}
{{#if (eq type 'attack')}} {{#if (eq type 'attack')}}
<div class="item-tags"> <div class="item-tags">
<div class="tag"> <div class="tag">
{{localize 'DAGGERHEART.GENERAL.unarmed'}} {{localize 'DAGGERHEART.GENERAL.unarmed'}}
</div> </div>
<div class="tag"> <div class="tag">
{{localize 'DAGGERHEART.CONFIG.ActionType.action'}} {{localize 'DAGGERHEART.CONFIG.ActionType.action'}}
</div> </div>
</div>
{{!-- Attack Tags End --}}
{{else}}
{{!-- Other elements Tags Start --}}
{{#with item}}
{{#if (not ../hideTags)}}
<div class="item-tags">
{{#each this._getTags as |tag|}}
<div class="tag">
{{tag}}
</div> </div>
{{/if}}
{{!-- Attack Block End --}}
{{!-- Weapon Block Start --}}
{{#if (eq type 'weapon')}}
{{#if (not hideTags)}}
<div class="item-tags">
<div class="tag">
{{localize (concat 'DAGGERHEART.CONFIG.Traits.' item.system.attack.roll.trait '.name')}}
</div>
<div class="tag">
{{localize (concat 'DAGGERHEART.CONFIG.Range.' item.system.attack.range '.name')}}
</div>
<div class="tag">
{{item.system.attack.damage.parts.0.value.dice}}
{{#if item.system.attack.damage.parts.0.value.bonus}} +
{{item.system.attack.damage.parts.0.value.bonus}}{{/if}}
(
{{#each item.system.attack.damage.parts.0.type as |type|}}
{{localize (concat 'DAGGERHEART.CONFIG.DamageType.' type '.abbreviation')}}
{{#unless @last}}|{{/unless}}
{{/each}} {{/each}}
)
</div> </div>
<div class="tag"> {{else if (not ../hideLabels)}}
{{localize (concat 'DAGGERHEART.CONFIG.Burden.' item.system.burden)}} <div class="item-lables">
<div class="label">
{{#each this._getLabels as |label|}}
{{ifThen label.value label.value label}}
{{#each label.icons as |icon|}}
<i class="fa-solid {{icon}}"></i>
{{/each}}
{{#if (not @last)}}
<span>-</span>
{{/if}}
{{/each}}
</div>
</div> </div>
</div> {{/if}}
{{else if (not hideLabels)}} {{/with}}
<div class="item-labels">
<div class="label">
{{localize (concat 'DAGGERHEART.CONFIG.Traits.' item.system.attack.roll.trait '.short')}}
{{localize (concat 'DAGGERHEART.CONFIG.Range.' item.system.attack.range '.short')}}
<span> - </span>
{{item.system.attack.damage.parts.0.value.dice}}
{{#if item.system.attack.damage.parts.0.value.bonus}} +
{{item.system.attack.damage.parts.0.value.bonus}}
{{/if}}
{{#with (lookup @root.config.GENERAL.damageTypes item.system.attack.damage.parts.0.type)}}
{{#each icon}}<i class="fa-solid {{this}}"></i>{{/each}}
{{/with}}
</div>
</div>
{{/if}} {{/if}}
{{/if}} {{!-- Other elements Tags End --}}
{{!-- Weapon Block End --}}
{{!-- Armor Block Start --}}
{{#if (eq type 'armor')}}
{{#if (not hideTags)}}
<div class="item-tags">
<div class="tag">{{localize "DAGGERHEART.ITEMS.Armor.baseScore"}}: {{item.system.baseScore}}</div>
<div class="tag">
{{localize "DAGGERHEART.ITEMS.Armor.baseThresholds.base"}}:
{{item.system.baseThresholds.major}} / {{item.system.baseThresholds.severe}}
</div>
</div>
{{else if (not hideLabels)}}
<div class="item-labels">
<div class="label">
{{localize "DAGGERHEART.ITEMS.Armor.baseScore"}}: {{item.system.baseScore}}
</div>
</div>
{{/if}}
{{/if}}
{{!-- Armor Block End --}}
{{!-- Domain Card Block Start --}}
{{#if (eq type 'domainCard')}}
{{#if (not hideTags)}}
<div class="item-tags">
<div class="tag">{{localize (concat 'DAGGERHEART.CONFIG.DomainCardTypes.' item.system.type)}}</div>
<div class="tag">{{localize (concat 'DAGGERHEART.GENERAL.Domain.' item.system.domain '.label')}}</div>
<div class="tag">
<span class="recall-label">{{localize "DAGGERHEART.ITEMS.DomainCard.recallCost"}}: </span>
<span class="recall-value">{{item.system.recallCost}}</span>
</div>
</div>
{{else if (not hideLabels)}}
<div class="item-labels">
<div class="label">
{{localize (concat 'DAGGERHEART.CONFIG.DomainCardTypes.' item.system.type)}} -
{{localize (concat 'DAGGERHEART.GENERAL.Domain.' item.system.domain '.label')}} -
<span class="recall-value">{{item.system.recallCost}}</span>
<i class="fa-solid fa-bolt"></i>
</div>
</div>
{{/if}}
{{/if}}
{{!-- Domain Card Block End --}}
{{!-- Effect Block Start --}}
{{#if (eq type 'effect')}}
{{#if (not hideTags)}}
<div class="item-tags">
<div class="tag">
{{localize item.parent.system.metadata.label}}: {{item.parent.name}}
</div>
<div class="tag">
{{#if item.duration.duration}}
{{localize 'DAGGERHEART.EFFECTS.Duration.temporary'}}
{{else}}
{{localize 'DAGGERHEART.EFFECTS.Duration.passive'}}
{{/if}}
</div>
{{#each item.localizedStatuses as |status|}}
<div class="tag">{{status.name}}</div>
{{/each}}
</div>
{{else if (not hideLabels)}}
{{!-- Empty --}}
{{/if}}
{{/if}}
{{!-- Effect Block End --}}
{{!-- Action Block Start --}}
{{#if (eq type 'action')}}
{{#if (not hideTags)}}
<div class="item-tags">
<div class="tag">{{localize (concat 'DAGGERHEART.ACTIONS.TYPES.' item.type '.name')}}</div>
<div class="tag">{{localize (concat 'DAGGERHEART.CONFIG.ActionType.' item.actionType)}}</div>
</div>
{{else if (not hideLabels)}}
{{!-- Empty --}}
{{/if}}
{{/if}}
{{!-- Action Block End --}}
</div> </div>
{{!-- Simple Resource --}} {{!-- Simple Resource --}}
@ -256,4 +150,4 @@ Parameters:
{{/each}} {{/each}}
</div> </div>
{{/if}} {{/if}}
</li> </li>