From 368b7f284abc0cfacffe86c4054d507c46a74a5f Mon Sep 17 00:00:00 2001 From: rwanoux Date: Sun, 28 Apr 2024 23:31:47 +0200 Subject: [PATCH] ajout config des traits + commentaires --- module/system/config.mjs | 19 +++- module/system/dialogs.mjs | 194 +++++++++++++++++++++++++++++--------- module/system/dice3d.mjs | 8 +- 3 files changed, 177 insertions(+), 44 deletions(-) diff --git a/module/system/config.mjs b/module/system/config.mjs index 8d599d0..f9a927c 100644 --- a/module/system/config.mjs +++ b/module/system/config.mjs @@ -158,4 +158,21 @@ VERMINE.origins = { "vaelor": "Vaelor", "valdheim": "Valdheim" } - +VERMINE.traits = { + "bien_conçu": { + name: "bien conçu", + description: "+1 Réussite automatique sur les jets de réparation. + 1 point de Fiabilité sur les jets de réparation de fortune.+ 1 Réussite automatique sur les jets de sabotage.", + value: 0 + }, + "cassant": { + name: "cassant", + description: "Toutes les pertes de Fiabilité subies sont majorées de 1 point.", + value: 0 // = la valeur "n" dans le tableau page 51 livre règles. 0 si pas de valeur + }, + "coque": { + name: "coque", + description: "Ignore les (n) premiers Dommages. Quand (n) est atteint, l’objet perd ce Trait.", + value: 1 // = la valeur "n" dans le tableau page 51 livre règles, indiquer 1 si valeur "n" présente + }, + // etc...etc... +} diff --git a/module/system/dialogs.mjs b/module/system/dialogs.mjs index 341190c..7e9e156 100644 --- a/module/system/dialogs.mjs +++ b/module/system/dialogs.mjs @@ -34,30 +34,21 @@ export class CombatResultDialog extends Dialog { } + + +/** + * Represents a dialog for rolling dice. + */ export class RollDialog extends Dialog { - static async create(data = { - label: null, - rolltype: null, - NoD: 1, - Reroll: false, - actorId: game.user.character.id - }) { - data.actor = await game.actors.get(data.actorId); - data.config = CONFIG.VERMINE; - let options = { classes: ["nocDialog"], width: 420, height: 'fit-content', 'z-index': 99999 }; - let html = await renderTemplate('systems/vermine2047/templates/roll-dialog.hbs', data); + /** + * Creates a new RollDialog instance. + * @param {Object} data - The data for the dialog. + * @param {HTMLElement} html - The HTML content of the dialog. + * @param {Object} options - The options for the dialog. + * @param {Function} close - The callback function for closing the dialog. + */ - return new RollDialog(data, html, options); - } - static get defaultOptions() { - return foundry.utils.mergeObject(super.defaultOptions, { - focus: true, - classes: ["dialog vermine-roll"], - - }); - } - /* -------------------------------------------- */ constructor(data, html, options, close = undefined) { let conf = { title: "jet de dés", @@ -78,33 +69,87 @@ export class RollDialog extends Dialog { }, close: close, } - return super({ ...conf, ...data }, options); - - - }; + + /** + * Creates a new RollDialog instance. + * @param {Object} data - The data for the dialog. + * @param {HTMLElement} html - The HTML content of the dialog. + * @param {Object} options - The options for the dialog. + * @param {Function} close - The callback function for closing the dialog. + */ + static async create(data = { + label: null, + rolltype: null, + NoD: 1, + Reroll: false, + actorId: game.user.character.id + }) { + // Retrieve the actor data based on the actorId + data.actor = await game.actors.get(data.actorId); + data.config = CONFIG.VERMINE; + // Define options for the dialog + let options = { classes: ["nocDialog"], width: 420, height: 'fit-content', 'z-index': 99999 }; + // Render the HTML template for the dialog + let html = await renderTemplate('systems/vermine2047/templates/roll-dialog.hbs', data); + + // Return a new RollDialog instance with the provided data, HTML, and options + return new RollDialog(data, html, options); + } + + /** + * Retrieves the default options for the RollDialog. + */ + static get defaultOptions() { + return foundry.utils.mergeObject(super.defaultOptions, { + focus: true, + classes: ["dialog vermine-roll"], + + }); + } + /** + * Retrieves the data for the dialog. + * @returns {Object} The context data for the dialog. + */ getData() { + // Get the context data from the superclass let context = super.getData(); context.data = this.data; context.config = CONFIG.VERMINE; return context; } + + /** + * Activates event listeners for the dialog. + * @param {HTMLElement} html - The HTML element of the dialog. + */ async activateListeners(html) { + // Activate event listeners from the superclass super.activateListeners(html); - this.getRollData(); + // Retrieve roll data and set up event listeners + await this.getRollData(); let rollInputs = html.find('[data-roll'); for (let inp of rollInputs) { + // Add event listener for roll input changes inp.addEventListener('change', await this.getRollData.bind(this)) }; let selectAbil = html.find('#ability')[0]; + // Set the maximum value for self control based on ability value html.find("#self_control")[0].max = selectAbil.value; selectAbil.addEventListener('change', this._onChangeAbility.bind(this)); let selfControl = html.find('#self_control')[0] + // Add event listener for self control changes selfControl.addEventListener('change', this._onChangeSelfControl.bind(this)); }; + + /** + * Retrieves the roll data for the dialog. + * @param {Event} ev - The event triggering the roll data retrieval. + */ async getRollData(ev) { console.log(this) + // Calculate and store the roll data this.rollData = { actor: this.data.actor, NoD: this.getDicePool(), @@ -115,78 +160,143 @@ export class RollDialog extends Dialog { self_control: this.getSelfControl(), max_effort: this.getMaxEffort() } - console.log('____________________________________calculating roll', this.rollData); - }; + /** + * Handles the change in self control value. + * @param {Event} ev - The event triggering the change in self control value. + */ _onChangeSelfControl(ev) { let html = this.element[0]; + // Update the displayed self control value based on the event html.querySelector('#self_control_value').innerText = ev.currentTarget.value; - - }; + + /** + * Retrieves the self control value from the HTML element. + * @returns {number} The self control value. + */ getSelfControl() { let html = this.element[0]; + // Parse and return the self control value from the HTML element let selfControl = parseInt(html.querySelector('#self_control').value) return selfControl } + + /** + * Retrieves the maximum effort value from the HTML element. + * @returns {number} The maximum effort value. + */ getMaxEffort() { let html = this.element[0]; + // Retrieve and return the maximum effort value from the HTML element return parseInt(html.querySelector('#ability').value); } + + /** + * Retrieves the selected totems from the HTML element. + * @returns {Object} An object containing the selected totems. + */ getTotems() { let html = this.element[0]; + // Check and store the status of human and adapted totems let totems = { - human: html.querySelector('#human-totem').checked, - adapted: html.querySelector('#adapted-totem').checked, + human: html.querySelector('#human-totem')?.checked, + adapted: html.querySelector('#adapted-totem')?.checked, } return totems } + + /** + * Handles the change in ability value. + * @param {Event} ev - The event triggering the change in ability value. + */ _onChangeAbility(ev) { let html = this.element[0]; + // Retrieve the selected ability score and update related elements let score = html.querySelector('#ability').options[html.querySelector('#ability').selectedIndex].value; - + // Check if the score is a number, otherwise set it to 0 if (!typeof score == "number") { score = 0 } html.querySelector('#abilityScore').value = score; html.querySelector('#self_control').max = score; - - } + /** + * Retrieves the total dice pool based on various factors. + * @returns {number} The total dice pool value. + */ getDicePool() { + // Retrieve the HTML element let html = this.element[0]; + // Get the ability value or set to 0 if not found let abilValue = html.querySelector('#ability').options[html.querySelector('#ability').selectedIndex].value || 0; + // Get the skill value or set to 0 if not found let skillValue = html.querySelector('#skill').options[html.querySelector('#skill').selectedIndex].dataset.pool || 0; + // Get the self control value let selfControl = html.querySelector('#self_control').value; + // Calculate bonuses based on certain conditions let bonuses = (html.querySelector('#usingSpecialization').checked ? 1 : 0) + (html.querySelector('#helped').checked ? 1 : 0) + (html.querySelector('#usingTools').checked ? 1 : 0); + // Calculate the total dice pool let total = parseInt(abilValue) + parseInt(selfControl) + parseInt(skillValue) + bonuses; - return total || 0; - } + + /** + * Retrieves the reroll value based on selected skill. + * @returns {number} The reroll value. + */ getReroll() { + // Retrieve the HTML element let html = this.element[0]; + // Get the selected skill index let selected = html.querySelector('#skill').selectedIndex; + // Get the reroll value from the selected skill or set to 0 if not found let reroll = html.querySelector('#skill').options[selected].dataset.reroll || 0; return parseInt(reroll) || 0; - } + + /** + * Retrieves the difficulty value based on selected option. + * @returns {number} The difficulty value. + */ + getDifficulty() { + // Retrieve the HTML element let html = this.element[0]; + // Get the selected index for difficulty let selected = html.querySelector('#difficulty').selectedIndex; + // Get the difficulty value from the selected option or set to 0 if not found let diff = html.querySelector('#difficulty').options[selected].value || 0; return parseInt(diff) || 0; } - roll() { + + /** + * Performs a dice roll based on the roll data and handles self control checks. + * @returns {Promise} A promise that resolves with the result of the dice roll. + */ + async roll() { + // Check if self control is required for the roll if (this.rollData.self_control > 0) { + // Check if the actor has enough self control if (this.rollData.actor.system.attributes.self_control.value < this.rollData.self_control) { - return ui.notifications.warn('vous navez pas assez de sang-froid') + // Display a warning message if self control is insufficient + ui.notifications.warn('vous navez pas assez de sang-froid'); + // Re-render the dialog + this.render(true); + return false; // Exit the function if self control is insufficient } } - return VermineUtils.roll({ ...this.rollData }) - } + // Deduct self control points if necessary + if (this.rollData.self_control > 0) { + // Update the actor's self control value + await this.rollData.actor.update({ "system.attributes.self_control.value": this.rollData.actor.system.attributes.self_control.value - this.rollData.self_control }); + } + + // Perform the dice roll using VermineUtils + return VermineUtils.roll({ ...this.rollData }); + } } \ No newline at end of file diff --git a/module/system/dice3d.mjs b/module/system/dice3d.mjs index f6f34bc..86c557a 100644 --- a/module/system/dice3d.mjs +++ b/module/system/dice3d.mjs @@ -1,4 +1,10 @@ -export async function initUserDice(dice3d) { + +// Ce fichier contient des fonctions pour initialiser des dés personnalisés pour chaque utilisateur du jeu. +// Les fonctions permettent de définir des couleurs de dés réguliers, de totem humain et adaptés en fonction de la couleur de l'utilisateur. +// De plus, il y a des fonctions pour assombrir et éclaircir une couleur donnée. + + +export async function initUserDice(dice3d, user) { let baseColor = game.user.color; dice3d.addColorset({ name: 'regular_' + game.user.name,