Skip to content

Commit ab69a88

Browse files
Merge pull request #97 from Treevyy/fallbacksecurity
Fallbacksecurity
2 parents 7ccb215 + 5772b22 commit ab69a88

File tree

14 files changed

+190
-185
lines changed

14 files changed

+190
-185
lines changed

client/src/App.tsx

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,23 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React from 'react';
22
import {
33
BrowserRouter as Router,
44
Routes,
55
Route,
66
useNavigate,
77
useLocation,
88
} from 'react-router-dom';
9+
910
import IntroPage from './components/screens/IntroPage';
1011
import { Login } from './components/screens/Login';
1112
import GameMap from './components/screens/GameMap';
1213
import GameOver from './components/screens/GameOver';
1314
import Victory from './components/screens/Victory';
1415
import Signup from './components/screens/Signup';
1516
import Questions from './components/screens/Questions';
16-
import './styles/codezilla.css';
17-
18-
// 🔊 Persistent background music that plays on interaction
19-
const PersistentBackgroundMusic: React.FC = () => {
20-
const audioRef = useRef<HTMLAudioElement | null>(null);
21-
22-
useEffect(() => {
23-
if (!audioRef.current) {
24-
const audio = new Audio('/black.sabbath.mp3');
25-
audio.loop = true;
26-
audio.volume = 0.03;
27-
audioRef.current = audio;
28-
29-
const play = () => {
30-
audio.play().catch((err) =>
31-
console.warn('Autoplay blocked or error playing:', err)
32-
);
33-
};
34-
35-
document.addEventListener('click', play, { once: true });
36-
}
17+
import LeaderBoard from './components/LeaderBoard';
3718

38-
return () => {
39-
audioRef.current?.pause();
40-
};
41-
}, []);
42-
43-
return null;
44-
};
19+
import './styles/codezilla.css';
20+
import BackgroundMusicProvider from './components/BackgroundMusicProvider';
4521

4622
const LogoutButton: React.FC = () => {
4723
const navigate = useNavigate();
@@ -60,14 +36,17 @@ const LogoutButton: React.FC = () => {
6036

6137
const AppContent: React.FC = () => {
6238
const location = useLocation();
39+
6340
const isGameActive =
6441
location.pathname.startsWith('/map') ||
6542
location.pathname.startsWith('/question');
6643

6744
return (
6845
<div className="app-wrapper">
6946
{/* Only load persistent music during gameplay */}
70-
{isGameActive && <PersistentBackgroundMusic />}
47+
{isGameActive && (
48+
<BackgroundMusicProvider src="/black.sabbath.mp3" volume={0.03} />
49+
)}
7150

7251
<img
7352
src="/codezilla_logo.png"
@@ -84,6 +63,8 @@ const AppContent: React.FC = () => {
8463
<Route path="/gameover" element={<GameOver />} />
8564
<Route path="/signup" element={<Signup />} />
8665
<Route path="/victory" element={<Victory />} />
66+
/* <Route path="/leaderboard" element={<LeaderBoard />} /> */
67+
8768
<Route path="/question/:id" element={<Questions />} />
8869
</Routes>
8970
</div>
@@ -99,4 +80,3 @@ const App: React.FC = () => {
9980
};
10081

10182
export default App;
102-

client/src/components/BackgroundMusic.tsx

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useEffect, useRef } from 'react';
2+
3+
interface Props {
4+
src: string;
5+
volume?: number;
6+
}
7+
8+
const BackgroundMusicProvider: React.FC<Props> = ({ src, volume = 0.03 }) => {
9+
const audioRef = useRef<HTMLAudioElement | null>(null);
10+
11+
useEffect(() => {
12+
if (!audioRef.current) {
13+
const audio = new Audio(src);
14+
audio.loop = true;
15+
audio.volume = volume;
16+
audioRef.current = audio;
17+
18+
// 🔒 Play only after a click event
19+
const handleUserInteraction = () => {
20+
audio.play().catch((err) =>
21+
console.warn('Autoplay blocked or error playing:', err)
22+
);
23+
document.removeEventListener('click', handleUserInteraction);
24+
};
25+
26+
document.addEventListener('click', handleUserInteraction, { once: true });
27+
}
28+
29+
return () => {
30+
audioRef.current?.pause();
31+
};
32+
}, [src, volume]);
33+
34+
return null;
35+
};
36+
37+
export default BackgroundMusicProvider;

client/src/components/LeaderBoard.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// import { useQuery } from '@apollo/client';
33
// import { GET_USERS } from '../graphql/queries'; // Adjust the import path as necessary
44

5-
// function LeaderBoard() {
5+
function LeaderBoard() {
66

77
// interface IUser {
88
// _id: string;
@@ -15,7 +15,6 @@
1515
// /* const [leaderboardData, setLeaderboardData] = useState([
1616
// { _id: 'kjhd63r9bhsef', username: 'Player1', correctAnswers: 100 },
1717
// { username: 'Player2', score: 90 },
18-
// { username: 'Player3', score: 80 },
1918
// { username: 'Player4', score: 70 },
2019
// { username: 'Player5', score: 60 },
2120
// ]);
@@ -53,6 +52,6 @@
5352
// </div>
5453

5554
// )
56-
// }
55+
}
5756

58-
// export default LeaderBoard
57+
export default LeaderBoard

client/src/components/screens/GameMap.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Minion from './Minions';
44

55
import { useBodyClass } from '../../utils/useBodyClass';
66
import { preloadSounds } from '../../utils/preloadSounds';
7-
import BackgroundMusic from '../BackgroundMusic';
7+
import BackgroundMusic from '../BackgroundMusicProvider';
88

99
import "../../styles/codezilla.css";
1010
import drDanImg from '../../../avatars/drdan2.png';
@@ -25,13 +25,13 @@ const GameMap: React.FC = () => {
2525
'/audio/Dan_correct/Dan-correct-1.wav',
2626
'/audio/Dan_correct/Dan-correct-2.wav',
2727
'/audio/Dan_correct/Dan-correct-3.wav',
28-
'/audio/Dan_correct/correctStar.wav',
28+
'/audio/Dan_correct/Dan-correct-4.wav',
2929
'/audio/Dan_incorrect/Dan-incorrect-1.wav',
3030
'/audio/Dan_incorrect/Dan-incorrect-2.wav',
3131
'/audio/Dan_incorrect/Dan-incorrect-3.wav',
3232
'/audio/Dan_incorrect/Dan-incorrect-4.wav',
33-
'/audio/Dan_incorrect/firstincorrect.wav',
34-
'/audio/5inarow.wav'
33+
'/audio/Dan_incorrect/Dan-incorrect-5.wav',
34+
//'/audio/5inarow.wav'
3535
]);
3636
}, []);
3737

0 commit comments

Comments
 (0)