about summary refs log tree commit diff stats
path: root/html/cards/script.js
blob: 38896782d7aad4c083cf69deae61da30ec873fac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
 * @typedef {Object} Card
 * @property {number} x
 * @property {number} y
 * @property {CardData} card
 * @property {boolean} isFaceUp
 */

/**
 * @typedef {Object} CardData
 * @property {string} suit
 * @property {string} value
 */

/**
 * @typedef {Object} GameState
 * @property {Card[]} cards
 * @property {Card|null} draggingCard
 * @property {CardData[]} deck
 * @property {{x: number, y: number}} stackPosition
 */

// Constants
const CARD_WIDTH = 100;
const CARD_HEIGHT = 150;
const PADDING = 10;
const SUITS = ['❤️', '♦️', '♣️', '♠️'];
const VALUES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
const PATTERN_SIZE = 10;
const INITIAL_CARD_X = 20;
const INITIAL_CARD_Y = 20;
const FONT_SIZE = '34px "pokemon-font", monospace';
const CARD_BORDER_COLOR = '#000000';
const CARD_FACE_COLOR = '#FFFFFF';
const DECK_COUNT = 4; // Can be changed to any number
const BASE_COLORS = [
    { primary: '#FF9900', secondary: '#FFCC00' }, // Original orange deck
    { primary: '#6B8E23', secondary: '#9ACD32' }, // Olive green deck
    { primary: '#4169E1', secondary: '#87CEEB' }, // Royal blue deck
    { primary: '#8B008B', secondary: '#DA70D6' }, // Purple deck
    { primary: '#CD853F', secondary: '#DEB887' }  // Brown deck
];

// Add new constants for pile layout
const PILE_SPACING = CARD_WIDTH + PADDING * 4; // Space between piles
const PILE_OFFSET = 5; // Vertical offset for stacked cards

// Canvas setup
const canvas = document.getElementById('cards');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// Pure functions
const shuffle = array => {
    const result = [...array];
    for (let i = result.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [result[i], result[j]] = [result[j], result[i]];
    }
    return result;
};

const createDeck = (deckIndex) => SUITS.flatMap(suit => 
    VALUES.map(value => ({ 
        suit, 
        value,
        deckId: deckIndex // Add deckId to track which deck a card belongs to
    }))
);

// Create multiple decks
const createDecks = (count) => {
    if (count > BASE_COLORS.length) {
        console.warn(`Only ${BASE_COLORS.length} unique deck colors are defined. Some decks will repeat colors.`);
    }
    return Array.from({ length: count }, (_, i) => createDeck(i)).flat();
};

// Create a more functional card factory
const createCard = (x, y, cardData) => Object.freeze({
    x: x + PADDING,
    y: y + PADDING,
    card: Object.freeze({ ...cardData }),
    isFaceUp: false
});

// Function to check if a point is within a card
const isPointInCard = (x, y, card) =>
    x >= card.x && x <= card.x + CARD_WIDTH && y >= card.y && y <= card.y + CARD_HEIGHT;

// Rendering functions
const clearCanvas = () => {
    ctx.fillStyle = 'beige';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
};

const drawCardBack = card => {
    ctx.fillRect(card.x, card.y, CARD_WIDTH, CARD_HEIGHT);
    drawRetroPattern(card);
    ctx.strokeStyle = CARD_BORDER_COLOR;
    ctx.strokeRect(card.x, card.y, CARD_WIDTH, CARD_HEIGHT);
};

const drawRetroPattern = card => {
    const checkeredSize = 10;
    const deckColors = BASE_COLORS[card.card.deckId % BASE_COLORS.length];
    
    for (let i = 0; i < CARD_WIDTH; i += checkeredSize) {
        for (let j = 0; j < CARD_HEIGHT; j += checkeredSize) {
            ctx.fillStyle = (Math.floor(i / checkeredSize) + Math.floor(j / checkeredSize)) % 2 === 0 
                ? deckColors.primary 
                : deckColors.secondary;
            ctx.fillRect(card.x + i, card.y + j, checkeredSize, checkeredSize);
        }
    }
};

