foundryvtt-reve-de-dragon/module/achat-vente/dialog-item-achat.js

119 lines
4.1 KiB
JavaScript
Raw Normal View History

import { Misc } from "../misc.js";
import { RdDUtility } from "../rdd-utility.js";
import { ChatVente } from "./chat-vente.js";
2021-05-07 17:27:02 +02:00
export class DialogItemAchat extends Dialog {
static preparerAchat(chatButton) {
return ChatVente.getDetailAchatVente(RdDUtility.findChatMessageId(chatButton))
}
static async onAcheter({ item, vendeur, acheteur, tailleLot, prixLot, nbLots, quantiteIllimite, chatMessageIdVente }) {
const venteData = {
item,
actingUserId: game.user.id,
vendeur,
acheteur,
tailleLot,
quantiteIllimite,
nbLots,
choix: { seForcer: false, supprimerSiZero: true },
prixLot,
isVente: prixLot > 0,
isConsommable: item.type == 'nourritureboisson' && acheteur?.isPersonnage(),
chatMessageIdVente
}
if (venteData.vendeur?.id == venteData.acheteur?.id) {
ui.notifications.info("Inutile de se vendre à soi-même")
return
}
DialogItemAchat.changeNombreLots(venteData, 1)
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-achat.html`, venteData)
new DialogItemAchat(html, venteData).render(true)
2021-05-07 17:27:02 +02: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;
}
}
constructor(html, venteData) {
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 = {};
if (venteData.isConsommable) {
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 => { } };
const acheteur = venteData.acheteur?.name ?? 'Un acheteur';
const vendeur = venteData.vendeur?.name ?? 'Un vendeur';
2021-05-07 17:27:02 +02:00
let conf = {
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() {
await this.html.find(".nombreLots").change();
(this.venteData.vendeur ?? this.venteData.acheteur).achatVente({
userId: game.user.id,
vendeurId: this.venteData.vendeur?.id,
acheteurId: this.venteData.acheteur?.id,
prixTotal: this.venteData.prixTotal,
chatMessageIdVente: this.venteData.chatMessageIdVente,
choix: this.venteData.choix,
vente: this.venteData
});
2021-05-07 17:27:02 +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);
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
}
setNombreLots(nbLots) {
if (!this.venteData.quantiteIllimite) {
if (!this.venteData.quantiteIllimite && nbLots > this.venteData.nbLots) {
ui.notifications.warn(`Seulement ${this.venteData.nbLots} lots disponibles, vous ne pouvez pas en prendre ${nbLots}`)
}
nbLots = Math.min(nbLots, this.venteData.nbLots);
}
DialogItemAchat.changeNombreLots(this.venteData, nbLots);
this.html.find(".nombreLots").val(nbLots);
this.html.find(".prixTotal").text(this.venteData.prixTotal);
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
}
2021-05-07 17:27:02 +02:00
}