Files
dragonroll/backend/src/services/item-foundry.js
Aran Roig af76cf1050
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m6s
Things
2026-10-13 22:50:51 +02:00

109 lines
3.7 KiB
JavaScript

const { numberOr, asText, plainText, stringList } = require('./foundry-fields');
// Foundry item types that are gear, mapped to a Dragonroll Item category.
// `equipment` is resolved into armor / shield from its armor type.
const CATEGORY_BY_FOUNDRY_TYPE = {
weapon: 'weapon',
equipment: 'equipment',
consumable: 'consumable',
tool: 'tool',
container: 'adventuring-gear',
loot: 'treasure',
// Item types from older D&D 5e system releases.
ammo: 'weapon',
potion: 'consumable',
poison: 'consumable',
scroll: 'consumable',
trinket: 'magic-item',
backpack: 'adventuring-gear'
};
// Foundry item types that are not gear: they never become Dragonroll items.
const NON_GEAR_FOUNDRY_TYPES = new Set([
'spell', 'class', 'subclass', 'background', 'feat', 'race', 'species', 'status', 'spellbook', 'language'
]);
// Item categories the Dragonroll Item model accepts.
const CATEGORIES = ['weapon', 'armor', 'shield', 'adventuring-gear', 'magic-item', 'consumable', 'tool', 'treasure', 'other'];
const RARITY_BY_FOUNDRY_VALUE = {
common: 'common',
uncommon: 'uncommon',
rare: 'rare',
'very-rare': 'very-rare',
veryrare: 'very-rare',
'very rare': 'very-rare',
legendary: 'legendary',
artifact: 'artifact'
};
// Fallback labels for weapon properties when the Foundry client did not send the
// ones from its own CONFIG (see serializeFoundryItem in the module).
const WEAPON_PROPERTY_LABELS = {
amm: 'ammunition',
fin: 'finesse',
foc: 'focus',
hvy: 'heavy',
lrp: 'long range',
lod: 'loading',
nid: 'noisy',
rch: 'reach',
rel: 'reload',
ret: 'retractable',
slo: 'slow firing',
thr: 'thrown',
thv: 'thrown',
two: 'two-handed',
ver: 'versatile'
};
function foundryItemRarity(foundryItem) {
const rarity = asText(foundryItem.system?.rarity).toLowerCase().trim();
return RARITY_BY_FOUNDRY_VALUE[rarity] || 'common';
}
// Returns null for Foundry documents that are not gear so the sync can skip them.
function foundryItemCategory(foundryItem, rarity) {
const type = asText(foundryItem.type).toLowerCase().trim();
if (NON_GEAR_FOUNDRY_TYPES.has(type)) return null;
if (type === 'equipment') {
return asText(foundryItem.system?.armor?.type).toLowerCase() === 'shield' ? 'shield' : 'armor';
}
const category = CATEGORY_BY_FOUNDRY_TYPE[type];
if (CATEGORIES.includes(category)) return category;
// Systems the module does not know about still get a usable Dragonroll item.
return rarity === 'common' ? 'other' : 'magic-item';
}
function foundryItemProperties(foundryItem) {
const provided = stringList(foundryItem.dragonroll?.properties);
if (provided.length) return provided.join(', ');
const properties = stringList(foundryItem.system?.properties?.value ?? foundryItem.system?.properties);
return properties.map((property) => {
const key = property.toLowerCase();
return Object.hasOwn(WEAPON_PROPERTY_LABELS, key) ? WEAPON_PROPERTY_LABELS[key] : property;
}).join(', ');
}
function foundryItemToItemData(foundryItem) {
const system = foundryItem.system || {};
const name = asText(foundryItem.name).trim();
const rarity = foundryItemRarity(foundryItem);
const category = foundryItemCategory(foundryItem, rarity);
if (!name || !category) return null;
return {
name,
category,
rarity,
quantity: Math.max(1, Math.trunc(numberOr(system.quantity, 1))),
weight: Math.max(0, numberOr(system.weight?.value ?? system.weight, 0)),
properties: foundryItemProperties(foundryItem),
description: plainText(system.description?.value ?? system.description)
};
}
module.exports = { foundryItemToItemData };