2021-05-07 17:27:02 +02:00
|
|
|
import { RdDActor } from "./actor.js";
|
|
|
|
import { HtmlUtility } from "./html-utility.js";
|
|
|
|
import { Misc } from "./misc.js";
|
|
|
|
import { RdDUtility } from "./rdd-utility.js";
|
|
|
|
|
|
|
|
export class DialogItemAchat extends Dialog {
|
|
|
|
|
|
|
|
static async onButtonAcheter(event) {
|
|
|
|
let jsondata = event.currentTarget.attributes['data-jsondata']?.value;
|
|
|
|
if (!jsondata) {
|
|
|
|
ui.notifications.warn("Impossible d'acheter: informations sur l'objet manquantes")
|
|
|
|
return;
|
|
|
|
}
|
2021-05-08 03:52:19 +02:00
|
|
|
const vendeurId = event.currentTarget.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const chatMessageIdVente = RdDUtility.findChatMessageId(event.currentTarget);
|
|
|
|
const itemData = JSON.parse(jsondata);
|
|
|
|
const prixLot = event.currentTarget.attributes['data-prixLot']?.value ?? 0;
|
|
|
|
let venteData = {
|
|
|
|
item: itemData,
|
2021-05-08 03:52:19 +02:00
|
|
|
vendeurId: vendeurId,
|
2021-05-07 17:27:02 +02:00
|
|
|
vendeur: Misc.data(vendeur),
|
|
|
|
acheteur: Misc.data(acheteur),
|
|
|
|
tailleLot: event.currentTarget.attributes['data-tailleLot']?.value ?? 1,
|
2021-05-20 02:49:48 +02:00
|
|
|
quantiteIllimite : event.currentTarget.attributes['data-quantiteIllimite']?.value == 'true',
|
2021-05-07 17:27:02 +02:00
|
|
|
quantiteNbLots: event.currentTarget.attributes['data-quantiteNbLots']?.value,
|
|
|
|
nombreLots: 1,
|
|
|
|
prixLot: prixLot,
|
|
|
|
prixTotal: prixLot,
|
|
|
|
isVente: prixLot > 0
|
|
|
|
};
|
|
|
|
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) {
|
|
|
|
let options = { classes: ["dialogachat"], width: 400, height: 300, 'z-index': 99999 };
|
|
|
|
|
|
|
|
const actionAchat = venteData.prixLot > 0 ? "Acheter" : "Prendre";
|
|
|
|
let conf = {
|
|
|
|
title: actionAchat,
|
|
|
|
content: html,
|
|
|
|
default: actionAchat,
|
|
|
|
buttons: {
|
|
|
|
[actionAchat]: { label: actionAchat, callback: it => { this.onAchat(); } },
|
|
|
|
"decliner": { label: "Décliner", callback: it => { } }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
super(conf, options);
|
|
|
|
|
|
|
|
this.vendeur = vendeur;
|
|
|
|
this.acheteur = acheteur;
|
|
|
|
this.chatMessageIdVente = chatMessageIdVente;
|
|
|
|
this.venteData = venteData;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onAchat() {
|
2021-06-04 19:35:27 +02:00
|
|
|
await $(".nombreLots").change();
|
2021-05-07 17:27:02 +02:00
|
|
|
(this.vendeur ?? this.acheteur).achatVente(
|
|
|
|
this.vendeur?.id,
|
|
|
|
this.acheteur?.id,
|
|
|
|
this.venteData,
|
|
|
|
this.chatMessageIdVente
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
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-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 00:08:42 +02:00
|
|
|
setNombreLots(nombreLots) {
|
2021-05-07 17:27:02 +02:00
|
|
|
this.venteData.nombreLots = nombreLots;
|
|
|
|
this.venteData.prixTotal = (nombreLots * this.venteData.prixLot).toFixed(2);
|
|
|
|
$(".prixTotal").text(this.venteData.prixTotal);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|