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

223 lines
8.1 KiB
JavaScript
Raw Normal View History

2020-12-09 16:52:03 +01:00
var hiscores = hiscores || {};
2020-12-10 21:44:56 +01:00
2022-05-01 09:50:53 +02:00
hiscores.apiURL = "https://dginovker-cors-anywhere.herokuapp.com/api.2009scape.org:3000";
2020-12-10 21:44:56 +01:00
2020-12-09 16:52:03 +01:00
hiscores.sName = [
2020-12-09 16:40:53 +01:00
"Attack",
"Defence",
"Strength",
"Hitpoints",
2020-12-09 16:40:53 +01:00
"Ranged",
"Prayer",
"Magic",
"Cooking",
"Woodcutting",
"Fletching",
"Fishing",
"Firemaking",
"Crafting",
"Smithing",
"Mining",
"Herblore",
"Agility",
"Thieving",
"Slayer",
"Farming",
"Runecrafting",
"Hunter",
"Construction",
"Summoning"
]
2020-12-09 17:13:31 +01:00
hiscores.page = 0;
2021-02-15 07:47:54 +01:00
hiscores.world = 1;
2020-12-09 17:13:31 +01:00
hiscores.currentSkillId = "";
hiscores.tableData = [];
hiscores.defaultTableData = [];
2021-01-05 17:17:18 +01:00
hiscores.linkLeftTabSkillNames = (loc = "hiscores") => {
2020-12-09 16:52:03 +01:00
hiscores.sName.forEach((skill, index) => {
2020-12-09 16:40:53 +01:00
row = document.getElementsByClassName(` ${skill} ico`)[0].addEventListener("click", function (e) {
2020-12-10 16:32:20 +01:00
e.preventDefault();
2021-01-05 17:17:18 +01:00
window.location.replace(`./${loc}.html?skill=${index}${hiscores.getFiltersAsURLparams()}`);
2020-12-09 16:40:53 +01:00
});
});
2020-12-10 16:32:20 +01:00
document.getElementsByClassName(` Overall ico`)[0].addEventListener("click", function (e) {
e.preventDefault();
2021-01-05 17:17:18 +01:00
window.location.replace(`./${loc}.html${hiscores.getFiltersAsURLparams()}`);
2020-12-10 16:32:20 +01:00
});
2020-12-09 16:52:03 +01:00
}
2021-01-07 22:21:34 +01:00
hiscores.linkLeftTabActivityNames = (loc = "activities") => {
document.getElementsByClassName(` Overall ico`)[0].addEventListener("click", function (e) {
e.preventDefault();
window.location.replace(`./${loc}.html${hiscores.getFiltersAsURLparams()}`);
});
2021-01-07 22:21:34 +01:00
document.getElementsByClassName(` Slayer ico`)[0].addEventListener("click", function (e) {
e.preventDefault();
window.location.replace(`./${loc}.html?filter=combat${hiscores.getFiltersAsURLparams()}`);
});
document.getElementsByClassName(` Summoning ico`)[0].addEventListener("click", function (e) {
e.preventDefault();
window.location.replace(`./${loc}.html?filter=miscellaneous${hiscores.getFiltersAsURLparams()}`);
});
}
2021-01-05 17:17:18 +01:00
hiscores.initializePageArrows = (loc = "hiscores") => {
2020-12-09 16:52:03 +01:00
document.getElementById("button-up").addEventListener("click", function (e) {
e.preventDefault();
if (hiscores.page > 0) {
hiscores.page--;
}
2020-12-10 15:48:43 +01:00
let pageRemovedWindowLocation = window.location.search.split(/\?page=\d*/).join('');
2021-01-05 17:17:18 +01:00
window.location.replace(`./${loc}.html${pageRemovedWindowLocation}?page=${hiscores.page}`);
2020-12-09 16:52:03 +01:00
});
document.getElementById("button-down").addEventListener("click", function (e) {
e.preventDefault();
hiscores.page++;
2020-12-10 15:48:43 +01:00
let pageRemovedWindowLocation = window.location.search.split(/\?page=\d*/).join('');
2021-01-05 17:17:18 +01:00
window.location.replace(`./${loc}.html${pageRemovedWindowLocation}?page=${hiscores.page}`);
2020-12-09 16:52:03 +01:00
});
}
2021-01-05 17:17:18 +01:00
hiscores.initalizeRightsideButtons = (loc = "hiscores") => {
2020-12-09 16:52:03 +01:00
document.getElementById("search_button").addEventListener("click", function (e) {
e.preventDefault();
2021-01-05 17:17:18 +01:00
window.location.replace(`./${loc}.html?player=${document.getElementById('search_name').value}${hiscores.getFiltersAsURLparams()}`);
2020-12-09 16:52:03 +01:00
});
document.getElementById("search_rank_submit").addEventListener("click", function (e) {
e.preventDefault();
if (document.getElementById('search_rank').value) {
hiscores.loadUserTable(hiscores.defaultTableData[document.getElementById('search_rank').value - 1].username)
hiscores.setHeadSkillText(hiscores.defaultTableData[document.getElementById('search_rank').value - 1].username + "'s ");
2020-12-09 16:52:03 +01:00
}
else {
hiscores.loadDefaultHSTable();
}
});
2020-12-10 15:48:43 +01:00
document.getElementById("filter_submit").addEventListener("click", function (e) {
e.preventDefault();
const ironparam = `?iron=${document.getElementById('check_iron').checked}`;
const ultironparam = `?ultiron=${document.getElementById('check_ultiron').checked}`;
const hcironparam = `?hciron=${document.getElementById('check_hciron').checked}`;
const maxXP = `?maxXP=${document.getElementById("maxXP").value}`;
2021-03-06 20:01:21 +01:00
const world = `?world=${getParam("world")}`;
window.location.replace(`./${loc}.html${ironparam}${ultironparam}${hcironparam}${maxXP}${world}`);
2020-12-10 15:48:43 +01:00
});
if (document.getElementById("filter_clear")) {
// Filter clear only appears when filters are present
document.getElementById("filter_clear").addEventListener("click", function (e) {
e.preventDefault();
let pageRemovedFiltersLocation = window.location.search.split(/\?iron=[A-z]+|\?ultiron=[A-z]+|\?hciron=[A-z]+|\?maxXP=[\d\.]+/).join('');
2021-01-05 17:17:18 +01:00
window.location.replace(`./${loc}.html${pageRemovedFiltersLocation}`);
2021-01-07 22:21:34 +01:00
})
}
2020-12-09 17:13:31 +01:00
}
2021-02-16 23:52:21 +01:00
hiscores.changePlaqueWorld = () => {
document.getElementById("worldplaqueid").innerText = `World ${hiscores.world} Hiscores`;
}
2021-03-06 20:07:46 +01:00
hiscores.updateLegendText = () => {
//document.getElementById("worldxprate").innerText = `World ${hiscores.world} default XP rate: ${hiscores.world === 1 ? 1 : 5}x`;
2021-03-06 20:07:46 +01:00
}
2021-02-16 23:52:21 +01:00
hiscores.addSkillsAndActivityFilters = () => {
document.getElementById("button-left").addEventListener("click", function (e) {
e.preventDefault();
window.location.replace(`./hiscores.html${hiscores.getFiltersAsURLparams()}`);
});
document.getElementById("button-right").addEventListener("click", function (e) {
e.preventDefault();
window.location.replace(`./activity.html${hiscores.getFiltersAsURLparams()}`);
});
}
2020-12-10 16:32:20 +01:00
hiscores.filter = (result) => {
return result.filter(result => {
// No filters
if (!getParam("iron")) {
return true;
}
if (Number(result.exp_multiplier) > Number(getParam("maxXP"))) {
return false;
}
// If all ironman filters are false, show everyone (only filter by exp)
if (getParam("iron") === "false" && getParam("ultiron") === "false" && getParam("hciron") === "false") {
return true;
}
// If some ironman filters are true, only show those
if (getParam("iron") === "true" && result.iron_mode == 1) {
return true;
}
if (getParam("hciron") === "true" && result.iron_mode == 2) {
return true;
}
if (getParam("ultiron") === "true" && result.iron_mode == 3) {
return true;
}
return false;
});
}
hiscores.getFiltersAsURLparams = () => {
2021-02-16 23:52:21 +01:00
let params = getParam("iron") ? `?iron=${getParam("iron")}?hciron=${getParam("hciron")}?ultiron=${getParam("ultiron")}?maxXP=${getParam("maxXP")}` : "";
params += getParam("world") ? `?world=${getParam("world")}` : "";
return params;
2020-12-10 16:32:20 +01:00
}
2021-03-06 20:01:21 +01:00
hiscores.formatName = (name, ironStatus = 0, xpRate, aposS = false,) => {
2020-12-09 17:13:31 +01:00
name = name.replaceAll("_", " ");
name = name.replace(/(^\w|\s\w)/g, match => match.toUpperCase()); // Capitalize first letter of each word
if (aposS) {
if (!name.endsWith('s')) {
name += "'s";
} else {
name += "'";
}
}
name = hiscores.getIronIcon(ironStatus) + name;
2021-03-06 20:01:21 +01:00
if ((getParam("world") === "1" && xpRate != 1) || (getParam("world") === "2" && xpRate != 5)) {
2021-03-06 20:07:46 +01:00
return name + ` <span style="color: rgba(${Math.max(0, 80 - Math.pow(xpRate, 1.7) * 10)}, 0, 0, 0.4);">${xpRate >= 10 ? Math.round(xpRate) : xpRate}x</span>`;
}
return name;
2020-12-09 17:13:31 +01:00
}
2020-12-09 22:12:19 +01:00
hiscores.setHeadSkillIcon = (icon) => {
if (icon.includes(".")) {
document.getElementById("scores_head_icon").src = icon;
} else {
document.getElementById("scores_head_icon").src = `../../site/img/hiscores/skill_icon_${icon.toLowerCase()}1eccb.gif`;
}
2020-12-09 17:13:31 +01:00
}
hiscores.setHeadSkillText = (text) => {
document.getElementById("scores_head_skill").innerHTML = text;
2020-12-09 23:20:08 +01:00
}
hiscores.getIronIcon = (ironStatus) => {
switch (ironStatus) {
case "1":
case 1:
return `<img src="../../site/img/osrsimg/ironman.webp" style="height: 11px"> `;
2020-12-09 23:20:08 +01:00
case "2":
case 2:
return `<img src="../../site/img/osrsimg/hcim.webp" style="height: 11px"> `;
2020-12-09 23:20:08 +01:00
case "3":
case 3:
return `<img src="../../site/img/osrsimg/ultimateironman.webp" style="height: 11px"> `;
2020-12-09 23:20:08 +01:00
default:
return "";
}
}