This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
foundryvtt-beam-saber/module/blades-roll.js

208 lines
5.1 KiB
JavaScript
Raw Normal View History

/**
* Roll Dice.
* @param {int} dice_amount
* @param {string} attribute_name
* @param {string} position
* @param {string} effect
*/
export async function bladesRoll(dice_amount, attribute_name = "", position = "risky", effect = "standard") {
2020-07-22 10:13:30 +00:00
// 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");
}
// ChatMessage.getSpeaker(controlledToken)
let zeromode = false;
if ( dice_amount < 0 ) { dice_amount = 0; }
if ( dice_amount == 0 ) { zeromode = true; dice_amount = 2; }
let r = new Roll( `${dice_amount}d6`, {} );
// show 3d Dice so Nice if enabled
2020-11-04 12:50:18 +00:00
r.roll();
if (niceDice) {
2020-07-31 09:31:13 +00:00
game.dice3d.showForRoll(r).then((displayed) => {
showChatRollMessage(r, zeromode, attribute_name, position, effect);
2020-11-04 12:50:18 +00:00
ui.chat.scrollBottom();
2020-07-24 09:51:04 +00:00
});
2020-07-22 10:13:30 +00:00
} else {
showChatRollMessage(r, zeromode, attribute_name, position, effect)
}
2020-07-24 09:51:04 +00:00
}
2020-07-24 09:51:04 +00:00
/**
* Shows Chat message.
*
* @param {Roll} r
* @param {Boolean} zeromode
* @param {String} attribute_name
* @param {string} position
* @param {string} effect
2020-07-24 09:51:04 +00:00
*/
async function showChatRollMessage(r, zeromode, attribute_name = "", position = "", effect = "") {
2020-07-24 09:51:04 +00:00
let speaker = ChatMessage.getSpeaker();
let isBelow070 = isNewerVersion('0.7.0', game.data.version);
2020-07-22 10:13:30 +00:00
let rolls = [];
2020-08-05 13:20:52 +00:00
let attribute_label = BladesHelpers.getAttributeLabel(attribute_name);
2020-07-24 09:51:04 +00:00
// Backward Compat for rolls.
2020-07-22 10:13:30 +00:00
if (isBelow070) {
rolls = (r.parts)[0].rolls;
} else {
rolls = (r.terms)[0].results;
}
2020-07-22 10:13:30 +00:00
// Retrieve Roll status.
let roll_status = getBladesRollStatus(rolls, zeromode);
let position_localize = '';
switch (position) {
case 'controlled':
position_localize = 'BITD.PositionControlled'
break;
case 'desperate':
position_localize = 'BITD.PositionDesperate'
break;
case 'risky':
default:
position_localize = 'BITD.PositionRisky'
}
let effect_localize = '';
switch (effect) {
case 'limited':
effect_localize = 'BITD.EffectLimited'
break;
case 'great':
effect_localize = 'BITD.EffectGreat'
break;
case 'standard':
default:
effect_localize = 'BITD.EffectStandard'
}
2020-12-16 10:20:27 +00:00
let result = await renderTemplate("systems/blades-in-the-dark/templates/blades-roll.html", {rolls: rolls, roll_status: roll_status, attribute_label: attribute_label, position: position, position_localize: position_localize, effect: effect, effect_localize: effect_localize});
2020-07-22 10:13:30 +00:00
let messageData = {
speaker: speaker,
content: result,
type: CONST.CHAT_MESSAGE_TYPES.OOC,
roll: r
}
CONFIG.ChatMessage.entityClass.create(messageData, {})
}
/**
* 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();
}
let roll_status = "failure"
if (sorted_rolls[0] === 6 && zeromode) {
roll_status = "critical-success";
}
else {
let use_die;
let prev_use_die = false;
if (zeromode) {
use_die = sorted_rolls[0];
}
else {
use_die = sorted_rolls[sorted_rolls.length - 1];
if (sorted_rolls.length - 2 >= 0) {
prev_use_die = sorted_rolls[sorted_rolls.length - 2]
}
}
// 1,2,3 = failure
if (use_die <= 3) {
roll_status = "failure";
}
// if 6 - check the prev highest one.
else if (use_die === 6) {
// 6,6 - critical success
if (prev_use_die && prev_use_die === 6) {
roll_status = "critical-success";
}
// 6 - success
else {
roll_status = "success";
}
}
// else (4,5) = partial success
else {
roll_status = "partial-success";
}
}
2020-07-22 10:13:30 +00:00
return roll_status;
}
2020-07-22 10:13:30 +00:00
/**
* Call a Roll popup.
*/
export async function simpleRollPopup() {
new Dialog({
title: `Simple Roll`,
content: `
<h2>${game.i18n.localize("BITD.RollSomeDice")}</h2>
<p>${game.i18n.localize("BITD.RollTokenDescription")}</p>
<form>
<div class="form-group">
<label>${game.i18n.localize("BITD.RollNumberOfDice")}:</label>
<select id="qty" name="qty">
${Array(11).fill().map((item, i) => `<option value="${i}">${i}d</option>`).join('')}
</select>
</div>
</form>
`,
buttons: {
yes: {
icon: "<i class='fas fa-check'></i>",
label: `Roll`,
callback: (html) => {
let diceQty = html.find('[name="qty"]')[0].value;
bladesRoll(diceQty);
},
},
no: {
icon: "<i class='fas fa-times'></i>",
label: game.i18n.localize('Cancel'),
},
},
default: "yes"
}).render(true);
}