-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (80 loc) · 2.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const axios = require("axios");
// Gets full data on X amount of matches
exports.getRecentMatches = async function (
server,
summonerName,
queue,
games,
apiKey
) {
try {
const queueType = await adjustQueue(queue);
const regionCodes = await getRegionCodes(server);
const accountId = await getAccountId(regionCodes[0], summonerName, apiKey);
// Endpoint to get match id list
const matchListEndpoint = `https://${regionCodes[1]}.api.riotgames.com/lol/match/v5/matches/by-puuid/{accountId}/ids?${queueType}&start=0&count=${games}&api_key=${apiKey}`;
// Endpoint to get detailed single match data
const matchDetailEndpoint = `https://${regionCodes[1]}.api.riotgames.com/lol/match/v5/matches/{matchId}?api_key=${apiKey}`;
const matchListResponse = await axios.get(
matchListEndpoint.replace("{accountId}", accountId)
);
const matchIds = matchListResponse.data;
// Hit detailed endpoint with each matchId
const requests = matchIds.map((matchId) =>
axios.get(matchDetailEndpoint.replace("{matchId}", matchId))
);
const responses = await Promise.all(requests);
const matches = responses.map((response) => response.data);
return matches;
} catch (err) {
console.log(err);
}
};
// Gets puuid from summonerName
async function getAccountId(region, summonerName, apiKey) {
try {
const summonerEndpoint = `https://${region}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${summonerName}?api_key=${apiKey}`;
const summonerResponse = await axios.get(summonerEndpoint);
return summonerResponse.data.puuid;
} catch (err) {
console.log(err);
}
}
// Gets specific API region prefixes for server
async function getRegionCodes(server) {
try {
const regionCodes = {
na: ["na1", "americas"],
lan: ["la1", "americas"],
las: ["la1", "americas"],
br: ["br1", "americas"],
oce: ["oc1", "americas"],
eune: ["eun1", "europe"],
euw: ["euw1", "europe"],
tr: ["tr1", "europe"],
ru: ["ru", "europe"],
jp: ["jp1", "asia"],
kr: ["kr", "asia"],
ph: ["ph1", "sea"],
sg: ["sg2", "sea"],
th: ["th2", "sea"],
tw: ["tw2", "sea"],
vn: ["vn2", "sea"],
};
const codes = regionCodes[server];
return codes;
} catch (err) {
console.log(err);
}
}
// Adjusts the API query for selected queue
async function adjustQueue(queue) {
try {
if (queue == "all") {
return "type=";
}
return "type=" + queue;
} catch (err) {
console.log(err);
}
}