2021-06-05 01:14:29 +02:00
|
|
|
|
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 async onButtonAcheter(event) {
|
2021-06-05 01:14:29 +02:00
|
|
|
const buttonAcheter = event.currentTarget;
|
|
|
|
if (!buttonAcheter.attributes['data-jsondata']?.value) {
|
2021-05-07 17:27:02 +02:00
|
|
|
ui.notifications.warn("Impossible d'acheter: informations sur l'objet manquantes")
|
|
|
|
return;
|
|
|
|
}
|
2021-06-05 01:14:29 +02:00
|
|
|
const chatMessageIdVente = RdDUtility.findChatMessageId(buttonAcheter);
|
|
|
|
|
|
|
|
const vendeurId = buttonAcheter.attributes['data-vendeurId']?.value;
|
2021-05-07 17:27:02 +02:00
|
|
|
const vendeur = vendeurId ? game.actors.get(vendeurId) : undefined;
|
|
|
|
const acheteur = RdDUtility.getSelectedActor();
|
|
|
|
|
|
|
|
if (!acheteur && !vendeur) {
|
|
|
|
ui.notifications.info("Pas d'acheteur ni de vendeur, aucun changement");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-05 01:14:29 +02:00
|
|
|
let venteData = DialogItemAchat.prepareVenteData(buttonAcheter, vendeurId, vendeur, acheteur);
|
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, vendeur, acheteur, venteData, chatMessageIdVente);
|
|
|
|
dialog.render(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(html, vendeur, acheteur, venteData, chatMessageIdVente) {
|
2021-06-21 23:37:33 +02:00
|
|
|
const isConsommable = venteData.item.type == 'nourritureboisson';
|
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.data.boisson ? "Boire" : "Manger", callback: it => { this.onAchatConsommer(); } }
|
|
|
|
}
|
|
|
|
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 = {
|
2021-11-05 00:42:44 +01:00
|
|
|
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.vendeur = vendeur;
|
|
|
|
this.acheteur = acheteur;
|
|
|
|
this.chatMessageIdVente = chatMessageIdVente;
|
|
|
|
this.venteData = venteData;
|
|
|
|
}
|
|
|
|
|
2021-06-05 01:14:29 +02:00
|
|
|
static prepareVenteData(buttonAcheter, vendeurId, vendeur, acheteur) {
|
|
|
|
const jsondata = buttonAcheter.attributes['data-jsondata']?.value;
|
2022-07-22 01:01:46 +02:00
|
|
|
const prixLot = parseInt(buttonAcheter.attributes['data-prixLot']?.value ?? 0);
|
2021-06-05 01:14:29 +02:00
|
|
|
let venteData = {
|
|
|
|
item: JSON.parse(jsondata),
|
|
|
|
vendeurId: vendeurId,
|
|
|
|
vendeur: Misc.data(vendeur),
|
|
|
|
acheteur: Misc.data(acheteur),
|
|
|
|
tailleLot: parseInt(buttonAcheter.attributes['data-tailleLot']?.value ?? 1),
|
|
|
|
quantiteIllimite: buttonAcheter.attributes['data-quantiteIllimite']?.value == 'true',
|
|
|
|
quantiteNbLots: parseInt(buttonAcheter.attributes['data-quantiteNbLots']?.value),
|
2021-06-21 23:37:33 +02:00
|
|
|
choix: {
|
|
|
|
nombreLots: 1,
|
|
|
|
seForcer: false,
|
|
|
|
supprimerSiZero: true
|
|
|
|
},
|
2021-06-05 01:14:29 +02:00
|
|
|
prixLot: prixLot,
|
|
|
|
prixTotal: prixLot,
|
|
|
|
isVente: prixLot > 0
|
|
|
|
};
|
|
|
|
return venteData;
|
|
|
|
}
|
|
|
|
|
2021-05-07 17:27:02 +02:00
|
|
|
async onAchat() {
|
2021-06-04 19:35:27 +02:00
|
|
|
await $(".nombreLots").change();
|
2021-06-05 01:14:29 +02:00
|
|
|
(this.vendeur ?? this.acheteur).achatVente({
|
2021-10-07 23:52:02 +02:00
|
|
|
userId: game.user.id,
|
2021-06-05 01:14:29 +02:00
|
|
|
vendeurId: this.vendeur?.id,
|
|
|
|
acheteurId: this.acheteur?.id,
|
|
|
|
prixTotal: this.venteData.prixTotal,
|
2021-06-21 23:37:33 +02:00
|
|
|
chatMessageIdVente: this.chatMessageIdVente,
|
|
|
|
choix: this.venteData.choix
|
2021-06-05 01:14:29 +02:00
|
|
|
});
|
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) {
|
2021-06-21 23:37:33 +02:00
|
|
|
this.venteData.choix.nombreLots = nombreLots;
|
2021-05-07 17:27:02 +02:00
|
|
|
this.venteData.prixTotal = (nombreLots * this.venteData.prixLot).toFixed(2);
|
|
|
|
$(".prixTotal").text(this.venteData.prixTotal);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|