Skip to content

Commit

Permalink
refactor: update for v10
Browse files Browse the repository at this point in the history
  • Loading branch information
akrigline committed Sep 3, 2022
1 parent e0fa097 commit 02ad185
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
26 changes: 15 additions & 11 deletions module.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "retroactive-advantage-5e",
"id": "retroactive-advantage-5e",
"title": "Retroactive Advantage DnD5e",
"description": "A module which allows rerolling d20 rolls from chat.",
"version": "1.0.0",
"version": "2.0.0",
"author": "Andrew Krigline",
"authors": [
{
Expand All @@ -13,21 +13,25 @@
"patreon": "ElfFriend_DnD"
}
],
"system": [ "dnd5e" ],
"scripts": [
"./scripts/retroactive-advantage-5e.js"
],
"styles": [
"./styles/retroactive-advantage-5e.css"
],
"dependencies": [
{
"name": "socketlib"
}
],
"socket": true,
"minimumCoreVersion": "9",
"compatibleCoreVersion": "9",
"relationships": {
"systems": [{
"id": "dnd5e",
"type": "system",
"compatibility": {
"minimum": "2.0.0"
}
}]
},
"compatibility": {
"minimum": 10
},
"socket": false,
"url": "https://github.com/ElfFriend-DnD/foundryvtt-retroactive-advantage-5e",
"bugs": "https://github.com/ElfFriend-DnD/foundryvtt-retroactive-advantage-5e/issues",
"changelog": "https://github.com/ElfFriend-DnD/foundryvtt-retroactive-advantage-5e/releases",
Expand Down
56 changes: 26 additions & 30 deletions scripts/retroactive-advantage-5e.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
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);
}

/**
* Handles creating a new D20Roll instance with the updated roll method and totals based on a given one
Expand Down Expand Up @@ -138,15 +133,6 @@ 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
Expand All @@ -165,27 +151,34 @@ class RetroAdvantage5e {
if (!messageId || !action || !chatMessage) {
throw new Error('Missing Information')
}


const [roll] = chatMessage.rolls;

if (!(roll instanceof CONFIG.Dice.D20Roll)) {
return;
}


let newD20Roll;

const messageOptions = {
userId: chatMessage.data.user,
whisper: chatMessage.data.whisper,
blind: chatMessage.data.blind,
speaker: chatMessage.data.speaker,
userId: chatMessage.user,
whisper: chatMessage.whisper,
blind: chatMessage.blind,
speaker: chatMessage.speaker,
};

switch (action) {
case 'dis': {
newD20Roll = await this._makeNewRoll(chatMessage.roll, DISADVANTAGE, messageOptions);
newD20Roll = await this._makeNewRoll(roll, DISADVANTAGE, messageOptions);
break;
}
case 'norm': {
newD20Roll = await this._makeNewRoll(chatMessage.roll, NORMAL, messageOptions);
newD20Roll = await this._makeNewRoll(roll, NORMAL, messageOptions);
break;
}
case 'adv': {
newD20Roll = await this._makeNewRoll(chatMessage.roll, ADVANTAGE, messageOptions);
newD20Roll = await this._makeNewRoll(roll, ADVANTAGE, messageOptions);
break;
}
}
Expand All @@ -202,13 +195,12 @@ class RetroAdvantage5e {
newMessageData,
);

this.log('New stuff d20 roll', { roll: chatMessage.roll, newD20Roll }, {
this.log('New stuff d20 roll', { roll: 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 @@ -234,15 +226,21 @@ class RetroAdvantage5e {
return;
}

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

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

const [roll] = chatMessage.rolls;

if (!(roll instanceof CONFIG.Dice.D20Roll)) {
return;
}

Expand All @@ -252,7 +250,7 @@ class RetroAdvantage5e {
ADVANTAGE
} = CONFIG.Dice.D20Roll.ADV_MODE;

const advantageMode = chatMessage.roll?.options?.advantageMode;
const advantageMode = roll?.options?.advantageMode;

const diceElement = html.querySelector('.dice-roll');
const messageContent = html.querySelector('.message-content');
Expand All @@ -275,5 +273,3 @@ Hooks.on("init", RetroAdvantage5e.init);
Hooks.once('devModeReady', ({ registerPackageDebugFlag }) => {
registerPackageDebugFlag(RetroAdvantage5e.MODULE_NAME);
});

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

0 comments on commit 02ad185

Please sign in to comment.