2024-06-01 00:17:39 +02:00
|
|
|
import { SYSTEM_RDD } from "../constants.js";
|
|
|
|
import { RdDUtility } from "../rdd-utility.js";
|
|
|
|
|
|
|
|
const DETAIL_VENTE = 'detailVente';
|
|
|
|
const NB_LOTS = 'nbLotss';
|
|
|
|
|
|
|
|
export class ChatVente {
|
|
|
|
|
|
|
|
static getDetailVente(chatMessageId) {
|
|
|
|
const chatMessage = game.messages.get(chatMessageId)
|
|
|
|
if (!chatMessage) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const nbLots = chatMessage.getFlag(SYSTEM_RDD, NB_LOTS)
|
|
|
|
const detail = foundry.utils.duplicate(chatMessage.getFlag(SYSTEM_RDD, DETAIL_VENTE))
|
|
|
|
if (!detail.item) {
|
|
|
|
ui.notifications.warn("Impossible d'acheter: informations sur l'objet manquantes")
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const vendeur = detail.vendeurId ? game.actors.get(detail.vendeurId) : undefined;
|
|
|
|
return foundry.utils.mergeObject(detail,
|
|
|
|
{
|
|
|
|
alias: vendeur?.name ?? game.user.name,
|
|
|
|
vendeur,
|
|
|
|
nbLots: nbLots,
|
|
|
|
chatMessageIdVente: chatMessageId
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDetailAchatVente(chatMessageId) {
|
|
|
|
const acheteur = RdDUtility.getSelectedActor()
|
|
|
|
const detail = ChatVente.getDetailVente(chatMessageId)
|
|
|
|
if (!acheteur && !detail.vendeur) {
|
|
|
|
ui.notifications.info("Pas d'acheteur ni de vendeur, aucun changement");
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return foundry.utils.mergeObject(detail, { acheteur })
|
|
|
|
}
|
|
|
|
|
2024-06-11 02:48:37 +02:00
|
|
|
static async diminuerQuantiteAchatVente(chatMessageId, quantite) {
|
2024-06-01 00:17:39 +02:00
|
|
|
const chatMessage = game.messages.get(chatMessageId)
|
|
|
|
const vente = ChatVente.getDetailVente(chatMessageId)
|
|
|
|
vente.nbLots = Math.max(0, vente.nbLots - quantite)
|
2024-06-11 02:48:37 +02:00
|
|
|
await chatMessage.setFlag(SYSTEM_RDD, NB_LOTS, vente.nbLots)
|
2024-06-01 00:17:39 +02:00
|
|
|
|
|
|
|
const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-vente-item.html', vente);
|
|
|
|
chatMessage.update({ content: html });
|
|
|
|
chatMessage.render(true);
|
|
|
|
}
|
|
|
|
|
2024-06-11 02:48:37 +02:00
|
|
|
static async displayAchatVente(vente) {
|
|
|
|
const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-vente-item.html', vente);
|
2024-06-01 00:17:39 +02:00
|
|
|
const chatMessage = await ChatMessage.create(RdDUtility.chatDataSetup(html))
|
2024-06-11 02:48:37 +02:00
|
|
|
await chatMessage.setFlag(SYSTEM_RDD, NB_LOTS, vente.nbLots)
|
|
|
|
await chatMessage.setFlag(SYSTEM_RDD, DETAIL_VENTE, {
|
|
|
|
item: vente.item,
|
|
|
|
properties: vente.item.getProprietes(),
|
|
|
|
vendeurId: vente.vendeurId,
|
|
|
|
tailleLot: vente.tailleLot,
|
|
|
|
quantiteIllimite: vente.quantiteIllimite,
|
|
|
|
prixLot: vente.prixLot
|
|
|
|
})
|
2024-06-01 00:17:39 +02:00
|
|
|
}
|
|
|
|
}
|