diff --git a/backend/src/routes/foundry-link.js b/backend/src/routes/foundry-link.js index 058bfb2a..e1bcd749 100644 --- a/backend/src/routes/foundry-link.js +++ b/backend/src/routes/foundry-link.js @@ -65,9 +65,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); @@ -305,12 +305,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; diff --git a/backend/src/services/actor-foundry.js b/backend/src/services/actor-foundry.js index ccdb180a..3d411899 100644 --- a/backend/src/services/actor-foundry.js +++ b/backend/src/services/actor-foundry.js @@ -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; } diff --git a/dragonroll-foundry/README.md b/dragonroll-foundry/README.md index 758843ee..fa0e65d4 100644 --- a/dragonroll-foundry/README.md +++ b/dragonroll-foundry/README.md @@ -73,9 +73,11 @@ queued while it was off are sent as the world catches up. With autosync off — or any time a single character needs to cross over right now — right-click an actor (or several selected ones) in the Foundry actor directory and pick **Dragonroll: Force Sync**. Those actors, together with the gear carried inside them, are pushed to the campaign immediately even -when autosync is off. It is an upsert only: it never prunes the campaign's other actors, which a full -snapshot does and a forced sync never claims to. Actors Dragonroll does not import (anything that is -not a character, NPC or creature) are refused with a notification. +when autosync is off, **replacing everything that campaign holds for them** (Syndria98/dragonroll#96): +what the campaign had edited on those actors is written over by what the world says. It replaces +those actors only — it never prunes the campaign's other actors, which a full snapshot does and a +forced sync never claims to. Actors Dragonroll does not import (anything that is not a character, NPC +or creature) are refused with a notification. The optional read-only snapshot bridge is not affected: it keeps publishing the world snapshot behind `/api/foundry/*`, which is never written into the campaign. diff --git a/dragonroll-foundry/scripts/api.js b/dragonroll-foundry/scripts/api.js index a92a7712..ca12d589 100644 --- a/dragonroll-foundry/scripts/api.js +++ b/dragonroll-foundry/scripts/api.js @@ -567,7 +567,7 @@ function reportActorSyncError(error) { console.warn(`${MODULE_ID} | Character sync failed and will retry.`, error); } -async function pushFoundryActors(actors, fullSync = false) { +async function pushFoundryActors(actors, fullSync = false, replace = false) { const token = getSyncToken(); if (!token || !game.user?.isGM || !game.world || (!actors.length && !fullSync)) return; if (game.system.id !== "dnd5e") { @@ -586,6 +586,9 @@ async function pushFoundryActors(actors, fullSync = false) { // The folder tree those world items stand in (see worldItemFolderSnapshots). folders: worldItemFolderSnapshots(), fullSync, + // A forced sync replaces the campaign's row for these actors outright; a live + // change or a full snapshot never sets this, so the campaign keeps its edits. + replace, }, token); for (const mapping of result.actors || []) { @@ -608,8 +611,9 @@ async function deleteDragonrollActor(foundryActorId) { } // Sending actors on purpose, whether or not autosync is on: this is the push the -// GM asks for. It upserts those actors only, so unlike a full snapshot it never -// prunes anything else the campaign holds. +// GM asks for. It replaces everything the campaign holds for those actors (Syndria98/ +// dragonroll#96), but it upserts them only, so unlike a full snapshot it never prunes +// anything else the campaign holds. async function forceSyncActorsToDragonroll(actors) { if (!game.user?.isGM || !game.world || !actors.length) return; if (!getSyncToken()) { @@ -625,7 +629,7 @@ async function forceSyncActorsToDragonroll(actors) { if (!supported.length) return; try { - await pushFoundryActors(supported); + await pushFoundryActors(supported, false, true); ui.notifications?.info(supported.length === 1 ? `${supported[0].name} was sent to the Dragonroll campaign.` : `${supported.length} actors were sent to the Dragonroll campaign.`); @@ -1640,7 +1644,8 @@ Hooks.once("ready", () => { }); // A GM sends selected actors to Dragonroll from the actor directory's context -// menu. This works with autosync on and off: it is the one push done on purpose. +// menu, replacing whatever the campaign holds for them. This works with autosync on +// and off: it is the one push done on purpose. Hooks.on("getActorDirectoryEntryContext", (html, options) => { if (!game.user?.isGM || !getSyncToken()) return; diff --git a/tests/character-sheet-details.test.js b/tests/character-sheet-details.test.js index 9980f45a..a9529a35 100644 --- a/tests/character-sheet-details.test.js +++ b/tests/character-sheet-details.test.js @@ -410,6 +410,36 @@ test("an actor that holds no traits does not clear what the campaign has", () => assert.ok(!("traits" in character), "a live edit that knows nothing about traits leaves the tab as it is"); }); +// A forced actor sync from the module's context menu replaces everything the campaign +// holds for that actor (Syndria98/dragonroll#96): the fields a live edit leaves out when +// the world says nothing are written empty instead of kept, and the campaign's own text +// on the actor's flag does not survive it. +test("a forced sync writes empty the fields a live edit would leave out", () => { + const actor = { + name: "Thalia", + system: {}, + items: [], + flags: { [MODULE_ID]: { traits: "What the campaign typed", proficiencies: "Armour, tools" } }, + }; + + const live = foundryActorToCharacter(actor); + assert.equal(live.traits, "What the campaign typed", "a live edit fills the traits tab from the actor's flag"); + assert.equal(live.proficiencies, "Armour, tools", "and the free-text proficiencies from it too"); + assert.ok( + !("tools" in live) && !("languages" in live) && !("skills" in live) && !("savingThrows" in live) && !("currency" in live), + "and says nothing about the toggles and lists the world has no data for" + ); + + const replaced = foundryActorToCharacter(actor, true); + assert.equal(replaced.traits, "", "a forced sync clears the traits the campaign typed"); + assert.equal(replaced.proficiencies, "", "down to the free-text proficiencies"); + assert.deepEqual(replaced.tools, []); + assert.deepEqual(replaced.languages, []); + assert.deepEqual(replaced.skills, {}); + assert.deepEqual(replaced.savingThrows, {}); + assert.deepEqual(replaced.currency, {}); +}); + // --- The module sends the names a sheet outside Foundry can read ----------------- function makeWorld({ actor, config = {}, worldItems = [] }) { diff --git a/tests/foundry-force-import.test.js b/tests/foundry-force-import.test.js index 8cddde7c..18e70a59 100644 --- a/tests/foundry-force-import.test.js +++ b/tests/foundry-force-import.test.js @@ -329,6 +329,46 @@ test("an actor the world has never been seen as becomes one row, and the next im assert.equal(sync.models["../models/Actor"].rows[0].foundryActorId, "actor-9"); }); +// A forced sync from the module's context menu (Syndria98/dragonroll#96) replaces +// everything the campaign holds for the actor; a live change is a merge and leaves the +// fields the world says nothing about to the campaign. +test("a forced actor sync clears what the campaign wrote; a live change keeps it", async () => { + const sync = loadFoundryWriters({ + seed: { actors: [{ + _id: "actor-1", + name: "Thalia", + foundryActorId: "actor-1", + traits: "A trait the campaign wrote", + tools: ["Thieves' tools"], + languages: ["Elvish"], + skills: { perception: true }, + savingThrows: { dex: true }, + currency: { gp: 50 }, + }] }, + }); + // The world says nothing about any of those: the fields a live edit leaves out. + const worldActor = { _id: "actor-1", name: "Thalia", type: "character", system: {} }; + + await sync.writers.saveFoundryActor(sync.campaign, worldActor, false, false); + let row = sync.models["../models/Actor"].rows[0]; + assert.equal(row.traits, "A trait the campaign wrote", "a live push leaves what the campaign wrote alone"); + assert.deepEqual(row.tools, ["Thieves' tools"]); + assert.deepEqual(row.languages, ["Elvish"]); + assert.deepEqual(row.skills, { perception: true }); + assert.deepEqual(row.savingThrows, { dex: true }); + assert.deepEqual(row.currency, { gp: 50 }); + + await sync.writers.saveFoundryActor(sync.campaign, worldActor, false, true); + row = sync.models["../models/Actor"].rows[0]; + assert.equal(row.name, "Thalia", "the actor is still the one the world sent"); + assert.equal(row.traits, "", "a forced sync clears the trait the campaign wrote"); + assert.deepEqual(row.tools, [], "and the tools, languages, skills, saving throws and coin it held"); + assert.deepEqual(row.languages, []); + assert.deepEqual(row.skills, {}); + assert.deepEqual(row.savingThrows, {}); + assert.deepEqual(row.currency, {}); +}); + test("a hand-made item folds into the import instead of doubling, and keeps being updated", async () => { const sync = loadFoundryWriters({ seed: { items: [{ _id: "item-1", name: "Longsword", quantity: 1 }] }, diff --git a/tests/foundry-sync-toggle.test.js b/tests/foundry-sync-toggle.test.js index 87c4c3d7..6bc3996d 100644 --- a/tests/foundry-sync-toggle.test.js +++ b/tests/foundry-sync-toggle.test.js @@ -207,6 +207,7 @@ test("autosync on: Foundry CRUD events keep syncing with Dragonroll", async () = const pushes = world.syncRequests(); assert.equal(pushes.length, 1); assert.equal(pushes[0].body.actors[0]._id, "actor-1"); + assert.equal(pushes[0].body.replace, false, "a live actor change never asks Dragonroll to replace the campaign's edits"); }); test("autosync off: CRUD requests stop syncing", async () => { @@ -260,6 +261,7 @@ test("force sync sends selected actors to Dragonroll even with autosync off", as const pushes = world.syncRequests(); assert.equal(pushes.length, 1); assert.equal(pushes[0].body.fullSync, false, "a forced actor never prunes the campaign"); + assert.equal(pushes[0].body.replace, true, "a forced sync replaces everything the campaign holds for those actors"); assert.deepEqual(pushes[0].body.actors.map((actor) => actor._id), ["actor-2"]); assert.deepEqual(world.notifications.filter(([kind]) => kind !== "info"), []); });