Skip to content

Commit

Permalink
refactor: pre-format numbers for sphere attribute tooltips
Browse files Browse the repository at this point in the history
This results in an uneven presentation when floating point numbers are
displayed, but avoids e.g. caster level parts from classes with lower
progressions being rounded up in the display, leading to confusing and
incorrect sources.
  • Loading branch information
Ethaks committed Jun 18, 2024
1 parent 26cbd43 commit 57f9d45
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/module/sphere-tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const getSources = (
.filter((source) => !source.modifier)
.map((source) => ({
...source,
value: source.value ?? 0,
value: formatValue(source.value),
type:
pf1.config.bonusTypes[(source.type as BonusType) || (source.modifier as BonusType)] ||
pf1.config.bonusTypes.untyped,
Expand All @@ -197,20 +197,46 @@ const getSources = (
.filter((source) => source.modifier)
.map((source) => ({
...source,
value: source.value ?? 0,
value: formatValue(source.value),
type: pf1.config.bonusTypes[(source.type as BonusType) || (source.modifier as BonusType)],
}));
const capped = (actor.sourceDetails[modCapPath] ?? [])
.filter(filterDummySources)
.map((source) => ({ ...source, value: source.value ?? 0, type: localize("CappedByHD") }));
.map((source) => ({
...source,
value: formatValue(source.value),
type: localize("CappedByHD"),
}));
return { base, bonus, capped };
};

/**
* Filter out sources taken from another source (e.g. base CL to sphere CL), implied by the name being the bonus type.
*
* @param source - The source to filter
*/
const filterDummySources = (source: SourceEntry): boolean => {
if (source.name in CONFIG.PF1.bonusTypes) return false;
return true;
};

/**
* Format a number value to be suitable for tooltip display.
* If the value is a floating point number, it is formatted to one decimal place and returned as string;
* this results in the tooltip display becoming uneven, but is necessary to display e.g. correct partial caster level parts.
*
* @param value - The value to format
* @param options - Formatting options
* @param options.sign - Whether to include a sign for positive values
*/
const formatValue = (value = 0, { sign = true } = {}): number | string => {
const isFloat = value % 1 !== 0;
if (!isFloat) return value;
const fixed = parseFloat(`${value}`).toFixed(1);
if (sign) return value >= 0 ? `+${fixed}` : fixed;
return fixed;
};

type SphereTooltipId =
| "cl"
| "msb"
Expand Down

0 comments on commit 57f9d45

Please sign in to comment.