Skip to content

Commit 0eaf846

Browse files
committed
Entire Match Reset Fixes
Entire match reset now works AND updates in the overlay.
1 parent 9b9fc75 commit 0eaf846

3 files changed

Lines changed: 383 additions & 45 deletions

File tree

overlays/pokemon-match.html

Lines changed: 158 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,68 @@
669669
// Socket event handlers
670670
socket.on('pokemon-match-update', (data) => {
671671
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
674720
if (data.stadium !== undefined) {
675-
console.log('Stadium data received:', data.stadium);
676721
matchState.stadium = data.stadium;
677722
}
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
678734
updateDisplay();
679735
});
680736

@@ -762,12 +818,34 @@
762818
});
763819

764820
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
766830
matchState.timer = { minutes: 50, seconds: 0 };
831+
832+
// Update display but DON'T start the timer
767833
updateMatchInfo();
768834
});
769835

770836
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
771849
matchState = {
772850
player1: {
773851
name: 'Player 1',
@@ -793,12 +871,14 @@
793871
},
794872
currentTurn: 1,
795873
timer: { minutes: 50, seconds: 0 },
874+
timerInterval: null,
796875
gameNumber: 1,
797876
matchFormat: 'Best of 3',
798877
stadium: null,
799-
visible: matchState.visible
878+
visible: wasVisible
800879
};
801-
pauseTimer();
880+
881+
// Force complete update
802882
updateDisplay();
803883
});
804884

@@ -833,21 +913,34 @@
833913
}
834914

835915
function updateDisplay() {
916+
console.log('Updating full display with state:', matchState);
917+
918+
// Update all player info
836919
updatePlayerInfo(1);
837920
updatePlayerInfo(2);
838921
updatePlayerRecord(1);
839922
updatePlayerRecord(2);
840923
updateMatchScore(1);
841924
updateMatchScore(2);
925+
926+
// Update Pokemon displays
842927
updateActiveDisplay(1);
843928
updateActiveDisplay(2);
844929
updateBenchDisplay(1);
845930
updateBenchDisplay(2);
931+
932+
// Update prize displays - these are critical
846933
updatePrizeDisplay(1);
847934
updatePrizeDisplay(2);
935+
936+
// Update turn actions
848937
updateTurnActions(1);
849938
updateTurnActions(2);
939+
940+
// Update match info
850941
updateMatchInfo();
942+
943+
// Update stadium
851944
updateStadiumDisplay();
852945
}
853946

@@ -882,8 +975,28 @@
882975

883976
function updatePlayerInfo(playerNum) {
884977
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+
}
8871000
}
8881001

8891002
function updatePlayerRecord(playerNum) {
@@ -967,15 +1080,46 @@
9671080

9681081
function updatePrizeDisplay(playerNum) {
9691082
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
9711084

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+
});
9761119
}
9771120

978-
prizeDiv.innerHTML = prizeHTML;
1121+
// Always update the count display
1122+
updatePlayerInfo(playerNum);
9791123
}
9801124

9811125
function updateMatchInfo() {

0 commit comments

Comments
 (0)