foundry-dsa41-game/src/module/globals/DSARoll.mjs

72 lines
2.6 KiB
JavaScript

/**
*
* @param {[{result: Number}]|String} rolledDice either the result of a roll or a roll-formula
* @param {Number} value the value of this dice roll
* @param {[number]} werte an array of values that the dice roll is compared against
* @param {{getRollData:() => {} }} owner the actor of this roll that is required when rolledDice is a roll-formula
* @param {Number} lowerThreshold this is the threshold against a critical success is counted against
* @param {Number} upperThreshold this is the threshold against a critical fumble is counted against
* @param {Number} countToMeisterlich amount of critical success are needed for the dice roll to be a critical success
* @param {Number} countToPatzer amount of critical fumbles are needed for the dice roll to be a critical failure
* @returns {{tap: number, meisterlich: boolean, patzer: boolean, evaluated: Roll.Evaluated}}
*/
const evaluateRoll = async (rolledDice, {
value,
werte = [],
owner,
lowerThreshold = 1,
upperThreshold = 20,
countToMeisterlich = 3,
countToPatzer = 3,
}) => {
let tap = value;
let meisterlichCounter = 0;
let patzerCounter = 0;
let failCounter = 0;
let evaluated = null
if (typeof rolledDice == "string") { // we need to roll it ourself
let roll1 = new Roll(rolledDice, owner.getRollData());
evaluated = await roll1.evaluate()
rolledDice = evaluated.terms[0].results
}
if (tap < 0) { // increases rolledDice by |tap| (as this defacto lowers the target value)
rolledDice = rolledDice.map(({result, active}) => {
return {
result: result - tap,
active
}
})
tap = 0 // and then reset tap to 0 as we applied the reduction
}
rolledDice.forEach((rolledDie, index) => {
if (tap < 0 && rolledDie.result > werte[index]) {
tap -= rolledDie.result - werte[index];
if (tap < 0) { // konnte nicht vollständig ausgeglichen werden
failCounter++;
}
} else if (rolledDie.result > werte[index]) { // taw ist bereits aufgebraucht und wert kann nicht ausgeglichen werden
tap -= rolledDie.result - werte[index];
failCounter++;
}
if (rolledDie.result <= lowerThreshold) meisterlichCounter++;
if (rolledDie.result > upperThreshold) patzerCounter++;
})
return {
tap,
meisterlich: meisterlichCounter === countToMeisterlich,
patzer: patzerCounter === countToPatzer,
evaluated
}
}
export {
evaluateRoll
}