Fix User ... lacks permission to update #701

Merged
uberwald merged 1 commits from VincentVk/foundryvtt-reve-de-dragon:v11 into v11 2024-06-11 07:52:59 +02:00
5 changed files with 57 additions and 41 deletions

View File

@ -38,10 +38,22 @@ export class ChatVente {
return foundry.utils.mergeObject(detail, { acheteur }) return foundry.utils.mergeObject(detail, { acheteur })
} }
static async diminuerQuantiteAchatVente(chatMessageId, quantite) {
const chatMessage = game.messages.get(chatMessageId)
const vente = ChatVente.getDetailVente(chatMessageId)
vente.nbLots = Math.max(0, vente.nbLots - quantite)
await chatMessage.setFlag(SYSTEM_RDD, NB_LOTS, vente.nbLots)
static async setDetailAchatVente(chatMessage, vente) { const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-vente-item.html', vente);
await chatMessage?.setFlag(SYSTEM_RDD, NB_LOTS, vente.nbLots) chatMessage.update({ content: html });
await chatMessage?.setFlag(SYSTEM_RDD, DETAIL_VENTE, { chatMessage.render(true);
}
static async displayAchatVente(vente) {
const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-vente-item.html', vente);
const chatMessage = await ChatMessage.create(RdDUtility.chatDataSetup(html))
await chatMessage.setFlag(SYSTEM_RDD, NB_LOTS, vente.nbLots)
await chatMessage.setFlag(SYSTEM_RDD, DETAIL_VENTE, {
item: vente.item, item: vente.item,
properties: vente.item.getProprietes(), properties: vente.item.getProprietes(),
vendeurId: vente.vendeurId, vendeurId: vente.vendeurId,
@ -50,21 +62,4 @@ export class ChatVente {
prixLot: vente.prixLot prixLot: vente.prixLot
}) })
} }
static async diminuerQuantite(chatMessageId, quantite) {
const chatMessage = game.messages.get(chatMessageId)
const vente = ChatVente.getDetailVente(chatMessageId)
vente.nbLots = Math.max(0, vente.nbLots - quantite)
await chatMessage?.setFlag(SYSTEM_RDD, NB_LOTS, vente.nbLots)
const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-vente-item.html', vente);
chatMessage.update({ content: html });
chatMessage.render(true);
}
static async displayAchatVente(venteData) {
const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-vente-item.html', venteData);
const chatMessage = await ChatMessage.create(RdDUtility.chatDataSetup(html))
await ChatVente.setDetailAchatVente(chatMessage, venteData)
}
} }

View File

