Get Champion by key or by ID with the latest DDragon version (Javascript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let championByIdCache = {}; | |
let championJson = {}; | |
async function getLatestChampionDDragon(language = "en_US") { | |
if (championJson[language]) | |
return championJson[language]; | |
let response; | |
let versionIndex = 0; | |
do { // I loop over versions because 9.22.1 is broken | |
const version = (await fetch("http://ddragon.leagueoflegends.com/api/versions.json").then(async(r) => await r.json()))[versionIndex++]; | |
response = await fetch(`https://ddragon.leagueoflegends.com/cdn/${version}/data/${language}/champion.json`); | |
} | |
while (!response.ok) | |
championJson[language] = await response.json(); | |
return championJson[language]; | |
} | |
async function getChampionByKey(key, language = "en_US") { | |
// Setup cache | |
if (!championByIdCache[language]) { | |
let json = await getLatestChampionDDragon(language); | |
championByIdCache[language] = {}; | |
for (var championName in json.data) { | |
if (!json.data.hasOwnProperty(championName)) | |
continue; | |
const champInfo = json.data[championName]; | |
championByIdCache[language][champInfo.key] = champInfo; | |
} | |
} | |
return championByIdCache[language][key]; | |
} | |
// NOTE: IN DDRAGON THE ID IS THE CLEAN NAME!!! It's also super-inconsistent, and broken at times. | |
// Cho'gath => Chogath, Wukong => Monkeyking, Fiddlesticks => Fiddlesticks/FiddleSticks (depending on what mood DDragon is in this patch) | |
async function getChampionByID(name, language = "en_US") { | |
return await getLatestChampionDDragon(language)[name]; | |
} | |
async function main() { | |
const annie = await getChampionByKey(1, "en_US"); | |
const leona = await getChampionByKey(89, "es_ES"); | |
const brand = await getChampionByID("brand"); | |
console.log(annie); | |
console.log(leona); | |
console.log(brand); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So helpful! <3