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"