+ res.sendFile(__dirname + "/html/" + file);
+ });
+}
+
+async function riotRequest(region, url, params, retries) {
+ if (retries < 1) throw "Error too many tries";
+ let req = "https://" + regions[region] + url + "?api_key=" + key;
+ for (p in params) {
+ req += "&" + p + "=" + params[p];
+ }
+ try {
+ let result = await request({uri: req, resolveWithFullResponse: true, json: true});
+ let appRateLimit = result.headers["x-app-rate-limit"];
+ let appRateLimitCount = result.headers["x-app-rate-limit-count"];
+ appRateLimit1 = appRateLimit.split(",")[0].split(":")[0];
+ appRateLimit120 = appRateLimit.split(",")[1].split(":")[0];
+ appRateLimitCount1 = appRateLimitCount.split(",")[0].split(":")[0];
+ appRateLimitCount120 = appRateLimitCount.split(",")[1].split(":")[0];
+ let delay1 = 1000 / (appRateLimit1 - 1);
+ let delay120 = 120000 / (appRateLimit120 - 1);
+ await sleep(Math.max(delay1, delay120));
+ console.log(Math.max(delay1, delay120));
+ return result.body;
+ } catch (err) {
+ console.log(err.message);
+ return await riotRequest(region, url, params, retries - 1);
+ }
+}
+
+async function getAllMatches(region, accountId) {
+ let matches = [];
+ let totalGames;
+ let bI = 0, eI = 99;
+
+ do {
+ let m = await riotRequest(region, "/lol/match/v4/matchlists/by-account/" + accountId, { beginIndex: bI, endIndex: eI }, 5);
+ console.log(m);
+ totalGames = m.totalGames;
+ matches = matches.concat(m.matches);
+ console.log("Added games " + bI + " to " + eI + ", " + matches.length + " of " + totalGames);
+ bI = eI + 1;
+ eI += 100;
+ } while (bI <= totalGames);
+
+ return matches;
+}
+
+// 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;
+function getChampions(cb) {
+ request("http://ddragon.leagueoflegends.com/cdn/" + patch + "/data/en_US/champion.json", (err, res, body) => {
+ champions = JSON.parse(body).data;
+ cb();