refactor(match-service): pass region outside of account object

This commit is contained in:
Kalane 2021-09-19 22:41:56 +02:00
parent b1466c4063
commit 0703174cfa
3 changed files with 27 additions and 13 deletions

View file

@ -26,7 +26,6 @@ export default class SummonersController {
if (!account) { if (!account) {
return response.json(null) return response.json(null)
} }
account.region = region
finalJSON.account = account finalJSON.account = account
// Summoner in DB // Summoner in DB
@ -36,7 +35,7 @@ export default class SummonersController {
finalJSON.account.names = await SummonerService.getAllSummonerNames(account, summonerDB) finalJSON.account.names = await SummonerService.getAllSummonerNames(account, summonerDB)
// MATCH LIST // MATCH LIST
finalJSON.matchList = await MatchService.updateMatchList(account, summonerDB) finalJSON.matchList = await MatchService.updateMatchList(account, region, summonerDB)
// All seasons the summoner has played // All seasons the summoner has played
// TODO: check if there is a way to do that with V5... // TODO: check if there is a way to do that with V5...

View file

@ -11,7 +11,6 @@ export interface SummonerDTO {
id: string id: string
puuid: string puuid: string
summonerLevel: number summonerLevel: number
region?: string
} }
export default class SummonerEndpoint { export default class SummonerEndpoint {
@ -23,6 +22,16 @@ export default class SummonerEndpoint {
this.limiter = limiter this.limiter = limiter
} }
public accountId(accountId: string, region: string): Promise<SummonerDTO> {
return new JaxRequest(
region,
this.config,
`summoner/v4/summoners/by-account/${accountId}`,
this.limiter,
36000
).execute()
}
public summonerId(summonerId: string, region: string): Promise<SummonerDTO> { public summonerId(summonerId: string, region: string): Promise<SummonerDTO> {
return new JaxRequest( return new JaxRequest(
region, region,

View file

@ -13,14 +13,15 @@ class MatchService {
/** /**
* Add 100 matches at a time to MatchList until the stopFetching condition is true * Add 100 matches at a time to MatchList until the stopFetching condition is true
* @param account of the summoner * @param account of the summoner
* @param region of the summoner
* @param stopFetching condition to stop fetching the MatchList * @param stopFetching condition to stop fetching the MatchList
*/ */
private async _fetchMatchListUntil(account: SummonerDTO, stopFetching: any) { private async _fetchMatchListUntil(account: SummonerDTO, region: string, stopFetching: any) {
let matchList: MatchlistDto = [] let matchList: MatchlistDto = []
let alreadyIn = false let alreadyIn = false
let index = 0 let index = 0
do { do {
let newMatchList = await Jax.Matchlist.puuid(account.puuid, account.region as string, index) let newMatchList = await Jax.Matchlist.puuid(account.puuid, region as string, index)
// Error while fetching Riot API // Error while fetching Riot API
if (!newMatchList) { if (!newMatchList) {
return matchList return matchList
@ -28,10 +29,7 @@ class MatchService {
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
if ( if (matchList[matchList.length - 1].split('_')[0].toLowerCase() !== region.toLowerCase()) {
matchList[matchList.length - 1].split('_')[0].toLowerCase() !==
account.region?.toLowerCase()
) {
alreadyIn = true alreadyIn = true
} }
index += 100 index += 100
@ -41,15 +39,23 @@ class MatchService {
/** /**
* Update the full MatchList of the summoner * Update the full MatchList of the summoner
*/ */
public async updateMatchList(account: SummonerDTO, summonerDB: Summoner): Promise<MatchlistDto> { public async updateMatchList(
account: SummonerDTO,
region: string,
summonerDB: Summoner
): Promise<MatchlistDto> {
console.time('matchList') console.time('matchList')
const currentMatchList = await summonerDB.related('matchList').query().orderBy('matchId', 'asc') const currentMatchList = await summonerDB.related('matchList').query().orderBy('matchId', 'asc')
const currentMatchListIds = currentMatchList.map((m) => m.matchId) const currentMatchListIds = currentMatchList.map((m) => m.matchId)
const newMatchList = await this._fetchMatchListUntil(account, (newMatchList: MatchlistDto) => { const newMatchList = await this._fetchMatchListUntil(
account,
region,
(newMatchList: MatchlistDto) => {
return currentMatchListIds.some((id) => id === newMatchList[newMatchList.length - 1]) return currentMatchListIds.some((id) => id === newMatchList[newMatchList.length - 1])
}) }
)
const matchListToSave: MatchlistDto = [] const matchListToSave: MatchlistDto = []
for (const matchId of newMatchList.reverse()) { for (const matchId of newMatchList.reverse()) {