Skip to content

Commit 1ecc26a

Browse files
committed
leaderboards
1 parent 9775776 commit 1ecc26a

File tree

5 files changed

+168
-21
lines changed

5 files changed

+168
-21
lines changed

css/style.css

+33-6
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,7 @@ video {
258258
margin:20px auto;
259259
border-radius:20px;
260260
}
261-
.time {
262-
263-
}
264-
.distance {
265-
266-
}
261+
267262
.circle {
268263
position:absolute;
269264
top:50%;
@@ -360,4 +355,36 @@ p {
360355
transition:0.2s;
361356
cursor:pointer;
362357
display:block;
358+
}
359+
.row {
360+
padding:10px 20px;
361+
display:block;
362+
background:var(--c1);
363+
color:var(--color);
364+
margin:10px;
365+
text-align: left !important;
366+
}
367+
.row div {
368+
padding:0px 10px;
369+
display:inline-block;
370+
}
371+
372+
.timelead {
373+
position:absolute;
374+
right: 30px;
375+
}
376+
#name {
377+
width:calc(100% - 86px);
378+
height:40px;
379+
line-height:60px;
380+
padding:10px 20px;
381+
margin:20px;
382+
background:var(--colorIn);
383+
color:var(--color);
384+
border-radius:60px;
385+
border:3px solid transparent;
386+
font-size: 25px;
387+
}
388+
#name:focus {
389+
border:3px solid var(--c1);
363390
}

index.html

+17-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ <h2 id="mapname"></h2>
6565

6666
</div>
6767
<div class='button mainPageBtn' onclick='printMap();'>Print</div>
68-
<div class='button mainPageBtn' onclick='tab(6);loadCourse();'>Start Course</div>
68+
<div class='button mainPageBtn' onclick='tab(17);loadCourse();'>Start Course</div>
69+
<div class='button mainPageBtn' id="mapleaders">Leaderboard</div>
6970
<div class='button mainPageAlt' onclick='tab(2);'>Back</div>
7071
<!-- Course Download Map or print and see details -->
7172
</div>
@@ -84,7 +85,7 @@ <h1>Results</h1>
8485
</table>
8586
<table class='courseTable'>
8687
<tr>
87-
<th>Elevation Change</th>
88+
<th>Elevation Gain</th>
8889
<th>Average Split</th>
8990
</tr>
9091
<tr>
@@ -207,8 +208,22 @@ <h3 id="eventtime"></h3>
207208
<div class='button mainPageBtn' id="eventbtn">Location</div>
208209
<div class='button mainPageAlt' onclick='tab(1);'>Back</div>
209210
</div>
211+
<!-- 16 -->
212+
<div class='tab'>
213+
<h1>LeaderBoard</h1>
214+
<h2 id="leaderboardtitle"></h2>
215+
<div id="leaderboard"></div>
216+
<div class='button mainPageAlt' onclick='tab(1);'>Back</div>
217+
</div>
218+
<!-- 17 -->
219+
<div class='tab'>
220+
<h1>Name</h1>
221+
<input id="name" type="text" placeholder="Type your name here..." maxlength="15" autocomplete="off" />
222+
<div class='button mainPageBtn' onclick='setName();'>Submit</div>
223+
</div>
210224
<div class="footer"></div>
211225
</div>
226+
<script src="js/firebase.js" type="module"></script>
212227
<script src="js/script.js"></script>
213228
</body>
214229

