]> gitweb.ps.run Git - lolstats/blobdiff - index.js
changes
[lolstats] / index.js
index e9cc8c5a6c648f1b779b1188c82e5b666f19d3bf..2fe7fc35b9cd54cde109bd18507ca5fee2f3f42a 100644 (file)
--- 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);
   }