Fi saves and challenges rolls

This commit is contained in:
2024-12-30 10:29:42 +01:00
parent ddf547b959
commit a7f7e26d01
33 changed files with 193 additions and 147 deletions

View File

@ -11,23 +11,11 @@ export default class LethalFantasyRoll extends Roll {
get type() {
return this.options.type
}
get isChallenge() {
return this.type === "challenge"
get titleFormula() {
return this.options.titleFormula
}
get isSave() {
return this.type === ROLL_TYPE.SAVE
}
get isResource() {
return this.type === ROLL_TYPE.RESOURCE
}
get isDamage() {
return this.type === ROLL_TYPE.DAMAGE
}
get target() {
return this.options.target
}
@ -167,14 +155,14 @@ export default class LethalFantasyRoll extends Roll {
static async prompt(options = {}) {
let dice = "1D20"
let maxValue = 20
let formula = "1D20"
let baseFormula = "1D20"
let modifierFormula = "1d0"
let hasModifier = true
let hasChangeDice = false
if (options.rollType === "challenge") {
if (options.rollType === "challenge" || options.rollType === "save") {
if ( options.rollTarget.rollKey === "dying") {
dice = options.rollTarget.value
maxValue = Number(options.rollTarget.value.match(/\d+/)[0])
formula = `${dice}`
hasModifier = false
hasChangeDice = true
} else {
@ -198,8 +186,7 @@ export default class LethalFantasyRoll extends Roll {
let targetName
let dialogContext = {
isSave: options.rollType === "save",
isChallenge: options.rollType === "challenge",
rollType: options.rollType,
rollTarget: options.rollTarget,
rollModes,
hasModifier,
@ -209,7 +196,7 @@ export default class LethalFantasyRoll extends Roll {
fieldRollMode,
choiceModifier,
choiceDice,
formula,
baseFormula,
dice,
hasTarget: options.hasTarget,
modifier,
@ -241,36 +228,40 @@ export default class LethalFantasyRoll extends Roll {
// If the user cancels the dialog, exit
if (rollContext === null) return
let treshold
let fullModifier = 0
let titleFormula = ""
dice = rollContext.changeDice || dice
if (options.rollType === "challenge") {
if (options.rollType === "challenge" || options.rollType === "save") {
if (hasModifier) {
let bonus = (options.rollTarget.rollKey === "dying") ? 0 : Number(options.rollTarget.value)
let bonus = Number(options.rollTarget.value)
fullModifier = rollContext.modifier === "" ? 0 : parseInt(rollContext.modifier, 10) + bonus
if (fullModifier < 0) {
let modAbs = Math.abs(fullModifier)
formula = `${dice} - (d${modAbs + 1} - 1)`
} else if (fullModifier > 0) {
formula = `${dice} + (d${fullModifier + 1} - 1)`
if (fullModifier === 0) {
modifierFormula = "0"
} else {
formula = `${dice} + 1d0`
}
let modAbs = Math.abs(fullModifier)
modifierFormula = `d${modAbs + 1} - 1`
}
let sign = fullModifier < 0 ? "-" : "+"
titleFormula = `${dice}E ${sign} ${modifierFormula}`
} else {
modifierFormula = "0"
fullModifier = 0
formula = `${dice}`
baseFormula = `${dice}`
titleFormula = `${dice}E`
}
}
maxValue = Number(dice.match(/\d+/)[0])
maxValue = Number(baseFormula.match(/\d+$/)[0]) // Update the max value agains
const rollData = {
type: options.rollType,
rollType: options.rollType,
target: options.rollTarget,
actorId: options.actorId,
actorName: options.actorName,
actorImage: options.actorImage,
rollMode: rollContext.visibility,
hasTarget: options.hasTarget,
titleFormula,
targetName,
...rollContext,
}
@ -285,39 +276,45 @@ export default class LethalFantasyRoll extends Roll {
*/
if (Hooks.call("fvtt-lethal-fantasy.preRoll", options, rollData) === false) return
const roll = new this(formula, options.data, rollData)
await roll.evaluate()
const rollBase = new this(baseFormula, options.data, rollData)
await rollBase.evaluate()
const rollModifier = new Roll(modifierFormula, options.data, rollData)
await rollModifier.evaluate()
let rollTotal = -1
let diceResults = []
let resultType
if (options.rollType === "challenge") {
let d20result = roll.dice[0].results[0].result
diceResults.push({ dice: `${dice}`, value: d20result})
let d20sum = d20result
while (d20result === maxValue) {
let r = await new Roll(`${dice}`).evaluate()
d20result = r.dice[0].results[0].result
diceResults.push( {dice: `${dice}-1`, value: d20result-1})
d20sum += (d20result - 1)
if (options.rollType === "challenge" || options.rollType === "save") {
let diceResult = rollBase.dice[0].results[0].result
diceResults.push({ dice: `${dice}`, value: diceResult})
let diceSum = diceResult
while (diceResult === maxValue) {
let r = await new Roll(baseFormula).evaluate()
diceResult = r.dice[0].results[0].result
diceResults.push( {dice: `${dice}-1`, value: diceResult-1})
diceSum += (diceResult - 1)
}
if (hasModifier) {
let minus1 = (fullModifier === 0) ? 0 : 1
diceResults.push({ dice: `${roll.dice[1].formula}-${minus1}`, value: roll.dice[1].results[0].result - minus1 })
rollTotal = Math.max(d20sum + roll.dice[1].results[0].result - minus1, 0)
if (fullModifier !== 0) {
diceResults.push({ dice: `${rollModifier.formula}`, value: rollModifier.total })
if ( fullModifier < 0) {
rollTotal = Math.max(diceSum - rollModifier.total, 0)
} else {
rollTotal = diceSum + rollModifier.total
}
} else {
rollTotal = d20sum
rollTotal = diceSum
}
} else if (options.rollType === ROLL_TYPE.RESOURCE) {
//resultType = roll.total === 1 || roll.total === 2 ? "failure" : "success"
}
roll.options.resultType = resultType
roll.options.introText = roll._createIntroText()
roll.options.introTextTooltip = roll._createIntroTextTooltip()
roll.options.rollTotal = rollTotal
roll.options.diceResults = diceResults
roll.options.rollTarget = options.rollTarget
rollBase.options.resultType = resultType
rollBase.options.introText = rollBase._createIntroText()
rollBase.options.introTextTooltip = rollBase._createIntroTextTooltip()
rollBase.options.rollTotal = rollTotal
rollBase.options.diceResults = diceResults
rollBase.options.rollTarget = options.rollTarget
rollBase.options.titleFormula = titleFormula
/**
* A hook event that fires after the roll has been made.
@ -328,9 +325,9 @@ export default class LethalFantasyRoll extends Roll {
@param {LethalFantasyRoll} roll The resulting roll.
* @returns {boolean} Explicitly return `false` to prevent roll to be made.
*/
if (Hooks.call("fvtt-lethal-fantasy.Roll", options, rollData, roll) === false) return
if (Hooks.call("fvtt-lethal-fantasy.Roll", options, rollData, rollBase) === false) return
return roll
return rollBase
}
/**
@ -343,9 +340,9 @@ export default class LethalFantasyRoll extends Roll {
static createTitle(type, target) {
switch (type) {
case "challenge":
return `${game.i18n.localize("LETHALFANTASY.Dialog.titleSave")} : ${game.i18n.localize(`LETHALFANTASY.Manager.${target}`)}`
case ROLL_TYPE.RESOURCE:
return `${game.i18n.localize("LETHALFANTASY.Dialog.titleResource")} : ${game.i18n.localize(`LETHALFANTASY.Manager.${target}`)}`
return `${game.i18n.localize("LETHALFANTASY.Label.titleChallenge")}`
case "save":
return `${game.i18n.localize("LETHALFANTASY.Label.titleSave")}`
case ROLL_TYPE.DAMAGE:
return `${game.i18n.localize("LETHALFANTASY.Dialog.titleDamage")} : ${target}`
case ROLL_TYPE.ATTACK:
@ -395,11 +392,10 @@ export default class LethalFantasyRoll extends Roll {
diceTotal: this.dice.reduce((t, d) => t + d.total, 0),
isGM: game.user.isGM,
formula: this.formula,
titleFormula: this.titleFormula,
rollType: this.type,
rollTarget: this.rollTarget,
total: this.rollTotal,
isSave: this.isSave,
isChallenge: this.isChallenge,
isFailure: this.isFailure,
actorId: this.actorId,
diceResults: this.diceResults,