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

113 lines
4.0 KiB
JavaScript
Raw Normal View History

import { Monnaie } from "./item-monnaie.js";
2021-05-07 17:27:02 +02:00
import { Misc } from "./misc.js";
import { RdDUtility } from "./rdd-utility.js";
export class DialogItemAchat extends Dialog {
static venteData(button) {
const vendeurId = button.attributes['data-vendeurId']?.value;
2021-05-07 17:27:02 +02:00
const vendeur = vendeurId ? game.actors.get(vendeurId) : undefined;
const acheteur = RdDUtility.getSelectedActor();
const json = button.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");
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
}
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,
tailleLot: parseInt(button.attributes['data-tailleLot']?.value ?? 1),
quantiteIllimite: button.attributes['data-quantiteIllimite']?.value == 'true',
quantiteNbLots: parseInt(button.attributes['data-quantiteNbLots']?.value),
choix: {
nombreLots: 1,
seForcer: false,
supprimerSiZero: true
},
prixLot: prixLot,
prixTotal: prixLot,
isVente: prixLot > 0,
chatMessageIdVente: RdDUtility.findChatMessageId(button)
};
}
static async onAcheter(venteData) {
2021-05-07 17:27:02 +02:00
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-achat.html`, venteData);
const dialog = new DialogItemAchat(html, venteData);
2021-05-07 17:27:02 +02:00
dialog.render(true);
}
constructor(html, venteData) {
const isConsommable = venteData.item.type == 'nourritureboisson' && venteData.acheteur?.isPersonnage();
2021-11-05 00:42:44 +01:00
let options = { classes: ["dialogachat"], width: 400, height: isConsommable ? 450 : 350, '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 (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 => { } };
2021-05-07 17:27:02 +02:00
let conf = {
title: venteData.acheteur ? venteData.acheteur.name + " - " + actionAchat : actionAchat,
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 $(".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);
2021-06-01 00:08:42 +02:00
html.find(".nombreLots").change(event => this.setNombreLots(Number(event.currentTarget.value)));
2021-06-21 23:37:33 +02:00
html.find(".se-forcer").change(event => this.setSeForcer(event));
}
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) {
if (nombreLots > this.venteData.quantiteNbLots) {
ui.notifications.warn(`Seulement ${this.venteData.quantiteNbLots} lots disponibles, vous ne pouvez pas en prendre ${nombreLots}`)
}
this.venteData.choix.nombreLots = Math.min(nombreLots, this.venteData.quantiteNbLots);
2021-05-07 17:27:02 +02:00
this.venteData.prixTotal = (nombreLots * this.venteData.prixLot).toFixed(2);
$(".nombreLots").val(this.venteData.choix.nombreLots);
2021-05-07 17:27:02 +02:00
$(".prixTotal").text(this.venteData.prixTotal);
}
}