diff --git a/fvtt-hamalron.mjs b/fvtt-hamalron.mjs index 2c0e7ba..c598de2 100644 --- a/fvtt-hamalron.mjs +++ b/fvtt-hamalron.mjs @@ -285,6 +285,24 @@ Hooks.on("createChatMessage", async (message, options, userId) => { }) +// Helper: ensure deck manager is initialized and has cards +async function ensureDeck() { + let dm = globalThis.HamalronTarotDeckManager?._instance + if (!dm) { + const DM = game.system.api.applications.HamalronTarotDeckManager + dm = new DM() + DM._instance = dm + } + if (dm.deck.length === 0) { + await dm.resetDeck() + } + if (dm.deck.length === 0) { + ui.notifications.warn(game.i18n.localize("HAMALRON.Notifications.deckEmpty")) + return null + } + return dm +} + // Chat action buttons (Sous tension / Opposé) $(document).on("click", "[data-action='sousTension']", async function () { const msgEl = $(this).closest(".chat-message") @@ -292,17 +310,13 @@ $(document).on("click", "[data-action='sousTension']", async function () { const msg = game.messages?.get(msgId) if (!msg) return - // Extract the player's total from the message content const totalText = msgEl.find(".calc-total")?.text()?.trim() const totalJoueur = Number(totalText) || 0 const actorName = msg.speaker?.alias || "Inconnu" - // Draw a card and apply tension modifier (default 0) - const deckManager = globalThis.HamalronTarotDeckManager?._instance - if (!deckManager || deckManager.deck.length === 0) { - ui.notifications.warn(game.i18n.localize("HAMALRON.Notifications.deckEmpty")) - return - } + const deckManager = await ensureDeck() + if (!deckManager) return + const drawn = deckManager.drawCards(1) if (!drawn.length) return const card = drawn[0] @@ -311,11 +325,10 @@ $(document).on("click", "[data-action='sousTension']", async function () { deckManager.addToDiscard(card) await deckManager.saveDeckState() - const totalMJ = valeurMJ - const success = totalJoueur >= totalMJ + const success = totalJoueur >= valeurMJ ChatMessage.create({ content: `
${actorName}: ${totalJoueur} vs MJ: ${card.name} (${totalMJ})
+${actorName}: ${totalJoueur} vs MJ: ${card.name} (${valeurMJ})
${success ? game.i18n.localize("HAMALRON.Label.success") : game.i18n.localize("HAMALRON.Label.failure")}