-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
105 lines (94 loc) · 2.69 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { useEffect, useState } from "react";
import {
collection,
getDocs,
getFirestore,
orderBy,
query,
} from "firebase/firestore";
import GameLevel from "./components/GameLevel";
import Heading from "./components/Heading";
import Home from "./components/Home";
import Leaderboard from "./components/Leaderboard";
import { initializeApp } from "firebase/app";
import firebaseConfig from "./firebaseConfig";
import "./styles/app.css";
function App() {
initializeApp(firebaseConfig);
const [levelsData, setLevelsData] = useState([]);
const [leaderboardData, setLeaderboardData] = useState([]);
const getLeaderboardData = async () => {
const leaderboardQuery = query(
collection(getFirestore(), "leaderboard"),
orderBy("level", "asc")
);
const leaderboardSnapshot = await getDocs(leaderboardQuery);
let newLeaderboardData = [];
leaderboardSnapshot.forEach((score) => {
newLeaderboardData.push(score.data());
});
setLeaderboardData(newLeaderboardData);
};
const getLevelData = async () => {
const levelsQuery = query(
collection(getFirestore(), "levelData"),
orderBy("level", "asc")
);
const levelsSnapshot = await getDocs(levelsQuery);
let newLevelData = [];
levelsSnapshot.forEach((level) => {
newLevelData.push(level.data());
});
setLevelsData(newLevelData);
};
useEffect(() => {
getLevelData();
getLeaderboardData();
}, []);
const isNameInLeaderboardRepeated = (name, level) => {
const scoresFromLevel = leaderboardData.filter(
(data) => data.level === level
);
const hasName =
scoresFromLevel.filter(
(score) => score.name.toLowerCase() === name.toLowerCase()
).length > 0;
return hasName;
};
return (
<BrowserRouter>
<Heading />
<Routes>
<Route path="/" element={<Home levelsData={levelsData} />} />
<Route
path="leaderboard"
element={
<Leaderboard
levelsData={levelsData}
leaderboardData={leaderboardData}
/>
}
>
<Route path=":level" element={<div></div>} />
</Route>
<Route path="game">
<Route
path=":level"
element={
<GameLevel
levelsData={levelsData}
isNameInLeaderboardRepeated={isNameInLeaderboardRepeated}
updateLeaderboardData={getLeaderboardData}
/>
}
/>
</Route>
</Routes>
</BrowserRouter>
);
}
export default App;
// TODOS:
// Add X mark in hit spots
// Add start button before game starts