All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m6s
57 lines
2.9 KiB
JavaScript
57 lines
2.9 KiB
JavaScript
const { numberOr, asText, plainText, stringList } = require('./foundry-fields');
|
|
|
|
function foundryActorToCharacter(foundryActor) {
|
|
const system = foundryActor.system || {};
|
|
const details = system.details || {};
|
|
const abilities = system.abilities || {};
|
|
const attributes = system.attributes || {};
|
|
const movement = attributes.movement || {};
|
|
const items = Array.isArray(foundryActor.items) ? foundryActor.items : [];
|
|
const classItem = items.find((item) => item.type === 'class');
|
|
const subclassItem = items.find((item) => item.type === 'subclass');
|
|
const backgroundItem = items.find((item) => item.type === 'background');
|
|
const traits = system.traits || {};
|
|
const savedDragonrollFields = foundryActor.flags?.['dragonroll-foundry'] || {};
|
|
const savedDragonrollType = savedDragonrollFields.actorType;
|
|
const foundryProficiencies = [
|
|
...stringList(traits.languages?.value),
|
|
...stringList(traits.armor?.value),
|
|
...stringList(traits.weapon?.value),
|
|
...stringList(traits.tool?.value)
|
|
].join(', ');
|
|
|
|
return {
|
|
name: asText(foundryActor.name).trim(),
|
|
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),
|
|
level: numberOr(details.level ?? classItem?.system?.levels, 1),
|
|
background: asText(details.background || backgroundItem?.name),
|
|
challengeRating: String(details.cr ?? ''),
|
|
description: plainText(details.biography?.value || details.biography),
|
|
image: asText(foundryActor.img || foundryActor.image),
|
|
abilityScores: Object.fromEntries(['str', 'dex', 'con', 'int', 'wis', 'cha'].map((key) => [
|
|
key,
|
|
numberOr(abilities[key]?.value, 10)
|
|
])),
|
|
armorClass: numberOr(attributes.ac?.value ?? attributes.ac, 10),
|
|
speed: numberOr(movement.walk ?? movement.value, 30),
|
|
proficiencies: foundryProficiencies || asText(savedDragonrollFields.proficiencies),
|
|
traits: asText(savedDragonrollFields.traits),
|
|
inventory: items
|
|
.filter((item) => !['class', 'subclass', 'background', 'spell'].includes(item.type))
|
|
.map((item) => ({
|
|
name: asText(item.name).trim(),
|
|
quantity: numberOr(item.system?.quantity, 1),
|
|
weight: numberOr(item.system?.weight?.value ?? item.system?.weight, 0)
|
|
}))
|
|
.filter((item) => item.name),
|
|
spells: items.filter((item) => item.type === 'spell').map((item) => asText(item.name)).filter(Boolean).join('\n')
|
|
};
|
|
}
|
|
|
|
module.exports = { foundryActorToCharacter };
|