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) {
const account = await SummonerService.getAccount(summoner.name, region)
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
} else {
summoner.rank = null
@ -63,7 +63,7 @@ class MatchController {
console.log('MATCH DETAILS ALREADY SAVED')
matchDetails = alreadySaved
} else {
matchDetails = await Jax.Match.get(gameId)
matchDetails = await Jax.Match.get(gameId, region)
matchDetails = await DetailedMatchTransformer.transform(matchDetails)
await DetailedMatch.create(matchDetails)
}

View file

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

View file

@ -15,7 +15,7 @@ class MatchService {
let alreadyIn = false
let index = 0
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]
alreadyIn = newMatchList.length === 0 || stopFetching(newMatchList)
// 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)
/* If we have to store some matches in the db */

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,7 +1,8 @@
const { promisify } = require('util')
class JaxRequest {
constructor(config, endpoint, limiter) {
constructor(region, config, endpoint, limiter) {
this.region = region
this.config = config
this.endpoint = endpoint
this.limiter = limiter
@ -13,7 +14,7 @@ class JaxRequest {
async execute() {
try {
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,
resolveWithFullResponse: false
})