Skip to content

Commit

Permalink
refactor: implement socket workaround for permission issue
Browse files Browse the repository at this point in the history
  • Loading branch information
akrigline committed Mar 13, 2022
1 parent 5be0fd4 commit 419913e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
8 changes: 7 additions & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "retroactive-advantage-5e",
"title": "Retroactive Advantage DnD5e",
"description": "A module which allows rerolling d20 rolls from chat.",
"version": "0.0.1",
"version": "0.0.2",
"author": "Andrew Krigline",
"authors": [
{
Expand All @@ -20,6 +20,12 @@
"styles": [
"./styles/retroactive-advantage-5e.css"
],
"dependencies": [
{
"name": "socketlib"
}
],
"socket": true,
"minimumCoreVersion": "9",
"compatibleCoreVersion": "9",
"url": "https://github.com/ElfFriend-DnD/foundryvtt-retroactive-advantage-5e",
Expand Down
27 changes: 23 additions & 4 deletions scripts/retroactive-advantage-5e.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
class RetroAdvantage5e {
static MODULE_NAME = "retroactive-advantage-5e";
static MODULE_TITLE = "Retroactive Advantage DnD5e";
static SOCKET;

static log(...args) {
if (game.modules.get('_dev-mode')?.api?.getPackageDebugValue(this.MODULE_NAME)) {
console.log(this.MODULE_TITLE, '|', ...args);
}
}

static initSocket = () => {
this.SOCKET = socketlib.registerModule(this.MODULE_NAME);
this.SOCKET.register('handleChatButton', this._handleChatButton);
}

/**
* Helper method to grab a new d20 result
* @returns
Expand Down Expand Up @@ -113,12 +119,21 @@ class RetroAdvantage5e {
return newD20Roll;
}

/**
* Requests that the GM execute the message update to bypass permissions issue
* @param {*} action
* @param {*} messageId
*/
static async _handleRequestChatButton(action, messageId) {
return this.SOCKET.executeAsGM(this._handleChatButton, action, messageId);
}

/**
* Handles our button clicks from the chat log
* @param {string} action
* @param {string} messageId
*/
static async _handleButtonClick(action, messageId) {
static _handleChatButton = async (action, messageId) => {
try {
const {
DISADVANTAGE,
Expand Down Expand Up @@ -151,18 +166,20 @@ class RetroAdvantage5e {

const newMessageData = await newD20Roll.toMessage({}, { create: false });
delete newMessageData.timestamp;
delete newMessageData.user;

const messageUpdate = foundry.utils.mergeObject(
chatMessage.toJSON(),
newMessageData,
);

this.log('New stuff d20 roll', { roll: chatMessage.roll, newD20Roll }, {
chatMessage,
newMessageData,
messageUpdate
});

// currently seems broken for players without socket workaround
return chatMessage.update(messageUpdate);
} catch (err) {
console.error('A problem occurred with Retroactive Advantage 5e:', err);
Expand All @@ -188,15 +205,15 @@ class RetroAdvantage5e {
return;
}

RetroAdvantage5e._handleButtonClick(action, messageId);
RetroAdvantage5e._handleRequestChatButton(action, messageId);
});
});

/**
* Adorn any chat message with a d20 roll with our buttons
*/
Hooks.on('renderChatMessage', async (chatMessage, [html]) => {
if (!chatMessage.isRoll || !(chatMessage.roll instanceof CONFIG.Dice.D20Roll)) {
if (!(chatMessage.isAuthor || chatMessage.isOwner) || !chatMessage.isRoll || !(chatMessage.roll instanceof CONFIG.Dice.D20Roll)) {
return;
}

Expand Down Expand Up @@ -229,3 +246,5 @@ Hooks.on("init", RetroAdvantage5e.init);
Hooks.once('devModeReady', ({ registerPackageDebugFlag }) => {
registerPackageDebugFlag(RetroAdvantage5e.MODULE_NAME);
});

Hooks.once("socketlib.ready", RetroAdvantage5e.initSocket);

0 comments on commit 419913e

Please sign in to comment.