Fix issue #96: When right clicking actor on foundry, show action to force sync to dragonroll
All checks were successful
Test / test (push) Successful in 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-15 22:53:03 +00:00
parent dcc6d7a14b
commit 967c704801
7 changed files with 115 additions and 14 deletions

View File

@@ -64,9 +64,9 @@ function resolveWorldAssets(fields, campaign) {
}
}
async function saveFoundryActor(campaign, source, fullSync = false) {
async function saveFoundryActor(campaign, source, fullSync = false, replace = false) {
const foundryActorId = String(source?._id || source?.id || '').trim().slice(0, 120);
const fields = foundryActorToCharacter(source || {});
const fields = foundryActorToCharacter(source || {}, replace);
if (!foundryActorId || !fields.name) return null;
resolveWorldAssets(fields, campaign);
@@ -304,12 +304,16 @@ router.post('/sync/snapshot', async (req, res) => {
}
const fullSync = req.body.fullSync === true;
// A forced actor sync from the module's context menu replaces the campaign's
// row for the actors it sends entirely (Syndria98/dragonroll#96). It stays
// clear of fullSync: it never prunes, unlike a reconnect's full snapshot.
const replace = req.body.replace === true;
// A module that knows nothing about item folders sends neither the tree
// nor the folder of an item, and then nothing here touches the folders.
const folderSources = Array.isArray(req.body.folders) ? req.body.folders : null;
const actors = [];
for (const source of actorSources || []) {
const mapping = await saveFoundryActor(campaign, source, fullSync);
const mapping = await saveFoundryActor(campaign, source, fullSync, replace);
if (mapping) actors.push(mapping);
}
const itemFolders = folderSources ? await syncFoundryItemFolders(campaign, folderSources) : null;

View File

@@ -130,7 +130,11 @@ function foundryTraits(source, items) {
return features.length ? features.join('\n\n') : undefined;
}
function foundryActorToCharacter(foundryActor) {
// `replace` is a forced actor sync from the module's context menu (Syndria98/dragonroll#96):
// it replaces everything the campaign holds for this actor, so the fields a live edit
// leaves out when the world says nothing are written empty instead of kept. A live Foundry
// change passes `false` and keeps the campaign's own toggles and text.
function foundryActorToCharacter(foundryActor, replace = false) {
const system = foundryActor.system || {};
const details = system.details || {};
const abilities = system.abilities || {};
@@ -169,7 +173,9 @@ function foundryActorToCharacter(foundryActor) {
])),
armorClass: numberOr(attributes.ac?.value ?? attributes.ac, 10),
speed: numberOr(movement.walk ?? movement.value, 30),
proficiencies: foundryProficiencies || asText(savedDragonrollFields.proficiencies),
// A replace takes the world's text even when it has none, so what Dragonroll
// cached on the actor's flag does not survive it; a live edit keeps that text.
proficiencies: foundryProficiencies || (replace ? '' : 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.
@@ -201,9 +207,21 @@ function foundryActorToCharacter(foundryActor) {
const currency = foundryCurrency(system);
if (currency) fields.currency = currency;
const worldTraits = foundryTraits(foundryActor, items);
const campaignTraits = asText(savedDragonrollFields.traits);
const campaignTraits = replace ? '' : asText(savedDragonrollFields.traits);
if (worldTraits || campaignTraits) fields.traits = worldTraits || campaignTraits;
// A replace writes the fields a live edit would leave out when the world says
// nothing, so the campaign's toggles, lists and text for this actor go with it and
// the row reads as the Foundry actor alone (Syndria98/dragonroll#96).
if (replace) {
if (fields.skills === undefined) fields.skills = {};
if (fields.savingThrows === undefined) fields.savingThrows = {};
if (fields.tools === undefined) fields.tools = [];
if (fields.languages === undefined) fields.languages = [];
if (fields.currency === undefined) fields.currency = {};
if (fields.traits === undefined) fields.traits = '';
}
return fields;
}