import { ROLL_TYPE } from "../config/system.mjs" import LethalFantasyUtils from "../utils.mjs" export default class LethalFantasyRoll extends Roll { /** * The HTML template path used to render dice checks of this type * @type {string} */ static CHAT_TEMPLATE = "systems/fvtt-lethal-fantasy/templates/chat-message.hbs" get type() { return this.options.type } get isChallenge() { return this.type === "challenge" } 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 } get value() { return this.options.value } get treshold() { return this.options.treshold } get actorId() { return this.options.actorId } get actorName() { return this.options.actorName } get actorImage() { return this.options.actorImage } get introText() { return this.options.introText } get introTextTooltip() { return this.options.introTextTooltip } get modifier() { return this.options.modifier } get resultType() { return this.options.resultType } get isFailure() { return this.resultType === "failure" } get hasTarget() { return this.options.hasTarget } get targetName() { return this.options.targetName } get targetArmor() { return this.options.targetArmor } get targetMalus() { return this.options.targetMalus } get realDamage() { return this.options.realDamage } get rollTotal() { return this.options.rollTotal } get diceResults() { return this.options.diceResults } get rollTarget() { return this.options.rollTarget } /** * Generates introductory text based on the roll type. * * @returns {string} The formatted introductory text for the roll. */ _createIntroText() { let text switch (this.type) { case ROLL_TYPE.SAVE: const saveLabel = game.i18n.localize(`LETHALFANTASY.Character.FIELDS.caracteristiques.${this.target}.valeur.label`) text = game.i18n.format("LETHALFANTASY.Roll.save", { save: saveLabel }) text = text.concat("
").concat(`Seuil : ${this.treshold}`) break case ROLL_TYPE.RESOURCE: const resourceLabel = game.i18n.localize(`LETHALFANTASY.Character.FIELDS.ressources.${this.target}.valeur.label`) text = game.i18n.format("LETHALFANTASY.Roll.resource", { resource: resourceLabel }) break case ROLL_TYPE.DAMAGE: const damageLabel = this.target text = game.i18n.format("LETHALFANTASY.Roll.damage", { item: damageLabel }) break case ROLL_TYPE.ATTACK: const attackLabel = this.target text = game.i18n.format("LETHALFANTASY.Roll.attack", { item: attackLabel }) break } return text } /** * Generates an introductory text tooltip with characteristics and modifiers. * * @returns {string} A formatted string containing the value, help, hindrance, and modifier. */ _createIntroTextTooltip() { let tooltip = game.i18n.format("LETHALFANTASY.Tooltip.saveIntroTextTooltip", { value: this.value, aide: this.aide, gene: this.gene, modificateur: this.modificateur }) if (this.hasTarget) { tooltip = tooltip.concat(`
Cible : ${this.targetName}`) } return tooltip } /** * Prompt the user with a dialog to configure and execute a roll. * * @param {Object} options Configuration options for the roll. * @param {string} options.rollType The type of roll being performed (e.g., RESOURCE, DAMAGE, ATTACK, SAVE). * @param {string} options.rollValue The initial value or formula for the roll. * @param {string} options.rollTarget The target of the roll. * @param {"="|"+"|"++"|"-"|"--"} options.rollAdvantage If there is an avantage (+), a disadvantage (-), a double advantage (++), a double disadvantage (--) or a normal roll (=). * @param {string} options.actorId The ID of the actor performing the roll. * @param {string} options.actorName The name of the actor performing the roll. * @param {string} options.actorImage The image of the actor performing the roll. * @param {boolean} options.hasTarget Whether the roll has a target. * @param {Object} options.target The target of the roll, if any. * @param {Object} options.data Additional data for the roll. * * @returns {Promise} The roll result or null if the dialog was cancelled. */ static async prompt(options = {}) { let dice = "1d20" let maxValue = 20 let formula = "1d20" if (options.rollType === "challenge") { if ( options.rollTarget.rollKey === "dying") { dice = options.rollTarget.value maxValue = Number(options.rollTarget.value.match(/\d+/)[0]) formula = `${dice}` } else { dice = "1d20" maxValue = 20 } } const rollModes = Object.fromEntries(Object.entries(CONFIG.Dice.rollModes).map(([key, value]) => [key, game.i18n.localize(value)])) const fieldRollMode = new foundry.data.fields.StringField({ choices: rollModes, blank: false, default: "public", }) const choiceModifier = { "-9": "-9", "-8": "-8", "-7": "-7", "-6": "-6", "-5": "-5", "-4": "-4", "-3": "-3", "-2": "-2", "-1": "-1", "+0": "0", "+1": "+1", "+2": "+2", "+3": "+3", "+4": "+4", "+5": "+5", "+6": "+6", "+7": "+7", "+8": "+8", "+9": "+9", "+10": "+10", "+11": "+11", "+12": "+12", "+13": "+13", "+14": "+14", "+15": "+15", "+16": "+16", "+17": "+17", "+18": "+18", "+19": "+19", "+20": "+20", "+21": "+21", "+22": "+22", "+23": "+23", "+24": "+24", "+25": "+25" } let modifier = "+0" let targetName let dialogContext = { isSave: options.rollType === "save", isChallenge: options.rollType === "challenge", rollTarget: options.rollTarget, rollModes, fieldRollMode, choiceModifier, formula, dice, hasTarget: options.hasTarget, modifier, targetName } const content = await renderTemplate("systems/fvtt-lethal-fantasy/templates/roll-dialog.hbs", dialogContext) const title = LethalFantasyRoll.createTitle(options.rollType, options.rollTarget) const label = game.i18n.localize("LETHALFANTASY.Roll.roll") const rollContext = await foundry.applications.api.DialogV2.wait({ window: { title: title }, classes: ["lethalfantasy"], content, buttons: [ { label: label, callback: (event, button, dialog) => { const output = Array.from(button.form.elements).reduce((obj, input) => { if (input.name) obj[input.name] = input.value return obj }, {}) return output }, }, ], rejectClose: false // Click on Close button will not launch an error }) // If the user cancels the dialog, exit if (rollContext === null) return let treshold let fullModifier = 0 if (options.rollType === "challenge") { let bonus = (options.rollTarget.rollKey === "dying") ? 0 : options.rollTarget.bonus 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)` } else { formula = `${dice} + 1d0` } } const rollData = { type: options.rollType, target: options.rollTarget, actorId: options.actorId, actorName: options.actorName, actorImage: options.actorImage, rollMode: rollContext.visibility, hasTarget: options.hasTarget, targetName, ...rollContext, } /** * A hook event that fires before the roll is made. * @function * @memberof hookEvents * @param {Object} options Options for the roll. * @param {Object} rollData All data related to the roll. * @returns {boolean} Explicitly return `false` to prevent roll to be made. */ if (Hooks.call("fvtt-lethal-fantasy.preRoll", options, rollData) === false) return const roll = new this(formula, options.data, rollData) await roll.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) } 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) } 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 /** * A hook event that fires after the roll has been made. * @function * @memberof hookEvents * @param {Object} options Options for the roll. * @param {Object} rollData All data related to the 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 return roll } /** * Creates a title based on the given type. * * @param {string} type The type of the roll. * @param {string} target The target of the roll. * @returns {string} The generated title. */ 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}`)}` case ROLL_TYPE.DAMAGE: return `${game.i18n.localize("LETHALFANTASY.Dialog.titleDamage")} : ${target}` case ROLL_TYPE.ATTACK: return `${game.i18n.localize("LETHALFANTASY.Dialog.titleAttack")} : ${target}` default: return game.i18n.localize("LETHALFANTASY.Dialog.titleStandard") } } /** @override */ async render(chatOptions = {}) { let chatData = await this._getChatCardData(chatOptions.isPrivate) return await renderTemplate(this.constructor.CHAT_TEMPLATE, chatData) } /** * Generates the data required for rendering a roll chat card. * * @param {boolean} isPrivate Indicates if the chat card is private. * @returns {Promise} A promise that resolves to an object containing the chat card data. * @property {Array} css - CSS classes for the chat card. * @property {Object} data - The data associated with the roll. * @property {number} diceTotal - The total value of the dice rolled. * @property {boolean} isGM - Indicates if the user is a Game Master. * @property {string} formula - The formula used for the roll. * @property {number} total - The total result of the roll. * @property {boolean} isSave - Indicates if the roll is a saving throw. * @property {boolean} isDamage - Indicates if the roll is for damage. * @property {boolean} isFailure - Indicates if the roll is a failure. * @property {string} actorId - The ID of the actor performing the roll. * @property {string} actingCharName - The name of the character performing the roll. * @property {string} actingCharImg - The image of the character performing the roll. * @property {string} introText - Introductory text for the roll. * @property {string} introTextTooltip - Tooltip for the introductory text. * @property {string} resultType - The type of result (e.g., success, failure). * @property {boolean} hasTarget - Indicates if the roll has a target. * @property {string} targetName - The name of the target. * @property {number} targetArmor - The armor value of the target. * @property {boolean} isPrivate - Indicates if the chat card is private. * @property {string} cssClass - The combined CSS classes as a single string. * @property {string} tooltip - The tooltip text for the chat card. */ async _getChatCardData(isPrivate) { const cardData = { css: [SYSTEM.id, "dice-roll"], data: this.data, diceTotal: this.dice.reduce((t, d) => t + d.total, 0), isGM: game.user.isGM, formula: this.formula, rollType: this.type, rollTarget: this.rollTarget, total: this.rollTotal, isSave: this.isSave, isChallenge: this.isChallenge, isFailure: this.isFailure, actorId: this.actorId, diceResults: this.diceResults, actingCharName: this.actorName, actingCharImg: this.actorImage, introText: this.introText, introTextTooltip: this.introTextTooltip, resultType: this.resultType, hasTarget: this.hasTarget, targetName: this.targetName, targetArmor: this.targetArmor, isPrivate: isPrivate } cardData.cssClass = cardData.css.join(" ") cardData.tooltip = isPrivate ? "" : await this.getTooltip() return cardData } /** * Converts the roll result to a chat message. * * @param {Object} [messageData={}] Additional data to include in the message. * @param {Object} options Options for message creation. * @param {string} options.rollMode The mode of the roll (e.g., public, private). * @param {boolean} [options.create=true] Whether to create the message. * @returns {Promise} - A promise that resolves when the message is created. */ async toMessage(messageData = {}, { rollMode, create = true } = {}) { super.toMessage( { isSave: this.isSave, isChallenge: this.isChallenge, isFailure: this.resultType === "failure", introText: this.introText, rollType: this.type, rollTarget: this.rollTarget, introTextTooltip: this.introTextTooltip, actingCharName: this.actorName, actingCharImg: this.actorImage, hasTarget: this.hasTarget, targetName: this.targetName, targetArmor: this.targetArmor, targetMalus: this.targetMalus, realDamage: this.realDamage, ...messageData, }, { rollMode: rollMode }, ) } }