Fix issue #89: Traits and items are mixed up
All checks were successful
Test / test (push) Successful in 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-15 21:36:44 +00:00
parent 98aa3be078
commit f8dae95563
7 changed files with 586 additions and 5 deletions

View File

@@ -9,6 +9,7 @@ const Item = require('../models/Item');
const Folder = require('../models/Folder');
const { foundryActorToCharacter } = require('../services/actor-foundry');
const { foundryItemToItemData } = require('../services/item-foundry');
const { joinFoundryAssetUrl } = require('../services/foundry-icons');
// Upper bound for the Foundry names remembered per synced item (see Item model).
const MAX_FOUNDRY_NAMES = 20;
@@ -41,10 +42,33 @@ function requestMatchesWorld(campaign, worldId) {
return typeof worldId === 'string' && worldId === campaign.foundryWorldId;
}
// Foundry serves its data directory from the world's own address, which is the one the
// campaign keeps under `settings.integrations.foundryUrl` — the icon window already loads
// the world's icons through it. The module sends the images it publishes as absolute URLs;
// the bridge's snapshot and older modules carry Foundry's relative paths, and a sheet
// reading one of those against Dragonroll's upload folder finds nothing, which is how the
// icons stopped loading (Syndria98/dragonroll#89). An absolute URL travels as it is, and so
// does a path when the campaign has no world address to read it against.
function worldAssetUrl(image, worldUrl) {
const value = String(image ?? '').trim();
if (!value || /^(https?:|data:|\/\/)/i.test(value)) return value;
return joinFoundryAssetUrl(worldUrl, value) || value;
}
// The images a world document arrives with, read against its world before they land.
function resolveWorldAssets(fields, campaign) {
const worldUrl = campaign?.settings?.integrations?.foundryUrl;
if (typeof fields.image === 'string') fields.image = worldAssetUrl(fields.image, worldUrl);
if (Array.isArray(fields.inventory)) {
for (const entry of fields.inventory) entry.icon = worldAssetUrl(entry.icon, worldUrl);
}
}
async function saveFoundryActor(campaign, source, fullSync = false) {
const foundryActorId = String(source?._id || source?.id || '').trim().slice(0, 120);
const fields = foundryActorToCharacter(source || {});
if (!foundryActorId || !fields.name) return null;
resolveWorldAssets(fields, campaign);
let actor = await Actor.findOne({ campaign: campaign._id, foundryActorId });
const dragonrollActorId = source?.flags?.['dragonroll-foundry']?.actorId;
@@ -85,6 +109,7 @@ async function saveFoundryItem(campaign, source, fullSync = false, folders = nul
const foundryItemId = String(source?._id || source?.id || '').trim().slice(0, 120);
const fields = foundryItemToItemData(source || {});
if (!foundryItemId || !fields) return null;
resolveWorldAssets(fields, campaign);
let item = await Item.findOne({ campaign: campaign._id, foundryItemId });
const dragonrollItemId = source?.flags?.['dragonroll-foundry']?.itemId;

View File

@@ -1,4 +1,5 @@
const { numberOr, asText, plainText, stringList } = require('./foundry-fields');
const { isFoundryGearType } = require('./item-foundry');
// D&D 5e keeps an actor's background, species, class and subclass as a reference to an
// embedded item: `system.details.background` and friends hold that item's id, and the
@@ -169,8 +170,11 @@ function foundryActorToCharacter(foundryActor) {
armorClass: numberOr(attributes.ac?.value ?? attributes.ac, 10),
speed: numberOr(movement.walk ?? movement.value, 30),
proficiencies: foundryProficiencies || asText(savedDragonrollFields.proficiencies),
// What the character carries: gear, and gear only (Syndria98/dragonroll#89). A feat
// is a trait — its tab shows it — and a race is the species; standing them in the
// inventory next to the longswords is how traits and items got mixed up.
inventory: items
.filter((item) => !['class', 'subclass', 'background', 'spell'].includes(item.type))
.filter((item) => isFoundryGearType(item.type))
.map((item) => ({
name: asText(item.name).trim(),
quantity: numberOr(item.system?.quantity, 1),

View File

@@ -23,6 +23,14 @@ const NON_GEAR_FOUNDRY_TYPES = new Set([
'spell', 'class', 'subclass', 'background', 'feat', 'race', 'species', 'status', 'spellbook', 'language'
]);
// Whether a Foundry document of this type is gear: something a character carries. Traits,
// species, spells and the rest are documents of the world that mean something else on a
// Dragonroll sheet — a feat is a trait and a race is a species — so they stand in the
// character's own fields, never in its inventory (Syndria98/dragonroll#89).
function isFoundryGearType(type) {
return !NON_GEAR_FOUNDRY_TYPES.has(asText(type).toLowerCase().trim());
}
// Item categories the Dragonroll Item model accepts.
const CATEGORIES = ['weapon', 'armor', 'shield', 'adventuring-gear', 'magic-item', 'consumable', 'tool', 'treasure', 'other'];
@@ -107,4 +115,4 @@ function foundryItemToItemData(foundryItem) {
};
}
module.exports = { foundryItemToItemData };
module.exports = { foundryItemToItemData, isFoundryGearType };