103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import type { Track } from '~/../types/types'
|
|
|
|
interface CardPosition {
|
|
x: number
|
|
y: number
|
|
}
|
|
|
|
type CardPositions = Record<string, Record<number, CardPosition>>
|
|
|
|
export const useCardStore = defineStore('card', {
|
|
state: () => ({
|
|
// Stocke les IDs des cartes déjà révélées
|
|
revealedCards: new Set<number>(),
|
|
// Stocke les positions personnalisées des cartes par box
|
|
// Format: { [boxId]: { [trackId]: { x: number, y: number } } }
|
|
cardPositions: {} as CardPositions
|
|
}),
|
|
|
|
actions: {
|
|
// Marquer une carte comme révélée
|
|
revealCard(trackId: number) {
|
|
this.revealedCards.add(trackId)
|
|
this.saveToLocalStorage()
|
|
},
|
|
|
|
// Vérifier si une carte est révélée
|
|
isCardRevealed(trackId: number): boolean {
|
|
return this.revealedCards.has(trackId)
|
|
},
|
|
|
|
// Définir la position d'une carte dans une box
|
|
setCardPosition(boxId: string, trackId: number, position: { x: number; y: number }) {
|
|
if (!this.cardPositions[boxId]) {
|
|
this.cardPositions[boxId] = {}
|
|
}
|
|
this.cardPositions[boxId][trackId] = position
|
|
this.saveToLocalStorage()
|
|
},
|
|
|
|
// Obtenir la position d'une carte dans une box
|
|
getCardPosition(boxId: string, trackId: number): { x: number; y: number } | undefined {
|
|
return this.cardPositions[boxId]?.[trackId]
|
|
},
|
|
|
|
// Sauvegarder l'état dans le localStorage
|
|
saveToLocalStorage() {
|
|
if (typeof window !== 'undefined') {
|
|
try {
|
|
localStorage.setItem(
|
|
'cardStore',
|
|
JSON.stringify({
|
|
revealedCards: Array.from(this.revealedCards),
|
|
cardPositions: this.cardPositions
|
|
})
|
|
)
|
|
} catch (e) {
|
|
console.error('Failed to save card store to localStorage', e)
|
|
}
|
|
}
|
|
},
|
|
|
|
// Charger l'état depuis le localStorage
|
|
loadFromLocalStorage() {
|
|
if (typeof window !== 'undefined') {
|
|
try {
|
|
const saved = localStorage.getItem('cardStore')
|
|
if (saved) {
|
|
const { revealedCards, cardPositions } = JSON.parse(saved)
|
|
if (Array.isArray(revealedCards)) {
|
|
this.revealedCards = new Set(
|
|
revealedCards.filter((id): id is number => typeof id === 'number')
|
|
)
|
|
}
|
|
if (cardPositions && typeof cardPositions === 'object') {
|
|
this.cardPositions = cardPositions
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error('Failed to load card store from localStorage', e)
|
|
}
|
|
}
|
|
},
|
|
|
|
// Initialiser le store
|
|
initialize() {
|
|
this.loadFromLocalStorage()
|
|
}
|
|
},
|
|
|
|
getters: {
|
|
// Getter pour la réactivité dans les templates
|
|
isRevealed: (state) => (trackId: number) => {
|
|
return state.revealedCards.has(trackId)
|
|
},
|
|
|
|
// Obtenir toutes les positions des cartes d'une box
|
|
getBoxCardPositions: (state) => (boxId: string) => {
|
|
return state.cardPositions[boxId] || {}
|
|
}
|
|
}
|
|
})
|