2022-12-23 00:34:17 +01:00
|
|
|
import { Misc } from "./misc.js";
|
2021-05-07 17:27:02 +02:00
|
|
|
import { RdDUtility } from "./rdd-utility.js";
|
|
|
|
|
|
|
|
export class DialogItemAchat extends Dialog {
|
|
|
|
|
2022-12-23 00:34:17 +01:00
|
|
|
static preparerAchat(chatButton) {
|
|
|
|
const vendeurId = chatButton.attributes['data-vendeurId']?.value;
|
2021-05-07 17:27:02 +02:00
|
|
|
const vendeur = vendeurId ? game.actors.get(vendeurId) : undefined;
|
|
|
|
const acheteur = RdDUtility.getSelectedActor();
|
2022-12-23 00:34:17 +01:00
|
|
|
const json = chatButton.attributes['data-jsondata']?.value;
|
2021-05-07 17:27:02 +02:00
|
|
|
if (!acheteur && !vendeur) {
|
|
|
|
ui.notifications.info("Pas d'acheteur ni de vendeur, aucun changement");
|
2022-10-07 19:05:56 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (!json) {
|
|
|
|
ui.notifications.warn("Impossible d'acheter: informations sur l'objet manquantes")
|
|
|
|
return undefined;
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 19:05:56 +02:00
|
|
|
return {
|
2022-12-23 00:34:17 +01:00
|
|
|
item: (json ? JSON.parse(json) : undefined),
|
|
|
|
vendeur,
|
|
|
|
acheteur,
|
|
|
|
nbLots: parseInt(chatButton.attributes['data-quantiteNbLots']?.value),
|
|
|
|
tailleLot: parseInt(chatButton.attributes['data-tailleLot']?.value ?? 1),
|
|
|
|
prixLot: Number(chatButton.attributes['data-prixLot']?.value ?? 0),
|
|
|
|
quantiteIllimite: chatButton.attributes['data-quantiteIllimite']?.value == 'true',
|
|
|
|
chatMessageIdVente: RdDUtility.findChatMessageId(chatButton),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static async onAcheter({ item, vendeur, acheteur, service, tailleLot, prixLot, nbLots, quantiteIllimite, chatMessageIdVente }) {
|
|
|
|
const venteData = {
|
|
|
|
item,
|
2022-11-13 21:56:23 +01:00
|
|
|
actingUserId: game.user.id,
|
2022-12-23 00:34:17 +01:00
|
|
|
vendeurId: vendeur?.id,
|
|
|
|
vendeur,
|
|
|
|
acheteur,
|
|
|
|
service,
|
|
|
|
tailleLot,
|
|
|
|
quantiteIllimite,
|
|
|
|
quantiteNbLots: nbLots,
|
|
|
|
choix: { seForcer: false, supprimerSiZero: true },
|
|
|
|
prixLot,
|
2022-10-07 19:05:56 +02:00
|
|
|
isVente: prixLot > 0,
|
2022-12-23 00:34:17 +01:00
|
|
|
isConsommable: item.type == 'nourritureboisson' && acheteur?.isPersonnage(),
|
|
|
|
chatMessageIdVente
|
2022-10-07 19:05:56 +02:00
|
|
|
};
|
2022-11-13 21:56:23 +01:00
|
|
|
|
2022-12-23 00:34:17 +01:00
|
|
|
DialogItemAchat.changeNombreLots(venteData, 1);
|
2021-05-07 17:27:02 +02:00
|
|
|
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-achat.html`, venteData);
|
2022-12-09 02:00:31 +01:00
|
|
|
new DialogItemAchat(html, venteData).render(true);
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-23 00:34:17 +01:00
|
|
|
static changeNombreLots(venteData, nombreLots) {
|
|
|
|
venteData.choix.nombreLots = nombreLots;
|
|
|
|
venteData.prixTotal = (nombreLots * venteData.prixLot).toFixed(2);
|
|
|
|
if (venteData.isConsommable) {
|
|
|
|
const doses = nombreLots * venteData.tailleLot;
|
|
|
|
venteData.totalSust = Misc.keepDecimals(doses * (venteData.item.system.sust ?? 0), 2);
|
|
|
|
venteData.totalDesaltere = venteData.item.system.boisson
|
|
|
|
? Misc.keepDecimals(doses * (venteData.item.system.desaltere ?? 0), 2)
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 19:05:56 +02:00
|
|
|
constructor(html, venteData) {
|
2022-11-16 02:46:26 +01:00
|
|
|
let options = { classes: ["dialogachat"], width: 400, height: 'fit-content', 'z-index': 99999 };
|
2021-05-07 17:27:02 +02:00
|
|
|
|
|
|
|
const actionAchat = venteData.prixLot > 0 ? "Acheter" : "Prendre";
|
2021-06-21 23:37:33 +02:00
|
|
|
const buttons = {};
|
2022-12-23 00:34:17 +01:00
|
|
|
if (venteData.isConsommable) {
|
2022-11-13 21:56:23 +01:00
|
|
|
buttons["consommer"] = { label: venteData.item.system.boisson ? "Boire" : "Manger", callback: it => this.onAchatConsommer() }
|
2021-06-21 23:37:33 +02:00
|
|
|
}
|
|
|
|
buttons[actionAchat] = { label: actionAchat, callback: it => { this.onAchat(); } };
|
|
|
|
buttons["decliner"] = { label: "Décliner", callback: it => { } };
|
2022-12-23 00:34:17 +01:00
|
|
|
const acheteur = venteData.acheteur?.name ?? 'Un acheteur';
|
|
|
|
const vendeur = (venteData.service ?? venteData.vendeur)?.name ?? 'Un vendeur';
|
2021-05-07 17:27:02 +02:00
|
|
|
let conf = {
|
2022-12-23 00:34:17 +01:00
|
|
|
title: `${acheteur} - ${actionAchat} à ${vendeur}`,
|
2021-05-07 17:27:02 +02:00
|
|
|
content: html,
|
|
|
|
default: actionAchat,
|
2021-06-21 23:37:33 +02:00
|
|
|
buttons: buttons
|
2021-05-07 17:27:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
super(conf, options);
|
|
|
|
this.venteData = venteData;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onAchat() {
|
2022-12-09 02:00:31 +01:00
|
|
|
await this.html.find(".nombreLots").change();
|
2022-10-07 19:05:56 +02:00
|
|
|
(this.venteData.vendeur ?? this.venteData.acheteur).achatVente({
|
2021-10-07 23:52:02 +02:00
|
|
|
userId: game.user.id,
|
2022-12-23 00:34:17 +01:00
|
|
|
serviceId: this.venteData.service?.id,
|
2022-10-07 19:05:56 +02:00
|
|
|
vendeurId: this.venteData.vendeur?.id,
|
|
|
|
acheteurId: this.venteData.acheteur?.id,
|
2021-06-05 01:14:29 +02:00
|
|
|
prixTotal: this.venteData.prixTotal,
|
2022-10-07 19:05:56 +02:00
|
|
|
chatMessageIdVente: this.venteData.chatMessageIdVente,
|
2022-11-13 21:56:23 +01:00
|
|
|
choix: this.venteData.choix,
|
|
|
|
vente: this.venteData
|
2021-06-05 01:14:29 +02:00
|
|
|
});
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
2022-09-23 01:32:18 +02:00
|
|
|
|
2021-06-21 23:37:33 +02:00
|
|
|
async onAchatConsommer() {
|
|
|
|
this.venteData.choix.consommer = true;
|
|
|
|
await this.onAchat();
|
|
|
|
}
|
2021-05-07 17:27:02 +02:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
activateListeners(html) {
|
|
|
|
super.activateListeners(html);
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html = html;
|
|
|
|
this.html.find(".nombreLots").change(event => this.setNombreLots(Number(event.currentTarget.value)));
|
|
|
|
this.html.find(".se-forcer").change(event => this.setSeForcer(event));
|
2021-06-21 23:37:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setSeForcer(event) {
|
|
|
|
this.venteData.choix.seForcer = event.currentTarget.checked;
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 00:08:42 +02:00
|
|
|
setNombreLots(nombreLots) {
|
2022-12-23 00:34:17 +01:00
|
|
|
|
|
|
|
if (!this.venteData.quantiteIllimite) {
|
|
|
|
if (!this.venteData.quantiteIllimite && nombreLots > this.venteData.quantiteNbLots) {
|
|
|
|
ui.notifications.warn(`Seulement ${this.venteData.quantiteNbLots} lots disponibles, vous ne pouvez pas en prendre ${nombreLots}`)
|
|
|
|
}
|
|
|
|
nombreLots = Math.min(nombreLots, this.venteData.quantiteNbLots);
|
2022-09-23 01:32:18 +02:00
|
|
|
}
|
2022-12-23 00:34:17 +01:00
|
|
|
|
|
|
|
DialogItemAchat.changeNombreLots(this.venteData, nombreLots);
|
|
|
|
|
|
|
|
this.html.find(".nombreLots").val(nombreLots);
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find(".prixTotal").text(this.venteData.prixTotal);
|
2022-12-23 00:34:17 +01:00
|
|
|
this.html.find("span.total-sust").text(this.venteData.totalSust);
|
|
|
|
this.html.find("span.total-desaltere").text(this.venteData.totalDesaltere);
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-23 00:34:17 +01:00
|
|
|
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|