From 72c549717240c39cea376d6ca06373ccd6943ae1 Mon Sep 17 00:00:00 2001 From: Megastruktur Date: Wed, 22 Jul 2020 13:13:30 +0300 Subject: [PATCH] Backward Dice Compatibility --- CHANGELOG.txt | 8 ++-- README.md | 6 ++- module/blades-roll.js | 99 ++++++++++++++++++++++++++----------------- 3 files changed, 67 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 930d63a..2d0ec4b 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,5 +1,5 @@ -v0.7.0 +v0.7.1 -- Fix Dice rolls -- Fix Cohorts -- Get ready for FoundryVTT 0.7.0 \ No newline at end of file +- Adds Dice-so-nice support for FoundryVTT <0.7.0 +- Adds a proper Dice backward compatibility support (FVTT <0.7.0) +- Refactors Dice functionality a bit \ No newline at end of file diff --git a/README.md b/README.md index 74978f4..a5afb23 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ Contact Discord: `megastruktur#5704` in case you find any bugs or if you have any suggestions. +Contributors: +- `megastruktur#5704` +- `Tyronne I’Saurus#5415` + ## Usage `"Item" - all classes, crew types, upgrades, items, abilities, upgrades, etc.` @@ -43,10 +47,8 @@ Crew Types: ![alt screen][screenshot_compendium] ## To be done in the nearest future -- Clocks - Friends/rivals section - Stress/Harm dynamic values (can be modified by abilities but for now are hardcoded) -- Enhance Dice Rolling (ability modifiers) ## Troubleshooting - If you can't find the drag-n-dropped item, refer to "All Items" tab on each sheet. diff --git a/module/blades-roll.js b/module/blades-roll.js index 0b76d6f..0849a29 100644 --- a/module/blades-roll.js +++ b/module/blades-roll.js @@ -5,26 +5,22 @@ */ export async function bladesRoll(dice_amount, attribute_name = "") { + // Is Dice So Nice enabled ? + let niceDice = false; + + try { + niceDice = game.settings.get('dice-so-nice', 'settings').enabled; + } catch { + console.log("Dice-is-nice! not enabled"); + } - // Is Dice So Nice enabled ? - let niceDice = ''; - try { - niceDice = true; - game.settings.get('dice-so-nice', 'settings').enabled; - - } catch { - niceDice = false; - } - -// Is FoundryVTT core version >= 0.7.0 ? -// if using >= 0.7.0 New api calls enabled and DiceSoNice disabled until module update -// if using < 0.7.0 Old Roll API calls used and DiceSoNice enabled - - let isBelow070 = isNewerVersion('0.7.0', game.data.version); - if (isBelow070==false) {niceDice=false;} - + // Is FoundryVTT core version >= 0.7.0 ? + // if using >= 0.7.0 New api calls enabled and DiceSoNice disabled until module update + // if using < 0.7.0 Old Roll API calls used and DiceSoNice enabled + let isBelow070 = isNewerVersion('0.7.0', game.data.version); + if (isBelow070 == false) { niceDice=false; } let speaker = ChatMessage.getSpeaker(); // ChatMessage.getSpeaker(controlledToken) @@ -35,32 +31,64 @@ export async function bladesRoll(dice_amount, attribute_name = "") { let r = new Roll( `${dice_amount}d6`, {} ); - r.roll(); - - // show 3d Dice so Nice if enabled if (niceDice) { - - game.dice3d.showForRoll(r).then(displayed => { }); - + game.dice3d.showForRoll(r).then(displayed => {}); + } else { + r.roll(); } - // r.toMessage(); // Might be better as a DicePool with keep high/keep low intelligence, // but I want to get my hands into this directly, and I think players // will want to see all the dice happening. - let rolls=''; - let sorted_rolls=''; + let rolls = []; + if (isBelow070) { + rolls = (r.parts)[0].rolls; + } else { + rolls = (r.terms)[0].results; + } - if (isBelow070) { rolls = (r.parts)[0].rolls;} else { rolls = (r.terms)[0].results; } + // Retrieve Roll status. + let roll_status = getBladesRollStatus(rolls, zeromode); + let result = await renderTemplate("systems/blades-in-the-dark/templates/blades-roll.html", {rolls: rolls, roll_status: roll_status, attribute_name: attribute_name}); + let messageData = { + speaker: speaker, + content: result, + type: CONST.CHAT_MESSAGE_TYPES.OOC, + roll: r + } + + CONFIG.ChatMessage.entityClass.create(messageData, {}) + + return result; +} + +/** + * Get status of the Roll. + * - failure + * - partial-success + * - success + * - critical-success + * @param {Array} rolls + * @param {Boolean} zeromode + */ +export function getBladesRollStatus(rolls, zeromode = false) { + + // Dice API has changed in 0.7.0 so need to keep that in mind. + let isBelow070 = isNewerVersion('0.7.0', game.data.version); + + let sorted_rolls = []; // Sort roll values from lowest to highest. - if (isBelow070) { sorted_rolls = rolls.map(i => i.roll).sort(); } else { sorted_rolls = rolls.map(i => i.result).sort(); } - + if (isBelow070) { + sorted_rolls = rolls.map(i => i.roll).sort(); + } else { + sorted_rolls = rolls.map(i => i.result).sort(); + } let roll_status = "failure" @@ -104,20 +132,11 @@ export async function bladesRoll(dice_amount, attribute_name = "") { } - let result = await renderTemplate("systems/blades-in-the-dark/templates/blades-roll.html", {rolls: rolls, roll_status: roll_status, attribute_name: attribute_name}); + return roll_status; - let messageData = { - speaker: speaker, - content: result, - type: CONST.CHAT_MESSAGE_TYPES.OOC, - roll: r - } - - CONFIG.ChatMessage.entityClass.create(messageData, {}) - - return result; } + /** * Call a Roll popup. */