Fix: Nourriture consommée par le bon user

This commit is contained in:
Vincent Vandemeulebrouck 2022-11-13 21:56:23 +01:00
parent 2fe6b472a2
commit ccb6709f5b
3 changed files with 39 additions and 23 deletions

View File

@ -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) {

View File

@ -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
});
}

View File

@ -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;
}
/* -------------------------------------------- */