Merge pull request 'Fix #96: When right clicking actor on foundry, show action to force sync to dragonroll' (#102) from ai/issue-96 into master
Reviewed-on: #102
This commit was merged in pull request #102.
This commit is contained in:
@@ -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 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;
|
if (!foundryActorId || !fields.name) return null;
|
||||||
resolveWorldAssets(fields, campaign);
|
resolveWorldAssets(fields, campaign);
|
||||||
|
|
||||||
@@ -304,12 +304,16 @@ router.post('/sync/snapshot', async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fullSync = req.body.fullSync === true;
|
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
|
// 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.
|
// 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 folderSources = Array.isArray(req.body.folders) ? req.body.folders : null;
|
||||||
const actors = [];
|
const actors = [];
|
||||||
for (const source of actorSources || []) {
|
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);
|
if (mapping) actors.push(mapping);
|
||||||
}
|
}
|
||||||
const itemFolders = folderSources ? await syncFoundryItemFolders(campaign, folderSources) : null;
|
const itemFolders = folderSources ? await syncFoundryItemFolders(campaign, folderSources) : null;
|
||||||
|
|||||||
@@ -130,7 +130,11 @@ function foundryTraits(source, items) {
|
|||||||
return features.length ? features.join('\n\n') : undefined;
|
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 system = foundryActor.system || {};
|
||||||
const details = system.details || {};
|
const details = system.details || {};
|
||||||
const abilities = system.abilities || {};
|
const abilities = system.abilities || {};
|
||||||
@@ -169,7 +173,9 @@ function foundryActorToCharacter(foundryActor) {
|
|||||||
])),
|
])),
|
||||||
armorClass: numberOr(attributes.ac?.value ?? attributes.ac, 10),
|
armorClass: numberOr(attributes.ac?.value ?? attributes.ac, 10),
|
||||||
speed: numberOr(movement.walk ?? movement.value, 30),
|
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
|
// 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
|
// 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 next to the longswords is how traits and items got mixed up.
|
||||||
@@ -201,9 +207,21 @@ function foundryActorToCharacter(foundryActor) {
|
|||||||
const currency = foundryCurrency(system);
|
const currency = foundryCurrency(system);
|
||||||
if (currency) fields.currency = currency;
|
if (currency) fields.currency = currency;
|
||||||
const worldTraits = foundryTraits(foundryActor, items);
|
const worldTraits = foundryTraits(foundryActor, items);
|
||||||
const campaignTraits = asText(savedDragonrollFields.traits);
|
const campaignTraits = replace ? '' : asText(savedDragonrollFields.traits);
|
||||||
if (worldTraits || campaignTraits) fields.traits = worldTraits || campaignTraits;
|
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;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
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**.
|
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
|
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
|
when autosync is off, **replacing everything that campaign holds for them** (Syndria98/dragonroll#96):
|
||||||
snapshot does and a forced sync never claims to. Actors Dragonroll does not import (anything that is
|
what the campaign had edited on those actors is written over by what the world says. It replaces
|
||||||
not a character, NPC or creature) are refused with a notification.
|
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
|
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.
|
`/api/foundry/*`, which is never written into the campaign.
|
||||||
|
|||||||
@@ -566,7 +566,7 @@ function reportActorSyncError(error) {
|
|||||||
console.warn(`${MODULE_ID} | Character sync failed and will retry.`, 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();
|
const token = getSyncToken();
|
||||||
if (!token || !game.user?.isGM || !game.world || (!actors.length && !fullSync)) return;
|
if (!token || !game.user?.isGM || !game.world || (!actors.length && !fullSync)) return;
|
||||||
if (game.system.id !== "dnd5e") {
|
if (game.system.id !== "dnd5e") {
|
||||||
@@ -585,6 +585,9 @@ async function pushFoundryActors(actors, fullSync = false) {
|
|||||||
// The folder tree those world items stand in (see worldItemFolderSnapshots).
|
// The folder tree those world items stand in (see worldItemFolderSnapshots).
|
||||||
folders: worldItemFolderSnapshots(),
|
folders: worldItemFolderSnapshots(),
|
||||||
fullSync,
|
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);
|
}, token);
|
||||||
|
|
||||||
for (const mapping of result.actors || []) {
|
for (const mapping of result.actors || []) {
|
||||||
@@ -607,8 +610,9 @@ async function deleteDragonrollActor(foundryActorId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sending actors on purpose, whether or not autosync is on: this is the push the
|
// 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
|
// GM asks for. It replaces everything the campaign holds for those actors (Syndria98/
|
||||||
// prunes anything else the campaign holds.
|
// dragonroll#96), but it upserts them only, so unlike a full snapshot it never prunes
|
||||||
|
// anything else the campaign holds.
|
||||||
async function forceSyncActorsToDragonroll(actors) {
|
async function forceSyncActorsToDragonroll(actors) {
|
||||||
if (!game.user?.isGM || !game.world || !actors.length) return;
|
if (!game.user?.isGM || !game.world || !actors.length) return;
|
||||||
if (!getSyncToken()) {
|
if (!getSyncToken()) {
|
||||||
@@ -624,7 +628,7 @@ async function forceSyncActorsToDragonroll(actors) {
|
|||||||
if (!supported.length) return;
|
if (!supported.length) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await pushFoundryActors(supported);
|
await pushFoundryActors(supported, false, true);
|
||||||
ui.notifications?.info(supported.length === 1
|
ui.notifications?.info(supported.length === 1
|
||||||
? `${supported[0].name} was sent to the Dragonroll campaign.`
|
? `${supported[0].name} was sent to the Dragonroll campaign.`
|
||||||
: `${supported.length} actors were sent to the Dragonroll campaign.`);
|
: `${supported.length} actors were sent to the Dragonroll campaign.`);
|
||||||
@@ -1484,7 +1488,8 @@ Hooks.once("ready", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// A GM sends selected actors to Dragonroll from the actor directory's context
|
// 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) => {
|
Hooks.on("getActorDirectoryEntryContext", (html, options) => {
|
||||||
if (!game.user?.isGM || !getSyncToken()) return;
|
if (!game.user?.isGM || !getSyncToken()) return;
|
||||||
|
|
||||||
|
|||||||
@@ -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");
|
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 -----------------
|
// --- The module sends the names a sheet outside Foundry can read -----------------
|
||||||
|
|
||||||
function makeWorld({ actor, config = {}, worldItems = [] }) {
|
function makeWorld({ actor, config = {}, worldItems = [] }) {
|
||||||
|
|||||||
@@ -328,6 +328,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");
|
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 () => {
|
test("a hand-made item folds into the import instead of doubling, and keeps being updated", async () => {
|
||||||
const sync = loadFoundryWriters({
|
const sync = loadFoundryWriters({
|
||||||
seed: { items: [{ _id: "item-1", name: "Longsword", quantity: 1 }] },
|
seed: { items: [{ _id: "item-1", name: "Longsword", quantity: 1 }] },
|
||||||
|
|||||||
@@ -207,6 +207,7 @@ test("autosync on: Foundry CRUD events keep syncing with Dragonroll", async () =
|
|||||||
const pushes = world.syncRequests();
|
const pushes = world.syncRequests();
|
||||||
assert.equal(pushes.length, 1);
|
assert.equal(pushes.length, 1);
|
||||||
assert.equal(pushes[0].body.actors[0]._id, "actor-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 () => {
|
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();
|
const pushes = world.syncRequests();
|
||||||
assert.equal(pushes.length, 1);
|
assert.equal(pushes.length, 1);
|
||||||
assert.equal(pushes[0].body.fullSync, false, "a forced actor never prunes the campaign");
|
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(pushes[0].body.actors.map((actor) => actor._id), ["actor-2"]);
|
||||||
assert.deepEqual(world.notifications.filter(([kind]) => kind !== "info"), []);
|
assert.deepEqual(world.notifications.filter(([kind]) => kind !== "info"), []);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user