Workable system, firt step
This commit is contained in:
@@ -4,8 +4,8 @@ export default class HamalronTarotDeckManager extends HandlebarsApplicationMixin
|
||||
constructor(options = {}) {
|
||||
super(options)
|
||||
this.deck = []
|
||||
this.drawnCards = []
|
||||
this.resetDeck()
|
||||
this.discard = []
|
||||
this.loadDeckState()
|
||||
}
|
||||
|
||||
/** @override */
|
||||
@@ -26,6 +26,10 @@ export default class HamalronTarotDeckManager extends HandlebarsApplicationMixin
|
||||
resetDeck: HamalronTarotDeckManager.#onResetDeck,
|
||||
drawCard: HamalronTarotDeckManager.#onDrawCard,
|
||||
drawMultiple: HamalronTarotDeckManager.#onDrawMultiple,
|
||||
shuffleDiscard: HamalronTarotDeckManager.#onShuffleDiscard,
|
||||
collectHands: HamalronTarotDeckManager.#onCollectHands,
|
||||
distributeCards: HamalronTarotDeckManager.#onDistributeCards,
|
||||
drawTension: HamalronTarotDeckManager.#onDrawTension,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -36,12 +40,39 @@ export default class HamalronTarotDeckManager extends HandlebarsApplicationMixin
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the current deck state to settings
|
||||
*/
|
||||
async saveDeckState() {
|
||||
const state = {
|
||||
deck: this.deck,
|
||||
discard: this.discard,
|
||||
}
|
||||
await game.settings.set("fvtt-hamalron", "tarotDeckState", state)
|
||||
console.log("Deck state saved:", state)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the deck state from settings
|
||||
*/
|
||||
loadDeckState() {
|
||||
const state = game.settings.get("fvtt-hamalron", "tarotDeckState")
|
||||
if (state && state.deck && state.deck.length > 0) {
|
||||
this.deck = state.deck
|
||||
this.discard = state.discard || []
|
||||
console.log("Deck state loaded:", state)
|
||||
} else {
|
||||
// Si aucun état sauvegardé, initialiser un nouveau deck
|
||||
this.resetDeck()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the deck to a full tarot deck
|
||||
*/
|
||||
resetDeck() {
|
||||
this.deck = []
|
||||
this.drawnCards = []
|
||||
this.discard = []
|
||||
|
||||
// Créer toutes les cartes du jeu de tarot
|
||||
const items = game.items.filter(i => i.type === "tarot")
|
||||
@@ -63,6 +94,7 @@ export default class HamalronTarotDeckManager extends HandlebarsApplicationMixin
|
||||
|
||||
// Mélanger le deck
|
||||
this.shuffleDeck()
|
||||
this.saveDeckState()
|
||||
this.render()
|
||||
}
|
||||
|
||||
@@ -90,7 +122,6 @@ export default class HamalronTarotDeckManager extends HandlebarsApplicationMixin
|
||||
const drawn = []
|
||||
for (let i = 0; i < count && this.deck.length > 0; i++) {
|
||||
const card = this.deck.shift()
|
||||
this.drawnCards.push(card)
|
||||
drawn.push(card)
|
||||
}
|
||||
|
||||
@@ -130,10 +161,10 @@ export default class HamalronTarotDeckManager extends HandlebarsApplicationMixin
|
||||
const context = await super._prepareContext(options)
|
||||
|
||||
context.deckCount = this.deck.length
|
||||
context.drawnCount = this.drawnCards.length
|
||||
context.totalCards = this.deck.length + this.drawnCards.length
|
||||
context.discardCount = this.discard.length
|
||||
context.totalCards = this.deck.length + this.discard.length
|
||||
context.hasCards = this.deck.length > 0
|
||||
context.drawnCards = this.drawnCards.slice(-5).reverse() // Show last 5 drawn cards
|
||||
context.hasDiscard = this.discard.length > 0
|
||||
|
||||
return context
|
||||
}
|
||||
@@ -166,6 +197,9 @@ export default class HamalronTarotDeckManager extends HandlebarsApplicationMixin
|
||||
const cards = this.drawCards(1)
|
||||
if (cards.length > 0) {
|
||||
await this.sendCardsToChat(cards)
|
||||
// Ajouter les cartes directement à la défausse
|
||||
cards.forEach(card => this.addToDiscard(card))
|
||||
this.saveDeckState()
|
||||
this.render()
|
||||
}
|
||||
}
|
||||
@@ -180,10 +214,316 @@ export default class HamalronTarotDeckManager extends HandlebarsApplicationMixin
|
||||
const cards = this.drawCards(count)
|
||||
if (cards.length > 0) {
|
||||
await this.sendCardsToChat(cards)
|
||||
// Ajouter les cartes directement à la défausse
|
||||
cards.forEach(card => this.addToDiscard(card))
|
||||
this.saveDeckState()
|
||||
this.render()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a card to the discard pile
|
||||
* @param {Object} card - Card to add to discard
|
||||
*/
|
||||
addToDiscard(card) {
|
||||
// Vérifier que la carte n'est pas déjà dans la défausse
|
||||
const cardExists = this.discard.some(c => c.id === card.id)
|
||||
if (!cardExists) {
|
||||
this.discard.push(card)
|
||||
this.saveDeckState()
|
||||
this.render()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all tarot cards from player characters and return them to the deck
|
||||
* @param {Event} event
|
||||
* @param {HTMLElement} target
|
||||
*/
|
||||
static async #onCollectHands(event, target) {
|
||||
const confirmed = await foundry.applications.api.DialogV2.confirm({
|
||||
window: { title: game.i18n.localize("HAMALRON.TarotDeck.CollectHandsConfirm") },
|
||||
content: `<p>${game.i18n.localize("HAMALRON.TarotDeck.CollectHandsWarning")}</p>`,
|
||||
rejectClose: false,
|
||||
modal: true,
|
||||
})
|
||||
|
||||
if (!confirmed) return
|
||||
|
||||
let totalCollected = 0
|
||||
|
||||
// Récupérer tous les acteurs de type personnage (incluant tous, pas seulement ceux avec propriété joueur)
|
||||
const playerCharacters = game.actors.filter(a => a.type === "personnage")
|
||||
|
||||
console.log("Found characters:", playerCharacters.length)
|
||||
|
||||
// Collecter toutes les cartes de tarot de tous les personnages
|
||||
for (const actor of playerCharacters) {
|
||||
const tarotCards = actor.items.filter(i => i.type === "tarot")
|
||||
console.log(`Actor ${actor.name} has ${tarotCards.length} tarot cards`)
|
||||
|
||||
if (tarotCards.length > 0) {
|
||||
// Supprimer les cartes de l'acteur
|
||||
const cardIds = tarotCards.map(c => c.id)
|
||||
console.log(`Deleting ${cardIds.length} cards from ${actor.name}`)
|
||||
await actor.deleteEmbeddedDocuments("Item", cardIds)
|
||||
console.log(`Deleted cards from ${actor.name}`)
|
||||
totalCollected += tarotCards.length
|
||||
}
|
||||
}
|
||||
|
||||
// Réinitialiser le deck avec toutes les cartes de tarot disponibles
|
||||
this.deck = []
|
||||
this.discard = []
|
||||
|
||||
const items = game.items.filter(i => i.type === "tarot")
|
||||
|
||||
if (items.length > 0) {
|
||||
// Créer un Map pour éviter les doublons (utiliser l'ID unique)
|
||||
const uniqueCards = new Map()
|
||||
|
||||
items.forEach(item => {
|
||||
const key = `${item.system.symbole}_${item.system.valeur}_${item.system.type}`
|
||||
if (!uniqueCards.has(key)) {
|
||||
uniqueCards.set(key, {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
img: item.img,
|
||||
symbole: item.system.symbole,
|
||||
valeur: item.system.valeur,
|
||||
type: item.system.type,
|
||||
description: item.system.description,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
this.deck = Array.from(uniqueCards.values())
|
||||
}
|
||||
|
||||
// Mélanger le deck
|
||||
this.shuffleDeck()
|
||||
|
||||
this.saveDeckState()
|
||||
|
||||
ui.notifications.info(
|
||||
game.i18n.format("HAMALRON.TarotDeck.HandsCollected", {
|
||||
collected: totalCollected,
|
||||
deckSize: this.deck.length
|
||||
})
|
||||
)
|
||||
|
||||
this.render()
|
||||
}
|
||||
|
||||
/**
|
||||
* Distribute 8 cards to all connected players
|
||||
* @param {Event} event
|
||||
* @param {HTMLElement} target
|
||||
*/
|
||||
static async #onDistributeCards(event, target) {
|
||||
if (this.deck.length < 8) {
|
||||
ui.notifications.warn(game.i18n.localize("HAMALRON.TarotDeck.NotEnoughCards"))
|
||||
return
|
||||
}
|
||||
|
||||
const confirmed = await foundry.applications.api.DialogV2.confirm({
|
||||
window: { title: game.i18n.localize("HAMALRON.TarotDeck.DistributeConfirm") },
|
||||
content: `<p>${game.i18n.localize("HAMALRON.TarotDeck.DistributeWarning")}</p>`,
|
||||
rejectClose: false,
|
||||
modal: true,
|
||||
})
|
||||
|
||||
if (!confirmed) return
|
||||
|
||||
let distributedCount = 0
|
||||
let playersCount = 0
|
||||
|
||||
// Récupérer tous les utilisateurs connectés (sauf le GM)
|
||||
const connectedPlayers = game.users.filter(u => u.active && !u.isGM)
|
||||
|
||||
for (const user of connectedPlayers) {
|
||||
// Trouver le personnage du joueur (premier actor de type personnage dont il est propriétaire)
|
||||
const playerCharacter = game.actors.find(a =>
|
||||
a.type === "personnage" &&
|
||||
a.testUserPermission(user, "OWNER")
|
||||
)
|
||||
|
||||
if (playerCharacter) {
|
||||
// Tirer 8 cartes pour ce personnage
|
||||
const cardsToDistribute = []
|
||||
for (let i = 0; i < 8 && this.deck.length > 0; i++) {
|
||||
const card = this.deck.shift()
|
||||
cardsToDistribute.push(card)
|
||||
}
|
||||
|
||||
// Créer les items sur l'acteur
|
||||
for (const card of cardsToDistribute) {
|
||||
const originalItem = game.items.get(card.id)
|
||||
if (originalItem) {
|
||||
await playerCharacter.createEmbeddedDocuments("Item", [originalItem.toObject()])
|
||||
distributedCount++
|
||||
|
||||
// Cas spécial : Le Mat
|
||||
if (card.valeur === "lemat") {
|
||||
console.log(`Le Mat distributed to ${playerCharacter.name}! Executing special rules...`)
|
||||
// Import de TirageTarot pour accéder à la fonction helper
|
||||
const { default: HamalronTirageTarot } = await import("../documents/tirage-tarot.mjs")
|
||||
await HamalronTirageTarot.handleLeMatEffect(playerCharacter, this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
playersCount++
|
||||
}
|
||||
}
|
||||
|
||||
this.saveDeckState()
|
||||
|
||||
ui.notifications.info(
|
||||
game.i18n.format("HAMALRON.TarotDeck.CardsDistributed", {
|
||||
cards: distributedCount,
|
||||
players: playersCount
|
||||
})
|
||||
)
|
||||
|
||||
this.render()
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuffle discard pile back into deck
|
||||
* @param {Event} event
|
||||
* @param {HTMLElement} target
|
||||
*/
|
||||
static async #onShuffleDiscard(event, target) {
|
||||
if (this.discard.length === 0) {
|
||||
ui.notifications.warn(game.i18n.localize("HAMALRON.TarotDeck.NoDiscard"))
|
||||
return
|
||||
}
|
||||
|
||||
// Ajouter toutes les cartes de la défausse au deck
|
||||
this.deck.push(...this.discard)
|
||||
this.discard = []
|
||||
|
||||
// Mélanger le deck
|
||||
this.shuffleDeck()
|
||||
|
||||
this.saveDeckState()
|
||||
|
||||
ui.notifications.info(game.i18n.localize("HAMALRON.TarotDeck.DiscardShuffled"))
|
||||
this.render()
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle draw tension action
|
||||
* @param {Event} event
|
||||
* @param {HTMLElement} target
|
||||
*/
|
||||
static async #onDrawTension(event, target) {
|
||||
// Import system config
|
||||
const { DIFFICULTY_CHOICES } = await import('../config/system.mjs')
|
||||
|
||||
// Créer les options de difficulté
|
||||
const difficulties = Object.entries(DIFFICULTY_CHOICES)
|
||||
.filter(([key]) => key !== 'soustension_oppose')
|
||||
.map(([key, value]) => ({
|
||||
value: key,
|
||||
label: value.label
|
||||
}))
|
||||
|
||||
// Créer le contenu HTML du dialogue
|
||||
const selectOptions = difficulties.map(d =>
|
||||
`<option value="${d.value}">${d.label}</option>`
|
||||
).join('')
|
||||
|
||||
const content = `
|
||||
<div class="form-group">
|
||||
<label>{{localize "HAMALRON.Label.difficulty"}}</label>
|
||||
<select id="tension-difficulty" style="width: 100%;">
|
||||
${selectOptions}
|
||||
</select>
|
||||
</div>
|
||||
`
|
||||
|
||||
// Afficher le dialogue
|
||||
const result = await foundry.applications.api.DialogV2.wait({
|
||||
window: { title: game.i18n.localize("HAMALRON.TarotDeck.SelectDifficulty") },
|
||||
content: content,
|
||||
buttons: [
|
||||
{
|
||||
action: "draw",
|
||||
label: game.i18n.localize("HAMALRON.TarotDeck.DrawCard"),
|
||||
default: true,
|
||||
callback: (event, button, dialog) => {
|
||||
const select = button.form.elements["tension-difficulty"]
|
||||
return select.value
|
||||
}
|
||||
},
|
||||
{
|
||||
action: "cancel",
|
||||
label: game.i18n.localize("Cancel")
|
||||
}
|
||||
],
|
||||
rejectClose: false,
|
||||
modal: true
|
||||
})
|
||||
|
||||
if (!result) return
|
||||
|
||||
// Tirer une carte
|
||||
const cards = this.drawCards(1)
|
||||
if (cards.length === 0) return
|
||||
|
||||
const card = cards[0]
|
||||
const difficultyKey = result
|
||||
const difficulty = DIFFICULTY_CHOICES[difficultyKey]
|
||||
|
||||
// Récupérer la valeur numérique de la carte
|
||||
const { TAROT_VALEURS } = await import('../config/system.mjs')
|
||||
const cardValue = TAROT_VALEURS[card.valeur]?.value || 0
|
||||
const tensionBonus = difficulty.soustension || 0
|
||||
const totalScore = cardValue + tensionBonus
|
||||
|
||||
// Afficher dans le chat
|
||||
await this.sendTensionDrawToChat(card, difficulty, cardValue, tensionBonus, totalScore)
|
||||
|
||||
// Ajouter à la défausse
|
||||
this.addToDiscard(card)
|
||||
this.saveDeckState()
|
||||
this.render()
|
||||
}
|
||||
|
||||
/**
|
||||
* Send tension draw to chat
|
||||
* @param {Object} card - The drawn card
|
||||
* @param {Object} difficulty - The selected difficulty
|
||||
* @param {number} cardValue - Card numeric value
|
||||
* @param {number} tensionBonus - Tension modifier
|
||||
* @param {number} totalScore - Total score
|
||||
*/
|
||||
async sendTensionDrawToChat(card, difficulty, cardValue, tensionBonus, totalScore) {
|
||||
const speaker = ChatMessage.getSpeaker({ alias: "Maître du Jeu" })
|
||||
|
||||
const content = await foundry.applications.handlebars.renderTemplate(
|
||||
"systems/fvtt-hamalron/templates/chat-tarot-tension.hbs",
|
||||
{
|
||||
card: card,
|
||||
symboleLabel: game.i18n.localize(`HAMALRON.Tarot.FIELDS.symbole.${card.symbole}`),
|
||||
valeurLabel: game.i18n.localize(`HAMALRON.Tarot.FIELDS.valeur.${card.valeur}`),
|
||||
typeLabel: game.i18n.localize(`HAMALRON.Tarot.FIELDS.type.${card.type}`),
|
||||
difficulty: difficulty,
|
||||
cardValue: cardValue,
|
||||
tensionBonus: tensionBonus,
|
||||
totalScore: totalScore
|
||||
}
|
||||
)
|
||||
|
||||
await ChatMessage.create({
|
||||
speaker: speaker,
|
||||
content: content,
|
||||
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the Tarot Deck Manager application
|
||||
* @returns {HamalronTarotDeckManager}
|
||||
|
||||
Reference in New Issue
Block a user