2021-04-11 12:07:43 +02:00
|
|
|
import { Grammar } from "./grammar.js";
|
|
|
|
import { Misc } from "./misc.js";
|
|
|
|
import { RdDUtility } from "./rdd-utility.js";
|
|
|
|
|
|
|
|
export class DialogFabriquerPotion extends Dialog {
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static async create(actor, item, dialogConfig) {
|
2022-09-23 02:23:20 +02:00
|
|
|
const min = DialogFabriquerPotion.nombreBrinsMinimum(item);
|
|
|
|
if (item.system.quantite < min) {
|
|
|
|
ui.notifications.warn(`Vous avez ${item.system.quantite} brins de ${item.name}, il en faut au moins ${min} pour faire une potion!`);
|
|
|
|
return;
|
|
|
|
}
|
2021-04-11 12:07:43 +02:00
|
|
|
let potionData = DialogFabriquerPotion.prepareData(actor, item);
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
const html = await renderTemplate(dialogConfig.html, potionData);
|
2021-04-11 12:07:43 +02:00
|
|
|
|
|
|
|
let options = { classes: ["dialogfabriquerpotion"], width: 600, height: 160, 'z-index': 99999 };
|
|
|
|
mergeObject(options, dialogConfig.options ?? {}, { overwrite: true })
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
new DialogFabriquerPotion(actor, potionData, html, options).render(true);
|
2021-04-11 12:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static prepareData(actor, item) {
|
2022-06-12 09:46:58 +02:00
|
|
|
let potionData = duplicate(item)
|
2022-09-23 02:23:20 +02:00
|
|
|
potionData.nbBrinsSelect = RdDUtility.buildListOptions(
|
|
|
|
DialogFabriquerPotion.nombreBrinsMinimum(item),
|
|
|
|
DialogFabriquerPotion.nombreBrinsOptimal(item));
|
|
|
|
potionData.nbBrins = Math.min(potionData.system.quantite, DialogFabriquerPotion.nombreBrinsOptimal(potionData));
|
|
|
|
potionData.herbebonus = item.system.niveau;
|
2021-04-11 12:07:43 +02:00
|
|
|
potionData.buttonName = "Fabriquer";
|
|
|
|
return potionData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-12-09 02:00:31 +01:00
|
|
|
constructor(actor, potionData, html, options) {
|
|
|
|
const conf = {
|
|
|
|
title: `Fabriquer une potion de ${potionData.system.categorie}`,
|
|
|
|
content: html,
|
|
|
|
default: 'fabriquer',
|
|
|
|
buttons: {
|
|
|
|
'fabriquer': {
|
|
|
|
label: potionData.buttonName, callback: it => this.onFabriquer(html)
|
|
|
|
}
|
2021-04-11 12:07:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
super(conf, options);
|
|
|
|
|
|
|
|
this.actor = actor;
|
|
|
|
this.potionData = potionData;
|
|
|
|
}
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
activateListeners(html) {
|
|
|
|
super.activateListeners(html);
|
|
|
|
this.html = html;
|
|
|
|
this.html.find("[name='nbBrins']").change(event => {
|
|
|
|
this.potionData.nbBrins = Misc.toInt(event.currentTarget.value);
|
|
|
|
const brinsManquants = Math.max(0, DialogFabriquerPotion.nombreBrinsOptimal(this.potionData) - this.potionData.nbBrins);
|
|
|
|
this.potionData.herbebonus = Math.max(0, this.potionData.system.niveau - brinsManquants)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
async onFabriquer(html) {
|
|
|
|
await this.html.find("[name='nbBrins']").change();
|
|
|
|
this.actor.fabriquerPotion(this.potionData);
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
2022-09-23 02:23:20 +02:00
|
|
|
static nombreBrinsMinimum(herbeData) {
|
|
|
|
switch (herbeData.system.categorie ?? '') {
|
|
|
|
case "Soin": return 1 + Math.max(0, 12 - 2 * herbeData.system.niveau);
|
|
|
|
case "Repos": return 1 + Math.max(0, 7 - 2 * herbeData.system.niveau);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nombreBrinsOptimal(herbeData) {
|
2022-09-07 00:09:17 +02:00
|
|
|
switch (herbeData.system.categorie ?? '') {
|
|
|
|
case "Soin": return 12 - herbeData.system.niveau;
|
|
|
|
case "Repos": return 7 - herbeData.system.niveau;
|
2021-06-04 19:30:29 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2021-04-11 12:07:43 +02:00
|
|
|
}
|