|
636 | 636 |
|
637 | 637 | // Function to check competition status from the database |
638 | 638 | async function checkCompetitionStatus() { |
639 | | - if (!browser || isHost() || isCompetitionActive) return; |
| 639 | + if (!browser) return; |
640 | 640 |
|
641 | 641 | // Don't check too frequently (max once per second) |
642 | 642 | const now = Date.now(); |
|
648 | 648 | if (!response.ok) return; |
649 | 649 |
|
650 | 650 | const data = await response.json(); |
651 | | - console.log(data); |
| 651 | + console.log('[COMPETITION] Status check:', data); |
652 | 652 |
|
653 | 653 | // If competition is active in the database but not locally, start it locally |
654 | 654 | if (data.isActive && !isCompetitionActive) { |
|
663 | 663 | startVideoStream(); |
664 | 664 |
|
665 | 665 | 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(); |
666 | 670 | } |
667 | 671 | } catch (err) { |
668 | 672 | console.error('Error checking competition status:', err); |
|
693 | 697 | if (!browser) return; |
694 | 698 |
|
695 | 699 | 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', { |
702 | 704 | method: 'POST', |
703 | 705 | headers: { |
704 | 706 | 'Content-Type': 'application/json' |
|
710 | 712 | duration: 30 // 30 seconds competition |
711 | 713 | }) |
712 | 714 | }); |
| 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 |
713 | 725 |
|
714 | 726 | // Start all timers |
715 | 727 | startCompetitionTimers(); |
|
719 | 731 |
|
720 | 732 | toast.success('Competition started!'); |
721 | 733 | } catch (err) { |
722 | | - console.error('[AGORA] Error starting competition:', err); |
| 734 | + console.error('[COMPETITION] Error starting competition:', err); |
723 | 735 | if (err instanceof Error) |
724 | 736 | 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; |
725 | 740 | } |
726 | 741 | } |
727 | 742 |
|
|
790 | 805 | .then(() => getCameras()) |
791 | 806 | .catch((err) => console.error('[CAMERA] Permission error:', err)); |
792 | 807 |
|
793 | | - // Start polling for competition status |
| 808 | + // Do an initial check immediately |
| 809 | + checkCompetitionStatus(); |
| 810 | + |
| 811 | + // Then start polling for competition status |
794 | 812 | competitionStatusInterval = setInterval(checkCompetitionStatus, 1000); |
795 | 813 | } |
796 | 814 |
|
|
0 commit comments