From 0703174cfa2ed05252560d452edae42d67d64467 Mon Sep 17 00:00:00 2001 From: Kalane Date: Sun, 19 Sep 2021 22:41:56 +0200 Subject: [PATCH] refactor(match-service): pass region outside of account object --- .../Controllers/Http/SummonersController.ts | 3 +-- .../Jax/src/Endpoints/SummonerEndpoint.ts | 11 +++++++- server-v2/app/Services/MatchService.ts | 26 ++++++++++++------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/server-v2/app/Controllers/Http/SummonersController.ts b/server-v2/app/Controllers/Http/SummonersController.ts index 8fde1f1..5fdb6dc 100644 --- a/server-v2/app/Controllers/Http/SummonersController.ts +++ b/server-v2/app/Controllers/Http/SummonersController.ts @@ -26,7 +26,6 @@ export default class SummonersController { if (!account) { return response.json(null) } - account.region = region finalJSON.account = account // Summoner in DB @@ -36,7 +35,7 @@ export default class SummonersController { finalJSON.account.names = await SummonerService.getAllSummonerNames(account, summonerDB) // MATCH LIST - finalJSON.matchList = await MatchService.updateMatchList(account, summonerDB) + finalJSON.matchList = await MatchService.updateMatchList(account, region, summonerDB) // All seasons the summoner has played // TODO: check if there is a way to do that with V5... diff --git a/server-v2/app/Services/Jax/src/Endpoints/SummonerEndpoint.ts b/server-v2/app/Services/Jax/src/Endpoints/SummonerEndpoint.ts index 9c7b2f9..09cbd31 100644 --- a/server-v2/app/Services/Jax/src/Endpoints/SummonerEndpoint.ts +++ b/server-v2/app/Services/Jax/src/Endpoints/SummonerEndpoint.ts @@ -11,7 +11,6 @@ export interface SummonerDTO { id: string puuid: string summonerLevel: number - region?: string } export default class SummonerEndpoint { @@ -23,6 +22,16 @@ export default class SummonerEndpoint { this.limiter = limiter } + public accountId(accountId: string, region: string): Promise { + return new JaxRequest( + region, + this.config, + `summoner/v4/summoners/by-account/${accountId}`, + this.limiter, + 36000 + ).execute() + } + public summonerId(summonerId: string, region: string): Promise { return new JaxRequest( region, diff --git a/server-v2/app/Services/MatchService.ts b/server-v2/app/Services/MatchService.ts index 3d48ece..925769f 100644 --- a/server-v2/app/Services/MatchService.ts +++ b/server-v2/app/Services/MatchService.ts @@ -13,14 +13,15 @@ class MatchService { /** * Add 100 matches at a time to MatchList until the stopFetching condition is true * @param account of the summoner + * @param region of the summoner * @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 alreadyIn = false let index = 0 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 if (!newMatchList) { return matchList @@ -28,10 +29,7 @@ class MatchService { matchList = [...matchList, ...newMatchList] alreadyIn = newMatchList.length === 0 || stopFetching(newMatchList) // If the match is made in another region : we stop fetching - if ( - matchList[matchList.length - 1].split('_')[0].toLowerCase() !== - account.region?.toLowerCase() - ) { + if (matchList[matchList.length - 1].split('_')[0].toLowerCase() !== region.toLowerCase()) { alreadyIn = true } index += 100 @@ -41,15 +39,23 @@ class MatchService { /** * Update the full MatchList of the summoner */ - public async updateMatchList(account: SummonerDTO, summonerDB: Summoner): Promise { + public async updateMatchList( + account: SummonerDTO, + region: string, + summonerDB: Summoner + ): Promise { console.time('matchList') const currentMatchList = await summonerDB.related('matchList').query().orderBy('matchId', 'asc') const currentMatchListIds = currentMatchList.map((m) => m.matchId) - const newMatchList = await this._fetchMatchListUntil(account, (newMatchList: MatchlistDto) => { - return currentMatchListIds.some((id) => id === newMatchList[newMatchList.length - 1]) - }) + const newMatchList = await this._fetchMatchListUntil( + account, + region, + (newMatchList: MatchlistDto) => { + return currentMatchListIds.some((id) => id === newMatchList[newMatchList.length - 1]) + } + ) const matchListToSave: MatchlistDto = [] for (const matchId of newMatchList.reverse()) {