diff --git a/backend/src/routes/foundry-link.js b/backend/src/routes/foundry-link.js index 678291e4..0cb1675b 100644 --- a/backend/src/routes/foundry-link.js +++ b/backend/src/routes/foundry-link.js @@ -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; diff --git a/backend/src/services/actor-foundry.js b/backend/src/services/actor-foundry.js index 8a23970c..ccdb180a 100644 --- a/backend/src/services/actor-foundry.js +++ b/backend/src/services/actor-foundry.js @@ -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), diff --git a/backend/src/services/item-foundry.js b/backend/src/services/item-foundry.js index d570e760..5c6fb5f8 100644 --- a/backend/src/services/item-foundry.js +++ b/backend/src/services/item-foundry.js @@ -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 }; diff --git a/dragonroll-foundry/scripts/api.js b/dragonroll-foundry/scripts/api.js index ade39d53..baeb988b 100644 --- a/dragonroll-foundry/scripts/api.js +++ b/dragonroll-foundry/scripts/api.js @@ -523,9 +523,16 @@ function isSupportedCharacterActor(actor) { return ["character", "npc", "creature"].includes(savedType || actor.type); } +// Embedded documents the world owns outright (Syndria98/dragonroll#89): a feat is a trait +// and a race is a species — they reach the campaign through its traits and species fields, +// never through its inventory. Dragonroll must neither mark them as its own nor delete one +// when it writes the character, and an older campaign still holding such a row in its +// inventory must be let to write it out of the campaign without the document going with it. +const WORLD_OWNED_EMBEDDED_TYPES = new Set(["feat", "race", "species", "status", "spellbook", "language"]); + async function markActorItemsAsSynced(actor) { const updates = actor.items.contents - .filter((item) => !item.getFlag?.(MODULE_ID, "syncedItem")) + .filter((item) => !item.getFlag?.(MODULE_ID, "syncedItem") && !WORLD_OWNED_EMBEDDED_TYPES.has(item.type)) .map((item) => ({ _id: item.id, [`flags.${MODULE_ID}.syncedItem`]: true, @@ -1194,7 +1201,19 @@ async function reconcileEmbeddedItems(actor, entry) { const existing = actor.items.contents.find((item) => item.name === wanted.name && item.type === wanted.type && item.getFlag?.(MODULE_ID, "syncedItem")) || actor.items.contents.find((item) => item.name === wanted.name - && item.getFlag?.(MODULE_ID, "syncedItem")); + && item.getFlag?.(MODULE_ID, "syncedItem") && !WORLD_OWNED_EMBEDDED_TYPES.has(item.type)); + // A carried row holding the name of one of the world's own trait or species documents + // — a trait a campaign that synced before #89 still has in its inventory — stands in + // the world as that document: no loot is made after its name, and the document itself + // is let be. Writing it out of the campaign is the campaign's to do, keeping it is the world's. + if (!existing && wanted.type === "loot") { + const worldOwned = actor.items.contents.find((item) => item.name === wanted.name + && WORLD_OWNED_EMBEDDED_TYPES.has(item.type)); + if (worldOwned) { + retainedIds.add(worldOwned.id); + continue; + } + } if (existing) { retainedIds.add(existing.id); const update = { _id: existing.id }; @@ -1220,7 +1239,8 @@ async function reconcileEmbeddedItems(actor, entry) { } const staleItemIds = actor.items.contents - .filter((item) => item.getFlag?.(MODULE_ID, "syncedItem") && !retainedIds.has(item.id)) + .filter((item) => item.getFlag?.(MODULE_ID, "syncedItem") && !retainedIds.has(item.id) + && !WORLD_OWNED_EMBEDDED_TYPES.has(item.type)) .map((item) => item.id); if (staleItemIds.length) await actor.deleteEmbeddedDocuments("Item", staleItemIds); } diff --git a/frontend/app/components/windows/CharacterSheetWindow.vue b/frontend/app/components/windows/CharacterSheetWindow.vue index 93b75ae8..aadd72ae 100644 --- a/frontend/app/components/windows/CharacterSheetWindow.vue +++ b/frontend/app/components/windows/CharacterSheetWindow.vue @@ -319,7 +319,10 @@ async function saveSheet() {
{{ error }}
-
+ +
@@ -387,146 +390,148 @@ async function saveSheet() {
-
-

{{ $t('campaigns.character-sheet.ability-scores') }}

-
-
- {{ stat.label }} - - {{ form.abilityScores[stat.key] }} - {{ abilityModifier(form.abilityScores[stat.key]) }} +
+
+

{{ $t('campaigns.character-sheet.ability-scores') }}

+
+
+ {{ stat.label }} + + {{ form.abilityScores[stat.key] }} + {{ abilityModifier(form.abilityScores[stat.key]) }} +
-
- -

{{ $t('campaigns.character-sheet.saving-throws') }}

-
- -
-
- -
-

{{ $t('campaigns.character-sheet.skills') }}

-
- -
-
- -
-

{{ $t('campaigns.character-sheet.proficiencies') }}

-