const drawCardFront = card => {
    ctx.fillStyle = CARD_FACE_COLOR;
    ctx.fillRect(card.x, card.y, CARD_WIDTH, CARD_HEIGHT);
    ctx.fillStyle = CARD_BORDER_COLOR;
    ctx.font = FONT_SIZE;
    ctx.strokeRect(card.x, card.y, CARD_WIDTH, CARD_HEIGHT);
    
    // Draw value and suit with a retro font style
    drawCardValue(card.card.value, card.x + 12, card.y + 42, 'left');
    drawCardSuit(card.card.suit, card.x + CARD_WIDTH / 2, card.y + CARD_HEIGHT / 2 + 20);
};

const drawCardValue = (value, x, y, alignment) => {
    ctx.textAlign = alignment;
    ctx.fillStyle = CARD_BORDER_COLOR;
    ctx.fillText(value, x, y);
};

const drawCardSuit = (suit, x, y) => {
    ctx.textAlign = 'center';
    ctx.fillStyle = CARD_BORDER_COLOR;
    ctx.fillText(suit, x, y);
};

const renderCard = card => {
    card.isFaceUp ? drawCardFront(card) : drawCardBack(card);
};

const renderAllCards = cards => {
    clearCanvas();
    cards.forEach(renderCard);
    // renderDeckStats(); // Add this line to show deck statistics
};

// State management
let gameState;

const initializeGameState = () => ({
    cards: [],
    draggingCard: null,
    deck: shuffle(createDecks(DECK_COUNT)),
    stackPosition: { x: 0, y: 0 }
});

const initializeGame = () => {
    try {
        gameState = initializeGameState();
        
        // Group cards by deck
        const cardsByDeck = gameState.deck.reduce((acc, cardData) => {
            const deckId = cardData.deckId;
            if (!acc[deckId]) acc[deckId] = [];
            acc[deckId].push(cardData);
            return acc;
        }, {});

        // Calculate starting X position to center all piles
        const totalWidth = PILE_SPACING * DECK_COUNT;
        const startX = (canvas.width - totalWidth) / 2;

        // Create cards for each deck in its own pile
        gameState.cards = Object.entries(cardsByDeck).flatMap(([deckId, deckCards]) => {
            const pileX = startX + (parseInt(deckId) * PILE_SPACING);
            
            return deckCards.map((cardData, indexInDeck) => 
                createCard(
                    pileX,
                    INITIAL_CARD_Y + (indexInDeck * PILE_OFFSET),
                    cardData
                )
            );
        });

        clearCanvas();
        renderAllCards(gameState.cards);
        
        setupEventListeners();
    } catch (error) {
        console.error('Failed to initialize game:', error);
        alert('Failed to initialize game. Please refresh the page.');
    }
};

const setupEventListeners = () => {
    canvas.addEventListener('mousedown', handleMouseDown);
    canvas.addEventListener('contextmenu', e => e.preventDefault());
    document.addEventListener('keydown', e => {
        if (e.key === 'q') handleResetGame();
    });
};

// Event handlers
const handleMouseMove = e => {
    if (!gameState.draggingCard) return;

    const rect = canvas.getBoundingClientRect();
    const newX = e.clientX - rect.left - dragOffset.x;
    const newY = e.clientY - rect.top - dragOffset.y;

    // Update the card's position immutably
    const updatedCard = moveCard(gameState.draggingCard, newX, newY);
    gameState.cards = gameState.cards.map(card => 
        card === gameState.draggingCard ? updatedCard : card
    );
    gameState.draggingCard = updatedCard;

    renderAllCards(gameState.cards);
};

const handleMouseUp = e => {
    if (!gameState.draggingCard) {
        const rect = canvas.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;

        // Check if a card was clicked
        const clickedCard = gameState.cards.slice().reverse().find(card => isPointInCard(x, y, card));
        if (clickedCard) {
            // Move the clicked card to the top of the stack
            gameState.cards = gameState.cards.filter(card => card !== clickedCard);
            gameState.cards.push(clickedCard);
            renderAllCards(gameState.cards); // Re-render all cards
        }
    }

    gameState.draggingCard = null;
    document.removeEventListener('mousemove', handleMouseMove);
    document.removeEventListener('mouseup', handleMouseUp);
};

