This commit is contained in:
@@ -1,30 +1,4 @@
|
||||
function numberOr(value, fallback) {
|
||||
const number = Number(value);
|
||||
return Number.isFinite(number) ? number : fallback;
|
||||
}
|
||||
|
||||
function asText(value) {
|
||||
return typeof value === 'string' ? value : '';
|
||||
}
|
||||
|
||||
function plainText(value) {
|
||||
return asText(value)
|
||||
.replace(/<br\s*\/?>/gi, '\n')
|
||||
.replace(/<\/p\s*>/gi, '\n')
|
||||
.replace(/<[^>]*>/g, ' ')
|
||||
.replace(/ /gi, ' ')
|
||||
.replace(/&/gi, '&')
|
||||
.replace(/</gi, '<')
|
||||
.replace(/>/gi, '>')
|
||||
.replace(/\s+\n/g, '\n')
|
||||
.trim();
|
||||
}
|
||||
|
||||
function traitList(value) {
|
||||
if (Array.isArray(value)) return value.map(asText).filter(Boolean);
|
||||
if (typeof value === 'string') return [value];
|
||||
return [];
|
||||
}
|
||||
const { numberOr, asText, plainText, stringList } = require('./foundry-fields');
|
||||
|
||||
function foundryActorToCharacter(foundryActor) {
|
||||
const system = foundryActor.system || {};
|
||||
@@ -40,10 +14,10 @@ function foundryActorToCharacter(foundryActor) {
|
||||
const savedDragonrollFields = foundryActor.flags?.['dragonroll-foundry'] || {};
|
||||
const savedDragonrollType = savedDragonrollFields.actorType;
|
||||
const foundryProficiencies = [
|
||||
...traitList(traits.languages?.value),
|
||||
...traitList(traits.armor?.value),
|
||||
...traitList(traits.weapon?.value),
|
||||
...traitList(traits.tool?.value)
|
||||
...stringList(traits.languages?.value),
|
||||
...stringList(traits.armor?.value),
|
||||
...stringList(traits.weapon?.value),
|
||||
...stringList(traits.tool?.value)
|
||||
].join(', ');
|
||||
|
||||
return {
|
||||
|
||||
32
backend/src/services/foundry-fields.js
Normal file
32
backend/src/services/foundry-fields.js
Normal file
@@ -0,0 +1,32 @@
|
||||
// Small helpers shared by the Foundry -> Dragonroll document mappers
|
||||
// (services/actor-foundry.js and services/item-foundry.js).
|
||||
|
||||
function numberOr(value, fallback) {
|
||||
const number = Number(value);
|
||||
return Number.isFinite(number) ? number : fallback;
|
||||
}
|
||||
|
||||
function asText(value) {
|
||||
return typeof value === 'string' ? value : '';
|
||||
}
|
||||
|
||||
function plainText(value) {
|
||||
return asText(value)
|
||||
.replace(/<br\s*\/?>/gi, '\n')
|
||||
.replace(/<\/p\s*>/gi, '\n')
|
||||
.replace(/<[^>]*>/g, ' ')
|
||||
.replace(/ /gi, ' ')
|
||||
.replace(/&/gi, '&')
|
||||
.replace(/</gi, '<')
|
||||
.replace(/>/gi, '>')
|
||||
.replace(/\s+\n/g, '\n')
|
||||
.trim();
|
||||
}
|
||||
|
||||
function stringList(value) {
|
||||
if (Array.isArray(value)) return value.map(asText).filter(Boolean);
|
||||
if (typeof value === 'string') return [value];
|
||||
return [];
|
||||
}
|
||||
|
||||
module.exports = { numberOr, asText, plainText, stringList };
|
||||
108
backend/src/services/item-foundry.js
Normal file
108
backend/src/services/item-foundry.js
Normal file
@@ -0,0 +1,108 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user