diff --git a/src/module/Challenges.js b/src/module/Challenges.js index 95e76ed..f1c0e4b 100644 --- a/src/module/Challenges.js +++ b/src/module/Challenges.js @@ -1,522 +1,721 @@ -import { Component } from "react" -import get from "../func/get" -import { serverToMachineReadable } from "../func/server" -import css from "../css/user.module.css" -import challengeCSS from "../css/challenges.module.css" -import getChallenge from "../func/getChallenge" +import { Component } from "react"; +import get from "../func/get"; +import { serverToMachineReadable } from "../func/server"; +import css from "../css/user.module.css"; +import challengeCSS from "../css/challenges.module.css"; +import getChallenge from "../func/getChallenge"; //import generateChallengePointElement from "../func/generateChallengePointElement" -import { checkExists } from "../func/arrayManipulationFunctions.js" -import config from "../config" -import ChallengeObject from "./ChallengeObject" +import { checkExists } from "../func/arrayManipulationFunctions.js"; +import config from "../config"; +import ChallengeObject from "./ChallengeObject"; -import { Fragment } from "react" -import filterCSS from "../css/filter.module.css" +import { Fragment } from "react"; +import filterCSS from "../css/filter.module.css"; -import Wrapper from "./Wrapper" +import Wrapper from "./Wrapper"; import Loadable from "react-loadable"; -import { setCookie } from "../func/cookiefunctions" -import Error from "./Error" -import { withTranslation } from "react-i18next" -import { capitalize } from "../func/stringManipulation" -import { storageKeys, getStorage, setStorage } from "../func/sessionStorageFunctions" +import { setCookie } from "../func/cookiefunctions"; +import Error from "./Error"; +import { withTranslation } from "react-i18next"; +import { capitalize } from "../func/stringManipulation"; +import { + storageKeys, + getStorage, + setStorage, +} from "../func/sessionStorageFunctions"; class Challenges extends Component { - constructor(props) { - super(props); - this.load = this.load.bind(this) - this.toLoad = { "content": [], "leaderboards": [] }; - this.loadChallenges = this.loadChallenges.bind(this) - this.changeFilter = this.changeFilter.bind(this) - this.applyFilters = this.applyFilterStyles.bind(this) - this.showChallenges = this.showChallenges.bind(this) - this.filter = getStorage(storageKeys.challengeFilters, { "category": [], "type": [], "gamemode": [] }) - this.searchFor = getStorage(storageKeys.challengeSearch, ""); - const showEvent = getStorage(storageKeys.event, true); - - const LoadEvent = Loadable({ - loader: (content) => import('./events/Event.js'), - loading: function () { - return
- }, - }); - - this.search = this.search.bind(this) - - this.changeDisplayMethod = this.changeDisplayMethod.bind(this) - - this.state = { - challenges: window.loadingUI, - event: , - translation: props.t, - showEvent: showEvent - } + constructor(props) { + super(props); + this.load = this.load.bind(this); + this.toLoad = { content: [], leaderboards: [] }; + this.loadChallenges = this.loadChallenges.bind(this); + this.changeFilter = this.changeFilter.bind(this); + this.applyFilters = this.applyFilterStyles.bind(this); + this.showChallenges = this.showChallenges.bind(this); + this.filter = getStorage(storageKeys.challengeFilters, { + category: [], + type: [], + gamemode: [], + }); + this.searchFor = getStorage(storageKeys.challengeSearch, ""); + const showEvent = getStorage(storageKeys.event, true); + + const LoadEvent = Loadable({ + loader: (content) => import("./events/Event.js"), + loading: function () { + return
; + }, + }); + + this.search = this.search.bind(this); + + this.changeDisplayMethod = this.changeDisplayMethod.bind(this); + + this.state = { + challenges: window.loadingUI, + event: , + translation: props.t, + showEvent: showEvent, + }; + } + + load() { + let server = serverToMachineReadable(window.region); + this.server = server; + + get( + `https://challenges.darkintaqt.com/api/dynamic-data/serve?region=euw1&lang=${window.language}`, + this.loadChallenges + ); + } + + loadChallenges(content) { + this.showChallenges(content); + } + + changeDisplayMethod(e) { + if (e.currentTarget.id === "full" && window.compactMode === true) { + window.compactMode = false; + this.showChallenges(window.JSONPREQUEST); + setCookie("filter", "false"); } - - load() { - let server = serverToMachineReadable(window.region) - this.server = server; - - - get(`https://challenges.darkintaqt.com/api/dynamic-data/serve?region=euw1&lang=${window.language}`, this.loadChallenges) - + if (e.currentTarget.id === "compact" && window.compactMode === false) { + window.compactMode = true; + this.showChallenges(window.JSONPREQUEST); + setCookie("filter", "true"); } + } - loadChallenges(content) { + showChallenges(challengeData) { + window.JSONPREQUEST = challengeData; - this.showChallenges(content) + let challenges = challengeData.challenges; - } + challenges.sort(function (a, b) { + return a["translation"]["name"] < b["translation"]["name"] + ? -1 + : +(a["translation"]["name"] > b["translation"]["name"]); + }); - changeDisplayMethod(e) { - if (e.currentTarget.id === "full" && window.compactMode === true) { - window.compactMode = false - this.showChallenges(window.JSONPREQUEST) - setCookie("filter", "false"); + let challengeObject = []; + + for (let i = 0; i < challenges.length; i++) { + const challenge = challenges[i]; + if (challenge.id < 10) { + if (challenge.id !== 0) { + continue; } - if (e.currentTarget.id === "compact" && window.compactMode === false) { - window.compactMode = true - this.showChallenges(window.JSONPREQUEST) - setCookie("filter", "true"); + } + let highestTier = "NONE", + queueIds = [], + parentId = 0, + obtainable = []; + + let ranks = config.tiers; + + for (let i2 = 1; i2 < ranks.length; i2++) { + const rank = ranks[i2]; + if (checkExists(challenge.thresholds[rank])) { + highestTier = ranks[i2]; } - } - - showChallenges(challengeData) { - window.JSONPREQUEST = challengeData - - let challenges = challengeData.challenges; - - challenges.sort(function (a, b) { - return a["translation"]["name"] < b["translation"]["name"] ? -1 : +(a["translation"]["name"] > b["translation"]["name"]) - }) - - let challengeObject = []; - - for (let i = 0; i < challenges.length; i++) { - const challenge = challenges[i]; - if (challenge.id < 10) { - if (challenge.id !== 0) { - continue - } - } - let highestTier = "NONE", - queueIds = [], - parentId = 0, - obtainable = []; - - let ranks = config.tiers - - for (let i2 = 1; i2 < ranks.length; i2++) { - const rank = ranks[i2]; - if (checkExists(challenge.thresholds[rank])) { - highestTier = ranks[i2] - } + } + + try { + if (checkExists(challenge.tags["parent"])) { + let c = 0; + let parentChallengeId = parseInt(challenge.tags["parent"]); + while (c < 10) { + let currentChallenge = getChallenge(parentChallengeId); + if ( + checkExists(currentChallenge["tags"]["parent"]) && + currentChallenge.id > 10 + ) { + parentChallengeId = parseInt( + getChallenge(parentChallengeId)["tags"]["parent"] + ); + } else { + c = 10; } - - try { - if (checkExists(challenge.tags["parent"])) { - let c = 0; - let parentChallengeId = parseInt(challenge.tags["parent"]) - while (c < 10) { - let currentChallenge = getChallenge(parentChallengeId) - if (checkExists(currentChallenge["tags"]["parent"]) && currentChallenge.id > 10) { - parentChallengeId = parseInt(getChallenge(parentChallengeId)["tags"]["parent"]) - } else { - c = 10; - } - } - parentId = getChallenge(parentChallengeId).id - } else if ([600006, 600010, 600011, 600012, 0].includes(challenge.id)) { - parentId = 600006 - } else if (checkExists(challenge.tags["isCapstone"])) { - parentId = challenge.id - } else { - parentId = 0 - } - if (challenge.tags["source"] === "CHALLENGES") { - if (this.filter.type.length > 0 && !this.filter.type.includes("progress")) { - continue - } - obtainable.push(
-

Obtainable by progressing challenges

- -
) - } - if (challenge.tags["source"] === "EOGD") { - if (this.filter.type.length > 0 && !this.filter.type.includes("ingame")) { - continue - } - // obtainable.push(
- //

Obtainable by playing games

- // - //
) - } - if (challenge.tags["source"] === "CAP_INVENTORY") { - if (this.filter.type.length > 0 && !this.filter.type.includes("inventory")) { - continue - } - obtainable.push(
-

Obtainable by collecting items

- -
) - } - if (challenge.tags["source"] === "CLASH") { - if (this.filter.type.length > 0 && !this.filter.type.includes("clash")) { - continue - } - obtainable.push(
-

Obtainable by playing clash

- Clash -
) - } - if (challenge.tags["source"] === "SUMMONER") { - if (this.filter.type.length > 0 && !this.filter.type.includes("profile")) { - continue - } - obtainable.push(
-

Obtainable by leveling up your profile

- -
) - } - if (challenge.tags["source"] === "ETERNALS") { - if (this.filter.type.length > 0 && !this.filter.type.includes("eternals")) { - continue - } - obtainable.push(
-

Obtainable by progressing eternals

- Eternals -
) - } - if (challenge.tags["source"] === "RANKED") { - if (this.filter.type.length > 0 && !this.filter.type.includes("ranked")) { - continue - } - // obtainable.push(
- //

Obtainable by playing ranked

- // - //
) - } - } catch (error) { - console.warn(error) - } - - if (challenge.queueIds.length > 0) { - let enabled = { - isAram: false, - isSR: false, - isBot: false, - isArena: false - } - - for (let i = 0; i < challenge.queueIds.length; i++) { - const queue = challenge.queueIds[i]; - if ([450, 930, 860].includes(queue)) { - enabled["isAram"] = true; - } - - if ([400, 420, 430, 440].includes(queue)) { - enabled["isSR"] = true; - } - - if ([830, 840, 850, 870, 880, 890].includes(queue)) { - enabled["isBot"] = true; - } - if ([1700, 1701, 1704].includes(queue)) { - enabled["isArena"] = true; - } - } - - if (enabled["isAram"] && enabled["isSR"] && enabled["isArena"]) { - queueIds.push(
-

All modes

- All modes -
) - } else if (enabled["isAram"] && !enabled["isSR"] && !enabled["isArena"]) { - if (this.filter.gamemode.length > 0 && !this.filter.gamemode.includes("aram")) { - continue - } - queueIds.push(
-

ARAM games only

- Aram games only -
) - } else if (!enabled["isAram"] && enabled["isSR"] && !enabled["isArena"]) { - if (this.filter.gamemode.length > 0 && !this.filter.gamemode.includes("summonersrift")) { - continue - } - queueIds.push(
-

Summoners Rift only

- Summoners Rift games only -
) - } else if (enabled["isArena"] && !enabled["isSR"] && !enabled["isAram"]) { - if (this.filter.gamemode.length > 0 && !this.filter.gamemode.includes("arena")) { - continue - } - queueIds.push(
-

Arena only

- Arena games only -
) - } else { - if (this.filter.gamemode.length > 0 && !this.filter.gamemode.includes("bot")) { - continue - } - queueIds.push(
-

Bot games only

- Bot games only -
) - } - } else if (this.filter.gamemode.length > 0) { - continue - } - - let parentName = getChallenge(parentId).translation.name; - - // Skip to next element if filter is on and not on element - if (this.filter.category.length > 0 && !this.filter.category.includes(parentId.toString())) { - if (this.filter.category.includes("seasonal-retired")) { - if (parentId !== 2022000 && parentId !== 2023000 && parentId !== 2024100) { - continue; - } - } else { - continue; - } - } - - // getChallenge(parseInt(challenge.parentCategory)) - - if (this.searchFor !== "" && (challenge.translation.name.toLowerCase().search(this.searchFor.toLowerCase()) === -1 && challenge.translation.description.toLowerCase().search(this.searchFor.toLowerCase()) === -1)) { - continue - } - - challengeObject.push( -
-

{parentName.charAt(0).toUpperCase() + parentName.slice(1).toLowerCase()} Category

- {config.images[parentId] ? {parentName.toLowerCase()} : null} -
- {queueIds} - {obtainable} - } - />) + } + parentId = getChallenge(parentChallengeId).id; + } else if ([600006, 600010, 600011, 600012, 0].includes(challenge.id)) { + parentId = 600006; + } else if (checkExists(challenge.tags["isCapstone"])) { + parentId = challenge.id; + } else { + parentId = 0; } - - document.title = "All League of Legends Challenges Overview" - - - this.setState({ - challenges: challengeObject, - }) - } - - componentDidMount() { - document.title = "Loading..." - this.applyFilterStyles(); - this.load() - } - - applyFilterStyles() { - const buttons = document.querySelectorAll('button[data-id]'); - const filters = this.filter; - if (filters == null) return; - - const dataFilters = [...filters.category, ...filters.type, ...filters.gamemode]; - - for (const button of buttons) { - const dataId = button.getAttribute("data-id"); - // Apply selected CSS if data id is found in saved filters. - if (dataFilters.includes(dataId)) button.classList.add(filterCSS["selected"]); + if (challenge.tags["source"] === "CHALLENGES") { + if ( + this.filter.type.length > 0 && + !this.filter.type.includes("progress") + ) { + continue; + } + obtainable.push( +
+

Obtainable by progressing challenges

+ +
+ ); + } + if (challenge.tags["source"] === "EOGD") { + if ( + this.filter.type.length > 0 && + !this.filter.type.includes("ingame") + ) { + continue; + } + // obtainable.push(
+ //

Obtainable by playing games

+ // + //
) + } + if (challenge.tags["source"] === "CAP_INVENTORY") { + if ( + this.filter.type.length > 0 && + !this.filter.type.includes("inventory") + ) { + continue; + } + obtainable.push( +
+

Obtainable by collecting items

+ +
+ ); + } + if (challenge.tags["source"] === "CLASH") { + if ( + this.filter.type.length > 0 && + !this.filter.type.includes("clash") + ) { + continue; + } + obtainable.push( +
+

Obtainable by playing clash

+ Clash +
+ ); + } + if (challenge.tags["source"] === "SUMMONER") { + if ( + this.filter.type.length > 0 && + !this.filter.type.includes("profile") + ) { + continue; + } + obtainable.push( +
+

Obtainable by leveling up your profile

+ +
+ ); + } + if (challenge.tags["source"] === "ETERNALS") { + if ( + this.filter.type.length > 0 && + !this.filter.type.includes("eternals") + ) { + continue; + } + obtainable.push( +
+

Obtainable by progressing eternals

+ Eternals +
+ ); + } + if (challenge.tags["source"] === "RANKED") { + if ( + this.filter.type.length > 0 && + !this.filter.type.includes("ranked") + ) { + continue; + } + // obtainable.push(
+ //

Obtainable by playing ranked

+ // + //
) + } + } catch (error) { + console.warn(error); + } + + if (challenge.queueIds.length > 0) { + let enabled = { + isAram: false, + isSR: false, + isBot: false, + isArena: false, + }; + + for (let i = 0; i < challenge.queueIds.length; i++) { + const queue = challenge.queueIds[i]; + if ([450, 930, 860].includes(queue)) { + enabled["isAram"] = true; + } + + if ([400, 420, 430, 440].includes(queue)) { + enabled["isSR"] = true; + } + + if ([830, 840, 850, 870, 880, 890].includes(queue)) { + enabled["isBot"] = true; + } + if ([1700, 1701, 1704].includes(queue)) { + enabled["isArena"] = true; + } } - } - - changeFilter(e) { - let button = e.currentTarget; - let c = button.getAttribute("data-id") - let category = this.filter[button.parentNode.getAttribute("category")]; - if (button.classList.length > 0) { - button.classList.remove(filterCSS["selected"]) - category = category.filter(x => x !== c); + if (enabled["isAram"] && enabled["isSR"] && enabled["isArena"]) { + queueIds.push( +
+

All modes

+ All modes +
+ ); + } else if ( + enabled["isAram"] && + !enabled["isSR"] && + !enabled["isArena"] + ) { + if ( + this.filter.gamemode.length > 0 && + !this.filter.gamemode.includes("aram") + ) { + continue; + } + queueIds.push( +
+

ARAM games only

+ Aram games only +
+ ); + } else if ( + !enabled["isAram"] && + enabled["isSR"] && + !enabled["isArena"] + ) { + if ( + this.filter.gamemode.length > 0 && + !this.filter.gamemode.includes("summonersrift") + ) { + continue; + } + queueIds.push( +
+

Summoners Rift only

+ Summoners Rift games only +
+ ); + } else if ( + enabled["isArena"] && + !enabled["isSR"] && + !enabled["isAram"] + ) { + if ( + this.filter.gamemode.length > 0 && + !this.filter.gamemode.includes("arena") + ) { + continue; + } + queueIds.push( +
+

Arena only

+ Arena games only +
+ ); } else { - button.classList.add(filterCSS["selected"]) - category.push(c) + if ( + this.filter.gamemode.length > 0 && + !this.filter.gamemode.includes("bot") + ) { + continue; + } + queueIds.push( +
+

Bot games only

+ Bot games only +
+ ); } - - switch (button.parentNode.getAttribute("category")) { - case "category": - this.filter = { - "category": category, - "type": this.filter.type, - "gamemode": this.filter.gamemode - } - - break; - case "type": - this.filter = { - "type": category, - "category": this.filter.category, - "gamemode": this.filter.gamemode - } - - break; - default: - this.filter = { - "gamemode": category, - "type": this.filter.type, - "category": this.filter.category - } - - break; + } else if (this.filter.gamemode.length > 0) { + continue; + } + + let parentName = getChallenge(parentId).translation.name; + + // Skip to next element if filter is on and not on element + if ( + this.filter.category.length > 0 && + !this.filter.category.includes(parentId.toString()) + ) { + if (this.filter.category.includes("seasonal-retired")) { + if ( + parentId !== 2022000 && + parentId !== 2023000 && + parentId !== 2024100 + ) { + continue; + } + } else { + continue; + } + } + + // getChallenge(parseInt(challenge.parentCategory)) + + const search = this.searchFor; + let found = search === ""; + const searchSplit = search.split(","); + for (let i = 0; i < searchSplit.length; i++) { + const key = searchSplit[i].trim(); + if ( + key !== "" && + (challenge.translation.name.toLowerCase().search(key.toLowerCase()) > + -1 || + challenge.translation.description + .toLowerCase() + .search(key.toLowerCase()) > -1) + ) { + found = true; } - setStorage(storageKeys.challengeFilters, this.filter); - this.showChallenges(window.JSONPREQUEST) + } + if (!found) continue; + + challengeObject.push( + +
+

+ {parentName.charAt(0).toUpperCase() + + parentName.slice(1).toLowerCase()}{" "} + Category +

+ {config.images[parentId] ? ( + {parentName.toLowerCase()} + ) : null} +
+ {queueIds} + {obtainable} + + } + /> + ); } - search(e) { - const value = e.target.value; - this.searchFor = value; - setStorage(storageKeys.challengeSearch, value) - this.showChallenges(window.JSONPREQUEST) + document.title = "All League of Legends Challenges Overview"; + + this.setState({ + challenges: challengeObject, + }); + } + + componentDidMount() { + document.title = "Loading..."; + this.applyFilterStyles(); + this.load(); + } + + applyFilterStyles() { + const buttons = document.querySelectorAll("button[data-id]"); + const filters = this.filter; + if (filters == null) return; + + const dataFilters = [ + ...filters.category, + ...filters.type, + ...filters.gamemode, + ]; + + for (const button of buttons) { + const dataId = button.getAttribute("data-id"); + // Apply selected CSS if data id is found in saved filters. + if (dataFilters.includes(dataId)) + button.classList.add(filterCSS["selected"]); + } + } + + changeFilter(e) { + let button = e.currentTarget; + let c = button.getAttribute("data-id"); + + let category = this.filter[button.parentNode.getAttribute("category")]; + if (button.classList.length > 0) { + button.classList.remove(filterCSS["selected"]); + category = category.filter((x) => x !== c); + } else { + button.classList.add(filterCSS["selected"]); + category.push(c); } - render() { - const t = this.state.translation - - if (document.location.pathname.slice(-1) === "/") { - return - } - - return + switch (button.parentNode.getAttribute("category")) { + case "category": + this.filter = { + category: category, + type: this.filter.type, + gamemode: this.filter.gamemode, + }; + + break; + case "type": + this.filter = { + type: category, + category: this.filter.category, + gamemode: this.filter.gamemode, + }; + + break; + default: + this.filter = { + gamemode: category, + type: this.filter.type, + category: this.filter.category, + }; + + break; + } + setStorage(storageKeys.challengeFilters, this.filter); + this.showChallenges(window.JSONPREQUEST); + } + + search(e) { + const value = e.target.value; + this.searchFor = value; + setStorage(storageKeys.challengeSearch, value); + this.showChallenges(window.JSONPREQUEST); + } + + render() { + const t = this.state.translation; + + if (document.location.pathname.slice(-1) === "/") { + return ; + } -
-

{t("List of all Challenges")}

-

{t("Overview and how to obtain them")}

+ return ( + +
+

{t("List of all Challenges")}

+

{t("Overview and how to obtain them")}

+
+ + {this.state.showEvent ? ( +
+ + {this.state.event} +
+ ) : null} + +
+
+
+ + +
+

Filter (multiple choices)

+
+

{t("Category")}

+ + + + + + + +
- {this.state.showEvent ? -
- - {this.state.event} -
- : null} - -
- -
-
- - -
-

Filter (multiple choices)

-
-

{t("Category")}

- - - - - - - - -
- -
-

{t("Type")}

- - - - - - - -
- -
-

{t("Gamemode")}

- - - - -
-
+
+

{t("Type")}

+ + + + + + +
-
- - { - this.state.challenges.length > 0 - ? this.state.challenges - :

Is it a bug? Is it a feature?

No! There are just no challenges within the current filters.
Maybe the challenges aren't released yet?

- } - {this.state.showEvent ? null : } + +
+

{t("Gamemode")}

+ + + +
- - } +
+
+
+ + {this.state.challenges.length > 0 ? ( + this.state.challenges + ) : ( +

+ Is it a bug? Is it a feature? +
+
+ No! There are just no challenges within the current filters. +
+ Maybe the challenges aren't released yet? +

+ )} + {this.state.showEvent ? null : ( + + )} +
+ + ); + } } -export default withTranslation()(Challenges) \ No newline at end of file +export default withTranslation()(Challenges); diff --git a/src/module/user/orderChallenges.js b/src/module/user/orderChallenges.js index 0e75fbd..fd9bc41 100644 --- a/src/module/user/orderChallenges.js +++ b/src/module/user/orderChallenges.js @@ -1,344 +1,437 @@ -import { checkExists } from "../../func/arrayManipulationFunctions.js" +import { checkExists } from "../../func/arrayManipulationFunctions.js"; import getChallenge from "../../func/getChallenge"; import { intToTier, tierToInt } from "../../func/tierFunctions"; function escapeRegExp(string) { - return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string } export function getNextLevel(current, masterOnly = false, pointsOnly = false) { - if (masterOnly === true) { - return "MASTER"; - } - let ranks = ["NONE", "IRON", "BRONZE", "SILVER", "GOLD", "PLATINUM", "DIAMOND", "MASTER", "GRANDMASTER", "CHALLENGER"] - for (let i = 0; i < ranks.length; i++) { - if (current === ranks[i]) { - if (ranks[i] === "CHALLENGER") { return "CHALLENGER" } - return ranks[i + 1]; - } - + if (masterOnly === true) { + return "MASTER"; + } + let ranks = [ + "NONE", + "IRON", + "BRONZE", + "SILVER", + "GOLD", + "PLATINUM", + "DIAMOND", + "MASTER", + "GRANDMASTER", + "CHALLENGER", + ]; + for (let i = 0; i < ranks.length; i++) { + if (current === ranks[i]) { + if (ranks[i] === "CHALLENGER") { + return "CHALLENGER"; + } + return ranks[i + 1]; } + } } - - -export function removeUnnecessaryChallenges(challengesArray, filters, masterOnly = false, pointsOnly = false, search = "", capstones = true) { - - let challenges = {}; - challengesArray.forEach(challenge => { - challenges[challenge[0]] = challenge; - }) - - if (search.length > 0) { - search = escapeRegExp((' ' + search).slice(1)); - } - - const challengesDetailed = (window.JSONPREQUEST || { challenges: [] }).challenges; - - return challengesDetailed.map(function (challengeData) { - - let challenge = [challengeData.id, 0, 0, 0, 0, [1]]; - - if (challenges[challengeData.id]) { - challenge = challenges[challengeData.id] +export function removeUnnecessaryChallenges( + challengesArray, + filters, + masterOnly = false, + pointsOnly = false, + search = "", + capstones = true +) { + let challenges = {}; + challengesArray.forEach((challenge) => { + challenges[challenge[0]] = challenge; + }); + + if (search.length > 0) { + search = escapeRegExp((" " + search).slice(1)); + } + + const challengesDetailed = (window.JSONPREQUEST || { challenges: [] }) + .challenges; + + return challengesDetailed + .map(function (challengeData) { + let challenge = [challengeData.id, 0, 0, 0, 0, [1]]; + + if (challenges[challengeData.id]) { + challenge = challenges[challengeData.id]; + } + + if (capstones === false) { + if (challengeData.tags.isCapstone === "Y") { + return null; } + } - if (capstones === false) { - if (challengeData.tags.isCapstone === "Y") { - return null; - } - } + if (challengeData === 0) { + return null; + } - if (challengeData === 0) { - return null - } + let pushLater = "none"; - let pushLater = "none" + challenge.push(challengeData); - challenge.push(challengeData) + if (!checkExists(challengeData.queueIds)) { + console.log(challengeData); + } - if (!checkExists(challengeData.queueIds)) { - console.log(challengeData) - } + if (challengeData.queueIds.length > 0) { + for (let i = 0; i < challengeData.queueIds.length; i++) { + const queue = challengeData.queueIds[i]; - if (challengeData.queueIds.length > 0) { - for (let i = 0; i < challengeData.queueIds.length; i++) { - - const queue = challengeData.queueIds[i]; - - if ([450, 930, 860].includes(queue)) { - pushLater = "aram" - if (filters.gamemode.length > 0 && !filters.gamemode.includes("aram")) { - return null - } - } - - if ([400, 420, 430, 440].includes(queue)) { - pushLater = "summonersrift" - if (filters.gamemode.length > 0 && !filters.gamemode.includes("summonersrift")) { - return null - } - } - - if ([830, 840, 850, 870, 880, 890].includes(queue)) { - pushLater = "bot" - if (filters.gamemode.length > 0 && !filters.gamemode.includes("bot")) { - return null - } - } - - if ([1700, 1701, 1704].includes(queue)) { - pushLater = "arena" - if (filters.gamemode.length > 0 && !filters.gamemode.includes("arena")) { - return null - } - } + if ([450, 930, 860].includes(queue)) { + pushLater = "aram"; + if ( + filters.gamemode.length > 0 && + !filters.gamemode.includes("aram") + ) { + return null; } - } else if (filters.gamemode.length > 0) { - if (filters.gamemode.includes("aram") && [101000, 101300, 101200, 101100].includes(challengeData.id)) { - pushLater = "aram" - } else { - return null + } + + if ([400, 420, 430, 440].includes(queue)) { + pushLater = "summonersrift"; + if ( + filters.gamemode.length > 0 && + !filters.gamemode.includes("summonersrift") + ) { + return null; } - } - - let parentId = 0 - - if (checkExists(challengeData.tags["parent"])) { - let iterationCount = 0; - let parentChallengeId = parseInt(challengeData.tags["parent"]) - while (iterationCount < 10) { - let currentChallenge = getChallenge(parentChallengeId) - if (checkExists(currentChallenge["tags"]["parent"]) && currentChallenge.id > 10) { - parentChallengeId = parseInt(getChallenge(parentChallengeId)["tags"]["parent"]) - } else { - iterationCount = 10; - } + } + + if ([830, 840, 850, 870, 880, 890].includes(queue)) { + pushLater = "bot"; + if ( + filters.gamemode.length > 0 && + !filters.gamemode.includes("bot") + ) { + return null; } - parentId = getChallenge(parentChallengeId).id - } else if ([600006, 600010, 600011, 600012, 0].includes(challengeData.id)) { - parentId = 600006 - } else if (checkExists(challengeData.tags["isCapstone"])) { - parentId = challengeData.id + } + + if ([1700, 1701, 1704].includes(queue)) { + pushLater = "arena"; + if ( + filters.gamemode.length > 0 && + !filters.gamemode.includes("arena") + ) { + return null; + } + } + } + } else if (filters.gamemode.length > 0) { + if ( + filters.gamemode.includes("aram") && + [101000, 101300, 101200, 101100].includes(challengeData.id) + ) { + pushLater = "aram"; } else { - parentId = 0 + return null; } - - challenge.push(pushLater) - - if (filters.category.length > 0 && !filters.category.includes(parentId.toString())) { - if (filters.category.includes("retired-seasonal")) { - if (parentId !== 2022000 && parentId !== 2023000 && parentId !== 2024100) { - return null - } - } else { - return null - } + } + + let parentId = 0; + + if (checkExists(challengeData.tags["parent"])) { + let iterationCount = 0; + let parentChallengeId = parseInt(challengeData.tags["parent"]); + while (iterationCount < 10) { + let currentChallenge = getChallenge(parentChallengeId); + if ( + checkExists(currentChallenge["tags"]["parent"]) && + currentChallenge.id > 10 + ) { + parentChallengeId = parseInt( + getChallenge(parentChallengeId)["tags"]["parent"] + ); + } else { + iterationCount = 10; + } } - if (filters.category.length === 0) { - if (parentId === 2022000 || parentId === 2023000) { - return null - } + parentId = getChallenge(parentChallengeId).id; + } else if ( + [600006, 600010, 600011, 600012, 0].includes(challengeData.id) + ) { + parentId = 600006; + } else if (checkExists(challengeData.tags["isCapstone"])) { + parentId = challengeData.id; + } else { + parentId = 0; + } + + challenge.push(pushLater); + + if ( + filters.category.length > 0 && + !filters.category.includes(parentId.toString()) + ) { + if (filters.category.includes("retired-seasonal")) { + if ( + parentId !== 2022000 && + parentId !== 2023000 && + parentId !== 2024100 + ) { + return null; + } + } else { + return null; } + } + if (filters.category.length === 0) { + if (parentId === 2022000 || parentId === 2023000) { + return null; + } + } - challenge.push(parentId) - - if (pointsOnly === true) { - if (challenge[1] >= 7) { - return null; - } - if (challenge[8] === 600006 || challenge[8].parent === 2022000 || challenge[8] === 2023000 || challenge[8] === 2024100 || challenge[8] === 2024200) { - return null; - } + challenge.push(parentId); - let challengeData = getChallenge(challenge[0]); + if (pointsOnly === true) { + if (challenge[1] >= 7) { + return null; + } + if ( + challenge[8] === 600006 || + challenge[8].parent === 2022000 || + challenge[8] === 2023000 || + challenge[8] === 2024100 || + challenge[8] === 2024200 + ) { + return null; + } - let highestThreshold = 0; - Object.keys(challengeData.thresholds).forEach((threshold) => { - const t = tierToInt(threshold); - if (t > highestThreshold) { - highestThreshold = t; - } - }); + let challengeData = getChallenge(challenge[0]); - if (challenge[1] === highestThreshold) { - return null - } + let highestThreshold = 0; + Object.keys(challengeData.thresholds).forEach((threshold) => { + const t = tierToInt(threshold); + if (t > highestThreshold) { + highestThreshold = t; + } + }); + if (challenge[1] === highestThreshold) { + return null; } - - if (search !== "" && (challenge[6].translation.name.toLowerCase().search(search.toLowerCase()) === -1 && challenge[6].translation.description.toLowerCase().search(search.toLowerCase()) === -1)) { - return null + } + + let found = search === ""; + const searchSplit = search.split(","); + for (let i = 0; i < searchSplit.length; i++) { + const key = searchSplit[i].trim(); + if ( + key !== "" && + (challenge[6].translation.name + .toLowerCase() + .search(key.toLowerCase()) > -1 || + challenge[6].translation.description + .toLowerCase() + .search(key.toLowerCase()) > -1) + ) { + found = true; } + } + if (!found) return null; - return challenge - })?.filter(x => x !== null) - + return challenge; + }) + ?.filter((x) => x !== null); } +export default function orderChallenges( + challenges, + filter, + extraFilter, + masterOnly, + pointsOnly, + search, + capstones +) { + challenges = removeUnnecessaryChallenges( + challenges, + extraFilter, + masterOnly, + pointsOnly, + search, + !capstones + ); + + let sortAlgorithm = function (a, b) { + const aTier = a[1]; + + const bTier = b[1]; + + if (aTier === bTier) { + if (a[5][0] === b[5][0]) { + if (a[5].length === 1) { + return 1; + } else { + if (a[5].length === 1) { + return -1; + } else { + return a[5][1] < b[5][1] ? -1 : 1; + } + } + } else { + return a[5][0] > b[5][0] ? 1 : -1; + } + } + return aTier > bTier ? -1 : 1; + }; -export default function orderChallenges(challenges, filter, extraFilter, masterOnly, pointsOnly, search, capstones) { + switch (filter) { + case "level": + break; - challenges = removeUnnecessaryChallenges(challenges, extraFilter, masterOnly, pointsOnly, search, !capstones) + case "levelup": + sortAlgorithm = function (a, b) { + let nextLevelA, nextLevelB; + const tierA = intToTier(a[1]); + const tierB = intToTier(b[1]); - let sortAlgorithm = function (a, b) { + let challenge = a[6]; - const aTier = a[1] + if ( + checkExists(challenge["thresholds"][getNextLevel(tierA, masterOnly)]) + ) { + nextLevelA = challenge["thresholds"][getNextLevel(tierA, masterOnly)]; + } else { + nextLevelA = challenge["thresholds"][tierA] + ? challenge["thresholds"][tierA] + : 1; + } + if (challenge.tier === "CHALLENGER" && challenge.leaderboard === true) { + nextLevelA = challenge["leaderboardThresholds"][0] ?? 0; + } - const bTier = b[1] + nextLevelA = a[2] / nextLevelA; - if (aTier === bTier) { - if (a[5][0] === b[5][0]) { - if (a[5].length === 1) { - return 1; - } else { - if (a[5].length === 1) { - return -1 - } else { - return a[5][1] < b[5][1] ? -1 : 1 - } - } - } else { - return a[5][0] > b[5][0] ? 1 : -1 - } + challenge = b[6]; + + if ( + checkExists(challenge["thresholds"][getNextLevel(tierB, masterOnly)]) + ) { + nextLevelB = challenge["thresholds"][getNextLevel(tierB, masterOnly)]; + } else { + nextLevelB = challenge["thresholds"][tierB] + ? challenge["thresholds"][tierB] + : 1; } - return aTier > bTier ? -1 : 1 - } + if (challenge.tier === "CHALLENGER" && challenge.leaderboard === true) { + nextLevelB = challenge["leaderboardThresholds"][0] ?? 0; + } + nextLevelB = b[2] / nextLevelB; - switch (filter) { - case 'level': - break; - - case 'levelup': - sortAlgorithm = function (a, b) { - let nextLevelA, nextLevelB - const tierA = intToTier(a[1]) - const tierB = intToTier(b[1]) - - let challenge = a[6]; - - if (checkExists(challenge["thresholds"][getNextLevel(tierA, masterOnly)])) { - nextLevelA = challenge["thresholds"][getNextLevel(tierA, masterOnly)] - } else { - nextLevelA = challenge["thresholds"][tierA] ? challenge["thresholds"][tierA] : 1; - } - if (challenge.tier === "CHALLENGER" && challenge.leaderboard === true) { - nextLevelA = challenge["leaderboardThresholds"][0] ?? 0 - } - - nextLevelA = a[2] / nextLevelA; - - challenge = b[6]; - - if (checkExists(challenge["thresholds"][getNextLevel(tierB, masterOnly)])) { - nextLevelB = challenge["thresholds"][getNextLevel(tierB, masterOnly)] - } else { - nextLevelB = challenge["thresholds"][tierB] ? challenge["thresholds"][tierB] : 1; - } - if (challenge.tier === "CHALLENGER" && challenge.leaderboard === true) { - nextLevelB = challenge["leaderboardThresholds"][0] ?? 0 - } - nextLevelB = b[2] / nextLevelB; - - if (nextLevelA >= 1 && nextLevelB >= 1) { - return 0 - } - - if (nextLevelA >= 1) { - return 5 - } - if (nextLevelB >= 1) { - return -5 - } - - if (Math.round(nextLevelA * 50) === Math.round(nextLevelB * 50)) { - return a[0] > b[0] ? -1 : 1 - } - - return Math.round(nextLevelA * 50) > Math.round(nextLevelB * 50) ? -1 : 1 + if (nextLevelA >= 1 && nextLevelB >= 1) { + return 0; + } - } - break; + if (nextLevelA >= 1) { + return 5; + } + if (nextLevelB >= 1) { + return -5; + } - case "alphabetic-a-z": - sortAlgorithm = function (a, b) { - const challenge = [a[6], b[6]] - return challenge[0]["translation"]["name"] < challenge[1]["translation"]["name"] ? -1 : +(challenge[0]["translation"]["name"] > challenge[1]["translation"]["name"]) - } - break; + if (Math.round(nextLevelA * 50) === Math.round(nextLevelB * 50)) { + return a[0] > b[0] ? -1 : 1; + } - case "alphabetic-z-a": - sortAlgorithm = function (a, b) { - const challenge = [a[6], b[6]] - return challenge[0]["translation"]["name"] > challenge[1]["translation"]["name"] ? -1 : +(challenge[0]["translation"]["name"] > challenge[1]["translation"]["name"]) - } - break; - - case "percentile": - sortAlgorithm = function (a, b) { - - if (a[5][0] === b[5][0]) { - // I DON'T LIKE THE SOLUTION, BUT IT WORKS - const cA = a[6] - - let positionA = 1e100, previousPositionA - - if (cA.leaderboard === true && a[5].length > 1) { - switch (intToTier(a[1])) { - case "GRANDMASTER": - previousPositionA = cA["leaderboardThresholds"][3] ?? 1 - break; - case "MASTER": - previousPositionA = cA["leaderboardThresholds"][5] ?? 1 - break; - default: - previousPositionA = 1 - break; - } - positionA = (previousPositionA - 1) + a[5][1] - } - - - - - const cB = b[6] - - let positionB = 1e100, previousPositionB - - if (cB.leaderboard === true && b[5].length > 1) { - switch (intToTier(b[1])) { - case "GRANDMASTER": - previousPositionB = cB["leaderboardThresholds"][3] ?? 1 - break; - case "MASTER": - previousPositionB = cB["leaderboardThresholds"][5] ?? 1 - break; - default: - previousPositionB = 1 - break; - } - positionB = (previousPositionB - 1) + b[5][1] - } - - - return positionA < positionB ? -1 : 1 - } - return a[5][0] < b[5][0] ? -1 : 1 + return Math.round(nextLevelA * 50) > Math.round(nextLevelB * 50) + ? -1 + : 1; + }; + break; + + case "alphabetic-a-z": + sortAlgorithm = function (a, b) { + const challenge = [a[6], b[6]]; + return challenge[0]["translation"]["name"] < + challenge[1]["translation"]["name"] + ? -1 + : +( + challenge[0]["translation"]["name"] > + challenge[1]["translation"]["name"] + ); + }; + break; + + case "alphabetic-z-a": + sortAlgorithm = function (a, b) { + const challenge = [a[6], b[6]]; + return challenge[0]["translation"]["name"] > + challenge[1]["translation"]["name"] + ? -1 + : +( + challenge[0]["translation"]["name"] > + challenge[1]["translation"]["name"] + ); + }; + break; + + case "percentile": + sortAlgorithm = function (a, b) { + if (a[5][0] === b[5][0]) { + // I DON'T LIKE THE SOLUTION, BUT IT WORKS + const cA = a[6]; + + let positionA = 1e100, + previousPositionA; + + if (cA.leaderboard === true && a[5].length > 1) { + switch (intToTier(a[1])) { + case "GRANDMASTER": + previousPositionA = cA["leaderboardThresholds"][3] ?? 1; + break; + case "MASTER": + previousPositionA = cA["leaderboardThresholds"][5] ?? 1; + break; + default: + previousPositionA = 1; + break; } - break; - - case "timestamp": - sortAlgorithm = function (a, b) { - if (a[4] === b[4]) { - return 0 - } else { - return a[4] > b[4] ? -1 : 1 - } + positionA = previousPositionA - 1 + a[5][1]; + } + + const cB = b[6]; + + let positionB = 1e100, + previousPositionB; + + if (cB.leaderboard === true && b[5].length > 1) { + switch (intToTier(b[1])) { + case "GRANDMASTER": + previousPositionB = cB["leaderboardThresholds"][3] ?? 1; + break; + case "MASTER": + previousPositionB = cB["leaderboardThresholds"][5] ?? 1; + break; + default: + previousPositionB = 1; + break; } - break; - default: - throw new Error(`Unknown Filter ${filter}`); - } + positionB = previousPositionB - 1 + b[5][1]; + } - return challenges.sort(sortAlgorithm) + return positionA < positionB ? -1 : 1; + } + return a[5][0] < b[5][0] ? -1 : 1; + }; + break; + + case "timestamp": + sortAlgorithm = function (a, b) { + if (a[4] === b[4]) { + return 0; + } else { + return a[4] > b[4] ? -1 : 1; + } + }; + break; + default: + throw new Error(`Unknown Filter ${filter}`); + } -} \ No newline at end of file + return challenges.sort(sortAlgorithm); +}