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

132 lines
5.2 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.loadDefaultHSTable = () => {
2020-12-09 17:13:31 +01:00
fetch(`${hiscores.apiURL}/highscores/getPlayersByTotal`)
2020-12-09 15:50:18 +01:00
.then(response => response.json())
.then(result => {
console.log(result[0]);
2020-12-09 17:13:31 +01:00
hiscores.tableData = result;
hiscores.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 17:13:31 +01:00
const playerData = hiscores.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";
2020-12-09 17:13:31 +01:00
row.childNodes[3].innerHTML = `<a href="./hiscores.html">${playerData ? hiscores.formatName(playerData.username) : ""}</a>`;
2020-12-09 15:20:58 +01:00
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 17:13:31 +01:00
fetch(`${hiscores.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]);
2020-12-09 17:13:31 +01:00
hiscores.tableData = result;
2020-12-09 16:52:03 +01:00
hiscores.populatePlayerHSTable();
2020-12-09 17:13:31 +01:00
hiscores.setHeadSkillText(hiscores.formatName(username, true));
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 17:13:31 +01:00
hiscores.setHeadSkillIcon("Constitution");
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 17:13:31 +01:00
row.childNodes[5].innerHTML = hiscores.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 17:13:31 +01:00
row.childNodes[7].innerHTML = Math.floor(hiscores.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 17:13:31 +01:00
fetch(`${hiscores.apiURL}/highscores/playersBySkill/${skillId}`)
2020-12-09 16:10:01 +01:00
.then(response => response.json())
.then(result => {
console.log(result[0]);
2020-12-09 17:13:31 +01:00
hiscores.tableData = result;
hiscores.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 = () => {
2020-12-09 17:13:31 +01:00
hiscores.setHeadSkillText(hiscores.sName[hiscores.currentSkillId]);
hiscores.setHeadSkillIcon(hiscores.sName[hiscores.currentSkillId]);
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 17:13:31 +01:00
const playerData = hiscores.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";
2020-12-09 17:13:31 +01:00
row.childNodes[3].innerHTML = `<a href="./hiscores.html">${playerData ? hiscores.formatName(playerData.username) : ""}</a>`;
2020-12-09 16:10:01 +01:00
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();