Import de foundry.applications.handlebars.renderTemplate au travers d'une constante pour éliminer les warnings
		
			
				
	
	
		
			110 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			110 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import { renderTemplate } from "./constants.js";
 | |
| import { Misc } from "./misc.js";
 | |
| 
 | |
| export class DialogConsommer extends Dialog {
 | |
| 
 | |
|   static async create(actor, item) {
 | |
|     const consommerData = DialogConsommer.prepareData(actor, item);
 | |
|     const html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-item-consommer.hbs', consommerData);
 | |
|     return new DialogConsommer(actor, item, consommerData, html)
 | |
|   }
 | |
| 
 | |
|   constructor(actor, item, consommerData, html) {
 | |
|     const options = { classes: ["dialogconsommer"], width: 350, height: 'fit-content', 'z-index': 99999 };
 | |
|     let conf = {
 | |
|       title: consommerData.title,
 | |
|       content: html,
 | |
|       default: consommerData.buttonName,
 | |
|       buttons: {
 | |
|         [consommerData.buttonName]: {
 | |
|           label: consommerData.buttonName, callback: async it => await this.onConsommer()
 | |
|         }
 | |
|       }
 | |
|     };
 | |
| 
 | |
|     super(conf, options);
 | |
| 
 | |
|     this.actor = actor;
 | |
|     this.item = item;
 | |
|     this.consommerData = consommerData;
 | |
|   }
 | |
| 
 | |
|   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();
 | |
|     await this.actor.consommer(this.item, this.consommerData.choix);
 | |
|   }
 | |
| 
 | |
|   /* -------------------------------------------- */
 | |
|   static prepareData(actor, item) {
 | |
|     let consommerData = {
 | |
|       item: foundry.utils.duplicate(item),
 | |
|       cuisine: actor.getCompetence('cuisine'),
 | |
|       choix: {
 | |
|         doses: 1,
 | |
|         seForcer: false,
 | |
|       }
 | |
|     }
 | |
|     switch (item.type) {
 | |
|       case 'herbe': case 'faune':
 | |
|         consommerData.title = 'Manger une portion crue: ';
 | |
|         consommerData.buttonName = "Manger";
 | |
|         break;
 | |
|       case 'nourritureboisson':
 | |
|         consommerData.title = item.system.boisson ? 'Boire une dose: ' : 'Manger une portion: ';
 | |
|         consommerData.buttonName = item.system.boisson ? "Boire" : "Manger";
 | |
|         break;
 | |
|       case 'potion':
 | |
|         consommerData.title = 'Boire la potion: ';
 | |
|         consommerData.buttonName = "Boire";
 | |
|         break;
 | |
|     }
 | |
|     consommerData.title += item.name;
 | |
|     DialogConsommer.calculDoses(consommerData, item)
 | |
|     return consommerData;
 | |
|   }
 | |
| 
 | |
|   static calculDoses(consommer, item) {
 | |
|     const doses = consommer.choix.doses;
 | |
|     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
 | |
|     }
 | |
|   }
 | |
| 
 | |
| 
 | |
|   setSeForcer(event) {
 | |
|     this.consommerData.choix.seForcer = event.currentTarget.checked;
 | |
|   }
 | |
| 
 | |
|   selectDoses(event) {
 | |
|     this.consommerData.choix.doses = Number(event.currentTarget.value);
 | |
|     DialogConsommer.calculDoses(this.consommerData, this.item);
 | |
|     this.html.find(".total-sust").text(this.consommerData.totalSust);
 | |
|     this.html.find(".total-desaltere").text(this.consommerData.totalDesaltere);
 | |
|   }
 | |
| } |