js/firebase.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-app.js";
2+
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-analytics.js";
3+
const firebaseConfig = {
4+
apiKey: "AIzaSyDROBDZ1MZfFiY0Yn54w9LVmDHsY0AJvmQ",
5+
authDomain: "takoma-park-orienteering.firebaseapp.com",
6+
databaseURL: "https://takoma-park-orienteering-default-rtdb.firebaseio.com",
7+
projectId: "takoma-park-orienteering",
8+
storageBucket: "takoma-park-orienteering.appspot.com",
9+
messagingSenderId: "654127400915",
10+
appId: "1:654127400915:web:cf14fa1308c6c5666ad126",
11+
measurementId: "G-1X0MRPN1C8"
12+
};
13+
14+
import {getDatabase, set, get, update, remove, ref, child}
15+
from "https://www.gstatic.com/firebasejs/10.12.3/firebase-database.js"
16+
try {
17+
const app = initializeApp(firebaseConfig);
18+
const analytics = getAnalytics(app);
19+
const db = getDatabase(app);
20+
21+
const insertData = function(orienteering, warning) {
22+
if (orienteering["endtime"] != 0) {
23+
set(ref(db, orienteering["course"]+"/"+(orienteering["name"]+new Date().getTime())), {
24+
Name:orienteering["name"],
25+
Time:(orienteering["endtime"]-orienteering["starttime"]+orienteering["bonustime"])
26+
}).then(() => {
27+
warning("Course Complete","Your course has been saved to the database.",false);
28+
localStorage.clear();
29+
}).catch((error) => {
30+
console.error("Error saving to database:", error);
31+
alert("An error occurred: " + error.message);
32+
});
33+
}
34+
};
35+
window.insertData = insertData;
36+
const requestData = function(course) {
37+
//request from firebase for leaderboard
38+
const leaderboardtitle = document.getElementById("leaderboardtitle");
39+
leaderboardtitle.textContent = course;
40+
const leaderboard = document.getElementById('leaderboard');
41+
leaderboard.innerHTML = "";
42+
const dbref = ref(db);
43+
get(child(dbref, course)).then((snapshot) => {
44+
const data = snapshot.val();
45+
if (data && Object.keys(data).length > 0) {
46+
let courseList = [];
47+
Object.values(data).forEach((value) => {
48+
courseList.push({
49+
Name: value.Name,
50+
Time: value.Time
51+
});
52+
});
53+
let sortedlist = courseList.sort((a, b) => (a.Time) - (b.Time));
54+
for (let r = 0; r < sortedlist.length; r++) {
55+
let rank = r + 1;
56+
const row = document.createElement('div');
57+
row.classList.add('row');
58+
const rankDiv = document.createElement('div');
59+
rankDiv.classList.add('rank');
60+
rankDiv.textContent = rank;
61+
const name = document.createElement('div');
62+
name.classList.add('name');
63+
name.textContent = sortedlist[r].Name;
64+
const time = document.createElement('div');
65+
time.classList.add('timelead');
66+
time.textContent = `${Math.floor((sortedlist[r].Time) / 60000)}: ${Math.round(((sortedlist[r].Time) % 60000) / 1000)}`;
67+
row.appendChild(rankDiv);
68+
row.appendChild(name);
69+
row.appendChild(time);
70+
leaderboard.appendChild(row);
71+
}
72+
} else {
73+
leaderboard.innerHTML = "No data found.";
74+
}
75+
});
76+
};
77+
window.requestData = requestData;
78+
} catch(error) {
79+
console.error("Error initializing Firebase:", error);
80+
};

js/script.js

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// ------------ Copywrite Dominik Honzak (c) 2024 ------------ //
22
//Created Entirely from scratch by Dominik Honzak//
33

