Enchanter des Potions
This commit is contained in:
150
module/item/potion.js
Normal file
150
module/item/potion.js
Normal file
@ -0,0 +1,150 @@
|
||||
import { Grammar } from "../grammar.js";
|
||||
import { RdDItem } from "../item.js";
|
||||
import { SystemCompendiums } from "../settings/system-compendiums.js";
|
||||
import { RdDTimestamp } from "../time/rdd-timestamp.js";
|
||||
import { DialogEnchanter } from "./potion/dialog-enchanter.js";
|
||||
|
||||
const POTION_MAGIQUE = ['AlchimieEnchante', 'ReposEnchante', 'SoinEnchante', 'AutreEnchante']
|
||||
const POTION_ENCHANTABLE = ['Alchimie', 'Repos', 'Soin', 'Autre']
|
||||
.concat(POTION_MAGIQUE)
|
||||
|
||||
const MAP_CATEGORIE_ENCHANTEMENT = [
|
||||
{ basique: 'Alchimie', enchante: 'AlchimieEnchante' },
|
||||
{ basique: 'Repos', enchante: 'ReposEnchante' },
|
||||
{ basique: 'Soin', enchante: 'SoinEnchante' },
|
||||
{ basique: 'Autre', enchante: 'AutreEnchante' }]
|
||||
|
||||
export class RdDItemPotion extends RdDItem {
|
||||
|
||||
static async herbesSoins() {
|
||||
return await RdDItemPotion.$listHerbes(it => Grammar.equalsInsensitive(it.system.categorie, 'Soin') && it.system.niveau > 0)
|
||||
}
|
||||
static async herbesRepos() {
|
||||
return await RdDItemPotion.$listHerbes(it => Grammar.equalsInsensitive(it.system.categorie, 'Repos') && it.system.niveau > 0)
|
||||
}
|
||||
|
||||
static async $listHerbes(filter) {
|
||||
const herbes = await SystemCompendiums.getWorldOrCompendiumItems('herbe', 'faune-flore-mineraux');
|
||||
return herbes.filter(filter)
|
||||
}
|
||||
|
||||
static get defaultIcon() {
|
||||
return "systems/foundryvtt-reve-de-dragon/icons/objets/liqueur_de_bagdol.webp"
|
||||
}
|
||||
|
||||
prepareDerivedData() {
|
||||
super.prepareDerivedData()
|
||||
this.system.puissance = this.system.magique ? this.calculPuissance() : 0
|
||||
}
|
||||
|
||||
isPotion() { return true }
|
||||
isEnchantable() { return POTION_ENCHANTABLE.includes(this.system.categorie) }
|
||||
isMagique() { return POTION_MAGIQUE.includes(this.system.categorie) }
|
||||
|
||||
getActions(options = { warnIfNot: true }) {
|
||||
const actionConsommer = this.prepareAction('Consommer', options.warnIfNot);
|
||||
if (this.isEnchantable()) {
|
||||
return [
|
||||
actionConsommer,
|
||||
this.prepareAction('Enchanter', options.warnIfNot)
|
||||
]
|
||||
}
|
||||
return [
|
||||
actionConsommer
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
getUtilisation() {
|
||||
switch (this.system.categorie) {
|
||||
case 'Alchimie': case 'AlchimieEnchante':
|
||||
case 'AlchimieAutre':
|
||||
return 'alchimie'
|
||||
case 'Cuisine': return 'cuisine'
|
||||
case 'Remede': case 'Repos': case 'ReposEnchante': case 'Soin': case 'SoinEnchante':
|
||||
return 'soins'
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
_potionChatData() {
|
||||
return [
|
||||
`<b>Rareté</b>: ${this.system.rarete}`,
|
||||
`<b>Catégorie</b>: ${this.system.categorie}`,
|
||||
...this._inventaireTemplateChatData()
|
||||
]
|
||||
}
|
||||
|
||||
async enchanterPotion() {
|
||||
const actor = this.parent;
|
||||
if (actor && (!actor.isPersonnage() || !actor.isHautRevant())) {
|
||||
ui.notifications.info('Seul un haut rêvant peut enchanter une potion')
|
||||
return
|
||||
}
|
||||
const dailog = await DialogEnchanter.create(this, actor, (updates) => this.$onEnchanterPotion(updates));
|
||||
dailog.render(true)
|
||||
}
|
||||
|
||||
perteRevePotion() {
|
||||
if (this.system.magique && !this.system.prpermanent && this.system.pr > 0) {
|
||||
const nouveauReve = Math.max(this.system.pr - 1, 0)
|
||||
return {
|
||||
_id: it._id,
|
||||
name: this.name,
|
||||
img: this.img,
|
||||
'system.pr': nouveauReve,
|
||||
'system.quantite': nouveauReve > 0 ? it.system.quantite : 0,
|
||||
'system.magique': nouveauReve > 0
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
async $onEnchanterPotion(enchanter) {
|
||||
if (enchanter.nouveaupr == 0) {
|
||||
await this.update({
|
||||
'system.pr': 0,
|
||||
'system.purifie': false,
|
||||
'system.magique': false,
|
||||
'system.categorie': this.categorieEnchantement().basique,
|
||||
'system.prpermanent': false,
|
||||
'system.prdate': 0,
|
||||
'system.quantite': this.parent ? 0 : this.system.quantite
|
||||
})
|
||||
}
|
||||
else {
|
||||
const updates = {
|
||||
'system.pr': enchanter.nouveaupr,
|
||||
'system.purifie': enchanter.purifier,
|
||||
'system.magique': true,
|
||||
'system.categorie': this.categorieEnchantement().enchante,
|
||||
'system.prpermanent': enchanter.prpermanent,
|
||||
'system.prdate': RdDItemPotion.dateEnchantement()
|
||||
}
|
||||
this.update(updates)
|
||||
}
|
||||
}
|
||||
|
||||
calculPuissance() { return this.system.herbebonus * this.system.pr }
|
||||
|
||||
categorieEnchantement() {
|
||||
const categorie = this.system.categorie
|
||||
const categorieEnchantement = MAP_CATEGORIE_ENCHANTEMENT.find(it => [it.basique, it.enchante].includes(categorie))
|
||||
return categorieEnchantement ?? { basique: categorie, enchante: categorie }
|
||||
}
|
||||
|
||||
static dateEnchantement() {
|
||||
return game.system.rdd.calendrier.getTimestamp().debutJournee()
|
||||
}
|
||||
|
||||
static buildHerbesList(listeHerbes, max) {
|
||||
let list = {}
|
||||
for (let herbe of listeHerbes) {
|
||||
let brins = max - herbe.system.niveau;
|
||||
list[herbe.name] = `${herbe.name} (Bonus: ${herbe.system.niveau}, Brins: ${brins})`;
|
||||
}
|
||||
list['Autre'] = 'Autre (Bonus: variable, Brins: variable)'
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
46
module/item/potion/dialog-enchanter.js
Normal file
46
module/item/potion/dialog-enchanter.js
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
export class DialogEnchanter extends Dialog {
|
||||
|
||||
static async create(item, actor, callback) {
|
||||
const enchanter = {
|
||||
actor: actor,
|
||||
item: item,
|
||||
nouveaupr: item.system.pr,
|
||||
prpermanent: item.system.prpermanent,
|
||||
purifier: false
|
||||
}
|
||||
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/item/potion/dialog-enchanter.hbs`, enchanter)
|
||||
return new DialogEnchanter(enchanter, html, callback)
|
||||
}
|
||||
|
||||
constructor(enchanter, html, callback) {
|
||||
let options = { classes: ["dialog-enchanter"], width: 400, height: 'fit-content', 'z-index': 99999 }
|
||||
let conf = {
|
||||
title: "Enchanter une potion",
|
||||
content: html,
|
||||
default: "enchanter",
|
||||
buttons: {
|
||||
"enchanter": { label: "Enchanter", callback: it => this.onEnchanter() }
|
||||
}
|
||||
};
|
||||
super(conf, options)
|
||||
|
||||
this.callback = callback
|
||||
this.enchanter = enchanter
|
||||
}
|
||||
|
||||
activateListeners(html) {
|
||||
super.activateListeners(html)
|
||||
this.html = html
|
||||
this.html.find("input.nouveaupr").change(event => this.enchanter.nouveaupr = Number(event.currentTarget.value))
|
||||
this.html.find("input.purifier").change(event => this.enchanter.purifier = event.currentTarget.checked)
|
||||
this.html.find("input.prpermanent").change(event => this.enchanter.prpermanent = event.currentTarget.checked)
|
||||
}
|
||||
|
||||
|
||||
async onEnchanter() {
|
||||
await this.html.find(".nouveaupr").change()
|
||||
this.callback(this.enchanter);
|
||||
}
|
||||
|
||||
}
|
57
module/item/sheet-potion.js
Normal file
57
module/item/sheet-potion.js
Normal file
@ -0,0 +1,57 @@
|
||||
import { ITEM_TYPES } from "../item.js";
|
||||
import { RdDTimestamp } from "../time/rdd-timestamp.js";
|
||||
import { RdDItemPotion } from "./potion.js";
|
||||
import { RdDItemInventaireSheet } from "./sheet-base-inventaire.js";
|
||||
|
||||
|
||||
export class RdDPotionItemSheet extends RdDItemInventaireSheet {
|
||||
|
||||
static get ITEM_TYPE() { return ITEM_TYPES.potion };
|
||||
|
||||
static $calculBonusHerbe(formData, herbesList, max) {
|
||||
if (Number(formData.system.herbebrins)) {
|
||||
let herbe = herbesList.find(h => h.name.toLowerCase() == formData.system.herbe.toLowerCase());
|
||||
if (herbe) {
|
||||
const brinsRequis = max - herbe.system.niveau;
|
||||
const brinsManquants = Math.max(brinsRequis - formData.system.herbebrins, 0);
|
||||
formData.system.herbebonus = Math.max(herbe.system.niveau - brinsManquants, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
get potion(){ return this.item }
|
||||
|
||||
async getData() {
|
||||
const formData = await super.getData()
|
||||
formData.enchantable = this.potion.isEnchantable()
|
||||
const enchantement = this.potion.categorieEnchantement()
|
||||
formData.isSoins = enchantement.basique == 'Soin'
|
||||
formData.isRepos = enchantement.basique == 'Repos'
|
||||
if (formData.isSoins) {
|
||||
const herbesSoins = await RdDItemPotion.herbesSoins()
|
||||
RdDPotionItemSheet.$calculBonusHerbe(formData, herbesSoins, 12);
|
||||
formData.herbesSoins = RdDItemPotion.buildHerbesList(herbesSoins, 12)
|
||||
}
|
||||
if (formData.isRepos) {
|
||||
const herbesRepos = await RdDItemPotion.herbesRepos()
|
||||
RdDPotionItemSheet.$calculBonusHerbe(formData, herbesRepos, 7);
|
||||
formData.herbesRepos = RdDItemPotion.buildHerbesList(herbesRepos, 7)
|
||||
}
|
||||
formData.dateActuelle = game.system.rdd.calendrier.dateCourante()
|
||||
formData.enchantement = RdDTimestamp.splitIndexDate(this.potion.system.prdate)
|
||||
return formData
|
||||
}
|
||||
|
||||
activateListeners(html) {
|
||||
super.activateListeners(html);
|
||||
this.html.find('.enchanter-potion').click((event) => this.potion.enchanterPotion())
|
||||
|
||||
this.html.find('.date-enchantement').change((event) => {
|
||||
const jour = Number(this.html.find('input.date-enchantement[name="enchantement.jour"]').val())
|
||||
const mois = RdDTimestamp.definition(this.html.find('select.date-enchantement[name="enchantement.mois"]').val())
|
||||
const indexDate = game.system.rdd.calendrier.getIndexFromDate(jour, mois.heure)
|
||||
this.potion.update({ 'system.prdate': indexDate })
|
||||
console.warn(`Date d'enchantement modifiée ${jour}/${mois.heure}: ${indexDate}`)
|
||||
});
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user