2009scape-website/services/m=hiscore/hiscores.js

139 lines
5.3 KiB
JavaScript
Raw Normal View History

2020-12-09 16:52:03 +01:00
var hiscores = hiscores || {};
2020-12-09 16:40:53 +01:00
2020-12-09 16:52:03 +01:00
hiscores.page = 0;
2020-12-09 16:10:01 +01:00
let currentSkillId = "";
2020-12-09 14:29:40 +01:00
2020-12-09 15:20:58 +01:00
const apiURL = "http://localhost:3000";
let tableData = [];
2020-12-09 16:40:53 +01:00
let defaultTableData = [];
2020-12-09 15:20:58 +01:00
2020-12-09 16:52:03 +01:00
hiscores.loadDefaultHSTable = () => {
2020-12-09 15:50:18 +01:00
fetch(`${apiURL}/highscores/getPlayersByTotal`)
.then(response => response.json())
.then(result => {
console.log(result[0]);
tableData = result;
2020-12-09 16:40:53 +01:00
defaultTableData = result;
2020-12-09 16:52:03 +01:00
hiscores.populateDefaultHSTable();
2020-12-09 15:50:18 +01:00
})
.catch(error => console.log('error', error));
}
2020-12-09 15:20:58 +01:00
2020-12-09 16:52:03 +01:00
hiscores.populateDefaultHSTable = () => {
2020-12-09 15:50:18 +01:00
for (let i = 1; i <= 24; i++) {
2020-12-09 14:35:42 +01:00
row = document.getElementsByClassName(`row row${i}`)[0];
2020-12-09 16:52:03 +01:00
const playerData = tableData[i + 24 * hiscores.page - 1];
2020-12-09 15:20:58 +01:00
row.childNodes[1].replaceWith(document.createElement("td"));
row.childNodes[1].className = "rankCol";
2020-12-09 16:52:03 +01:00
row.childNodes[1].innerHTML = i + 24 * hiscores.page;
2020-12-09 15:20:58 +01:00
row.childNodes[3].replaceWith(document.createElement("td"));
row.childNodes[3].className = "alL";
row.childNodes[3].innerHTML = `<a href="./hiscores.html">${playerData ? playerData.username : ""}</a>`;
row.childNodes[3].addEventListener("click", function (e) {
e.preventDefault();
2020-12-09 16:52:03 +01:00
hiscores.loadUserTable(playerData.username);
2020-12-09 15:20:58 +01:00
});
row.childNodes[5].replaceWith(document.createElement("td"));
row.childNodes[5].className = "alL";
row.childNodes[5].innerHTML = playerData ? playerData.level : "";
row.childNodes[7].replaceWith(document.createElement("td"));
row.childNodes[7].className = "alL";
row.childNodes[7].innerHTML = playerData ? Math.floor(playerData.xp).toLocaleString() : "";
}
}
2020-12-09 16:52:03 +01:00
hiscores.loadUserTable = (username) => {
2020-12-09 15:50:18 +01:00
fetch(`${apiURL}/highscores/playerSkills/${username.toLowerCase()}`)
2020-12-09 15:20:58 +01:00
.then(response => response.json())
.then(result => {
2020-12-09 15:50:18 +01:00
document.getElementById('search_name').style.color = 'black';
2020-12-09 15:20:58 +01:00
console.log(result[0]);
tableData = result;
2020-12-09 16:52:03 +01:00
hiscores.populatePlayerHSTable();
2020-12-09 16:40:53 +01:00
document.getElementById("scores_head_skill").innerText = username + "'s ";
2020-12-09 15:20:58 +01:00
})
2020-12-09 15:50:18 +01:00
.catch(error => {
document.getElementById('search_name').style.color = 'red';
console.log('error', error)
});
2020-12-09 15:20:58 +01:00
}
2020-12-09 16:52:03 +01:00
hiscores.populatePlayerHSTable = () => {
2020-12-09 15:50:18 +01:00
for (let i = 1; i <= 24; i++) {
2020-12-09 15:20:58 +01:00
row = document.getElementsByClassName(`row row${i}`)[0];
row.childNodes[1].replaceWith(document.createElement("td"));
row.childNodes[1].className = "rankCol";
2020-12-09 15:50:18 +01:00
row.childNodes[1].innerHTML = "0";
2020-12-09 14:29:40 +01:00
2020-12-09 14:35:42 +01:00
row.childNodes[3].replaceWith(document.createElement("td"));
row.childNodes[3].className = "alL";
2020-12-09 16:52:03 +01:00
row.childNodes[3].innerHTML = `<a href="./hiscores.html">${hiscores.sName[i - 1]}</a>`;
2020-12-09 16:15:30 +01:00
row.childNodes[3].addEventListener("click", e => {
e.preventDefault();
2020-12-09 16:52:03 +01:00
hiscores.loadSkillTable(i - 1);
2020-12-09 16:15:30 +01:00
})
2020-12-09 14:29:40 +01:00
2020-12-09 14:35:42 +01:00
row.childNodes[5].replaceWith(document.createElement("td"));
row.childNodes[5].className = "alL";
2020-12-09 15:50:18 +01:00
row.childNodes[5].innerHTML = tableData[i - 1].static;
2020-12-09 14:35:42 +01:00
row.childNodes[7].replaceWith(document.createElement("td"));
row.childNodes[7].className = "alL";
2020-12-09 15:50:18 +01:00
row.childNodes[7].innerHTML = Math.floor(tableData[i - 1].experience).toLocaleString();
2020-12-09 14:35:42 +01:00
}
}
2020-12-09 16:52:03 +01:00
hiscores.loadSkillTable = (skillId) => {
2020-12-09 16:10:01 +01:00
fetch(`${apiURL}/highscores/playersBySkill/${skillId}`)
.then(response => response.json())
.then(result => {
console.log(result[0]);
tableData = result;
currentSkillId = skillId;
2020-12-09 16:52:03 +01:00
hiscores.populateSkillHSTable();
2020-12-09 16:10:01 +01:00
})
.catch(error => console.log('error', error));
}
2020-12-09 16:52:03 +01:00
hiscores.populateSkillHSTable = () => {
document.getElementById("scores_head_skill").innerText = hiscores.sName[currentSkillId];
document.getElementById("scores_head_icon").src = `../../site/img/hiscores/skill_icon_${hiscores.sName[currentSkillId].toLowerCase()}1eccb.gif`;
2020-12-09 16:15:30 +01:00
2020-12-09 16:10:01 +01:00
for (let i = 1; i <= 24; i++) {
row = document.getElementsByClassName(`row row${i}`)[0];
2020-12-09 16:52:03 +01:00
const playerData = tableData[i + 24 * hiscores.page - 1];
2020-12-09 16:10:01 +01:00
row.childNodes[1].replaceWith(document.createElement("td"));
row.childNodes[1].className = "rankCol";
2020-12-09 16:52:03 +01:00
row.childNodes[1].innerHTML = i + 24 * hiscores.page;
2020-12-09 16:10:01 +01:00
row.childNodes[3].replaceWith(document.createElement("td"));
row.childNodes[3].className = "alL";
row.childNodes[3].innerHTML = `<a href="./hiscores.html">${playerData ? playerData.username : ""}</a>`;
row.childNodes[3].addEventListener("click", function (e) {
e.preventDefault();
2020-12-09 16:52:03 +01:00
hiscores.loadUserTable(playerData.username);
2020-12-09 16:10:01 +01:00
});
row.childNodes[5].replaceWith(document.createElement("td"));
row.childNodes[5].className = "alL";
row.childNodes[5].innerHTML = playerData ? playerData.level : "";
row.childNodes[7].replaceWith(document.createElement("td"));
row.childNodes[7].className = "alL";
row.childNodes[7].innerHTML = playerData ? Math.floor(playerData.xp).toLocaleString() : "";
}
}
2020-12-09 14:35:42 +01:00
2020-12-09 16:52:03 +01:00
hiscores.initializePageArrows();
hiscores.initalizeRightsideButtons();
hiscores.linkLeftTabSkillNames();
2020-12-09 16:40:53 +01:00
2020-12-09 16:52:03 +01:00
hiscores.loadDefaultHSTable();