|
669 | 669 | // Socket event handlers |
670 | 670 | socket.on('pokemon-match-update', (data) => { |
671 | 671 | console.log('Pokemon match update received:', data); |
672 | | - if (data.player1) matchState.player1 = { ...matchState.player1, ...data.player1 }; |
673 | | - if (data.player2) matchState.player2 = { ...matchState.player2, ...data.player2 }; |
| 672 | + |
| 673 | + // Update player 1 |
| 674 | + if (data.player1) { |
| 675 | + // Merge basic data |
| 676 | + Object.keys(data.player1).forEach(key => { |
| 677 | + if (key !== 'prizes' && key !== 'prizesTaken') { |
| 678 | + matchState.player1[key] = data.player1[key]; |
| 679 | + } |
| 680 | + }); |
| 681 | + |
| 682 | + // Handle prizes specially |
| 683 | + if (data.player1.prizesTaken !== undefined) { |
| 684 | + matchState.player1.prizesTaken = data.player1.prizesTaken; |
| 685 | + matchState.player1.prizes = 6 - data.player1.prizesTaken.length; |
| 686 | + } else if (data.player1.prizes !== undefined) { |
| 687 | + if (typeof data.player1.prizes === 'number') { |
| 688 | + matchState.player1.prizes = data.player1.prizes; |
| 689 | + if (!data.player1.prizesTaken) { |
| 690 | + matchState.player1.prizesTaken = []; |
| 691 | + } |
| 692 | + } |
| 693 | + } |
| 694 | + } |
| 695 | + |
| 696 | + // Update player 2 |
| 697 | + if (data.player2) { |
| 698 | + // Merge basic data |
| 699 | + Object.keys(data.player2).forEach(key => { |
| 700 | + if (key !== 'prizes' && key !== 'prizesTaken') { |
| 701 | + matchState.player2[key] = data.player2[key]; |
| 702 | + } |
| 703 | + }); |
| 704 | + |
| 705 | + // Handle prizes specially |
| 706 | + if (data.player2.prizesTaken !== undefined) { |
| 707 | + matchState.player2.prizesTaken = data.player2.prizesTaken; |
| 708 | + matchState.player2.prizes = 6 - data.player2.prizesTaken.length; |
| 709 | + } else if (data.player2.prizes !== undefined) { |
| 710 | + if (typeof data.player2.prizes === 'number') { |
| 711 | + matchState.player2.prizes = data.player2.prizes; |
| 712 | + if (!data.player2.prizesTaken) { |
| 713 | + matchState.player2.prizesTaken = []; |
| 714 | + } |
| 715 | + } |
| 716 | + } |
| 717 | + } |
| 718 | + |
| 719 | + // Update other data |
674 | 720 | if (data.stadium !== undefined) { |
675 | | - console.log('Stadium data received:', data.stadium); |
676 | 721 | matchState.stadium = data.stadium; |
677 | 722 | } |
| 723 | + if (data.currentTurn !== undefined) { |
| 724 | + matchState.currentTurn = data.currentTurn; |
| 725 | + } |
| 726 | + if (data.gameNumber !== undefined) { |
| 727 | + matchState.gameNumber = data.gameNumber; |
| 728 | + } |
| 729 | + if (data.matchFormat !== undefined) { |
| 730 | + matchState.matchFormat = data.matchFormat; |
| 731 | + } |
| 732 | + |
| 733 | + // Always update display |
678 | 734 | updateDisplay(); |
679 | 735 | }); |
680 | 736 |
|
|
762 | 818 | }); |
763 | 819 |
|
764 | 820 | socket.on('timer-reset', () => { |
765 | | - pauseTimer(); |
| 821 | + console.log('Timer reset received'); |
| 822 | + |
| 823 | + // Clear any existing timer interval |
| 824 | + if (matchState.timerInterval) { |
| 825 | + clearInterval(matchState.timerInterval); |
| 826 | + matchState.timerInterval = null; |
| 827 | + } |
| 828 | + |
| 829 | + // Reset timer values |
766 | 830 | matchState.timer = { minutes: 50, seconds: 0 }; |
| 831 | + |
| 832 | + // Update display but DON'T start the timer |
767 | 833 | updateMatchInfo(); |
768 | 834 | }); |
769 | 835 |
|
770 | 836 | socket.on('match-reset', () => { |
| 837 | + console.log('Match reset received'); |
| 838 | + |
| 839 | + // Preserve visibility |
| 840 | + const wasVisible = matchState.visible; |
| 841 | + |
| 842 | + // Clear any running timer |
| 843 | + if (matchState.timerInterval) { |
| 844 | + clearInterval(matchState.timerInterval); |
| 845 | + matchState.timerInterval = null; |
| 846 | + } |
| 847 | + |
| 848 | + // Full reset |
771 | 849 | matchState = { |
772 | 850 | player1: { |
773 | 851 | name: 'Player 1', |
|
793 | 871 | }, |
794 | 872 | currentTurn: 1, |
795 | 873 | timer: { minutes: 50, seconds: 0 }, |
| 874 | + timerInterval: null, |
796 | 875 | gameNumber: 1, |
797 | 876 | matchFormat: 'Best of 3', |
798 | 877 | stadium: null, |
799 | | - visible: matchState.visible |
| 878 | + visible: wasVisible |
800 | 879 | }; |
801 | | - pauseTimer(); |
| 880 | + |
| 881 | + // Force complete update |
802 | 882 | updateDisplay(); |
803 | 883 | }); |
804 | 884 |
|
|
833 | 913 | } |
834 | 914 |
|
835 | 915 | function updateDisplay() { |
| 916 | + console.log('Updating full display with state:', matchState); |
| 917 | + |
| 918 | + // Update all player info |
836 | 919 | updatePlayerInfo(1); |
837 | 920 | updatePlayerInfo(2); |
838 | 921 | updatePlayerRecord(1); |
839 | 922 | updatePlayerRecord(2); |
840 | 923 | updateMatchScore(1); |
841 | 924 | updateMatchScore(2); |
| 925 | + |
| 926 | + // Update Pokemon displays |
842 | 927 | updateActiveDisplay(1); |
843 | 928 | updateActiveDisplay(2); |
844 | 929 | updateBenchDisplay(1); |
845 | 930 | updateBenchDisplay(2); |
| 931 | + |
| 932 | + // Update prize displays - these are critical |
846 | 933 | updatePrizeDisplay(1); |
847 | 934 | updatePrizeDisplay(2); |
| 935 | + |
| 936 | + // Update turn actions |
848 | 937 | updateTurnActions(1); |
849 | 938 | updateTurnActions(2); |
| 939 | + |
| 940 | + // Update match info |
850 | 941 | updateMatchInfo(); |
| 942 | + |
| 943 | + // Update stadium |
851 | 944 | updateStadiumDisplay(); |
852 | 945 | } |
853 | 946 |
|
|
882 | 975 |
|
883 | 976 | function updatePlayerInfo(playerNum) { |
884 | 977 | const player = matchState[`player${playerNum}`]; |
885 | | - document.getElementById(`player${playerNum}Name`).textContent = player.name; |
886 | | - document.getElementById(`player${playerNum}Prizes`).textContent = player.prizes; |
| 978 | + const nameEl = document.getElementById(`player${playerNum}Name`); |
| 979 | + const prizesEl = document.getElementById(`player${playerNum}Prizes`); |
| 980 | + |
| 981 | + if (nameEl) { |
| 982 | + nameEl.textContent = player.name || `Player ${playerNum}`; |
| 983 | + } |
| 984 | + |
| 985 | + if (prizesEl) { |
| 986 | + // Always display as a number |
| 987 | + let remainingPrizes = 6; |
| 988 | + |
| 989 | + if (typeof player.prizes === 'number') { |
| 990 | + remainingPrizes = player.prizes; |
| 991 | + } else if (Array.isArray(player.prizesTaken)) { |
| 992 | + remainingPrizes = 6 - player.prizesTaken.length; |
| 993 | + } else if (Array.isArray(player.prizes)) { |
| 994 | + |
| 995 | + remainingPrizes = player.prizes.filter(p => !p).length; |
| 996 | + } |
| 997 | + |
| 998 | + prizesEl.textContent = remainingPrizes; |
| 999 | + } |
887 | 1000 | } |
888 | 1001 |
|
889 | 1002 | function updatePlayerRecord(playerNum) { |
|
967 | 1080 |
|
968 | 1081 | function updatePrizeDisplay(playerNum) { |
969 | 1082 | const player = matchState[`player${playerNum}`]; |
970 | | - const prizeDiv = document.getElementById(`player${playerNum}PrizeCards`); |
| 1083 | + const prizeContainer = document.getElementById(`player${playerNum}PrizeCards`); // This is the correct ID |
971 | 1084 |
|
972 | | - let prizeHTML = ''; |
973 | | - for (let i = 0; i < 6; i++) { |
974 | | - const taken = player.prizesTaken.includes(i); |
975 | | - prizeHTML += `<div class="prize-card ${taken ? 'taken' : ''}"></div>`; |
| 1085 | + if (!prizeContainer) { |
| 1086 | + console.error(`Prize container not found for player ${playerNum}`); |
| 1087 | + return; |
| 1088 | + } |
| 1089 | + |
| 1090 | + const prizeCards = prizeContainer.querySelectorAll('.prize-card'); |
| 1091 | + |
| 1092 | + // Clear all taken states first |
| 1093 | + prizeCards.forEach(card => card.classList.remove('taken')); |
| 1094 | + |
| 1095 | + // Handle prizesTaken array (preferred format) |
| 1096 | + if (player.prizesTaken && Array.isArray(player.prizesTaken)) { |
| 1097 | + prizeCards.forEach((card, index) => { |
| 1098 | + if (player.prizesTaken.includes(index)) { |
| 1099 | + card.classList.add('taken'); |
| 1100 | + } |
| 1101 | + }); |
| 1102 | + } |
| 1103 | + // Handle old boolean array format |
| 1104 | + else if (Array.isArray(player.prizes) && typeof player.prizes[0] === 'boolean') { |
| 1105 | + prizeCards.forEach((card, index) => { |
| 1106 | + if (player.prizes[index] === true) { |
| 1107 | + card.classList.add('taken'); |
| 1108 | + } |
| 1109 | + }); |
| 1110 | + } |
| 1111 | + // Handle simple prize count |
| 1112 | + else if (typeof player.prizes === 'number') { |
| 1113 | + const takenCount = 6 - player.prizes; |
| 1114 | + prizeCards.forEach((card, index) => { |
| 1115 | + if (index < takenCount) { |
| 1116 | + card.classList.add('taken'); |
| 1117 | + } |
| 1118 | + }); |
976 | 1119 | } |
977 | 1120 |
|
978 | | - prizeDiv.innerHTML = prizeHTML; |
| 1121 | + // Always update the count display |
| 1122 | + updatePlayerInfo(playerNum); |
979 | 1123 | } |
980 | 1124 |
|
981 | 1125 | function updateMatchInfo() { |
|
0 commit comments