foundryvtt-reve-de-dragon/module/dialog-consommer.js

82 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-04-06 23:44:57 +02:00
import { Misc } from "./misc.js";
export class DialogConsommer extends Dialog {
2021-04-11 23:01:10 +02:00
static async create(actor, item, template = undefined, options = {}) {
const consommerData = DialogConsommer.prepareData(actor, item, options);
const html = await renderTemplate(template ?? `systems/foundryvtt-reve-de-dragon/templates/consommer/dialog-${Misc.data(item).type}.html`, consommerData);
return new DialogConsommer(actor, item, consommerData, html, options)
}
constructor(actor, item, consommerData, html, options = {}) {
mergeObject(options, { classes: ["dialogconsommer"], width: 600, height: 500, 'z-index': 99999 }, { overwrite: false })
2021-04-06 23:44:57 +02:00
let conf = {
title: consommerData.title,
2021-04-11 23:01:10 +02:00
content: html,
2021-04-06 23:44:57 +02:00
default: consommerData.buttonName,
2021-04-11 23:01:10 +02:00
buttons: {
[consommerData.buttonName]: {
label: consommerData.buttonName, callback: it => {
this.actor.consommer(this.item, this.consommerData.choix);
}
}
}
2021-04-06 23:44:57 +02:00
};
2021-04-11 23:01:10 +02:00
super(conf, options);
2021-04-06 23:44:57 +02:00
2021-04-11 23:01:10 +02:00
this.actor = actor;
this.item = item;
this.consommerData = consommerData;
2021-04-06 23:44:57 +02:00
}
2021-04-11 23:01:10 +02:00
/* -------------------------------------------- */
static prepareData(actor, item, options) {
const itemData = duplicate(Misc.data(item));
let consommerData = {
item: itemData,
2021-04-16 00:29:08 +02:00
cuisine: Misc.data(actor.getCompetence('cuisine')),
2021-04-11 23:01:10 +02:00
choix: {
doses: options.doses ?? 1,
seForcer: options.seForcer ?? false,
}
}
switch (itemData.type) {
2021-04-06 23:44:57 +02:00
case 'nourritureboisson':
2021-04-11 23:01:10 +02:00
consommerData.title = itemData.data.boisson ? `${itemData.name}: boire une dose` : `${itemData.name}: manger une portion`;
consommerData.buttonName = itemData.data.boisson ? "Boire" : "Manger";
2021-04-06 23:44:57 +02:00
break;
case 'potion':
2021-04-12 00:16:23 +02:00
consommerData.title = `${itemData.name}: boire la potion`;
2021-04-06 23:44:57 +02:00
consommerData.buttonName = "Boire";
break;
}
2021-04-11 23:01:10 +02:00
DialogConsommer.calculDoses(consommerData, consommerData.choix.doses)
2021-04-06 23:44:57 +02:00
return consommerData;
}
2021-04-11 23:01:10 +02:00
static calculDoses(consommerData) {
const doses = consommerData.choix.doses;
consommerData.totalSust = Misc.keepDecimals(doses * (consommerData.item.data.sust ?? 0), 2);
consommerData.totalDesaltere = consommerData.item.data.boisson
? Misc.keepDecimals(doses * (consommerData.item.data.desaltere ?? 0), 2)
: 0;
2021-04-06 23:44:57 +02:00
}
2021-04-11 23:01:10 +02:00
2021-04-10 21:07:53 +02:00
/* -------------------------------------------- */
2021-04-06 23:44:57 +02:00
activateListeners(html) {
super.activateListeners(html);
2021-04-11 23:01:10 +02:00
html.find(".se-forcer").change(event => {
this.consommerData.choix.seForcer = event.currentTarget.checked;
});
2021-04-06 23:44:57 +02:00
html.find(".consommer-doses").change(event => {
2021-04-11 23:01:10 +02:00
this.consommerData.choix.doses = Number(event.currentTarget.value);
DialogConsommer.calculDoses(this.consommerData);
$(".total-sust").text(this.consommerData.totalSust)
$(".total-desaltere").text(this.consommerData.totalDesaltere)
2021-04-06 23:44:57 +02:00
});
}
}