2021-04-06 23:44:57 +02:00
|
|
|
import { Misc } from "./misc.js";
|
|
|
|
|
|
|
|
export class DialogConsommer extends Dialog {
|
|
|
|
|
2022-12-05 15:29:00 +01:00
|
|
|
static async create(actor, item, onActionItem = async () => { }) {
|
2021-12-05 01:50:09 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-05 15:29:00 +01:00
|
|
|
constructor(actor, item, consommerData, html, onActionItem = async () => { }) {
|
2022-11-16 02:46:26 +01:00
|
|
|
const options = { classes: ["dialogconsommer"], width: 350, height: 'fit-content', '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]: {
|
2021-12-05 01:50:09 +01:00
|
|
|
label: consommerData.buttonName, callback: async it => {
|
2022-12-09 02:00:31 +01:00
|
|
|
await this.onConsommer();
|
2022-12-05 15:29:00 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
activateListeners(html) {
|
|
|
|
super.activateListeners(html);
|
|
|
|
this.html = html;
|
|
|
|
this.html.find(".se-forcer").change(event => this.setSeForcer(event));
|
|
|
|
this.html.find(".consommer-doses").change(event => this.selectDoses(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
async onConsommer() {
|
|
|
|
await this.html.find(".se-forcer").change();
|
|
|
|
await this.html.find(".consommer-doses").change();
|
2021-12-05 01:50:09 +01:00
|
|
|
await this.actor.consommer(this.item, this.consommerData.choix);
|
2021-06-04 19:35:27 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 23:01:10 +02:00
|
|
|
/* -------------------------------------------- */
|
2021-12-05 01:50:09 +01:00
|
|
|
static prepareData(actor, item) {
|
2021-04-11 23:01:10 +02:00
|
|
|
let consommerData = {
|
2022-12-05 15:29:00 +01:00
|
|
|
item: duplicate(item),
|
2022-06-12 09:46:58 +02:00
|
|
|
cuisine: actor.getCompetence('cuisine'),
|
2021-04-11 23:01:10 +02:00
|
|
|
choix: {
|
2021-12-05 01:50:09 +01:00
|
|
|
doses: 1,
|
|
|
|
seForcer: false,
|
2021-04-11 23:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-07 18:47:56 +02:00
|
|
|
switch (item.type) {
|
2022-12-03 22:32:32 +01:00
|
|
|
case 'herbe': case 'faune':
|
|
|
|
consommerData.title = 'Manger une portion crue: ';
|
|
|
|
consommerData.buttonName = "Manger";
|
|
|
|
break;
|
2021-04-06 23:44:57 +02:00
|
|
|
case 'nourritureboisson':
|
2022-12-03 22:32:32 +01:00
|
|
|
consommerData.title = item.system.boisson ? 'Boire une dose: ' : 'Manger une portion: ';
|
2022-09-07 18:47:56 +02:00
|
|
|
consommerData.buttonName = item.system.boisson ? "Boire" : "Manger";
|
2021-04-06 23:44:57 +02:00
|
|
|
break;
|
|
|
|
case 'potion':
|
2022-12-03 22:32:32 +01:00
|
|
|
consommerData.title = 'Boire la potion: ';
|
2021-04-06 23:44:57 +02:00
|
|
|
consommerData.buttonName = "Boire";
|
|
|
|
break;
|
|
|
|
}
|
2022-12-03 22:32:32 +01:00
|
|
|
consommerData.title += item.name;
|
|
|
|
DialogConsommer.calculDoses(consommerData, item)
|
2021-04-06 23:44:57 +02:00
|
|
|
return consommerData;
|
|
|
|
}
|
|
|
|
|
2022-12-03 22:32:32 +01:00
|
|
|
static calculDoses(consommer, item) {
|
2022-09-07 18:47:56 +02:00
|
|
|
const doses = consommer.choix.doses;
|
2022-12-03 22:32:32 +01:00
|
|
|
switch (item.type) {
|
|
|
|
case 'herbe': case 'faune':
|
|
|
|
consommer.totalSust = doses;
|
|
|
|
consommer.totalDesaltere = 0;
|
|
|
|
consommer.choix.sust = 1;
|
|
|
|
consommer.choix.quantite = 0;
|
|
|
|
consommer.choix.encombrement = Misc.keepDecimals(consommer.item.system.encombrement / item.system.sust, 2);
|
|
|
|
return;
|
|
|
|
case 'nourritureboisson':
|
|
|
|
consommer.choix.sust = consommer.item.system.sust;
|
|
|
|
consommer.choix.quantite = doses;
|
|
|
|
consommer.choix.encombrement = 0
|
|
|
|
consommer.totalSust = Misc.keepDecimals(doses * (consommer.item.system.sust ?? 0), 2);
|
|
|
|
consommer.totalDesaltere = consommer.item.system.boisson
|
|
|
|
? Misc.keepDecimals(doses * (consommer.item.system.desaltere ?? 0), 2)
|
|
|
|
: 0;
|
|
|
|
break;
|
|
|
|
case 'potion':
|
|
|
|
consommer.totalSust = 0
|
|
|
|
consommer.totalDesaltere = 0
|
|
|
|
}
|
2021-04-06 23:44:57 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 23:01:10 +02:00
|
|
|
|
2021-06-04 19:35:27 +02:00
|
|
|
setSeForcer(event) {
|
|
|
|
this.consommerData.choix.seForcer = event.currentTarget.checked;
|
2021-04-06 23:44:57 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 19:35:27 +02:00
|
|
|
selectDoses(event) {
|
|
|
|
this.consommerData.choix.doses = Number(event.currentTarget.value);
|
2022-12-03 22:32:32 +01:00
|
|
|
DialogConsommer.calculDoses(this.consommerData, this.item);
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find(".total-sust").text(this.consommerData.totalSust);
|
|
|
|
this.html.find(".total-desaltere").text(this.consommerData.totalDesaltere);
|
2021-06-04 19:35:27 +02:00
|
|
|
}
|
2021-04-06 23:44:57 +02:00
|
|
|
}
|