From 8c42f1b240e1c8fc8c2f06f97b8bdba98ab0e8d1 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Fri, 13 Jun 2025 19:27:39 -0600 Subject: [PATCH 1/3] Add a utility to the API for inferring a chat message's roll mode (and update the docs) --- module/api.mjs | 2 ++ module/utils/inferRollMode.mjs | 36 +++++++++++++++++++ .../_source/English_pBOyeBDuTeowuDOE.json | 6 ++-- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 module/utils/inferRollMode.mjs diff --git a/module/api.mjs b/module/api.mjs index 9faffaa..274f255 100644 --- a/module/api.mjs +++ b/module/api.mjs @@ -13,6 +13,7 @@ import { UserFlagDatabase } from "./utils/databases/UserFlag.mjs"; import { barGraphSchema, numberBucketSchema, rowSchema, stringBucketSchema, tableSchema } from "./utils/databases/model.mjs"; import { determinePrivacyFromRollMode, filterPrivateRows, PrivacyMode } from "./utils/privacy.mjs"; import { validateBucketConfig, validateValue } from "./utils/buckets.mjs"; +import { inferRollMode } from "./utils/inferRollMode.mjs"; const { deepFreeze } = foundry.utils; @@ -25,6 +26,7 @@ export const api = deepFreeze({ }, utils: { determinePrivacyFromRollMode, + inferRollMode, filterPrivateRows, validateValue, validateBucketConfig, diff --git a/module/utils/inferRollMode.mjs b/module/utils/inferRollMode.mjs new file mode 100644 index 0000000..4880c21 --- /dev/null +++ b/module/utils/inferRollMode.mjs @@ -0,0 +1,36 @@ +/** + * A helper function to try and infer what roll mode was used when creating a + * chat message in case the roll mode was not provided during the createChatMessage + * hook for whatever reason. + * + * **Disclaimer**: This inference is not totally correct. Particularly when inferring + * a GM's message, as it won't be able to distinguish between a self-roll and a + * private GM roll when it's + * + * @param {ChatMessage} message The ChatMessage document to infer from + * @returns The Foundry-specified roll mode + */ +export function inferRollMode(message) { + const whisperCount = message.whisper.length; + if (whisperCount === 0) { + return CONST.DICE_ROLL_MODES.PUBLIC; + }; + + if (whisperCount === 1 && message.whisper[0] === game.user.id) { + return CONST.DICE_ROLL_MODES.SELF; + }; + + let allGMs = true; + for (const userID of message.whisper) { + const user = game.users.get(userID); + if (!user) { continue }; + allGMs &&= user.isGM; + }; + + if (!allGMs) { + return CONST.DICE_ROLL_MODES.PUBLIC; + }; + return message.blind + ? CONST.DICE_ROLL_MODES.BLIND + : CONST.DICE_ROLL_MODES.PRIVATE; +}; diff --git a/packs/docs/_source/English_pBOyeBDuTeowuDOE.json b/packs/docs/_source/English_pBOyeBDuTeowuDOE.json index 91393f7..f6e18d1 100644 --- a/packs/docs/_source/English_pBOyeBDuTeowuDOE.json +++ b/packs/docs/_source/English_pBOyeBDuTeowuDOE.json @@ -250,7 +250,7 @@ "image": {}, "text": { "format": 1, - "content": "

The module provides a multitude of utility functions through it's API for usage however desired. This will go over them and describe their purpose.

filterPrivateRows

This method is intended to take @UUID[Compendium.stat-tracker.docs.JournalEntry.pBOyeBDuTeowuDOE.JournalEntryPage.S7Z6mZ0JablJVQJu]{rows} provided by the database and filter out any that the user would not be able to see normally. This is usually called by the database adapters so there's unlikely to be any reason to use it externally.

Available under <api>.utils.filterPrivateRows.

validateValue

Available under <api>.utils.validateValue.

validateBucketConfig

Available under <api>.utils.validateBucketConfig.

" + "content": "

The module provides a multitude of utility functions through it's API for usage however desired. This will go over them and describe their purpose.

filterPrivateRows

This method is intended to take @UUID[Compendium.stat-tracker.docs.JournalEntry.pBOyeBDuTeowuDOE.JournalEntryPage.S7Z6mZ0JablJVQJu]{rows} provided by the database and filter out any that the user would not be able to see normally. This is usually called by the database adapters so there's unlikely to be any reason to use it externally.

Available under <api>.utils.filterPrivateRows.

inferRollMode

This utility is intended to try and determine what roll mode was used to create a chat message. The inference is not entirely accurate because it struggles to differentiate between a GM rolling with a Private GM Roll and a Self Roll when there is only one GM present in the world.

Available under <api>.utils.inferRollMode

validateValue

Available under <api>.utils.validateValue.

validateBucketConfig

Available under <api>.utils.validateBucketConfig.

" }, "video": { "controls": true, @@ -266,11 +266,11 @@ "compendiumSource": null, "duplicateSource": null, "exportSource": null, - "coreVersion": "13.344", + "coreVersion": "13.345", "systemId": "empty-system", "systemVersion": "0.0.0", "createdTime": 1748330904988, - "modifiedTime": 1748394635911, + "modifiedTime": 1749864406851, "lastModifiedBy": "t2sWGWEYSMFrfBu3" }, "_key": "!journal.pages!pBOyeBDuTeowuDOE.TQzWrVTEz4oQhLPD" From 8905cb05bc365397cfde2c22dbad20ecce474b04 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Fri, 13 Jun 2025 19:28:08 -0600 Subject: [PATCH 2/3] Update the message listening to use createChatMessage instead of preCreateChatMessage --- module/hooks/createChatMessage.mjs | 41 +++++++++++++++++++++++++++ module/hooks/preCreateChatMessage.mjs | 30 -------------------- module/main.mjs | 2 +- 3 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 module/hooks/createChatMessage.mjs delete mode 100644 module/hooks/preCreateChatMessage.mjs diff --git a/module/hooks/createChatMessage.mjs b/module/hooks/createChatMessage.mjs new file mode 100644 index 0000000..12d1bdf --- /dev/null +++ b/module/hooks/createChatMessage.mjs @@ -0,0 +1,41 @@ +import { determinePrivacyFromRollMode } from "../utils/privacy.mjs"; +import { inferRollMode } from "../utils/inferRollMode.mjs"; + +Hooks.on(`createChatMessage`, (message, options, author) => { + console.log({ message, options, author}); + const isSelf = author === game.user.id; + const isNew = options.action === `create`; + const hasRolls = message.rolls?.length > 0; + const autoTracking = game.settings.get(__ID__, `autoTrackRolls`); + if (!isSelf || !isNew || !hasRolls || !autoTracking) { return }; + + /** An object of dice denomination to database rows */ + const rows = {}; + + const privacy = determinePrivacyFromRollMode(options.rollMode ?? inferRollMode(message)); + + /* + Goes through all of the dice within the message and grabs their result in order + to batch-save them all to the database handler. + */ + for (const roll of message.rolls) { + for (const die of roll.dice) { + const size = die.denomination; + rows[size] ??= []; + for (const result of die.results) { + rows[size].push({ privacy, value: result.result }); + }; + }; + }; + + // save all the rows, then rerender once we're properly done + for (const denomination in rows) { + CONFIG.stats.db.createRows( + `Dice/${denomination}`, + author, + rows[denomination], + { rerender: false }, + ); + }; + CONFIG.stats.db.render({ userUpdated: author }); +}); diff --git a/module/hooks/preCreateChatMessage.mjs b/module/hooks/preCreateChatMessage.mjs deleted file mode 100644 index 05a4d41..0000000 --- a/module/hooks/preCreateChatMessage.mjs +++ /dev/null @@ -1,30 +0,0 @@ -import { determinePrivacyFromRollMode } from "../utils/privacy.mjs"; - -Hooks.on(`preCreateChatMessage`, (_message, context, options, author) => { - const isNew = options.action === `create`; - const hasRolls = context.rolls?.length > 0; - const autoTracking = game.settings.get(__ID__, `autoTrackRolls`); - if (!isNew || !hasRolls || !autoTracking) { return }; - - /** An object of dice denomination to rows to add */ - const rows = {}; - - const privacy = determinePrivacyFromRollMode(options.rollMode); - for (const roll of context.rolls) { - for (const die of roll.dice) { - const size = die.denomination; - rows[size] ??= []; - for (const result of die.results) { - rows[size].push({ privacy, value: result.result }); - }; - }; - }; - - for (const denomination in rows) { - CONFIG.stats.db.createRows( - `Dice/${denomination}`, - author, - rows[denomination], - ); - }; -}); diff --git a/module/main.mjs b/module/main.mjs index 12d5422..4a90d3f 100644 --- a/module/main.mjs +++ b/module/main.mjs @@ -5,7 +5,7 @@ import "./hooks/init.mjs"; import "./hooks/ready.mjs"; // Document Hooks -import "./hooks/preCreateChatMessage.mjs"; +import "./hooks/createChatMessage.mjs"; // Dev Only imports if (import.meta.env.DEV) { From 1a8fcf04ab8838d59ed97d6a0ff0d3c01ae73216 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 22 Jun 2025 10:30:23 -0600 Subject: [PATCH 3/3] Remove stray log --- module/hooks/createChatMessage.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/module/hooks/createChatMessage.mjs b/module/hooks/createChatMessage.mjs index 12d1bdf..e686c70 100644 --- a/module/hooks/createChatMessage.mjs +++ b/module/hooks/createChatMessage.mjs @@ -2,7 +2,6 @@ import { determinePrivacyFromRollMode } from "../utils/privacy.mjs"; import { inferRollMode } from "../utils/inferRollMode.mjs"; Hooks.on(`createChatMessage`, (message, options, author) => { - console.log({ message, options, author}); const isSelf = author === game.user.id; const isNew = options.action === `create`; const hasRolls = message.rolls?.length > 0;