import { VermineExperience } from "../experience.mjs"; const { HandlebarsApplicationMixin } = foundry.applications.api; /** * Dialogue d'achat d'une Adaptation/Mutation avec les Dés d'Évolution * accumulés. Vérifie le coût (niveau 1-4), la limite du Mode de jeu et les * restrictions sur les Mutations ; en cas de succès, crée l'item et efface * les Dés d'Évolution accumulés. */ export default class BuyEvolutionDialog extends HandlebarsApplicationMixin(foundry.applications.api.ApplicationV2) { #actor; static DEFAULT_OPTIONS = { classes: ["vermine-roll", "buy-evolution"], tag: "form", window: { icon: "fas fa-cart-shopping", resizable: false }, position: { width: 480, height: 460 }, actions: { buy: BuyEvolutionDialog.#onBuy, cancel: BuyEvolutionDialog.#onCancel } }; static PARTS = { main: { template: "systems/vermine2047/templates/dialogs/buy-evolution-dialog.hbs", scrollable: [""] } }; static async create({ actorId }) { const actor = await game.actors.get(actorId); if (!actor) { ui.notifications.warn(game.i18n.localize("VERMINE.error_no_actor_selected")); return null; } return new BuyEvolutionDialog({ actor }); } constructor(options = {}) { super(options); this.#actor = options.actor; } get title() { return game.i18n.localize("VERMINE.buy_evolution"); } async _prepareContext() { const actor = this.#actor; const mode = VermineExperience.gameMode(); const modeLabels = { 1: game.i18n.localize("VERMINE.mode_survie"), 2: game.i18n.localize("VERMINE.mode_cauchemar"), 3: game.i18n.localize("VERMINE.mode_apocalypse") }; const levels = []; for (let value = 1; value <= 4; value++) { levels.push({ value, cost: value }); } return { evolutionDice: actor.system?.experience?.evolution || 0, defaultName: game.i18n.localize("ITEMS.new_evolution"), levels, totalLevels: VermineExperience.evolutionTotalLevels(actor), modeMax: VermineExperience.evolutionMaxForMode(mode), modeLabel: modeLabels[mode] || "" }; } static async #onCancel(event, target) { this.close(); } static async #onBuy(event, target) { const actor = this.#actor; const name = this.element.querySelector("#evolution-name")?.value?.trim() || ""; const cost = parseInt(this.element.querySelector("#evolution-level")?.value, 10) || 0; const mutation = this.element.querySelector("#evolution-mutation")?.checked || false; if (!name) { ui.notifications.warn(game.i18n.localize("VERMINE.error_evolution_no_name")); return; } const error = VermineExperience.validateEvolutionPurchase(actor, cost, mutation); if (error) { ui.notifications.warn(game.i18n.localize(error)); return; } const created = await actor.createEmbeddedDocuments("Item", [{ name, type: "evolution", system: { level: { value: cost }, mutation } }]); // N'efface les Dés d'Évolution que si l'item a bien été créé. if (!created || created.length === 0) { ui.notifications.warn(game.i18n.localize("VERMINE.error_evolution_create")); return; } await actor.update({ "system.experience.evolution": 0 }); ui.notifications.info(game.i18n.localize("VERMINE.buy_evolution_done")); this.close(); } }