]> gitweb.ps.run Git - lolstats/blob - index.js
changes
[lolstats] / index.js
1 const express = require("express")
2 const fs = require("fs")
3 const request = require("request-promise-native")
4 const app = express()
5 const port = 3000
6 const sleep = require('util').promisify(setTimeout)
7
8 const patch = "8.24.1";
9 const key = "RGAPI-c6cae96a-c4b0-4842-9017-ddd736f3f628";
10 let appRateLimit1;
11 let appRateLimit120;
12 let appRateLimitCount1;
13 let appRateLimitCount120;
14
15 let rules = [
16   ["/lol", "index.html"],
17   ["/lol/script.js", "script.js"],
18   ["/lol/style.css", "style.css"],
19 ];
20
21 for (i in rules) {
22   let path = rules[i][0];
23   let file = rules[i][1];
24   app.get(path, (req, res) => {
25     res.sendFile(__dirname + "/html/" + file);
26   });
27 }
28
29 const regions = {
30   "BR": "br1.api.riotgames.com",
31   "EUNE": "eun1.api.riotgames.com",
32   "EUW": "euw1.api.riotgames.com",
33   "JP": "jp1.api.riotgames.com",
34   "KR": "kr.api.riotgames.com",
35   "LAN": "la1.api.riotgames.com",
36   "LAS": "la2.api.riotgames.com",
37   "NA": "na1.api.riotgames.com",
38   "OCE": "oc1.api.riotgames.com",
39   "TR": "tr1.api.riotgames.com",
40   "RU": "ru.api.riotgames.com",
41   "PBE": "pbe1.api.riotgames.com",
42 }; 
43
44 async function riotRequest(region, url, params, retries) {
45   if (retries < 1) throw "Error too many tries";
46   let req = "https://" + regions[region] + url + "?api_key=" + key;
47   for (p in params) {
48     req += "&" + p + "=" + params[p];
49   }
50   try {
51     let result = await request({uri: req, resolveWithFullResponse: true, json: true});
52     let appRateLimit = result.headers["x-app-rate-limit"];
53     let appRateLimitCount = result.headers["x-app-rate-limit-count"];
54     appRateLimit1 = appRateLimit.split(",")[0].split(":")[0];
55     appRateLimit120 = appRateLimit.split(",")[1].split(":")[0];
56     appRateLimitCount1 = appRateLimitCount.split(",")[0].split(":")[0];
57     appRateLimitCount120 = appRateLimitCount.split(",")[1].split(":")[0];
58     let delay1 = 1000 / (appRateLimit1 - 1);
59     let delay120 = 120000 / (appRateLimit120 - 1);
60     await sleep(Math.max(delay1, delay120));
61     console.log(Math.max(delay1, delay120));
62     return result.body;
63   } catch (err) {
64     console.log(err.message);
65     return await riotRequest(region, url, params, retries - 1);
66   }
67 }
68
69 async function getAllMatches(region, accountId) {
70   let matches = [];
71   let totalGames;
72   let bI = 0, eI = 99;
73
74   do {
75     let m = await riotRequest(region, "/lol/match/v4/matchlists/by-account/" + accountId, { beginIndex: bI, endIndex: eI }, 5);
76     console.log(m);
77     totalGames = m.totalGames;
78     matches = matches.concat(m.matches);
79     console.log("Added games " + bI + " to " + eI + ", " + matches.length + " of " + totalGames);
80     bI = eI + 1;
81     eI += 100;
82   } while (bI <= totalGames);
83
84   return matches;
85 }
86
87 // Static Data
88 // -----------
89 let champions = null;
90 let champLookup = {};
91 function getChampions(cb) {
92   request("http://ddragon.leagueoflegends.com/cdn/" + patch + "/data/en_US/champion.json", (err, res, body) => {
93     champions = JSON.parse(body).data;
94     for (c in champions) {
95       champLookup[champions[c].key] = c;
96     }
97     cb();
98   });
99 }
100 app.get("/lol/champions", (req, res) => {
101   if (champions == null)
102     getChampions(() => {
103       res.send(JSON.stringify(Object.keys(champions)));
104     });
105   else
106     res.send(JSON.stringify(Object.keys(champions)));
107 });
108 app.get("/lol/champlookup", (req, res) => {
109   if (champions == null)
110     getChampions(() => {
111       res.send(JSON.stringify(champLookup));
112     });
113   else
114     res.send(JSON.stringify(champLookup));
115 });
116 let users = {};
117 if (fs.existsSync("users.js")) {
118   fs.readFile("users.js", (err, data) => {
119     users = JSON.parse(data);
120   });
121 }
122 app.get("/lol/matches", async (req, res) => {
123   let region = req.query.region;
124   let summoner = req.query.summoner;
125   let regionUrl = regions[region];
126   try {
127     let data = await riotRequest(region, "/lol/summoner/v4/summoners/by-name/" + summoner, {}, 5);
128     let accountId = data.accountId;
129     if (users[accountId]) {
130       res.send(JSON.stringify(users[accountId]));
131       return;
132     }
133     let matches = await getAllMatches(region, accountId);
134     users[accountId] = matches;
135     fs.writeFile("users.js", JSON.stringify(users), (err) => {
136       if (err) console.log("Error writing file: " + err);
137     });
138     res.send(JSON.stringify(matches));
139   } catch (err) {
140     console.log(err);
141   }
142 });
143
144 // -----------
145
146 app.listen(port, () => {
147   console.log("Listening on port %d", port)
148 });