From 76e86f3fffc0c55a9f5dd77e1018bd0fb598a254 Mon Sep 17 00:00:00 2001 From: jalensailin Date: Thu, 21 Sep 2023 12:27:48 -0400 Subject: [PATCH] display %modifier in rolls if applicable --- module/actor/actor-sheet.js | 12 ------- module/deltagreen.js | 4 +-- module/other/utility-functions.js | 56 ++++++++++++++++++++----------- module/roll/roll.js | 21 +++++++----- 4 files changed, 51 insertions(+), 42 deletions(-) diff --git a/module/actor/actor-sheet.js b/module/actor/actor-sheet.js index 46de9fe..534884c 100644 --- a/module/actor/actor-sheet.js +++ b/module/actor/actor-sheet.js @@ -583,18 +583,6 @@ export class DeltaGreenActorSheet extends ActorSheet { return this.actor.createEmbeddedDocuments("Item", [itemData]); } - _formatStringWithLeadingPlus(number){ - let s = ""; - - if(number > 0){ - s += "+" - } - - s += number.toString(); - - return s; - } - /** * Handle clickable rolls. * diff --git a/module/deltagreen.js b/module/deltagreen.js index 7a10303..4159a63 100644 --- a/module/deltagreen.js +++ b/module/deltagreen.js @@ -9,7 +9,7 @@ import * as DGRolls from "./roll/roll.js"; import { registerSystemSettings } from "./settings.js" import { preloadHandlebarsTemplates } from "./templates.js"; import { ParseDeltaGreenStatBlock } from "./other/stat-parser-macro.js"; -import { localizeWithFallback } from "./other/utility-functions.js"; +import DGUtils from "./other/utility-functions.js"; Hooks.once('init', async function() { @@ -52,7 +52,7 @@ Hooks.once('init', async function() { // Add Handlebars helpers Handlebars.registerHelper('localizeWithFallback', function(value, fallbackValue) { - return localizeWithFallback(value, fallbackValue); + return DGUtils.localizeWithFallback(value, fallbackValue); }); Handlebars.registerHelper('concat', function() { diff --git a/module/other/utility-functions.js b/module/other/utility-functions.js index 9d4d0ea..705c7fe 100644 --- a/module/other/utility-functions.js +++ b/module/other/utility-functions.js @@ -1,24 +1,40 @@ -// Attempt to localize a keyword, and gracefully fallback to something else if the key does not exist. -// This way people using incomplete translations will not as many crashes or bugs when playing against a newly updated system. -export function localizeWithFallback(key, fallback){ - try{ - let translatedValue = game.i18n.localize(key); +/* globals game */ - if(translatedValue != key){ - return translatedValue; - } - else{ - - console.warn(`Untranslated localization key '${key}'.`); - // can't have single quotes in a handlebars string literal input, so sort of hack in an escape character for them. - fallback = fallback.replace("'", "'"); - - return fallback; - } - } - catch(ex){ - console.warn(ex); - return fallback; +export default class DGUtils { + /** + * Attempt to localize a keyword, and gracefully fallback to something else if the key does not exist. + * This way people using incomplete translations will not as many crashes or bugs when playing against a newly updated system. + * + * @param {String} key - localization key + * @param {String} fallback - string to fall back to if translation does not exist for key + * @returns + */ + static localizeWithFallback(key, fallback){ + try { + let translatedValue = game.i18n.localize(key); + if (translatedValue != key) { + return translatedValue; + } else { + console.warn(`Untranslated localization key '${key}'.`); + // can't have single quotes in a handlebars string literal input, so sort of hack in an escape character for them. + fallback = fallback.replace("'", "'"); + return fallback; + } + } catch(ex) { + console.warn(ex); + return fallback; } + } + + /** + * Format a number with a leading plus. + * + * @param {Number} number + * @returns {String} - the stringified number with a leading plus if applicable + * + */ + static formatStringWithLeadingPlus(number){ + return number > 0 ? `+${number}` : number.toString(); + } } \ No newline at end of file diff --git a/module/roll/roll.js b/module/roll/roll.js index a81ce71..f0cb1ac 100644 --- a/module/roll/roll.js +++ b/module/roll/roll.js @@ -1,6 +1,6 @@ /* globals game Roll ChatMessage AudioHelper renderTemplate Dialog */ -import { localizeWithFallback } from "../other/utility-functions.js" +import DGUtils from "../other/utility-functions.js" export class DGRoll extends Roll { /** @@ -150,11 +150,11 @@ export class DGPercentileRoll extends DGRoll { return new Promise((resolve, reject) => { new Dialog({ content: html, - title: localizeWithFallback("DG.ModifySkillRollDialogue.Title", "Modify Roll"), + title: DGUtils.localizeWithFallback("DG.ModifySkillRollDialogue.Title", "Modify Roll"), default: "roll", buttons: { roll:{ - label: localizeWithFallback("DG.Roll.Roll", "Roll"), + label: DGUtils.localizeWithFallback("DG.Roll.Roll", "Roll"), callback: html => { try { let targetModifier = html.find("[name='targetModifier']").val(); // this is text as a heads up @@ -198,12 +198,13 @@ export class DGPercentileRoll extends DGRoll { rollMode = 'blindroll'; } - let label = ''; + let label = `${game.i18n.localize("DG.Roll.Rolling")} ${this.localizedKey} ${game.i18n.localize("DG.Roll.Target")} ${this.target + this.modifier}`; // "Inhuman" stat being rolled. See function for details. if (this.isInhuman) { label = `${game.i18n.localize("DG.Roll.Rolling")} ${this.localizedKey} [${game.i18n.localize("DG.Roll.Inhuman").toUpperCase()}] ${game.i18n.localize("DG.Roll.Target")} ${this.target + this.modifier}`; - } else { - label = `${game.i18n.localize("DG.Roll.Rolling")} ${this.localizedKey} ${game.i18n.localize("DG.Roll.Target")} ${this.target + this.modifier}`; + } + if (this.modifier) { + label += ` (${DGUtils.formatStringWithLeadingPlus(this.modifier)}%)` } let resultString = '', styleOverride = ''; @@ -356,7 +357,11 @@ export class DGLethalityRoll extends DGPercentileRoll { } const { nonLethalDamage } = this; - let label = `${game.i18n.localize("DG.Roll.Rolling")} ${game.i18n.localize("DG.Roll.Lethality").toUpperCase()} ${game.i18n.localize("DG.Roll.For")} ${this.item.name.toUpperCase()} ${game.i18n.localize("DG.Roll.Target")} ${this.target}`; + let label = `${game.i18n.localize("DG.Roll.Rolling")} ${game.i18n.localize("DG.Roll.Lethality").toUpperCase()} ${game.i18n.localize("DG.Roll.For")} ${this.item.name.toUpperCase()} ${game.i18n.localize("DG.Roll.Target")} ${this.target + this.modifier}`; + if (this.modifier) { + label += ` (${DGUtils.formatStringWithLeadingPlus(this.modifier)}%)` + } + let html = ''; html += `
`; html += `
`; @@ -528,7 +533,7 @@ export class DGSanityDamageRoll extends DGRoll { const [lowResult, highResult] = this.damageResults; - const flavor = `Rolling ${localizeWithFallback('DG.Generic.SanDamage', 'SAN DAMAGE')} For ${lowDie.formula} / ${highDie.formula}`; + const flavor = `Rolling ${DGUtils.localizeWithFallback('DG.Generic.SanDamage', 'SAN DAMAGE')} For ${lowDie.formula} / ${highDie.formula}`; let html = ''; html += `
`;