-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (106 loc) · 4.65 KB
/
script.js
File metadata and controls
116 lines (106 loc) · 4.65 KB
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
106
107
108
109
110
111
112
113
114
115
116
import ApiUtilities from "./utilities.js";
//grab nodes
const notificationSection = document.querySelector('.notificationSection');
const seasonTitle = document.querySelector('.seasonTitle');
const seasonLeaderboardTitle = document.querySelector('.seasonLeaderboardTitle');
const seasonTimeLeft = document.querySelector('.seasonTimeLeft');
const startDate = document.querySelector('.startDate');
const endDate = document.querySelector('.endDate');
const playerCount = document.querySelector('.playerCount');
const hamburger = document.querySelector('.hamburger');
const navMenu = document.querySelector('.toolList');
hamburger.addEventListener("click", mobileMenu);
function mobileMenu() {
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
}
function updateSeasonInfo() {
fetch('https://data.ninjakiwi.com/battles2/homs')
.then((response) => response.json())
.then((json) => updateAll(json))
}
function updateAll(json) {
const leaderboardData = ApiUtilities.getActiveLeaderboard(json.body);
console.log(leaderboardData)
// Display no live season popup if there is no live season on the api
!leaderboardData.liveLbExists ? seasonNotLivePopup(leaderboardData.activelb.name) : null
getLeaderboardInfo(leaderboardData.activelb.leaderboard);
setInterval(updateText, 100, leaderboardData.activelb);
updateSeasonTitle(leaderboardData.activelb);
}
function getLeaderboardInfo(url) {
fetch(url)
.then((response) => response.json())
.then((json) => createLeaderboard(json))
}
function seasonNotLivePopup(replacementSeasonName) {
let notificationContainer = document.createElement('div');
notificationContainer.classList.add('notification');
let notificationText = document.createElement('p');
notificationText.textContent =
`Note: there's currently no active leaderboard on Ninja Kiwi's API! For now, here's ${replacementSeasonName}!`
notificationContainer.appendChild(notificationText);
notificationSection.appendChild(notificationContainer);
}
function createLeaderboard(json) {
const leaderboardBody = document.querySelector('.leaderboard>tbody');
leaderboardBody.innerHTML = '';
for (let i = 0; i < 20; i++) {
let player = json.body[i];
let playerRow = document.createElement('tr');
playerRow.classList.add('playerRow');
let playerName = document.createElement('td');
playerName.classList.add('playerName');
let playerScore = document.createElement('td');
playerScore.classList.add('playerScore');
let playerPlace = document.createElement('td');
if (i+1 < 4) {
playerPlace.classList.add(`place${i+1}`);
playerRow.classList.add('firstPlace');
}
playerPlace.classList.add('playerPlace');
playerPlace.textContent = i+1;
playerScore.textContent = player.score;
playerName.textContent = player.displayName;
playerRow.appendChild(playerName);
playerRow.appendChild(playerScore);
playerRow.appendChild(playerPlace);
playerRow.dataset.profileURL = player.profile;
playerRow.classList.add('playerRow');
playerRow.addEventListener('click', () => {
window.location.href ='playerInfo/playerInfo.html?' + playerRow.dataset.profileURL.split('/').at(-1);
});
leaderboardBody.appendChild(playerRow);
}
}
function updateText(data) {
seasonTitle.textContent = data.name
let timeLeft = getTimeLeft(data);
seasonTimeLeft.innerHTML = `<b>Time Left:</b> ${timeLeft.days} Days, ${timeLeft.hours} Hours, ${timeLeft.minutes} Minutes`
const seasonStart = new Date(data.start)
const seasonEnd = new Date(data.end)
startDate.textContent = seasonStart.toLocaleString()
endDate.textContent = seasonEnd.toLocaleString()
playerCount.textContent = data.totalScores
}
function updateSeasonTitle(data) {
seasonLeaderboardTitle.textContent = data.name
}
function getTimeLeft(data) {
const todayDate = new Date()
// console.log(todayDate.toLocaleDateString());
let endDate = new Date(data.end);
// console.log(endDate.toLocaleDateString());
let timeToEnd = (endDate - todayDate) /1000
timeToEnd/=60 //seconds to minutes
timeToEnd/=60 //minutes to hours
let daysLeft = timeToEnd / 24 //hours to days
daysLeft = Math.floor(daysLeft)
let hoursLeft = timeToEnd % 24 //take remainder hours
let minutesLeft = (hoursLeft*60)%60;
minutesLeft = Math.floor(minutesLeft)
hoursLeft = Math.floor(hoursLeft)
// console.log(daysLeft, hoursLeft, minutesLeft);
return {days : daysLeft, hours : hoursLeft, minutes : minutesLeft}
}
updateSeasonInfo();