about summary refs log tree commit diff stats
path: root/html
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-01-11 15:55:36 -0500
committerelioat <elioat@tilde.institute>2025-01-11 15:55:36 -0500
commitbf9458eaed789d5f8c4487a16edd2295dbcbbe61 (patch)
tree032d74eb1f23b355e8e80c580b24ed98c53ee494 /html
parent0f764dbcc4873755b0d4355dbf3f03a9a39d27d4 (diff)
downloadtour-bf9458eaed789d5f8c4487a16edd2295dbcbbe61.tar.gz
*
Diffstat (limited to 'html')
-rw-r--r--html/cards/script.js70
1 files changed, 57 insertions, 13 deletions
diff --git a/html/cards/script.js b/html/cards/script.js
index 36897c2..34f9296 100644
--- a/html/cards/script.js
+++ b/html/cards/script.js
@@ -51,7 +51,12 @@ const ctx = canvas.getContext('2d');
 canvas.width = window.innerWidth;
 canvas.height = window.innerHeight;
 
-// Pure functions
+
+/**
+ * Shuffles an array in place and returns a new shuffled array.
+ * @param {Array} array - The array to shuffle.
+ * @returns {Array} A new array containing the shuffled elements.
+ */
 const shuffle = array => {
     const result = [...array];
     for (let i = result.length - 1; i > 0; i--) {
@@ -61,6 +66,12 @@ const shuffle = array => {
     return result;
 };
 
+
+/**
+ * Creates a deck of cards for a given deck index.
+ * @param {number} deckIndex - The index of the deck being created.
+ * @returns {Array} An array of card objects, each containing suit, value, and deckId.
+ */
 const createDeck = (deckIndex) => SUITS.flatMap(suit => 
     VALUES.map(value => ({ 
         suit, 
@@ -69,7 +80,14 @@ const createDeck = (deckIndex) => SUITS.flatMap(suit =>
     }))
 );
 
-// Create multiple decks
+/**
+ * Creates multiple decks of cards based on the specified count.
+ * If the count exceeds the number of unique deck colors defined, 
+ * some decks will repeat colors.
+ * 
+ * @param {number} count - The number of decks to create.
+ * @returns {Array} An array of card objects, each containing suit, value, and deckId.
+ */
 const createDecks = (count) => {
     if (count > BASE_COLORS.length) {
         console.warn(`Only ${BASE_COLORS.length} unique deck colors are defined. Some decks will repeat colors.`);
@@ -77,7 +95,6 @@ const createDecks = (count) => {
     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,
@@ -85,11 +102,17 @@ const createCard = (x, y, cardData) => Object.freeze({
     isFaceUp: false
 });
 
-// Function to check if a point is within a card
+/**
+ * Determines if a point is within a card.
+ * Used to determine where to hold a card when dragging.
+ * @param {any} x
+ * @param {any} y
+ * @param {any} card
+ * @returns {any}
+ */
 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);
@@ -140,6 +163,10 @@ const drawCardSuit = (suit, x, y) => {
     ctx.fillText(suit, x, y);
 };
 
+/**
+ * Renders a card, determining which side to draw based on its face-up state.
+ * @param {Card} card - The card to render.
+ */
 const renderCard = card => {
     card.isFaceUp ? drawCardFront(card) : drawCardBack(card);
 };
@@ -149,7 +176,6 @@ const renderAllCards = cards => {
     cards.forEach(renderCard);
 };
 
-// State management
 let gameState;
 
 const initializeGameState = () => ({
@@ -206,7 +232,6 @@ const setupEventListeners = () => {
     });
 };
 
-// Event handlers
 const handleMouseMove = e => {
     if (!gameState.draggingCard) return;
 
@@ -247,9 +272,22 @@ const handleMouseUp = e => {
 
 let dragOffset = { x: 0, y: 0 }; // To store the offset of the click position
 
+/**
+ * Finds the card that was clicked.
+ * @param {number} x - The x-coordinate of the click.
+ * @param {number} y - The y-coordinate of the click.
+ * @param {Card[]} cards - The list of cards to search through.
+ * @returns {Card|null} The card that was clicked, or null if no card was clicked.
+ */
 const findClickedCard = (x, y, cards) => 
     cards.slice().reverse().find(card => isPointInCard(x, y, card));
 
+/**
+ * Moves a card to the top of the stack.
+ * @param {Card} targetCard - The card to move to the top.
+ * @param {Card[]} cards - The list of cards to search through.
+ * @returns {Card[]} A new array with the target card moved to the top.
+ */
 const moveCardToTop = (targetCard, cards) => [
     ...cards.filter(card => card !== targetCard),
     targetCard
@@ -287,13 +325,19 @@ const handleMouseDown = e => {
     }
 };
 
-// Add this function to handle the reset confirmation
 const handleResetGame = () => {
     if (confirm("Would you like to reset the cards?")) {
         resetCardsToOriginalPiles();
     }
 };
 
+/**
+ * Moves a card to a new position.
+ * @param {Card} card - The card to move.
+ * @param {number} newX - The new x-coordinate for the card.
+ * @param {number} newY - The new y-coordinate for the card.
+ * @returns {Card} A new card object with updated position.
+ */
 const moveCard = (card, newX, newY) => ({
     ...card,
     x: newX,
@@ -305,7 +349,10 @@ const toggleCardFace = card => ({
     isFaceUp: !card.isFaceUp
 });
 
-// Add a function to get deck statistics
+/**
+ * Calculates some stats for each deck, including total and face-up counts.
+ * @returns {Map} A map containing the total and face-up counts for each deck.
+ */
 const getDeckStats = () => {
     const stats = new Map();
     gameState.cards.forEach(card => {
@@ -319,7 +366,6 @@ const getDeckStats = () => {
     return stats;
 };
 
-// Optional: Add a display for deck statistics
 const renderDeckStats = () => {
     const stats = getDeckStats();
     ctx.font = '16px "pokemon-font", monospace';
@@ -342,7 +388,7 @@ const renderDeckStats = () => {
     });
 };
 
-// Optional: Add a function to reset cards to their original piles
+// FIXME: this is too complicated, and would probably work better if I had a better way of handling state. 
 const resetCardsToOriginalPiles = () => {
     const totalWidth = PILE_SPACING * DECK_COUNT;
     const startX = (canvas.width - totalWidth) / 2;
@@ -369,10 +415,8 @@ const resetCardsToOriginalPiles = () => {
     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());
href='#n413'>413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914