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