From 3e366a4c44bd44fb5db7e5c0bb2643d48f836831 Mon Sep 17 00:00:00 2001 From: Chris Ryan Date: Wed, 10 Dec 2025 22:09:28 +1000 Subject: [PATCH] Error checking for the fate type parsing in all potential problem locations --- daggerheart.mjs | 8 +++---- module/enrichers/FateRollEnricher.mjs | 34 +++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/daggerheart.mjs b/daggerheart.mjs index 3e4d05c8..7758adb5 100644 --- a/daggerheart.mjs +++ b/daggerheart.mjs @@ -10,7 +10,7 @@ import { enricherConfig, enricherRenderSetup } from './module/enrichers/_module. import { getCommandTarget, rollCommandToJSON } from './module/helpers/utils.mjs'; import { BaseRoll, DHRoll, DualityRoll, D20Roll, DamageRoll, FateRoll } from './module/dice/_module.mjs'; import { enrichedDualityRoll } from './module/enrichers/DualityRollEnricher.mjs'; -import { enrichedFateRoll } from './module/enrichers/FateRollEnricher.mjs'; +import { enrichedFateRoll, getFateType } from './module/enrichers/FateRollEnricher.mjs'; import { handlebarsRegistration, runMigrations, @@ -259,12 +259,10 @@ Hooks.on('chatMessage', (_, message) => { const { result: rollCommand, flavor } = result; - const fateTypeFromRollCommand = rollCommand?.type ? - (rollCommand?.type?.toLowerCase() == "fear" ? "Fear" : - (rollCommand?.type?.toLowerCase() == "hope" ? "Hope" : "BAD")) : "Hope"; + const fateTypeFromRollCommand = getFateType(rollCommand?.type); if (fateTypeFromRollCommand == "BAD") { - ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.fateParsing')); + ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.fateParsing') + ". Bad Fate Type. Valid Fate Types are 'Hope' and 'Fear'."); return false; } diff --git a/module/enrichers/FateRollEnricher.mjs b/module/enrichers/FateRollEnricher.mjs index 84d820db..d48c6e55 100644 --- a/module/enrichers/FateRollEnricher.mjs +++ b/module/enrichers/FateRollEnricher.mjs @@ -4,11 +4,33 @@ export default function DhFateRollEnricher(match, _options) { const roll = rollCommandToJSON(match[1], match[0]); if (!roll) return match[0]; + const fateTypeFromRoll = getFateType(roll?.type); + + if (fateTypeFromRoll == "BAD") { + ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.fateParsing') + ". Bad Fate Type. Valid Fate Types are 'Hope' and 'Fear'."); + return; + } + return getFateMessage(roll.result, roll?.flavor); } +export function getFateType(fateTypeValue) { + const fateTypeFromValue = fateTypeValue ? + (fateTypeValue.toLowerCase() == "fear" ? "Fear" : + (fateTypeValue.toLowerCase() == "hope" ? "Hope" : "BAD")) : "Hope"; + + + return fateTypeFromValue; +} + function getFateMessage(roll, flavor) { - const fateType = roll?.type ?? 'Hope'; + const fateType = getFateType(roll?.type); + + if (fateType == "BAD") { + ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.fateParsing') + ". Bad Fate Type. Valid Fate Types are 'Hope' and 'Fear'."); + return ''; + } + const fateTypeLocalized = fateType === "Hope" ? game.i18n.localize("DAGGERHEART.GENERAL.hope") : game.i18n.localize("DAGGERHEART.GENERAL.fear"); const title = flavor ?? fateTypeLocalized + ' ' + @@ -34,13 +56,21 @@ function getFateMessage(roll, flavor) { export const renderFateButton = async event => { const button = event.currentTarget, target = getCommandTarget({ allowNull: true }); + console.log("button", button); + + const fateTypeFromButton = getFateType(button.dataset?.fatetype); + + if (fateTypeFromButton == "BAD") { + ui.notifications.error(game.i18n.localize('DAGGERHEART.UI.Notifications.fateParsing') + ". Bad Fate Type. Valid Fate Types are 'Hope' and 'Fear'."); + return; + } await enrichedFateRoll( { target, title: button.dataset.title, label: button.dataset.label, - fateType: button.dataset.fatetype + fateType: fateTypeFromButton }, event );