Fix issue #54: Adding a background to an actor bug
Some checks failed
Test / test (push) Failing after 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-15 13:14:36 +00:00
parent 4b6200960f
commit 7e1ed4475e
3 changed files with 489 additions and 9 deletions

View File

@@ -1,5 +1,28 @@
const { numberOr, asText, plainText, stringList } = require('./foundry-fields');
// 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
// field's `fallback` option hands back the bare id when the item is gone. Dragonroll
// holds names, so a reference is read through the actor's items and an id that resolves
// to nothing is dropped. An id travelling back as a name is how Foundry ends up with a
// background item — and a details reference — that does not exist (Syndria98/dragonroll#54).
const FOUNDRY_DOCUMENT_ID = /^[A-Za-z0-9_-]{16}$/;
function looksLikeFoundryId(value) {
// Sixteen plain letters is a name; the ids Foundry generates carry a digit.
return FOUNDRY_DOCUMENT_ID.test(value) && /[0-9]/.test(value) && /[A-Za-z]/.test(value);
}
// The name a `system.details` entry stands for: an embedded item's id resolves to that
// item's name, and plain text (older system releases store the name here) is the name.
function detailsName(value, items) {
const text = asText(value).trim();
if (!text) return '';
const referenced = items.find((item) => asText(item?._id ?? item?.id).trim() === text);
if (referenced) return asText(referenced.name).trim();
return looksLikeFoundryId(text) ? '' : text;
}
function foundryActorToCharacter(foundryActor) {
const system = foundryActor.system || {};
const details = system.details || {};
@@ -25,11 +48,11 @@ function foundryActorToCharacter(foundryActor) {
type: ['character', 'npc', 'creature'].includes(savedDragonrollType)
? savedDragonrollType
: (['character', 'npc', 'creature'].includes(foundryActor.type) ? foundryActor.type : 'character'),
species: asText(details.race || details.species),
className: asText(details.class || classItem?.name),
subclass: asText(details.subclass || subclassItem?.name),
species: detailsName(details.race || details.species, items),
className: detailsName(details.class, items) || asText(classItem?.name),
subclass: detailsName(details.subclass, items) || asText(subclassItem?.name),
level: numberOr(details.level ?? classItem?.system?.levels, 1),
background: asText(details.background || backgroundItem?.name),
background: detailsName(details.background, items) || asText(backgroundItem?.name),
challengeRating: String(details.cr ?? ''),
description: plainText(details.biography?.value || details.biography),
image: asText(foundryActor.img || foundryActor.image),