4-
54
const replaceGit = "/EagleProject"; /* /EagleProject
65
*/
76
const basic = {
@@ -56,7 +55,8 @@ const mapArray = document.getElementById("maps");
5655
const mapDetails = {
5756
name: document.getElementById("mapname"),
5857
display: document.getElementById("mapdisplay"),
59-
detail: document.getElementById("mapdetail")
58+
detail: document.getElementById("mapdetail"),
59+
leaders: document.getElementById("mapleaders")
6060
};
6161
const resultsPage = {
6262
controls: document.getElementById("courseControlLength_off"),
@@ -206,7 +206,7 @@ const loadMap = function(i) {
206206
else {
207207
mapDetails.display.src = basic.maps[i]["Map"];
208208
}
209-
209+
mapDetails.leaders.onclick = function() {openLeaderboard(basic.maps[i]["Name"]);};
210210
mapDetails.detail.innerHTML = "";
211211
tab(3);
212212

@@ -332,7 +332,7 @@ let accuracyLat = (accuracy/(2*Math.PI*6371000*Math.cos(toRad(targetLat))/360)).
332332
}
333333
else {
334334

335-
testing.innerHTML = ("off by: "+calculateDistance(currentLat,currentLong,targetLat,targetLong)+`m<div onclick='warning("Cords","`+currentLat.toFixed(6)+", "+currentLong.toFixed(6)+` <br> `+targetLat.toFixed(6)+", "+targetLong.toFixed(6)+`",false);'>testing</div>`);
335+
//testing.innerHTML = ("off by: "+calculateDistance(currentLat,currentLong,targetLat,targetLong)+`m<div onclick='warning("Cords","`+currentLat.toFixed(6)+", "+currentLong.toFixed(6)+` <br> `+targetLat.toFixed(6)+", "+targetLong.toFixed(6)+`",false);'>testing</div>`);
336336
if (!(position.coords.accuracy <= 15)) {
337337
tab(11);
338338
setTimeout(function() {
@@ -359,7 +359,7 @@ const showError = function(error) {
359359
case error.UNKNOWN_ERROR:
360360
break;
361361
}
362-
warning("Location Request","Your device likely does not have location services enabled.</p><b class='grey'>IOS: </b>aA → Settings → Scroll Down → Location → Allow<br><b class='grey'>Android: </b>⋮ → Scroll Down → ⓘ → Permissions → Location → On",true);
362+
warning("Location Request","Your device likely does not have location services enabled.</p><b class='grey'>Safari: </b>aA → Settings → Scroll Down → Location → Allow<br><b class='grey'>Android: </b>⋮ → Scroll Down → ⓘ → Permissions → Location → On",true);
363363
};
364364
const getLocation = function() {
365365
if (navigator.geolocation) {
@@ -401,11 +401,28 @@ const endCourse = function() {
401401
document.getElementsByClassName('time')[0].textContent = "Time: "+(parseInt(cc/60)+':'+cc1);
402402
resultsPage.controls.textContent = orienteering["Controls"].length;
403403
resultsPage.distance.textContent = orienteering["distance"].toFixed(1)+" / "+ orienteering["length"].toFixed(1)+ " km";
404-
resultsPage.elevation.textContent = orienteering["elevation"].reduce((acc, curr) => acc + curr, 0).toFixed(1) + "m";
404+
const elevations = orienteering["elevation"];
405+
let elevationGain = 0;
406+
let elevationLoss = 0;
407+
408+
for (let i = 1; i < elevations.length; i++) {
409+
const diff = elevations[i] - elevations[i - 1];
410+
if (diff > 0) {
411+
elevationGain += diff;
412+
} else {
413+
elevationLoss -= diff; // Using subtraction to handle negative differences
414+
}
415+
}
416+
417+
const gainString = elevationGain.toFixed(1) + "m";
418+
const lossString = elevationLoss.toFixed(1) + "m";
419+
resultsPage.elevation.textContent = gainString;
405420
resultsPage.split.textContent = Math.round(cc / (orienteering["Controls"].length - 1)) + "s";
406421

407422

408-
localStorage.clear();
423+
// Now use `imported.insertData` to call the function
424+
insertData(orienteering, warning);
425+
409426
};
410427
const repeating = function() {
411428
getLocation();
@@ -419,7 +436,7 @@ const loadCourse = function() {
419436
document.getElementById("courseControlLength_on").textContent = (basic.maps[orienteering["courseindex"]]["Controls"].length-2);
420437
document.getElementById("courseDistance_on").textContent = orienteering["length"].toFixed(1)+" km";
421438
document.getElementById("courseDifficulty_on").style.background = basic.colorhue[orienteering["difficulty"]];
422-
warning("Warning","We suggest that you refrain from staring at your phone while doing the course as this may result in injury. Be aware that some controls are placed off of paths. Conditions such as weather may impact how dangerous a course is. <br>Please be aware that although this is a website it will still save your course where you left off so you can turn off your phone while running. (if the screen is off distance mesurement will be slightly off)",true);
439+
warning("Warning","We suggest that you refrain from staring at your phone while doing the course as this may result in injury. Be aware that some controls are placed off of paths. Conditions such as weather may impact how dangerous a course is. <br>Please be aware that although this is a website it will still save your course where you left off so you can turn off your phone while running. (please be aware distance measurement may have issues)",true);
423440
};
424441
const openOptions = function() {
425442
tab(9);
@@ -493,6 +510,7 @@ basic.maps = JSON.parse(responseText)["Maps"];
493510
}
494511
else {
495512
orienteering = JSON.parse(`{
513+
"name":"Anonymous",
496514
"course":"",
497515
"courseindex":null,
498516
"difficulty":0,
@@ -596,4 +614,15 @@ const hotCold = function(dist) {
596614
document.querySelector(':root').style.setProperty('--bg_overlay', "rgba("+rgb.r+","+rgb.g+","+rgb.b+",0.65)");
597615
//change background overlay color
598616
}
617+
};
618+
const openLeaderboard = function(crs) {
619+
tab(16);
620+
requestData(crs);
621+
};
622+
const setName = function() {
623+
const val = document.getElementById("name").value;
624+
if (val) {
625+
orienteering["name"] = document.getElementById("name").value;
626+
}
627+
tab(6);loadCourse();
599628
};

json/maps.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"Name":"Montgomery to Prince Georges",
56-
"Difficulty":6,
56+
"Difficulty":5,
5757
"Controls":[
5858
"38.982549, -77.010404",
5959
"38.983112012139244, -77.0091138084131",
@@ -63,10 +63,6 @@
6363
"38.97880250033363, -76.99336304599285",
6464
"38.98140409626414, -76.99095170742015",
6565
"",
66-
"",
67-
"",
68-
"",
69-
"",
7066
"38.982104, -77.010948"
7167
],
7268
"Map":"",

0 commit comments

Comments
 (0)