|
| 1 | +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 2 | + |
1 | 3 | module.exports = (client, defaultRegion, regionMap) => ({ |
2 | 4 | /** |
3 | 5 | * Fetch account data by Riot ID. |
@@ -81,4 +83,94 @@ module.exports = (client, defaultRegion, regionMap) => ({ |
81 | 83 | throw this._handleError(error); |
82 | 84 | } |
83 | 85 | }, |
| 86 | + |
| 87 | + /** |
| 88 | + * Fetch match timeline data by match ID. |
| 89 | + * @param {string} matchId - Match ID (e.g., "EUW1_1234567890"). |
| 90 | + * @param {string} [region] - Region code used to resolve shard routing. |
| 91 | + * @returns {Promise<object>} Match timeline payload. |
| 92 | + */ |
| 93 | + async getMatchTimelineById(matchId, region = defaultRegion) { |
| 94 | + const shard = regionMap[region].shard; |
| 95 | + try { |
| 96 | + const response = await client.get(`/lol/match/v5/matches/${matchId}/timeline`, { |
| 97 | + baseURL: `https://${shard}`, |
| 98 | + }); |
| 99 | + return response.data; |
| 100 | + } catch (error) { |
| 101 | + throw this._handleError(error); |
| 102 | + } |
| 103 | + }, |
| 104 | + |
| 105 | + /** |
| 106 | + * Fetch all match IDs for a PUUID using Riot's max page size (100). |
| 107 | + * @param {string} puuid - Player PUUID. |
| 108 | + * @param {object} [options] - Riot filters: startTime, endTime, queue, type, start. |
| 109 | + * @param {string} [region] - Region code used to resolve shard routing. |
| 110 | + * @param {object} [pacing] - Pacing controls. |
| 111 | + * @param {number} [pacing.delayMs=1250] - Delay between page requests. |
| 112 | + * @param {number|null} [pacing.maxMatches=null] - Optional cap on total IDs returned. |
| 113 | + * @returns {Promise<string[]>} All matched IDs. |
| 114 | + */ |
| 115 | + async getMatchlistByPuuidAll(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) && pacing.maxMatches >= 0 ? pacing.maxMatches : null; |
| 119 | + const filters = { |
| 120 | + startTime: options.startTime, |
| 121 | + endTime: options.endTime, |
| 122 | + queue: options.queue, |
| 123 | + type: options.type, |
| 124 | + }; |
| 125 | + |
| 126 | + let start = baseStart; |
| 127 | + const allMatchIds = []; |
| 128 | + |
| 129 | + while (true) { |
| 130 | + const remaining = maxMatches === null ? 100 : Math.min(100, maxMatches - allMatchIds.length); |
| 131 | + if (remaining <= 0) break; |
| 132 | + |
| 133 | + const page = await this.getMatchlistByPuuid(puuid, { ...filters, start, count: remaining }, region); |
| 134 | + allMatchIds.push(...page); |
| 135 | + |
| 136 | + if (page.length < remaining) break; |
| 137 | + start += page.length; |
| 138 | + if (pageDelayMs > 0) await sleep(pageDelayMs); |
| 139 | + } |
| 140 | + |
| 141 | + return allMatchIds; |
| 142 | + }, |
| 143 | + |
| 144 | + /** |
| 145 | + * Fetch match IDs and full match payloads for each ID. |
| 146 | + * @param {string} puuid - Player PUUID. |
| 147 | + * @param {object} [options] - Riot filters: startTime, endTime, queue, type, start. |
| 148 | + * @param {string} [region] - Region code used to resolve shard routing. |
| 149 | + * @param {object} [pacing] - Pacing controls. |
| 150 | + * @param {number} [pacing.pageDelayMs=1250] - Delay between matchlist page requests. |
| 151 | + * @param {number} [pacing.detailDelayMs=1250] - Delay between match detail requests. |
| 152 | + * @param {number|null} [pacing.maxMatches=null] - Optional cap on total matches fetched. |
| 153 | + * @returns {Promise<{matchIds: string[], matches: object[]}>} IDs and full match payloads. |
| 154 | + */ |
| 155 | + async getMatchesWithDetailsByPuuid(puuid, options = {}, region = defaultRegion, pacing = {}) { |
| 156 | + const pageDelayMs = Number.isInteger(pacing.pageDelayMs) ? pacing.pageDelayMs : 1250; |
| 157 | + const detailDelayMs = Number.isInteger(pacing.detailDelayMs) ? pacing.detailDelayMs : 1250; |
| 158 | + const maxMatches = Number.isInteger(pacing.maxMatches) && pacing.maxMatches >= 0 ? pacing.maxMatches : null; |
| 159 | + const matchIds = await this.getMatchlistByPuuidAll( |
| 160 | + puuid, |
| 161 | + options, |
| 162 | + region, |
| 163 | + { delayMs: pageDelayMs, maxMatches } |
| 164 | + ); |
| 165 | + const matches = []; |
| 166 | + |
| 167 | + for (let i = 0; i < matchIds.length; i += 1) { |
| 168 | + matches.push(await this.getMatchById(matchIds[i], region)); |
| 169 | + if (detailDelayMs > 0 && i < matchIds.length - 1) { |
| 170 | + await sleep(detailDelayMs); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return { matchIds, matches }; |
| 175 | + }, |
84 | 176 | }); |
0 commit comments