Skip to content

Commit 4145fd4

Browse files
committed
feat: improve competition status handling with enhanced logging and error management
1 parent 3f5edd3 commit 4145fd4

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

src/routes/(app)/compete/[roomId]/+page.svelte

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@
636636
637637
// Function to check competition status from the database
638638
async function checkCompetitionStatus() {
639-
if (!browser || isHost() || isCompetitionActive) return;
639+
if (!browser) return;
640640
641641
// Don't check too frequently (max once per second)
642642
const now = Date.now();
@@ -648,7 +648,7 @@
648648
if (!response.ok) return;
649649
650650
const data = await response.json();
651-
console.log(data);
651+
console.log('[COMPETITION] Status check:', data);
652652
653653
// If competition is active in the database but not locally, start it locally
654654
if (data.isActive && !isCompetitionActive) {
@@ -663,6 +663,10 @@
663663
startVideoStream();
664664
665665
toast.success('Competition started by host!');
666+
} else if (!data.isActive && isCompetitionActive) {
667+
// Competition ended in the database but still active locally
668+
console.log('[COMPETITION] Competition ended in database');
669+
endCompetition();
666670
}
667671
} catch (err) {
668672
console.error('Error checking competition status:', err);
@@ -693,12 +697,10 @@
693697
if (!browser) return;
694698
695699
try {
696-
console.log('[AGORA] Starting competition');
697-
isCompetitionActive = true;
698-
competitionTimeInSeconds = 30; // Reset timer to 30 seconds
699-
700-
// Update competition status in database
701-
await fetch('/api/competition/status', {
700+
console.log('[COMPETITION] Starting competition as host');
701+
702+
// Update competition status in database FIRST
703+
const response = await fetch('/api/competition/status', {
702704
method: 'POST',
703705
headers: {
704706
'Content-Type': 'application/json'
@@ -710,6 +712,16 @@
710712
duration: 30 // 30 seconds competition
711713
})
712714
});
715+
716+
if (!response.ok) {
717+
throw new Error('Failed to update competition status in database');
718+
}
719+
720+
console.log('[COMPETITION] Database updated successfully');
721+
722+
// Then update local state
723+
isCompetitionActive = true;
724+
competitionTimeInSeconds = 30; // Reset timer to 30 seconds
713725
714726
// Start all timers
715727
startCompetitionTimers();
@@ -719,9 +731,12 @@
719731
720732
toast.success('Competition started!');
721733
} catch (err) {
722-
console.error('[AGORA] Error starting competition:', err);
734+
console.error('[COMPETITION] Error starting competition:', err);
723735
if (err instanceof Error)
724736
error = err.message || 'An error occurred while starting the competition';
737+
738+
// If we failed to update database, don't proceed with competition
739+
isCompetitionActive = false;
725740
}
726741
}
727742
@@ -790,7 +805,10 @@
790805
.then(() => getCameras())
791806
.catch((err) => console.error('[CAMERA] Permission error:', err));
792807
793-
// Start polling for competition status
808+
// Do an initial check immediately
809+
checkCompetitionStatus();
810+
811+
// Then start polling for competition status
794812
competitionStatusInterval = setInterval(checkCompetitionStatus, 1000);
795813
}
796814

0 commit comments

Comments
 (0)