|
1 | 1 | const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 2 | +const SOLO_QUEUE = 'RANKED_SOLO_5x5'; |
| 3 | +const FLEX_QUEUE = 'RANKED_FLEX_SR'; |
| 4 | +const withRankMetrics = (entry) => { |
| 5 | + if (!entry) return null; |
| 6 | + const wins = Number(entry.wins) || 0; |
| 7 | + const losses = Number(entry.losses) || 0; |
| 8 | + const gamesPlayed = wins + losses; |
| 9 | + const winRate = gamesPlayed > 0 ? Number(((wins / gamesPlayed) * 100).toFixed(2)) : 0; |
| 10 | + return { ...entry, winRate }; |
| 11 | +}; |
2 | 12 |
|
3 | 13 | module.exports = (client, defaultRegion, regionMap) => ({ |
4 | 14 | /** |
@@ -46,6 +56,37 @@ module.exports = (client, defaultRegion, regionMap) => ({ |
46 | 56 | } |
47 | 57 | }, |
48 | 58 |
|
| 59 | + /** |
| 60 | + * Fetch all ranked entries for a player PUUID. |
| 61 | + * @param {string} puuid - Player PUUID. |
| 62 | + * @param {string} [region] - Region code used to resolve platform routing. |
| 63 | + * @returns {Promise<object[]>} Ranked entries from League-V4. |
| 64 | + */ |
| 65 | + async getRankEntriesByPuuid(puuid, region = defaultRegion) { |
| 66 | + const platform = regionMap[region].platform; |
| 67 | + try { |
| 68 | + const response = await client.get(`/lol/league/v4/entries/by-puuid/${encodeURIComponent(puuid)}`, { |
| 69 | + baseURL: `https://${platform}`, |
| 70 | + }); |
| 71 | + return response.data; |
| 72 | + } catch (error) { |
| 73 | + throw this._handleError(error); |
| 74 | + } |
| 75 | + }, |
| 76 | + |
| 77 | + /** |
| 78 | + * Fetch ranked info split into Solo and Flex queues. |
| 79 | + * @param {string} puuid - Player PUUID. |
| 80 | + * @param {string} [region] - Region code used to resolve platform routing. |
| 81 | + * @returns {Promise<{solo: object|null, flex: object|null, entries: object[]}>} Queue-split rank payload. |
| 82 | + */ |
| 83 | + async getRankByPuuid(puuid, region = defaultRegion) { |
| 84 | + const entries = await this.getRankEntriesByPuuid(puuid, region); |
| 85 | + const solo = withRankMetrics(entries.find((entry) => entry.queueType === SOLO_QUEUE) || null); |
| 86 | + const flex = withRankMetrics(entries.find((entry) => entry.queueType === FLEX_QUEUE) || null); |
| 87 | + return { solo, flex, entries }; |
| 88 | + }, |
| 89 | + |
49 | 90 | /** |
50 | 91 | * Fetch match IDs for a PUUID from Match-V5. |
51 | 92 | * @param {string} puuid - Player PUUID. |
|
0 commit comments