Fix issue #83: Add more things to the character sheet
All checks were successful
Test / test (push) Successful in 2s
All checks were successful
Test / test (push) Successful in 2s
This commit is contained in:
@@ -34,10 +34,57 @@ const ActorSchema = new Schema({
|
||||
max: { type: Number, min: 0, default: 1 }
|
||||
},
|
||||
proficiencies: { type: String, default: '' },
|
||||
// The skills the character is proficient with (Syndria98/dragonroll#83). Dragonroll holds
|
||||
// whole names; services/actor-foundry.js translates the short keys a Foundry world uses.
|
||||
skills: {
|
||||
acrobatics: { type: Boolean, default: false },
|
||||
animalHandling: { type: Boolean, default: false },
|
||||
arcana: { type: Boolean, default: false },
|
||||
athletics: { type: Boolean, default: false },
|
||||
deception: { type: Boolean, default: false },
|
||||
history: { type: Boolean, default: false },
|
||||
insight: { type: Boolean, default: false },
|
||||
intimidation: { type: Boolean, default: false },
|
||||
investigation: { type: Boolean, default: false },
|
||||
medicine: { type: Boolean, default: false },
|
||||
nature: { type: Boolean, default: false },
|
||||
perception: { type: Boolean, default: false },
|
||||
performance: { type: Boolean, default: false },
|
||||
persuasion: { type: Boolean, default: false },
|
||||
religion: { type: Boolean, default: false },
|
||||
sleightOfHand: { type: Boolean, default: false },
|
||||
stealth: { type: Boolean, default: false },
|
||||
survival: { type: Boolean, default: false }
|
||||
},
|
||||
// Which of the six saving throws the character adds its proficiency bonus to.
|
||||
savingThrows: {
|
||||
str: { type: Boolean, default: false },
|
||||
dex: { type: Boolean, default: false },
|
||||
con: { type: Boolean, default: false },
|
||||
int: { type: Boolean, default: false },
|
||||
wis: { type: Boolean, default: false },
|
||||
cha: { type: Boolean, default: false }
|
||||
},
|
||||
// Tool and language proficiencies: plain names, one per entry. The free-form
|
||||
// `proficiencies` text keeps whatever else the character is proficient with
|
||||
// (armour and weapons, say), which has no list of its own.
|
||||
tools: [{ type: String, trim: true }],
|
||||
languages: [{ type: String, trim: true }],
|
||||
// The coins the character holds, in the five D&D denominations.
|
||||
currency: {
|
||||
pp: { type: Number, min: 0, default: 0 },
|
||||
gp: { type: Number, min: 0, default: 0 },
|
||||
ep: { type: Number, min: 0, default: 0 },
|
||||
sp: { type: Number, min: 0, default: 0 },
|
||||
cp: { type: Number, min: 0, default: 0 }
|
||||
},
|
||||
inventory: [{
|
||||
name: { type: String, trim: true, default: '' },
|
||||
quantity: { type: Number, min: 1, default: 1 },
|
||||
weight: { type: Number, min: 0, default: 0 }
|
||||
weight: { type: Number, min: 0, default: 0 },
|
||||
// The gear's own picture: what a Foundry world sends with the item it stands
|
||||
// for, shown on the left of the inventory row.
|
||||
icon: { type: String, default: '' }
|
||||
}],
|
||||
traits: { type: String, default: '' },
|
||||
spells: { type: String, default: '' },
|
||||
|
||||
@@ -22,6 +22,13 @@ function barPool(existing, value) {
|
||||
return { current, max };
|
||||
}
|
||||
|
||||
// The tool and language lists (Syndria98/dragonroll#83): plain names, blank ones and
|
||||
// repeats dropped.
|
||||
function textList(value) {
|
||||
if (!Array.isArray(value)) return [];
|
||||
return [...new Set(value.map((name) => String(name).trim()).filter(Boolean))];
|
||||
}
|
||||
|
||||
// Folders the campaign's actors are grouped into, in the shape the sidebar groups by
|
||||
// (the notes, items and maps each keep a tree of their own, see models/Folder.js).
|
||||
async function actorFolders(campaignId) {
|
||||
@@ -65,7 +72,10 @@ router.post('/create', async (req, res) => {
|
||||
|
||||
router.post('/update', async (req, res) => {
|
||||
try {
|
||||
const { id, name, species, className, subclass, level, abilityScores, armorClass, speed, hitPoints, hitDice, proficiencies, inventory, traits, spells, image, folder } = req.body;
|
||||
const {
|
||||
id, name, species, className, subclass, level, abilityScores, armorClass, speed, hitPoints, hitDice,
|
||||
proficiencies, skills, savingThrows, tools, languages, currency, inventory, traits, spells, image, folder
|
||||
} = req.body;
|
||||
if (!id) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const actor = await Actor.findById(id);
|
||||
@@ -92,6 +102,14 @@ router.post('/update', async (req, res) => {
|
||||
if (hitPoints !== undefined) actor.hitPoints = barPool(actor.hitPoints, hitPoints);
|
||||
if (hitDice !== undefined) actor.hitDice = barPool(actor.hitDice, hitDice);
|
||||
if (proficiencies !== undefined) actor.proficiencies = proficiencies;
|
||||
// The lists the sheet toggles and fills in (Syndria98/dragonroll#83): which skills
|
||||
// and saving throws the character is proficient with, its tools, its languages and
|
||||
// the coins it carries.
|
||||
if (skills !== undefined) actor.skills = skills;
|
||||
if (savingThrows !== undefined) actor.savingThrows = savingThrows;
|
||||
if (tools !== undefined) actor.tools = textList(tools);
|
||||
if (languages !== undefined) actor.languages = textList(languages);
|
||||
if (currency !== undefined) actor.currency = currency;
|
||||
if (inventory !== undefined) actor.inventory = inventory;
|
||||
if (traits !== undefined) actor.traits = traits;
|
||||
if (spells !== undefined) actor.spells = spells;
|
||||
|
||||
@@ -23,6 +23,112 @@ function detailsName(value, items) {
|
||||
return looksLikeFoundryId(text) ? '' : text;
|
||||
}
|
||||
|
||||
// --- Skills, saving throws, tools, languages and coins (Syndria98/dragonroll#83) ---
|
||||
|
||||
// The eighteen skills a 5e character has, as the system keys them and as Dragonroll
|
||||
// names them. The short keys mean nothing outside Foundry, and Dragonroll's sheet holds
|
||||
// the names it shows a user.
|
||||
const FOUNDRY_SKILLS = {
|
||||
acr: 'acrobatics', ani: 'animalHandling', arc: 'arcana', ath: 'athletics', dec: 'deception',
|
||||
his: 'history', ins: 'insight', itm: 'intimidation', inv: 'investigation', med: 'medicine',
|
||||
nat: 'nature', prc: 'perception', perf: 'performance', prs: 'persuasion', rel: 'religion',
|
||||
sle: 'sleightOfHand', stl: 'stealth', sur: 'survival'
|
||||
};
|
||||
const ABILITIES = ['str', 'dex', 'con', 'int', 'wis', 'cha'];
|
||||
const CURRENCIES = ['pp', 'gp', 'ep', 'sp', 'cp'];
|
||||
|
||||
// Whether one of the system's proficiency fields says the character has that
|
||||
// proficiency. The system has held it several ways across its releases — a boolean, a
|
||||
// number of proficiency steps, a list of proficiency modes ('prof', 'exp'), or one of
|
||||
// those under `value`, `proficient` or `prof` — and worlds run different releases of
|
||||
// it. Anything above nothing counts as proficient.
|
||||
function isProficient(value, depth = 0) {
|
||||
if (value === undefined || value === null || depth > 3) return false;
|
||||
if (typeof value === 'boolean') return value;
|
||||
if (typeof value === 'number') return value > 0;
|
||||
if (typeof value === 'string') {
|
||||
const text = value.trim();
|
||||
if (!text) return false;
|
||||
const number = Number(text);
|
||||
return Number.isFinite(number) ? number > 0 : text.toLowerCase() !== 'none';
|
||||
}
|
||||
if (Array.isArray(value)) return value.some((entry) => isProficient(entry, depth + 1));
|
||||
return ['value', 'proficient', 'prof'].some((key) => isProficient(value[key], depth + 1));
|
||||
}
|
||||
|
||||
// A toggled map (skill name or ability → proficient) from whatever the actor lists.
|
||||
// `null` says the actor says nothing about them at all, which is what keeps a live
|
||||
// Foundry edit from clearing the toggles the campaign set by hand.
|
||||
function toggleMap(sources) {
|
||||
const toggled = {};
|
||||
for (const [name, value] of Object.entries(sources)) {
|
||||
if (value !== undefined) toggled[name] = isProficient(value);
|
||||
}
|
||||
return Object.keys(toggled).length ? toggled : null;
|
||||
}
|
||||
|
||||
function foundrySkills(system) {
|
||||
const skills = system.skills && typeof system.skills === 'object' ? system.skills : {};
|
||||
return toggleMap(Object.fromEntries(Object.entries(FOUNDRY_SKILLS)
|
||||
.map(([key, name]) => [name, skills[key]])));
|
||||
}
|
||||
|
||||
function foundrySavingThrows(system) {
|
||||
const saves = system.saves && typeof system.saves === 'object' ? system.saves : {};
|
||||
return toggleMap(Object.fromEntries(ABILITIES.map((key) => [
|
||||
key,
|
||||
system.savingThrows?.[key] ?? system.abilities?.[key]?.save ?? saves[key]
|
||||
])));
|
||||
}
|
||||
|
||||
// Tools and languages: the module sends the labels its own CONFIG holds, because a
|
||||
// world's proficiency lists are keys — and, since the third system release, language
|
||||
// documents — that mean nothing outside Foundry. Without them the keys are the best
|
||||
// there is to name a proficiency with, and a bare Foundry document id is worse than
|
||||
// nothing at all. `null` says the actor says nothing about the kind, `[]` that it has
|
||||
// none of it. Dragonroll names its lists in full; the world keeps its tools under
|
||||
// `system.traits.tool` and its languages under `system.traits.languages`.
|
||||
const TRAIT_FIELDS = { tools: 'tool', languages: 'languages' };
|
||||
|
||||
function foundryTraitNames(actor, kind) {
|
||||
const labels = actor.dragonroll?.[kind];
|
||||
if (Array.isArray(labels)) {
|
||||
return [...new Set(labels.map((label) => asText(label).trim()).filter(Boolean))];
|
||||
}
|
||||
const trait = actor.system?.traits?.[TRAIT_FIELDS[kind] ?? kind];
|
||||
if (!trait || typeof trait !== 'object') return null;
|
||||
const names = [
|
||||
...stringList(trait.value),
|
||||
...asText(trait.custom).split(',').map((name) => name.trim())
|
||||
].filter((name) => name && !looksLikeFoundryId(name));
|
||||
return [...new Set(names)];
|
||||
}
|
||||
|
||||
function foundryCurrency(system) {
|
||||
const coins = system.currency;
|
||||
if (!coins || typeof coins !== 'object') return null;
|
||||
return Object.fromEntries(CURRENCIES.map((key) => [key, Math.max(0, numberOr(coins[key], 0))]));
|
||||
}
|
||||
|
||||
// The character's traits as the world writes them: the free-text details field some
|
||||
// system releases keep, or its trait items, which are how a monster's traits and a
|
||||
// character's racial and class features are written. `undefined` says the world holds
|
||||
// no traits to show.
|
||||
function foundryTraits(source, items) {
|
||||
const text = plainText(source.system?.details?.trait);
|
||||
if (text) return text;
|
||||
const features = items
|
||||
.filter((item) => item.type === 'feat' && stringList(item.system?.featType?.value ?? item.system?.featType)
|
||||
.some((type) => asText(type).toLowerCase().startsWith('tr')))
|
||||
.map((item) => {
|
||||
const name = asText(item.name).trim();
|
||||
const description = plainText(item.system?.description?.value ?? item.system?.description);
|
||||
return name ? `${name}\n${description}`.trim() : '';
|
||||
})
|
||||
.filter(Boolean);
|
||||
return features.length ? features.join('\n\n') : undefined;
|
||||
}
|
||||
|
||||
function foundryActorToCharacter(foundryActor) {
|
||||
const system = foundryActor.system || {};
|
||||
const details = system.details || {};
|
||||
@@ -43,7 +149,7 @@ function foundryActorToCharacter(foundryActor) {
|
||||
...stringList(traits.tool?.value)
|
||||
].join(', ');
|
||||
|
||||
return {
|
||||
const fields = {
|
||||
name: asText(foundryActor.name).trim(),
|
||||
type: ['character', 'npc', 'creature'].includes(savedDragonrollType)
|
||||
? savedDragonrollType
|
||||
@@ -56,24 +162,45 @@ function foundryActorToCharacter(foundryActor) {
|
||||
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) => [
|
||||
abilityScores: Object.fromEntries(ABILITIES.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)
|
||||
weight: numberOr(item.system?.weight?.value ?? item.system?.weight, 0),
|
||||
icon: asText(item.img).trim()
|
||||
}))
|
||||
.filter((item) => item.name),
|
||||
spells: items.filter((item) => item.type === 'spell').map((item) => asText(item.name)).filter(Boolean).join('\n')
|
||||
};
|
||||
|
||||
// What the world says about each of these, when it says anything: leaving a field
|
||||
// out of the character is what keeps a live Foundry edit from clearing what the
|
||||
// campaign toggled or wrote for it. Traits lead from the world when it has them, the
|
||||
// way the proficiencies do; the text the module carries back on its flag is what the
|
||||
// tab shows for an actor the world says nothing about.
|
||||
const skills = foundrySkills(system);
|
||||
if (skills) fields.skills = skills;
|
||||
const savingThrows = foundrySavingThrows(system);
|
||||
if (savingThrows) fields.savingThrows = savingThrows;
|
||||
const tools = foundryTraitNames(foundryActor, 'tools');
|
||||
if (tools) fields.tools = tools;
|
||||
const languages = foundryTraitNames(foundryActor, 'languages');
|
||||
if (languages) fields.languages = languages;
|
||||
const currency = foundryCurrency(system);
|
||||
if (currency) fields.currency = currency;
|
||||
const worldTraits = foundryTraits(foundryActor, items);
|
||||
const campaignTraits = asText(savedDragonrollFields.traits);
|
||||
if (worldTraits || campaignTraits) fields.traits = worldTraits || campaignTraits;
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
module.exports = { foundryActorToCharacter };
|
||||
|
||||
Reference in New Issue
Block a user