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

@@ -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);
}