refactor: give summoner region to JaxProvider for every call to Riot API

This commit is contained in:
Valentin Kaelin 2019-12-02 20:41:05 +01:00
parent 83e616d974
commit c77b5bb05d
10 changed files with 23 additions and 16 deletions

View file

@ -17,7 +17,7 @@ class MatchController {
async _getPlayerRank(summoner, region) { async _getPlayerRank(summoner, region) {
const account = await SummonerService.getAccount(summoner.name, region) const account = await SummonerService.getAccount(summoner.name, region)
if (account) { if (account) {
const ranked = await SummonerService.getRanked(account) const ranked = await SummonerService.getRanked(account, region)
summoner.rank = ranked.soloQ ? (({ tier, shortName }) => ({ tier, shortName }))(ranked.soloQ) : null summoner.rank = ranked.soloQ ? (({ tier, shortName }) => ({ tier, shortName }))(ranked.soloQ) : null
} else { } else {
summoner.rank = null summoner.rank = null
@ -63,7 +63,7 @@ class MatchController {
console.log('MATCH DETAILS ALREADY SAVED') console.log('MATCH DETAILS ALREADY SAVED')
matchDetails = alreadySaved matchDetails = alreadySaved
} else { } else {
matchDetails = await Jax.Match.get(gameId) matchDetails = await Jax.Match.get(gameId, region)
matchDetails = await DetailedMatchTransformer.transform(matchDetails) matchDetails = await DetailedMatchTransformer.transform(matchDetails)
await DetailedMatch.create(matchDetails) await DetailedMatch.create(matchDetails)
} }

View file

@ -38,11 +38,11 @@ class SummonerController {
) )
// CURRENT GAME // CURRENT GAME
const currentGame = await Jax.Spectator.summonerID(account.id) const currentGame = await Jax.Spectator.summonerID(account.id, region)
finalJSON.playing = !!currentGame finalJSON.playing = !!currentGame
// RANKED STATS // RANKED STATS
finalJSON.ranked = await SummonerService.getRanked(account) finalJSON.ranked = await SummonerService.getRanked(account, region)
// MATCH LIST // MATCH LIST
await MatchService.updateMatchList(account, summonerDB) await MatchService.updateMatchList(account, summonerDB)

View file

@ -15,7 +15,7 @@ class MatchService {
let alreadyIn = false let alreadyIn = false
let index = 0 let index = 0
do { do {
let newMatchList = (await Jax.Matchlist.accountID(account.accountId, index)).matches let newMatchList = (await Jax.Matchlist.accountID(account.accountId, account.region, index)).matches
matchList = [...matchList, ...newMatchList] matchList = [...matchList, ...newMatchList]
alreadyIn = newMatchList.length === 0 || stopFetching(newMatchList) alreadyIn = newMatchList.length === 0 || stopFetching(newMatchList)
// If the match is made in another region : we stop fetching // If the match is made in another region : we stop fetching
@ -96,7 +96,7 @@ class MatchService {
} }
} }
const requests = matchesToGetFromRiot.map(Jax.Match.get) const requests = matchesToGetFromRiot.map(gameId => Jax.Match.get(gameId, account.region))
let matchesFromApi = await Promise.all(requests) let matchesFromApi = await Promise.all(requests)
/* If we have to store some matches in the db */ /* If we have to store some matches in the db */

View file

@ -34,7 +34,7 @@ class SummonerService {
return JSON.parse(accountCache) return JSON.parse(accountCache)
} }
const account = await Jax.Summoner.summonerName(name) const account = await Jax.Summoner.summonerName(name, region)
if (account) { if (account) {
await Redis.set(`${region}-${name}`, JSON.stringify(account), 'EX', 36000) await Redis.set(`${region}-${name}`, JSON.stringify(account), 'EX', 36000)
} }
@ -44,15 +44,16 @@ class SummonerService {
/** /**
* Get ranked data for a specific Summoner * Get ranked data for a specific Summoner
* @param account * @param account
* @param region
*/ */
async getRanked(account) { async getRanked(account, region) {
const rankedCache = await Redis.get(`ranked-${account.puuid}`) const rankedCache = await Redis.get(`ranked-${account.puuid}`)
if (rankedCache) { if (rankedCache) {
console.log('RANKED CACHED') console.log('RANKED CACHED')
return JSON.parse(rankedCache) return JSON.parse(rankedCache)
} }
const ranked = await Jax.League.summonerID(account.id) const ranked = await Jax.League.summonerID(account.id, region)
const result = { const result = {
soloQ: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_SOLO_5x5')) || null, soloQ: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_SOLO_5x5')) || null,
flex5v5: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_FLEX_SR')) || null, flex5v5: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_FLEX_SR')) || null,

View file

@ -6,8 +6,9 @@ class LeagueEndpoint {
this.limiter = limiter this.limiter = limiter
} }
summonerID(summonerID) { summonerID(summonerID, region) {
return new JaxRequest( return new JaxRequest(
region,
this.config, this.config,
`league/v4/entries/by-summoner/${summonerID}`, `league/v4/entries/by-summoner/${summonerID}`,
this.limiter this.limiter

View file

@ -8,8 +8,9 @@ class MatchEndpoint {
this.get = this.get.bind(this) this.get = this.get.bind(this)
} }
get(matchID) { get(matchID, region) {
return new JaxRequest( return new JaxRequest(
region,
this.config, this.config,
`match/v4/matches/${matchID}`, `match/v4/matches/${matchID}`,
this.limiter this.limiter

View file

@ -6,8 +6,9 @@ class MatchlistEndpoint {
this.limiter = limiter this.limiter = limiter
} }
accountID(accountID, beginIndex = 0) { accountID(accountID, region, beginIndex = 0) {
return new JaxRequest( return new JaxRequest(
region,
this.config, this.config,
`match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`, `match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`,
this.limiter this.limiter

View file

@ -6,8 +6,9 @@ class SpectatorEndpoint {
this.limiter = limiter this.limiter = limiter
} }
summonerID(summonerID) { summonerID(summonerID, region) {
return new JaxRequest( return new JaxRequest(
region,
this.config, this.config,
`spectator/v4/active-games/by-summoner/${summonerID}`, `spectator/v4/active-games/by-summoner/${summonerID}`,
this.limiter this.limiter

View file

@ -6,8 +6,9 @@ class SummonerEndpoint {
this.limiter = limiter this.limiter = limiter
} }
summonerName(summonerName) { summonerName(summonerName, region) {
return new JaxRequest( return new JaxRequest(
region,
this.config, this.config,
`summoner/v4/summoners/by-name/${encodeURI(summonerName)}`, `summoner/v4/summoners/by-name/${encodeURI(summonerName)}`,
this.limiter this.limiter

View file

@ -1,7 +1,8 @@
const { promisify } = require('util') const { promisify } = require('util')
class JaxRequest { class JaxRequest {
constructor(config, endpoint, limiter) { constructor(region, config, endpoint, limiter) {
this.region = region
this.config = config this.config = config
this.endpoint = endpoint this.endpoint = endpoint
this.limiter = limiter this.limiter = limiter
@ -13,7 +14,7 @@ class JaxRequest {
async execute() { async execute() {
try { try {
const resp = await this.limiter.executing({ const resp = await this.limiter.executing({
url: `https://${this.config.region}.api.riotgames.com/lol/${this.endpoint}`, url: `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`,
token: this.config.key, token: this.config.key,
resolveWithFullResponse: false resolveWithFullResponse: false
}) })