diff --git a/system/scripts/dice/dietype/l5r-base-die.js b/system/scripts/dice/dietype/l5r-base-die.js index 8077083..7ff5e76 100644 --- a/system/scripts/dice/dietype/l5r-base-die.js +++ b/system/scripts/dice/dietype/l5r-base-die.js @@ -33,6 +33,7 @@ export class L5rBaseDie extends DiceTerm { /** * Return a standardized representation for the displayed formula associated with this DiceTerm + * @return {string} * @override */ get formula() { @@ -53,6 +54,8 @@ export class L5rBaseDie extends DiceTerm { /** * Return the url of the result face + * @param {string|number} result + * @return {string} */ static getResultSrc(result) { return `${CONFIG.l5r5e.paths.assets}dices/default/${this.FACES[result].image}.svg`; @@ -62,13 +65,18 @@ export class L5rBaseDie extends DiceTerm { * Return the total result of the DiceTerm if it has been evaluated * Always zero for L5R dices to not count in total for regular dices * @override + * @return {number|string} */ get total() { return 0; } /** - * Evaluate the roll term, populating the results Array + * Evaluate the term, processing its inputs and finalizing its total. + * @param {boolean} minimize Minimize the result, obtaining the smallest possible value. + * @param {boolean} maximize Maximize the result, obtaining the largest possible value. + * @param {boolean} async Evaluate the term asynchronously, receiving a Promise as the returned value. This will become the default behavior in version 10.x + * @return {L5rBaseDie} The evaluated RollTerm * @override */ evaluate({ minimize = false, maximize = false, async = false } = {}) { @@ -78,7 +86,7 @@ export class L5rBaseDie extends DiceTerm { // Roll the initial number of dice for (let n = 1; n <= this.number; n++) { - this.roll({ minimize, maximize, async }); + this.roll({ minimize, maximize, async }); // TODO async/await in v10.x currently the inline roll is sync } // Apply modifiers @@ -112,15 +120,22 @@ export class L5rBaseDie extends DiceTerm { /** * Roll the DiceTerm by mapping a random uniform draw against the faces of the dice term + * @param {Object} options + * @return {DiceTermResult} * @override */ - roll(options) { + roll(options = { minimize: false, maximize: false }) { const roll = super.roll(options); //roll.l5r5e = this.l5r5e; return roll; } - /** @override */ + /** + * Construct a DiceTerm from a provided data object + * @param {object} data Provided data from an un-serialized term + * @return {DiceTerm} The constructed RollTerm + * @override + */ static fromData(data) { const roll = super.fromData(data); roll.l5r5e = data.l5r5e; @@ -128,7 +143,8 @@ export class L5rBaseDie extends DiceTerm { } /** - * Represent the data of the Roll as an object suitable for JSON serialization + * Represent the data of the DiceTerm as an object suitable for JSON serialization + * @return {string} * @override */ toJSON() { diff --git a/system/scripts/dice/roll-n-keep-dialog.js b/system/scripts/dice/roll-n-keep-dialog.js index 4df5505..7366721 100644 --- a/system/scripts/dice/roll-n-keep-dialog.js +++ b/system/scripts/dice/roll-n-keep-dialog.js @@ -577,7 +577,7 @@ export class RollnKeepDialog extends FormApplication { }; // Fill the data - roll.evaluate(); + roll.evaluate({ async: false }); // Modify results roll.terms.map((term) => { diff --git a/system/scripts/dice/roll.js b/system/scripts/dice/roll.js index 8c741e4..89f04a9 100644 --- a/system/scripts/dice/roll.js +++ b/system/scripts/dice/roll.js @@ -151,8 +151,6 @@ export class RollL5r5e extends Roll { this.l5r5e.summary[props] += parseInt(term.l5r5e[props]); }); this.l5r5e.summary.totalSuccess += term.totalSuccess; - - // TODO Others advantage/disadvantage } /** @@ -160,31 +158,28 @@ export class RollL5r5e extends Roll { * @override */ get total() { - return null; // TODO Bug 0.8.x : If not returning a null/0 value the chat ignore the template. If i do this, no inline but "/r 1dr" work fine :'( + if (!this._evaluated) { + return null; + } - // if (!this._evaluated) { - // return null; - // } - // - // let total = ""; - // - // // Regular dices total (eg 6) - // if (this.l5r5e.dicesTypes.std) { - // total = this._total; - // } - // - // // Add L5R summary - // if (this.l5r5e.dicesTypes.l5r) { - // const summary = this.l5r5e.summary; - // total += - // (this.l5r5e.dicesTypes.std ? " | " : "") + - // ["success", "explosive", "opportunity", "strife"] - // .map((props) => (summary[props] > 0 ? ` ${summary[props]}` : null)) - // .filter((c) => !!c) - // .join(" | "); - // } - // - // return total; + let total = ""; + + // Regular dices total (eg 6) + if (this.l5r5e.dicesTypes.std) { + total = this._total; + } + + // Add L5R summary + if (this.l5r5e.dicesTypes.l5r) { + const summary = this.l5r5e.summary; + total += + (this.l5r5e.dicesTypes.std ? " | " : "") + + ["success", "explosive", "opportunity", "strife"] + .map((props) => (summary[props] > 0 ? ` ${summary[props]}` : null)) + .filter((c) => !!c) + .join(" | "); + } + return total; } /** @@ -302,18 +297,13 @@ export class RollL5r5e extends Roll { async toMessage(messageData = {}, { rollMode = null, create = true } = {}) { // Perform the roll, if it has not yet been rolled if (!this._evaluated) { - this.evaluate(); + this.evaluate({ async: false }); } + // RollMode const rMode = rollMode || messageData.rollMode || game.settings.get("core", "rollMode"); - if (["gmroll", "blindroll"].includes(rMode)) { - messageData.whisper = ChatMessage.getWhisperRecipients("GM"); - } - if (rMode === "blindroll") { - messageData.blind = true; - } - if (rMode === "selfroll") { - messageData.whisper = [game.user.id]; + if (rollMode) { + ChatMessage.applyRollMode(messageData, rMode); } // Prepare chat data @@ -334,7 +324,11 @@ export class RollL5r5e extends Roll { messageData.roll = this; // Either create the message or just return the chat data - const message = await ChatMessage.implementation.create(messageData, { rollMode: rMode, temporary: !create }); + const message = await ChatMessage.implementation.create(messageData, { + rollMode: rMode, + temporary: !create, + isL5r5eTemplate: true, + }); return create ? message : message.data; } @@ -369,8 +363,8 @@ export class RollL5r5e extends Roll { toJSON() { const json = super.toJSON(); - json.data = duplicate(this.data); - json.l5r5e = duplicate(this.l5r5e); + json.data = foundry.utils.duplicate(this.data); + json.l5r5e = foundry.utils.duplicate(this.l5r5e); // lightweight the Actor if (json.l5r5e.actor) { diff --git a/system/scripts/hooks.js b/system/scripts/hooks.js index 6236cae..66feb3a 100644 --- a/system/scripts/hooks.js +++ b/system/scripts/hooks.js @@ -60,6 +60,32 @@ export default class HooksL5r5e { }); } + /** + * Modify the content for chat roll command + * + * The 0.8.x introduce a weird behavior, it get to roll.total as content event if a template was defined. + * So we need to intercept a chat roll (/r 1dr) for having them use the right template and not the inline (=total) + * + * @param {ChatMessage} document + * @param {Object} data + * @param {Object} options + * @param {string} userId + * @return {boolean} + */ + static preCreateChatMessage(document, data, options, userId) { + // Roll from DP have the "isL5r5eTemplate" option set + if (!document.isRoll || options?.isL5r5eTemplate || !document.data?.roll) { + return; + } + + // So now we have our wrong message only, redo it using the roll + const roll = game.l5r5e.RollL5r5e.fromJSON(document.data.roll); + roll.toMessage(); + + // Return false to let the system known we handled this + return false; + } + /** * Chat Message */ diff --git a/system/scripts/main-l5r5e.js b/system/scripts/main-l5r5e.js index 5eec5fe..dc9312e 100644 --- a/system/scripts/main-l5r5e.js +++ b/system/scripts/main-l5r5e.js @@ -172,3 +172,6 @@ Hooks.on("renderChatMessage", (message, html, data) => HooksL5r5e.renderChatMess Hooks.on("renderCombatTracker", (app, html, data) => HooksL5r5e.renderCombatTracker(app, html, data)); Hooks.on("renderCompendium", async (app, html, data) => HooksL5r5e.renderCompendium(app, html, data)); Hooks.on("diceSoNiceRollStart", (messageId, context) => HooksL5r5e.diceSoNiceRollStart(messageId, context)); +Hooks.on("preCreateChatMessage", (document, data, options, userId) => + HooksL5r5e.preCreateChatMessage(document, data, options, userId) +);