diff --git a/src/handlebars/handlebars-helpers.ts b/src/handlebars/handlebars-helpers.ts index 669262eb..dcdde633 100644 --- a/src/handlebars/handlebars-helpers.ts +++ b/src/handlebars/handlebars-helpers.ts @@ -13,4 +13,64 @@ const helpers = { }, isEmpty: (input: Array | null | undefined): boolean => (input?.length ?? 0) === 0, + + toRomanNumerals, }; + +function toRomanNumerals(number: number): string { + return toRomanNumeralsRecurse(number, numerals) ?? ""; +} + +function toRomanNumeralsRecurse(number: number, numerals: number[]): string | undefined { + if (number === 0) { + return undefined; + } + const currentNumeral = numerals[0]; + if (currentNumeral === undefined) { + return undefined; + } + + const quotient = number / currentNumeral; + const remainder = number % currentNumeral; + const numberAsNumerals = numeralLookup[currentNumeral]?.repeat(quotient); + if (numberAsNumerals === undefined) { + return undefined; + } + const [, ...remainingNumerals] = numerals; + return numberAsNumerals + (toRomanNumeralsRecurse(remainder, remainingNumerals) ?? ""); +} + +const numeralLookup: Record = { + 1: "I", + 2: "II", + 3: "III", + 4: "IV", + 5: "V", + 6: "VI", + 7: "VII", + 8: "VIII", + 9: "IX", + 10: "X", + 20: "XX", + 30: "XXX", + 40: "XL", + 50: "L", + 60: "LX", + 70: "LXX", + 80: "LXXX", + 90: "XC", + 100: "C", + 200: "CC", + 300: "CCC", + 400: "CD", + 500: "D", + 600: "DC", + 700: "DCC", + 800: "DCCC", + 900: "CM", + 1000: "M", +}; + +const numerals = [ + 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, +]; diff --git a/templates/sheets/actor/tabs/character-abilities.hbs b/templates/sheets/actor/tabs/character-abilities.hbs index d1369f87..64ac89d1 100644 --- a/templates/sheets/actor/tabs/character-abilities.hbs +++ b/templates/sheets/actor/tabs/character-abilities.hbs @@ -18,7 +18,7 @@ SPDX-License-Identifier: MIT {{#each itemsByType.talent as |itemData id|}} {{#> systems/ds4/templates/sheets/actor/components/item-list-entry.hbs itemData=itemData}} {{!-- rank --}} -
{{itemData.data.rank.total}}
+
{{toRomanNumerals itemData.data.rank.total}}
{{/systems/ds4/templates/sheets/actor/components/item-list-entry.hbs}} {{/each}}