let dragOffset = { x: 0, y: 0 }; // To store the offset of the click position

const findClickedCard = (x, y, cards) => 
    cards.slice().reverse().find(card => isPointInCard(x, y, card));

const moveCardToTop = (targetCard, cards) => [
    ...cards.filter(card => card !== targetCard),
    targetCard
];

const handleMouseDown = e => {
    const rect = canvas.getBoundingClientRect();
    const x = e.clientX - rect.left;
    const y = e.clientY - rect.top;

    if (e.button === 2) {
        e.preventDefault();
        const clickedCard = findClickedCard(x, y, gameState.cards);
        if (clickedCard) {
            const updatedCard = toggleCardFace(clickedCard);
            gameState.cards = gameState.cards.map(card => 
                card === clickedCard ? updatedCard : card
            );
            renderAllCards(gameState.cards);
        }
        return;
    }

    const clickedCard = findClickedCard(x, y, gameState.cards);
    if (clickedCard) {
        gameState.draggingCard = clickedCard;
        dragOffset = {
            x: x - clickedCard.x,
            y: y - clickedCard.y
        };
        gameState.cards = moveCardToTop(clickedCard, gameState.cards);
        
        document.addEventListener('mousemove', handleMouseMove);
        document.addEventListener('mouseup', handleMouseUp);
    }
};

// Add this function to handle the reset confirmation
const handleResetGame = () => {
    if (confirm("Would you like to reset the cards?")) {
        resetCardsToOriginalPiles();
    }
};

const moveCard = (card, newX, newY) => ({
    ...card,
    x: newX,
    y: newY
});

const toggleCardFace = card => ({
    ...card,
    isFaceUp: !card.isFaceUp
});

// Add a function to get deck statistics
const getDeckStats = () => {
    const stats = new Map();
    gameState.cards.forEach(card => {
        const deckId = card.card.deckId;
        const current = stats.get(deckId) || { total: 0, faceUp: 0 };
        stats.set(deckId, {
            total: current.total + 1,
            faceUp: current.faceUp + (card.isFaceUp ? 1 : 0)
        });
    });
    return stats;
};

// Optional: Add a display for deck statistics
const renderDeckStats = () => {
    const stats = getDeckStats();
    ctx.font = '16px "pokemon-font", monospace';
    
    // Calculate the same starting X position as the piles
    const totalWidth = PILE_SPACING * DECK_COUNT;
    const startX = (canvas.width - totalWidth) / 2;
    
    stats.forEach((stat, deckId) => {
        const colors = BASE_COLORS[deckId % BASE_COLORS.length];
        const pileX = startX + (deckId * PILE_SPACING);
        
        ctx.fillStyle = colors.primary;
        ctx.textAlign = 'center';
        ctx.fillText(
            `Deck ${deckId + 1}: ${stat.faceUp}/${stat.total}`, 
            pileX + CARD_WIDTH / 2,
            INITIAL_CARD_Y - 10
        );
    });
};

// Optional: Add a function to reset cards to their original piles
const resetCardsToOriginalPiles = () => {
    const totalWidth = PILE_SPACING * DECK_COUNT;
    const startX = (canvas.width - totalWidth) / 2;

    // Group cards by deck
    const cardsByDeck = gameState.cards.reduce((acc, card) => {
        const deckId = card.card.deckId;
        if (!acc[deckId]) acc[deckId] = [];
        acc[deckId].push(card);
        return acc;
    }, {});

    // Reset position for each deck
    Object.entries(cardsByDeck).forEach(([deckId, deckCards]) => {
        const pileX = startX + (parseInt(deckId) * PILE_SPACING);
        
        deckCards.forEach((card, index) => {
            card.x = pileX;
            card.y = INITIAL_CARD_Y + (index * PILE_OFFSET);
            card.isFaceUp = false;
        });
    });

    renderAllCards(gameState.cards);
};

// Start the game
initializeGame();

// Clean up on window unload
window.addEventListener('unload', () => {
    canvas.removeEventListener('mousedown', handleMouseDown);
    canvas.removeEventListener('contextmenu', e => e.preventDefault());
});