Skip to content

Commit

Permalink
refactor: Data model cleanup
Browse files Browse the repository at this point in the history
- give all models another pass
- factor out more common sections
  • Loading branch information
Shourn committed Jan 17, 2025
1 parent 6819430 commit fb247f7
Show file tree
Hide file tree
Showing 18 changed files with 313 additions and 336 deletions.
97 changes: 92 additions & 5 deletions module/checks/common-sections.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CHECK_FLAVOR } from './default-section-order.mjs';

/**
* @param {CheckRenderData} sections
* @param {string} description
Expand All @@ -11,8 +13,8 @@ const description = (sections, description, summary, order) => {
data: {
summary,
description: await TextEditor.enrichHTML(description),
order,
},
order,
}));
}
};
Expand All @@ -33,23 +35,108 @@ const clock = (sections, clock, order) => {
}));
};

/**
* @typedef Tag
* @property {string} [tag] gets localized
* @property {any} [value] doesn't get localized
* @property {string} [tooltip] tooltip to attach to the tag, gets localized
* @property {boolean} [flip] switches the position of tag and value
* @property {string} [separator] placed between tag and
* @property {any} [show] can be omitted, if defined and falsy doesn't render tag
*/

/**
* @param {CheckRenderData} sections
* @param {Tag[]} tags
* @param {number} [order]
*/
const tags = (sections, tags = [], order) => {
tags = tags.filter((tag) => !('show' in tag) || tag.show);
if (tags.length > 0) {
sections.push(async () => ({
partial: 'systems/projectfu/templates/chat/partials/chat-item-tags.hbs',
data: {
tags: tags,
},
order: order,
}));
}
};

/**
* @param {CheckRenderData} sections
* @param {string} quality
* @param {number} [order]
*/
const quality = (sections, quality, order) => {
if (quality) {
sections.push({
partial: 'systems/projectfu/templates/chat/partials/chat-item-quality.hbs',
data: {
quality,
},
order,
});
}
};

