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

91 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2021-04-06 23:44:57 +02:00
import { Misc } from "./misc.js";
export class DialogConsommer extends Dialog {
static async create(actor, item, onActionItem = async ()=>{}) {
const consommerData = DialogConsommer.prepareData(actor, item);
const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-item-consommer.html', consommerData);
return new DialogConsommer(actor, item, consommerData, html, onActionItem)
2021-04-11 23:01:10 +02:00
}
constructor(actor, item, consommerData, html, onActionItem = async ()=>{}) {
const options = { classes: ["dialogconsommer"], width: 350, height: 450, 'z-index': 99999 };
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: async it => {
await this.onConsommer(it);
await onActionItem();}
2021-04-11 23:01:10 +02:00
}
}
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
}
async onConsommer(event) {
await $(".se-forcer").change();
await $(".consommer-doses").change();
await this.actor.consommer(this.item, this.consommerData.choix);
}
2021-04-11 23:01:10 +02:00
/* -------------------------------------------- */
static prepareData(actor, item) {
2022-06-12 09:46:58 +02:00
const itemData = duplicate(item);
2021-04-11 23:01:10 +02:00
let consommerData = {
item: itemData,
2022-06-12 09:46:58 +02:00
cuisine: actor.getCompetence('cuisine'),
2021-04-11 23:01:10 +02:00
choix: {
doses: 1,
seForcer: false,
2021-04-11 23:01:10 +02:00
}
}
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);
html.find(".se-forcer").change(event => this.setSeForcer(event));
html.find(".consommer-doses").change(event => this.selectDoses(event));
}
setSeForcer(event) {
this.consommerData.choix.seForcer = event.currentTarget.checked;
2021-04-06 23:44:57 +02:00
}
selectDoses(event) {
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
}