X-Git-Url: https://gitweb.ps.run/lolstats/blobdiff_plain/52d1869f8b5510dab338fcabcefa48b86980a9f1..836aefac5aba1a288561cf1bd5a2cdb9a060097e:/index.js diff --git a/index.js b/index.js index e9cc8c5..2fe7fc3 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,21 @@ for (i in rules) { }); } +const regions = { + "BR": "br1.api.riotgames.com", + "EUNE": "eun1.api.riotgames.com", + "EUW": "euw1.api.riotgames.com", + "JP": "jp1.api.riotgames.com", + "KR": "kr.api.riotgames.com", + "LAN": "la1.api.riotgames.com", + "LAS": "la2.api.riotgames.com", + "NA": "na1.api.riotgames.com", + "OCE": "oc1.api.riotgames.com", + "TR": "tr1.api.riotgames.com", + "RU": "ru.api.riotgames.com", + "PBE": "pbe1.api.riotgames.com", +}; + async function riotRequest(region, url, params, retries) { if (retries < 1) throw "Error too many tries"; let req = "https://" + regions[region] + url + "?api_key=" + key; @@ -71,28 +86,14 @@ async function getAllMatches(region, accountId) { // Static Data // ----------- - -const regions = { - "BR": "br1.api.riotgames.com", - "EUNE": "eun1.api.riotgames.com", - "EUW": "euw1.api.riotgames.com", - "JP": "jp1.api.riotgames.com", - "KR": "kr.api.riotgames.com", - "LAN": "la1.api.riotgames.com", - "LAS": "la2.api.riotgames.com", - "NA": "na1.api.riotgames.com", - "OCE": "oc1.api.riotgames.com", - "TR": "tr1.api.riotgames.com", - "RU": "ru.api.riotgames.com", - "PBE": "pbe1.api.riotgames.com", -}; -app.get("/lol/regions", (req, res) => { - res.send(JSON.stringify(regions)); -}); let champions = null; +let champLookup = {}; function getChampions(cb) { request("http://ddragon.leagueoflegends.com/cdn/" + patch + "/data/en_US/champion.json", (err, res, body) => { champions = JSON.parse(body).data; + for (c in champions) { + champLookup[champions[c].key] = c; + } cb(); }); } @@ -104,6 +105,14 @@ app.get("/lol/champions", (req, res) => { else res.send(JSON.stringify(Object.keys(champions))); }); +app.get("/lol/champlookup", (req, res) => { + if (champions == null) + getChampions(() => { + res.send(JSON.stringify(champLookup)); + }); + else + res.send(JSON.stringify(champLookup)); +}); let users = {}; if (fs.existsSync("users.js")) { fs.readFile("users.js", (err, data) => { @@ -124,9 +133,9 @@ app.get("/lol/matches", async (req, res) => { let matches = await getAllMatches(region, accountId); users[accountId] = matches; fs.writeFile("users.js", JSON.stringify(users), (err) => { - console.log("Error writing file: " + err); + if (err) console.log("Error writing file: " + err); }); - res.send(matches); + res.send(JSON.stringify(matches)); } catch (err) { console.log(err); }