fix: boutons Sous tension/Opposé auto-initialisent le deck
ensureDeck() crée et reset le deck manager si nécessaire avant de tirer une carte pour les boutons du chat.
This commit is contained in:
+26
-16
@@ -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: `<div class="fvtt-hamalron"><h3>${game.i18n.localize("HAMALRON.Label.sousTension")}</h3>
|
||||
<p><strong>${actorName}</strong>: ${totalJoueur} vs <strong>MJ</strong>: ${card.name} (${totalMJ})</p>
|
||||
<p><strong>${actorName}</strong>: ${totalJoueur} vs <strong>MJ</strong>: ${card.name} (${valeurMJ})</p>
|
||||
<p>${success ? game.i18n.localize("HAMALRON.Label.success") : game.i18n.localize("HAMALRON.Label.failure")}</p></div>`,
|
||||
style: CONST.CHAT_MESSAGE_STYLES.OTHER,
|
||||
})
|
||||
@@ -331,12 +344,9 @@ $(document).on("click", "[data-action='oppose']", async function () {
|
||||
const totalJoueur = Number(totalText) || 0
|
||||
const actorName = msg.speaker?.alias || "Inconnu"
|
||||
|
||||
// Draw opposition card
|
||||
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]
|
||||
|
||||
Reference in New Issue
Block a user