/**
* @param {CheckRenderData} sections
* @param {{tag?:string, value?: string}[]} tags tags are localized, values are rendered as is
* @param {ProgressDataModel} resource
* @param {number} [order]
*/
const tags = (sections, tags, order) => {
const resource = (sections, resource, order) => {
sections.push(async () => ({
partial: 'systems/projectfu/templates/chat/partials/chat-item-tags.hbs',
partial: 'systems/projectfu/templates/chat/partials/chat-resource-details.hbs',
data: {
tags,
data: resource,
},
order: order,
}));
};

/**
* Sets chat message flavor by default. Specify order for other usecases.
* @param {CheckRenderData} sections
* @param {{name: string, img: string, id: string, uuid: string}} item
* @param {number} [order]
*/
const itemFlavor = (sections, item, order = CHECK_FLAVOR) => {
sections.push({
order: order,
partial: 'systems/projectfu/templates/chat/chat-check-flavor-item.hbs',
data: {
name: item.name,
img: item.img,
id: item.id,
uuid: item.uuid,
},
});
};

/**
* @param {CheckRenderData} sections
* @param {string} opportunity
* @param {number} [order]
*/
const opportunity = (sections, opportunity, order) => {
if (opportunity) {
sections.push({
partial: 'systems/projectfu/templates/chat/partials/chat-item-opportunity.hbs',
data: {
opportunity: opportunity,
},
order: order,
});
}
};

export const CommonSections = Object.freeze({
description,
clock,
tags,
quality,
resource,
itemFlavor,
opportunity,
});
36 changes: 8 additions & 28 deletions module/documents/items/accessory/accessory-data-model.mjs
Original file line number Diff line number Diff line change
@@ -1,49 +1,29 @@
import { CheckHooks } from '../../../checks/check-hooks.mjs';
import { deprecationNotice } from '../../../helpers/deprecation-helper.mjs';
import { CommonSections } from '../../../checks/common-sections.mjs';

Hooks.on(CheckHooks.renderCheck, (sections, check, actor, item) => {
if (item?.system instanceof AccessoryDataModel) {
const tags = [
CommonSections.tags(sections, [
{
tag: 'FU.DefenseAbbr',
value: item.system.def.value,
show: item.system.def.value,
},
{
tag: 'FU.MagicDefenseAbbr',
value: item.system.mdef.value,
show: item.system.mdef.value,
},
{
tag: 'FU.InitiativeAbbr',
value: item.system.init.value,
show: item.system.init.value,
},
].filter(({ value }) => value !== 0);
if (tags.length > 0) {
sections.push({
partial: 'systems/projectfu/templates/chat/partials/chat-item-tags.hbs',
data: {
tags,
},
});
}
]);

if (item.system.quality.value) {
sections.push({
partial: 'systems/projectfu/templates/chat/partials/chat-item-quality.hbs',
data: {
quality: item.system.quality.value,
},
});
}

if (item.system.summary.value || item.system.description) {
sections.push(async () => ({
partial: 'systems/projectfu/templates/chat/partials/chat-item-description.hbs',
data: {
summary: item.system.summary.value,
description: await TextEditor.enrichHTML(item.system.description),
},
}));
}
CommonSections.quality(sections, item.system.quality.value);
CommonSections.description(sections, item.system.description, item.system.summary.value);
}
});

Expand Down
40 changes: 9 additions & 31 deletions module/documents/items/armor/armor-data-model.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { CheckHooks } from '../../../checks/check-hooks.mjs';
import { deprecationNotice } from '../../../helpers/deprecation-helper.mjs';
import { FU } from '../../../helpers/config.mjs';
import { ArmorMigrations } from './armor-migrations.mjs';
import { CommonSections } from '../../../checks/common-sections.mjs';

Hooks.on(CheckHooks.renderCheck, (sections, check, actor, item) => {
if (item?.system instanceof ArmorDataModel) {
const tags = [
CommonSections.tags(sections, [
{
tag: 'FU.Martial',
show: item.system.isMartial.value,
},
{
tag: 'FU.DefenseAbbr',
value: item.system.def.attribute ? `${game.i18n.localize(FU.attributeAbbreviations[item.system.def.attribute])} + ${item.system.def.value}` : `${item.system.def.value}`,
Expand All @@ -18,37 +23,10 @@ Hooks.on(CheckHooks.renderCheck, (sections, check, actor, item) => {
tag: 'FU.InitiativeAbbr',
value: item.system.init.value,
},
];
if (item.system.isMartial.value) {
tags.unshift({
tag: 'FU.Martial',
});
}
sections.push({
partial: 'systems/projectfu/templates/chat/partials/chat-item-tags.hbs',
data: {
tags,
},
});

if (item.system.quality.value) {
sections.push({
partial: 'systems/projectfu/templates/chat/partials/chat-item-quality.hbs',
data: {
quality: item.system.quality.value,
},
});
}
]);

if (item.system.summary.value || item.system.description) {
sections.push(async () => ({
partial: 'systems/projectfu/templates/chat/partials/chat-item-description.hbs',
data: {
summary: item.system.summary.value,
description: await TextEditor.enrichHTML(item.system.description),
},
}));
}
CommonSections.quality(sections, item.system.quality.value);
CommonSections.description(sections, item.system.description, item.system.summary.value);
}
});

Expand Down
11 changes: 2 additions & 9 deletions module/documents/items/behavior/behavior-data-model.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { CheckHooks } from '../../../checks/check-hooks.mjs';
import { CommonSections } from '../../../checks/common-sections.mjs';

Hooks.on(CheckHooks.renderCheck, (sections, check, actor, item) => {
if (item?.system instanceof BehaviorDataModel) {
if (item.system.summary.value || item.system.description) {
sections.push(async () => ({
partial: 'systems/projectfu/templates/chat/partials/chat-item-description.hbs',
data: {
summary: item.system.summary.value,
description: await TextEditor.enrichHTML(item.system.description),
},
}));
}
CommonSections.description(sections, item.system.description, item.system.summary.value);
}
});

Expand Down
22 changes: 4 additions & 18 deletions module/documents/items/class/class-data-model.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ClassMigrations } from './class-migrations.mjs';
import { CheckHooks } from '../../../checks/check-hooks.mjs';
import { CommonSections } from '../../../checks/common-sections.mjs';

const tagProperties = {
'benefits.resources.hp.value': 'FU.BenefitHp',
Expand All @@ -19,25 +20,10 @@ const tagProperties = {

Hooks.on(CheckHooks.renderCheck, (sections, check, actor, item) => {
if (item?.system instanceof ClassDataModel) {
const tags = Object.entries(tagProperties)
.filter(([property]) => foundry.utils.getProperty(item.system, property))
.map(([, translation]) => ({ tag: translation }));
if (tags.length > 0) {
sections.push({
partial: 'systems/projectfu/templates/chat/partials/chat-item-tags.hbs',
data: { tags },
});
}
const tags = Object.entries(tagProperties).map(([key, translation]) => ({ tag: translation, show: foundry.utils.getProperty(item.system, key) }));
CommonSections.tags(sections, tags);

if (item.system.summary.value || item.system.description) {
sections.push(async () => ({
partial: 'systems/projectfu/templates/chat/partials/chat-item-description.hbs',
data: {
summary: item.system.summary.value,
description: await TextEditor.enrichHTML(item.system.description),
},
}));
}
CommonSections.description(sections, item.system.description, item.system.summary.value);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@ import { RollableClassFeatureDataModel } from './class-feature-data-model.mjs';
import { ChecksV2 } from '../../../checks/checks-v2.mjs';
import { CheckHooks } from '../../../checks/check-hooks.mjs';
import { slugify } from '../../../util.mjs';
import { CommonSections } from '../../../checks/common-sections.mjs';

Hooks.on(CheckHooks.renderCheck, (sections, check, actor, item) => {
if (item?.system instanceof ClassFeatureTypeDataModel && !(item.system.data instanceof RollableClassFeatureDataModel)) {
if (item.system.summary.value || item.system.description) {
sections.push(
TextEditor.enrichHTML(item.system.description).then((v) => ({
partial: 'systems/projectfu/templates/chat/partials/chat-item-description.hbs',
data: {
summary: item.system.summary.value,
description: v,
},
})),
);
}
CommonSections.description(sections, item.system.description, item.system.summary.value);
}
});

Expand Down
23 changes: 3 additions & 20 deletions module/documents/items/consumable/consumable-data-model.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import { CheckHooks } from '../../../checks/check-hooks.mjs';
import { FU } from '../../../helpers/config.mjs';
import { CommonSections } from '../../../checks/common-sections.mjs';

Hooks.on(CheckHooks.renderCheck, (sections, check, actor, item) => {
if (item?.system instanceof ConsumableDataModel) {
sections.push({
partial: 'systems/projectfu/templates/chat/partials/chat-item-tags.hbs',
data: {
tags: [
{
tag: `${item.system.ipCost.value} ${game.i18n.localize('FU.InventoryAbbr')}`,
},
],
},
});

if (item.system.summary.value || item.system.description) {
sections.push(async () => ({
partial: 'systems/projectfu/templates/chat/partials/chat-item-description.hbs',
data: {
summary: item.system.summary.value,
description: await TextEditor.enrichHTML(item.system.description),
},
}));
}
CommonSections.tags(sections, [{ tag: FU.consumableType[item.system.subtype.value] }, { tag: 'FU.InventoryAbbr', value: item.system.ipCost.value, flip: true }]);
CommonSections.description(sections, item.system.description, item.system.summary.value);
}
});

Expand Down
Loading

0 comments on commit fb247f7

Please sign in to comment.