|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +exports.default = riotEndpoints; |
| 4 | +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 5 | +const SOLO_QUEUE = 'RANKED_SOLO_5x5'; |
| 6 | +const FLEX_QUEUE = 'RANKED_FLEX_SR'; |
| 7 | +const withRankMetrics = (entry) => { |
| 8 | + if (!entry) |
| 9 | + return null; |
| 10 | + const wins = Number(entry.wins) || 0; |
| 11 | + const losses = Number(entry.losses) || 0; |
| 12 | + const gamesPlayed = wins + losses; |
| 13 | + const winRate = gamesPlayed > 0 ? Number(((wins / gamesPlayed) * 100).toFixed(2)) : 0; |
| 14 | + return { ...entry, winRate }; |
| 15 | +}; |
| 16 | +const coerceRegion = (region, regionMap) => { |
| 17 | + const upper = region.toUpperCase(); |
| 18 | + if (!regionMap[upper]) |
| 19 | + throw new Error(`Invalid region: ${region}`); |
| 20 | + return upper; |
| 21 | +}; |
| 22 | +function riotEndpoints({ client, defaultRegion, regionMap, handleError, }) { |
| 23 | + const getAccountByRiotId = async (riotId, tagLine = null, region = defaultRegion) => { |
| 24 | + const resolvedRegion = coerceRegion(region, regionMap); |
| 25 | + let gameName; |
| 26 | + let tag; |
| 27 | + if (riotId.includes('#')) { |
| 28 | + [gameName, tag] = riotId.split('#'); |
| 29 | + } |
| 30 | + else { |
| 31 | + gameName = riotId; |
| 32 | + tag = tagLine || ''; |
| 33 | + } |
| 34 | + if (!tag) |
| 35 | + throw new Error('TagLine is required for getAccountByRiotId'); |
| 36 | + const shard = regionMap[resolvedRegion].shard; |
| 37 | + try { |
| 38 | + const response = await client.get(`/riot/account/v1/accounts/by-riot-id/${encodeURIComponent(gameName)}/${encodeURIComponent(tag)}`, { baseURL: `https://${shard}` }); |
| 39 | + return response.data; |
| 40 | + } |
| 41 | + catch (error) { |
| 42 | + throw handleError(error); |
| 43 | + } |
| 44 | + }; |
| 45 | + const getSummonerByPuuid = async (puuid, region = defaultRegion) => { |
| 46 | + const resolvedRegion = coerceRegion(region, regionMap); |
| 47 | + const platform = regionMap[resolvedRegion].platform; |
| 48 | + try { |
| 49 | + const response = await client.get(`/lol/summoner/v4/summoners/by-puuid/${encodeURIComponent(puuid)}`, { baseURL: `https://${platform}` }); |
| 50 | + return response.data; |
| 51 | + } |
| 52 | + catch (error) { |
| 53 | + throw handleError(error); |
| 54 | + } |
| 55 | + }; |
| 56 | + const getRankEntriesByPuuid = async (puuid, region = defaultRegion) => { |
| 57 | + const resolvedRegion = coerceRegion(region, regionMap); |
| 58 | + const platform = regionMap[resolvedRegion].platform; |
| 59 | + try { |
| 60 | + const response = await client.get(`/lol/league/v4/entries/by-puuid/${encodeURIComponent(puuid)}`, { |
| 61 | + baseURL: `https://${platform}`, |
| 62 | + }); |
| 63 | + return response.data; |
| 64 | + } |
| 65 | + catch (error) { |
| 66 | + throw handleError(error); |
| 67 | + } |
| 68 | + }; |
| 69 | + const getRankByPuuid = async (puuid, region = defaultRegion) => { |
| 70 | + const entries = await getRankEntriesByPuuid(puuid, region); |
| 71 | + const solo = withRankMetrics(entries.find((entry) => entry.queueType === SOLO_QUEUE) || null); |
| 72 | + const flex = withRankMetrics(entries.find((entry) => entry.queueType === FLEX_QUEUE) || null); |
| 73 | + return { solo, flex, entries }; |
| 74 | + }; |
| 75 | + const getMatchlistByPuuid = async (puuid, options = {}, region = defaultRegion) => { |
| 76 | + const resolvedRegion = coerceRegion(region, regionMap); |
| 77 | + const shard = regionMap[resolvedRegion].shard; |
| 78 | + try { |
| 79 | + const response = await client.get(`/lol/match/v5/matches/by-puuid/${encodeURIComponent(puuid)}/ids`, { |
| 80 | + baseURL: `https://${shard}`, |
| 81 | + params: options, |
| 82 | + }); |
| 83 | + return response.data; |
| 84 | + } |
| 85 | + catch (error) { |
| 86 | + throw handleError(error); |
| 87 | + } |
| 88 | + }; |
| 89 | + const getMatchById = async (matchId, region = defaultRegion) => { |
| 90 | + const resolvedRegion = coerceRegion(region, regionMap); |
| 91 | + const shard = regionMap[resolvedRegion].shard; |
| 92 | + try { |
| 93 | + const response = await client.get(`/lol/match/v5/matches/${matchId}`, { |
| 94 | + baseURL: `https://${shard}`, |
| 95 | + }); |
| 96 | + return response.data; |
| 97 | + } |
| 98 | + catch (error) { |
| 99 | + throw handleError(error); |
| 100 | + } |
| 101 | + }; |
| 102 | + const getMatchTimelineById = async (matchId, region = defaultRegion) => { |
| 103 | + const resolvedRegion = coerceRegion(region, regionMap); |
| 104 | + const shard = regionMap[resolvedRegion].shard; |
| 105 | + try { |
| 106 | + const response = await client.get(`/lol/match/v5/matches/${matchId}/timeline`, { |
| 107 | + baseURL: `https://${shard}`, |
| 108 | + }); |
| 109 | + return response.data; |
| 110 | + } |
| 111 | + catch (error) { |
| 112 | + throw handleError(error); |
| 113 | + } |
| 114 | + }; |
| 115 | + const getMatchlistByPuuidAll = async (puuid, options = {}, region = defaultRegion, pacing = {}) => { |
| 116 | + const baseStart = Number.isInteger(options.start) ? options.start : 0; |
| 117 | + const pageDelayMs = Number.isInteger(pacing.delayMs) ? pacing.delayMs : 1250; |
| 118 | + const maxMatches = Number.isInteger(pacing.maxMatches) && Number(pacing.maxMatches) >= 0 ? Number(pacing.maxMatches) : null; |
| 119 | + const filters = { |
| 120 | + startTime: options.startTime, |
| 121 | + endTime: options.endTime, |
| 122 | + queue: options.queue, |
| 123 | + type: options.type, |
| 124 | + }; |
| 125 | + let start = baseStart; |
| 126 | + const allMatchIds = []; |
| 127 | + while (true) { |
| 128 | + const remaining = maxMatches === null ? 100 : Math.min(100, maxMatches - allMatchIds.length); |
| 129 | + if (remaining <= 0) |
| 130 | + break; |
| 131 | + const page = await getMatchlistByPuuid(puuid, { ...filters, start, count: remaining }, region); |
| 132 | + allMatchIds.push(...page); |
| 133 | + if (page.length < remaining) |
| 134 | + break; |
| 135 | + start += page.length; |
| 136 | + if (pageDelayMs > 0) |
| 137 | + await sleep(pageDelayMs); |
| 138 | + } |
| 139 | + return allMatchIds; |
| 140 | + }; |
| 141 | + const getMatchesWithDetailsByPuuid = async (puuid, options = {}, region = defaultRegion, pacing = {}) => { |
| 142 | + const pageDelayMs = Number.isInteger(pacing.pageDelayMs) ? pacing.pageDelayMs : 1250; |
| 143 | + const detailDelayMs = Number.isInteger(pacing.detailDelayMs) ? pacing.detailDelayMs : 1250; |
| 144 | + const maxMatches = Number.isInteger(pacing.maxMatches) && Number(pacing.maxMatches) >= 0 ? Number(pacing.maxMatches) : null; |
| 145 | + const matchIds = await getMatchlistByPuuidAll(puuid, options, region, { delayMs: pageDelayMs, maxMatches }); |
| 146 | + const matches = []; |
| 147 | + for (let i = 0; i < matchIds.length; i += 1) { |
| 148 | + matches.push(await getMatchById(matchIds[i], region)); |
| 149 | + if (detailDelayMs > 0 && i < matchIds.length - 1) { |
| 150 | + await sleep(detailDelayMs); |
| 151 | + } |
| 152 | + } |
| 153 | + return { matchIds, matches }; |
| 154 | + }; |
| 155 | + return { |
| 156 | + getAccountByRiotId, |
| 157 | + getSummonerByPuuid, |
| 158 | + getRankEntriesByPuuid, |
| 159 | + getRankByPuuid, |
| 160 | + getMatchlistByPuuid, |
| 161 | + getMatchById, |
| 162 | + getMatchTimelineById, |
| 163 | + getMatchlistByPuuidAll, |
| 164 | + getMatchesWithDetailsByPuuid, |
| 165 | + }; |
| 166 | +} |
0 commit comments