From 1efdca028ce119312ee4409f1d1d1b7cafdd51cd Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Fri, 8 Oct 2021 23:28:55 +0200 Subject: [PATCH] =?UTF-8?q?Stress=20am=C3=A9lior=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commande: /stress Ouvre une fenêtre pour gérer finement le stress (ou l'expérience) Commande: /stress Stress donné à un acteur/joueur avec un nom proche Notifications des joueurs qui reçoivent du stress --- module/actor.js | 27 +++++++++++--- module/dialog-stress.js | 72 ++++++++++++++++++++++++++++++++++++ module/misc.js | 10 +++++ module/rdd-commands.js | 48 +++++++++++++++++++++--- module/rdd-utility.js | 26 ------------- templates/dialog-stress.html | 24 ++++++++++++ 6 files changed, 171 insertions(+), 36 deletions(-) create mode 100644 module/dialog-stress.js create mode 100644 templates/dialog-stress.html diff --git a/module/actor.js b/module/actor.js index 132d668b..57e34ce6 100644 --- a/module/actor.js +++ b/module/actor.js @@ -1002,17 +1002,34 @@ export class RdDActor extends Actor { /* -------------------------------------------- */ async updateCompteurValue(fieldName, fieldValue, raison = 'Inconnue') { await this.update({ [`data.compteurs.${fieldName}.value`]: fieldValue }); - if (fieldName == 'stress') { - await this.updateExperienceLog('stress', fieldValue, raison); - } + await this.addStressExperienceLog(fieldName, fieldValue, 'forcé: '+raison); } /* -------------------------------------------- */ async addCompteurValue(fieldName, fieldValue, raison = 'Inconnue') { let oldValue = (Misc.templateData(this)).compteurs[fieldName].value; await this.update({ [`data.compteurs.${fieldName}.value`]: Number(oldValue) + Number(fieldValue) }); - if (fieldName == 'stress') { - await this.updateExperienceLog('stress', fieldValue, raison); + await this.addStressExperienceLog(fieldName, fieldValue, raison); + } + + async addStressExperienceLog(fieldName, fieldValue, raison) { + switch (fieldName) { + case 'stress': case 'experience': + await this.updateExperienceLog(fieldName, fieldValue, raison); + } + } + + /* -------------------------------------------- */ + distribuerStress(compteur, stress, motif) { + if (game.user.isGM && this.hasPlayerOwner && this.isPersonnage()) { + switch (compteur) { + case 'stress': case 'experience': + const message = `${this.name} a reçu ${stress} points ${compteur == 'stress' ? "de stress" : "d'expérience"} (raison : ${motif})`; + this.addCompteurValue(compteur, stress, motif); + ui.notifications.info(message); + game.users.players.filter(player => player.active && player.character?.id == this.id) + .forEach(player => ChatUtility.notifyUser(player.id, 'info', message)); + } } } diff --git a/module/dialog-stress.js b/module/dialog-stress.js new file mode 100644 index 00000000..210b9918 --- /dev/null +++ b/module/dialog-stress.js @@ -0,0 +1,72 @@ +import { Misc } from "./misc.js"; + +export class DialogStress extends Dialog { + + static async distribuerStress() { + let dialogData = { + motif: "Motif", + stress: 10, + immediat: false, + actors: game.actors.filter(actor => actor.hasPlayerOwner && actor.isPersonnage()) + .map(actor => { + let actorData = duplicate(Misc.data(actor)); + actorData.selected = actor.hasPlayerOwner; + return actorData; + }) + }; + + const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/dialog-stress.html", dialogData); + new DialogStress(dialogData, html) + .render(true); + } + + constructor(dialogData, html) { + let options = { classes: ["DialogStress"], width: 400, height: 320, 'z-index': 99999 }; + let conf = { + title: "Donner du stress", + content: html, + buttons: { + "Stress": { label: "Stress !", callback: it => { this._onStress(); } } + } + }; + super(conf, options); + this.dialogData = dialogData; + } + + async _onStress() { + this.validerStress(); + const compteur = this.dialogData.immediat ? 'experience' : 'stress'; + const stress = this.dialogData.stress; + const motif = this.dialogData.motif; + + this.dialogData.actors.filter(it => it.selected) + .map(it => game.actors.get(it._id)) + .forEach(actor => actor.distribuerStress(compteur, stress, motif)); + } + + + validerStress() { + this.dialogData.motif = $("[name='motif']").val(); + this.dialogData.stress = $("[name='stress']").val(); + this.dialogData.immediat = $("[name='immediat']").prop("checked");; + } + + /* -------------------------------------------- */ + activateListeners(html) { + super.activateListeners(html); + html.find(".select-actor").change((event) => this.onSelectActor(event)); + } + + async onSelectActor(event) { + event.preventDefault(); + const options = event.currentTarget.options; + for (var i = 0; i < options.length; i++) { // looping over the options + const actorId = options[i].attributes["data-actor-id"].value; + const actor = this.dialogData.actors.find(it => it._id == actorId); + if (actor) { + actor.selected = options[i].selected; + } + }; + } + +} \ No newline at end of file diff --git a/module/misc.js b/module/misc.js index ea0d7aa1..25ceacd3 100644 --- a/module/misc.js +++ b/module/misc.js @@ -133,6 +133,16 @@ export class Misc { return game.user.id == Misc.connectedGMOrUser(); } + /* -------------------------------------------- */ + static findPlayer(name) { + return Misc.findFirstLike(name, game.users, it=>it.name,'joueurs'); + } + + /* -------------------------------------------- */ + static findActor(name, actors = game.actors, description= 'acteurs') { + return Misc.findFirstLike(name, actors, it=>it.name, description); + } + /* -------------------------------------------- */ static findFirstLike(value, elements, mapper = it=>it.name, description = 'valeurs') { value = Grammar.toLowerCaseNoAccent(value); diff --git a/module/rdd-commands.js b/module/rdd-commands.js index c6ff622f..ef2840b3 100644 --- a/module/rdd-commands.js +++ b/module/rdd-commands.js @@ -1,6 +1,7 @@ /* -------------------------------------------- */ import { DialogCreateSigneDraconique } from "./dialog-create-signedraconique.js"; +import { DialogStress } from "./dialog-stress.js"; import { RdDItemCompetence } from "./item-competence.js"; import { Misc } from "./misc.js"; import { RdDCarac } from "./rdd-carac.js"; @@ -97,11 +98,12 @@ export class RdDCommands { }); rddCommands.registerCommand({ - path: ["/stress"], func: (content, msg, params) => RdDUtility.distribuerStress(params[0], params[1], params[2]), + path: ["/stress"], func: (content, msg, params) => rddCommands.distribuerStress(params), descr: `Distribue du stress aux personnages. Exemples: +
/stress : Ouvre une fenêtre pour donner du stress ou de l'expérience à un ensemble de personnages
/stress 6 : Distribue 6 points des Stress à tout les personnages joueurs, sans raison renseignée -
/stress 6 Tigre : Distribue 6 points des Stress à tout les personnages joueurs, à cause d'un Tigre Vert -
/stress 6 Glou Paulo : Distribue 6 points de Stres à l'acteur connecté au joueur Paulo, à cause d'un Glou` +
/stress 6 Tigre : Distribue 6 points des Stress à tout les personnages joueurs, à cause d'un Tigre (Vert) +
/stress 6 Glou Paulo : Distribue 6 points de Stress au personnage Paulon ou au personnage joueur Paulo, à cause d'un Glou` }); game.system.rdd.commands = rddCommands; @@ -289,7 +291,7 @@ export class RdDCommands { finalLevel: diff, showDice: true, diviseurSignificative: significative ? 2 : 1, - show: { title: "Table de résolution"} + show: { title: "Table de résolution" } }; await RdDResolutionTable.rollData(rollData); RdDCommands._chatAnswer(msg, await RdDResolutionTable.buildRollDataHtml(rollData)); @@ -297,7 +299,7 @@ export class RdDCommands { /* -------------------------------------------- */ async rollDeDraconique(msg) { - let ddr = await RdDDice.rollTotal("1dr + 7", { showDice:true }); + let ddr = await RdDDice.rollTotal("1dr + 7", { showDice: true }); RdDCommands._chatAnswer(msg, `Lancer d'un Dé draconique: ${ddr}`); } @@ -351,5 +353,41 @@ export class RdDCommands { }); return true; } + + async distribuerStress(params) { + if (!game.user.isGM) { + ui.notifications.warn("Seul le MJ est autorisé à utiliser la commande /stress"); + return false; + } + if (params.length == 0) { + DialogStress.distribuerStress(); + } + else { + let stress = params[0] + if (stress == undefined) { + ui.notifications.warn("Pas de valeur de stress à distribuer!"); + return; + } + + let motif = params[1]; + let name = params[2]; + if (name == undefined) { + for (let actor of game.actors) { + actor.distribuerStress('stress', stress, motif); + } + } else { + //console.log(stressValue, nomJoueur); + let actor = Misc.findActor(name, game.actors.filter(it => it.hasPlayerOwner)) ?? Misc.findPlayer(name)?.character + if (actor) { + actor.distribuerStress('stress', stress, motif); + } + else { + ui.notifications.warn(`Pas de personnage ou de joueur correspondant à ${name}!`); + } + } + + } + return true; + } } diff --git a/module/rdd-utility.js b/module/rdd-utility.js index 2fec2e3d..9dc188bb 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -887,32 +887,6 @@ export class RdDUtility { } } - /*-------------------------------------------- */ - static distribuerStress(stressValue, raison = 'Inconnu', nomJoueur = undefined) { - if (game.user.isGM) { - if (stressValue == undefined){ - ui.notifications.warn("Pas de valeur de stress à distribuer!"); - return; - } - if (nomJoueur == undefined) { - for (let actor of game.actors) { - if (actor.hasPlayerOwner && actor.isPersonnage() ) { - actor.addCompteurValue('stress', stressValue, raison); - ui.notifications.info(`${actor.name} a reçu ${stressValue} points de Stress (raison : ${raison})`); - } - } - } else { - //console.log(stressValue, nomJoueur); - let joueur = game.users.find(user => user.name.toLowerCase() == nomJoueur.toLowerCase()); - //console.log("Player", joueur, joueur.character ); - joueur.character.addCompteurValue('stress', stressValue, raison); - ui.notifications.info(`${joueur.character.name} a reçu ${stressValue} points de Stress (raison : ${raison})`); - } - } else { - ui.notifications.warn("Seul le MJ est autorisé à utiliser la commande /stress"); - } - } - /*-------------------------------------------- */ static async onRenderChatMessage(app, html, msg) { // TODO diff --git a/templates/dialog-stress.html b/templates/dialog-stress.html new file mode 100644 index 00000000..4273ab70 --- /dev/null +++ b/templates/dialog-stress.html @@ -0,0 +1,24 @@ +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
\ No newline at end of file