@ -2626,13 +2626,13 @@ export class RdDActor extends RdDBaseActorSang {
/* -------------------------------------------- */ /* -------------------------------------------- */
async resetItemUse() { async resetItemUse() {
await this.unsetFlag(SYSTEM_RDD, 'itemUse');
await this.setFlag(SYSTEM_RDD, 'itemUse', {}); await this.setFlag(SYSTEM_RDD, 'itemUse', {});
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
async incDecItemUse(itemId, inc = 1) { async incDecItemUse(itemId, inc = 1) {
let itemUse = foundry.utils.duplicate(this.getFlag(SYSTEM_RDD, 'itemUse') ?? {}); const currentItemUse = this.getFlag(SYSTEM_RDD, 'itemUse');
let itemUse = currentItemUse ? foundry.utils.duplicate(currentItemUse) : {};
itemUse[itemId] = (itemUse[itemId] ?? 0) + inc; itemUse[itemId] = (itemUse[itemId] ?? 0) + inc;
await this.setFlag(SYSTEM_RDD, 'itemUse', itemUse); await this.setFlag(SYSTEM_RDD, 'itemUse', itemUse);
console.log("ITEM USE INC", inc, itemUse); console.log("ITEM USE INC", inc, itemUse);

View File

@ -32,10 +32,10 @@ export class RdDBaseActor extends Actor {
} }
static init() { static init() {
Hooks.on("preUpdateItem", (item, change, options, id) => RdDBaseActor.getParentActor(item)?.onPreUpdateItem(item, change, options, id)); Hooks.on("preUpdateItem", (item, change, options, id) => Misc.documentIfResponsible(item.parent)?.onPreUpdateItem(item, change, options, id))
Hooks.on("createItem", (item, options, id) => RdDBaseActor.getParentActor(item)?.onCreateItem(item, options, id)); Hooks.on("createItem", (item, options, id) => Misc.documentIfResponsible(item.parent)?.onCreateItem(item, options, id))
Hooks.on("deleteItem", (item, options, id) => RdDBaseActor.getParentActor(item)?.onDeleteItem(item, options, id)); Hooks.on("deleteItem", (item, options, id) => Misc.documentIfResponsible(item.parent)?.onDeleteItem(item, options, id))
Hooks.on("updateActor", (actor, change, options, actorId) => actor.onUpdateActor(change, options, actorId)); Hooks.on("updateActor", (actor, change, options, actorId) => Misc.documentIfResponsible(actor)?.onUpdateActor(change, options, actorId))
} }
static onSocketMessage(sockmsg) { static onSocketMessage(sockmsg) {
@ -82,10 +82,6 @@ export class RdDBaseActor extends Actor {
static extractActorMin = (actor) => { return { id: actor?.id, type: actor?.type, name: actor?.name, img: actor?.img }; }; static extractActorMin = (actor) => { return { id: actor?.id, type: actor?.type, name: actor?.name, img: actor?.img }; };
static getParentActor(document) {
return document?.parent instanceof Actor ? document.parent : undefined
}
/** /**
* Cette methode surcharge Actor.create() pour ajouter si besoin des Items par défaut: * Cette methode surcharge Actor.create() pour ajouter si besoin des Items par défaut:
* compétences et monnaies. * compétences et monnaies.
@ -381,7 +377,7 @@ export class RdDBaseActor extends Actor {
ChatUtility.removeChatMessageId(achat.chatMessageIdVente); ChatUtility.removeChatMessageId(achat.chatMessageIdVente);
} }
else if (achat.chatMessageIdVente) { else if (achat.chatMessageIdVente) {
await ChatVente.diminuerQuantite(achat.chatMessageIdVente, achat.choix.nombreLots) await ChatVente.diminuerQuantiteAchatVente(achat.chatMessageIdVente, achat.choix.nombreLots)
} }
} }
} }

View File

@ -149,14 +149,13 @@ export class ChatUtility {
} }
static async setMessageData(chatMessage, key, flag) { static async setMessageData(chatMessage, key, flag) {
if (flag) { if (flag && chatMessage.isAuthor) {
await chatMessage.setFlag(SYSTEM_RDD, key, JSON.stringify(flag)); await chatMessage.setFlag(SYSTEM_RDD, key, flag)
} }
} }
static getMessageData(chatMessage, key) { static getMessageData(chatMessage, key) {
const json = chatMessage.getFlag(SYSTEM_RDD, key); return chatMessage.getFlag(SYSTEM_RDD, key);
return json ? JSON.parse(json) : undefined;
} }
static getChatMessage(event) { static getChatMessage(event) {
@ -175,6 +174,8 @@ export class ChatUtility {
} }
static async onCreateChatMessage(chatMessage, options, id) { static async onCreateChatMessage(chatMessage, options, id) {
await chatMessage.setFlag(SYSTEM_RDD, 'rdd-timestamp', game.system.rdd.calendrier.getTimestamp()); if (chatMessage.isAuthor) {
await chatMessage.setFlag(SYSTEM_RDD, 'rdd-timestamp', game.system.rdd.calendrier.getTimestamp());
}
} }
} }

View File

@ -166,15 +166,39 @@ export class Misc {
} }
static firstConnectedGM() { static firstConnectedGM() {
return game.users.filter(u => u.isGM && u.active).sort(Misc.ascending(u => u.id)).find(u => u.isGM && u.active); return game.users.sort(Misc.ascending(u => u.id)).find(u => u.isGM && u.active);
}
static connectedGMs() {
return game.users.filter(u => u.isGM && u.active);
} }
static isOwnerPlayer(actor, user = undefined) { /**
return actor.testUserPermission(user ?? game.user, CONST.DOCUMENT_PERMISSION_LEVELS.OWNER) * This helper method allows to get the docuument, for a single user (either first connected GM, or the owner
* if there is no connected GMs), or else return undefined.
*
* This allows for example update hooks that should apply modifications to actors to be called only for one
* user (preventing the "User ... lacks permission to update Item" that was occuring on hooks when Item updates
* were triggering other changes)
*
* @param {*} document the Document with is potentially an Actor
* @returns the actor if either the game.user is the first connected GM, or if the game.user is the owner
* and there is no connected GM
*/
static documentIfResponsible(document) {
if (document instanceof Document) {
if (Misc.isUniqueConnectedGM() || (Misc.connectedGMs().length == 0 && Misc.isOwnerPlayer(document))) {
return document
}
}
return undefined
} }
static isOwnerPlayerOrUniqueConnectedGM(actor, user = undefined) { static isOwnerPlayer(actor) {
return Misc.isOwnerPlayer(actor, user) ?? Misc.isUniqueConnectedGM(); return actor.testUserPermission(game.user, CONST.DOCUMENT_PERMISSION_LEVELS.OWNER)
}
static isOwnerPlayerOrUniqueConnectedGM(actor) {
return Misc.isOwnerPlayer(actor) ?? Misc.isUniqueConnectedGM();
} }
/** /**