From ccb6709f5b5f0ebffe3baca22ca66cad4c994fa5 Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Sun, 13 Nov 2022 21:56:23 +0100 Subject: [PATCH] =?UTF-8?q?Fix:=20Nourriture=20consomm=C3=A9e=20par=20le?= =?UTF-8?q?=20bon=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/actor.js | 49 ++++++++++++++++++++++--------------- module/dialog-item-achat.js | 7 ++++-- module/misc.js | 6 ++++- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/module/actor.js b/module/actor.js index 5d6b291b..91a4714b 100644 --- a/module/actor.js +++ b/module/actor.js @@ -65,7 +65,7 @@ export class RdDActor extends Actor { static onSocketMessage(sockmsg) { switch (sockmsg.msg) { case "msg_remote_actor_call": - return RdDActor.onRemoteActorCall(sockmsg.data); + return RdDActor.onRemoteActorCall(sockmsg.data, sockmsg.userId); case "msg_reset_nombre_astral": console.log("RESET ASTRAL", game.user.character); game.user.character.resetNombreAstral(); @@ -73,23 +73,26 @@ export class RdDActor extends Actor { } } - static remoteActorCall(callData, canExecuteLocally = () => Misc.isUniqueConnectedGM()) { - if (canExecuteLocally()) { - RdDActor.onRemoteActorCall(callData); + static remoteActorCall(callData, userId = undefined) { + userId = userId ?? Misc.firstConnectedGMId(); + if (userId == game.user.id) { + RdDActor.onRemoteActorCall(callData, userId); return false; } else { - game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_remote_actor_call", data: callData }); + game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_remote_actor_call", data: callData, userId: userId }); return true; } } - static onRemoteActorCall(callData) { - const actor = game.actors.get(callData?.actorId); - if (Misc.isOwnerPlayerOrUniqueConnectedGM(actor)) { // Seul le joueur choisi effectue l'appel: le joueur courant si propriétaire de l'actor, ou le MJ sinon - const args = callData.args; - console.info(`RdDActor.onRemoteActorCall: pour l'Actor ${callData.actorId}, appel de RdDActor.${callData.method}(`, ...args, ')'); - actor[callData.method](...args); + static onRemoteActorCall(callData, userId) { + if (userId == game.user.id) { + const actor = game.actors.get(callData?.actorId); + if (Misc.isOwnerPlayerOrUniqueConnectedGM(actor)) { // Seul le joueur choisi effectue l'appel: le joueur courant si propriétaire de l'actor, ou le MJ sinon + const args = callData.args; + console.info(`RdDActor.onRemoteActorCall: pour l'Actor ${callData.actorId}, appel de RdDActor.${callData.method}(`, ...args, ')'); + actor[callData.method](...args); + } } } @@ -299,7 +302,7 @@ export class RdDActor extends Actor { /* -------------------------------------------- */ getObjet(id) { - return id ? this.items.find(it => it.id == id) : undefined; + return this.getEmbeddedDocument('Item', id); } listItemsData(type) { @@ -1932,15 +1935,24 @@ export class RdDActor extends Actor { async consommer(item, choix) { switch (item.type) { case 'nourritureboisson': - return await this.consommerNourritureboisson(item, choix); + return await this.consommerNourritureboisson(item.id, choix); case 'potion': return await this.consommerPotion(item) } } /* -------------------------------------------- */ - async consommerNourritureboisson(item, choix = { doses: 1, seForcer: false, supprimerSiZero: false }) { - + async consommerNourritureboisson(itemId, choix = { doses: 1, seForcer: false, supprimerSiZero: false}, userId = undefined) { + if (userId != undefined && userId != game.user.id) { + RdDActor.remoteActorCall({ + actorId: this.id, + method: 'consommerNourritureboisson', + args: [itemId, choix, userId] + }, + userId) + return; + } + const item = this.getObjet(itemId) if (item.type != 'nourritureboisson') { return; } @@ -3649,10 +3661,7 @@ export class RdDActor extends Actor { const acheteur = achat.acheteurId ? game.actors.get(achat.acheteurId) : undefined; const vendeur = achat.vendeurId ? game.actors.get(achat.vendeurId) : undefined; - const messageVente = game.messages.get(achat.chatMessageIdVente); - const html = await messageVente.getHTML(); - const button = html.find(".button-acheter")[0]; - const vente = DialogItemAchat.venteData(button); + const vente = achat.vente; const itemId = vente.item._id; const isItemEmpilable = "quantite" in vente.item.system; @@ -3692,7 +3701,7 @@ export class RdDActor extends Actor { let items = await acheteur.createEmbeddedDocuments("Item", listeAchat); if (achat.choix.consommer && vente.item.type == 'nourritureboisson') { achat.choix.doses = achat.choix.nombreLots; - await acheteur.consommerNourritureboisson(items[0], achat.choix); + await acheteur.consommerNourritureboisson(items[0].id, achat.choix, vente.actingUserId); } } if (coutDeniers > 0) { diff --git a/module/dialog-item-achat.js b/module/dialog-item-achat.js index a7c3ec91..1a0c6ef7 100644 --- a/module/dialog-item-achat.js +++ b/module/dialog-item-achat.js @@ -22,6 +22,7 @@ export class DialogItemAchat extends Dialog { const prixLot = Monnaie.arrondiDeniers(button.attributes['data-prixLot']?.value ?? 0); return { item: json ? JSON.parse(json) : undefined, + actingUserId: game.user.id, vendeurId: vendeurId, vendeur: vendeur, acheteur: acheteur, @@ -39,6 +40,7 @@ export class DialogItemAchat extends Dialog { chatMessageIdVente: RdDUtility.findChatMessageId(button) }; } + static async onAcheter(venteData) { const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-achat.html`, venteData); const dialog = new DialogItemAchat(html, venteData); @@ -52,7 +54,7 @@ export class DialogItemAchat extends Dialog { const actionAchat = venteData.prixLot > 0 ? "Acheter" : "Prendre"; const buttons = {}; if (isConsommable) { - buttons["consommer"] = { label: venteData.item.system.boisson ? "Boire" : "Manger", callback: it => { this.onAchatConsommer(); } } + buttons["consommer"] = { label: venteData.item.system.boisson ? "Boire" : "Manger", callback: it => this.onAchatConsommer() } } buttons[actionAchat] = { label: actionAchat, callback: it => { this.onAchat(); } }; buttons["decliner"] = { label: "Décliner", callback: it => { } }; @@ -76,7 +78,8 @@ export class DialogItemAchat extends Dialog { acheteurId: this.venteData.acheteur?.id, prixTotal: this.venteData.prixTotal, chatMessageIdVente: this.venteData.chatMessageIdVente, - choix: this.venteData.choix + choix: this.venteData.choix, + vente: this.venteData }); } diff --git a/module/misc.js b/module/misc.js index 7f851799..aec166d4 100644 --- a/module/misc.js +++ b/module/misc.js @@ -133,7 +133,11 @@ export class Misc { * @returns true pour un seul utilisateur: le premier GM connecté par ordre d'id */ static isUniqueConnectedGM() { - return game.user.id == Misc.firstConnectedGM()?.id; + return game.user.id == Misc.firstConnectedGMId(); + } + + static firstConnectedGMId() { + return Misc.firstConnectedGM()?.id; } /* -------------------------------------------- */