Foundry and discord bot
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
This commit is contained in:
82
backend/src/services/actor-foundry.js
Normal file
82
backend/src/services/actor-foundry.js
Normal file
@@ -0,0 +1,82 @@
|
||||
function numberOr(value, fallback) {
|
||||
const number = Number(value);
|
||||
return Number.isFinite(number) ? number : fallback;
|
||||
}
|
||||
|
||||
function asText(value) {
|
||||
return typeof value === 'string' ? value : '';
|
||||
}
|
||||
|
||||
function plainText(value) {
|
||||
return asText(value)
|
||||
.replace(/<br\s*\/?>/gi, '\n')
|
||||
.replace(/<\/p\s*>/gi, '\n')
|
||||
.replace(/<[^>]*>/g, ' ')
|
||||
.replace(/ /gi, ' ')
|
||||
.replace(/&/gi, '&')
|
||||
.replace(/</gi, '<')
|
||||
.replace(/>/gi, '>')
|
||||
.replace(/\s+\n/g, '\n')
|
||||
.trim();
|
||||
}
|
||||
|
||||
function traitList(value) {
|
||||
if (Array.isArray(value)) return value.map(asText).filter(Boolean);
|
||||
if (typeof value === 'string') return [value];
|
||||
return [];
|
||||
}
|
||||
|
||||
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 = [
|
||||
...traitList(traits.languages?.value),
|
||||
...traitList(traits.armor?.value),
|
||||
...traitList(traits.weapon?.value),
|
||||
...traitList(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 };
|
||||
Reference in New Issue
Block a user