From 2bac504b0f8d3565cafd0d20830246e608bbe724 Mon Sep 17 00:00:00 2001 From: Kalane Date: Sun, 12 Sep 2021 21:47:48 +0200 Subject: [PATCH] feat: start summoner/overview endpoint --- .../Controllers/Http/SummonersController.ts | 26 ++++++++++- server-v2/app/Services/MatchService.ts | 43 ++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/server-v2/app/Controllers/Http/SummonersController.ts b/server-v2/app/Controllers/Http/SummonersController.ts index 6733b0c..add3518 100644 --- a/server-v2/app/Controllers/Http/SummonersController.ts +++ b/server-v2/app/Controllers/Http/SummonersController.ts @@ -5,6 +5,7 @@ import Jax from 'App/Services/Jax' import MatchService from 'App/Services/MatchService' import SummonerService from 'App/Services/SummonerService' import SummonerBasicValidator from 'App/Validators/SummonerBasicValidator' +import SummonerOverviewValidator from 'App/Validators/SummonerOverviewValidator' export default class SummonersController { public async basic({ request, response }: HttpContextContract) { @@ -51,7 +52,30 @@ export default class SummonersController { return response.json(finalJSON) } - public async overview({ response }: HttpContextContract) { + public async overview({ request, response }: HttpContextContract) { + console.time('OVERVIEW_REQUEST') + const { puuid, accountId, region, season } = await request.validate(SummonerOverviewValidator) + const finalJSON: any = {} + + // Summoner in DB + const summonerDB = await Summoner.firstOrCreate({ puuid: puuid }) + + // MATCHES BASIC + const matchIds = await summonerDB + .related('matchList') + .query() + .select('matchId') + .orderBy('matchId', 'desc') + .limit(10) + + finalJSON.matchesDetails = await MatchService.getMatches(region, matchIds, summonerDB) + + // TODO: STATS + // console.time('STATS') + // finalJSON.stats = 'todo' + // console.timeEnd('STATS') + + console.timeEnd('OVERVIEW_REQUEST') return response.json('OVERVIEW REQUEST') } } diff --git a/server-v2/app/Services/MatchService.ts b/server-v2/app/Services/MatchService.ts index f2c4a97..b54302c 100644 --- a/server-v2/app/Services/MatchService.ts +++ b/server-v2/app/Services/MatchService.ts @@ -3,6 +3,8 @@ import { MatchlistDto } from './Jax/src/Endpoints/MatchlistEndpoint' import { SummonerDTO } from './Jax/src/Endpoints/SummonerEndpoint' import Summoner from 'App/Models/Summoner' import Database from '@ioc:Adonis/Lucid/Database' +import SummonerMatchlist from 'App/Models/SummonerMatchlist' +import MatchParser from 'App/Parsers/MatchParser' class MatchService { /** @@ -35,8 +37,6 @@ class MatchService { } /** * Update the full MatchList of the summoner - * @param account of the summoner - * @param summonerDB summoner in the database */ public async updateMatchList(account: SummonerDTO, summonerDB: Summoner): Promise { console.time('matchList') @@ -72,6 +72,45 @@ class MatchService { console.timeEnd('matchList') return currentMatchListIds } + + /** + * Fetch list of matches for a specific Summoner + */ + public async getMatches(region: string, matchList: SummonerMatchlist[], summonerDB: Summoner) { + console.time('getMatches') + + let matches: any[] = [] // Todo: add type of serialized matches here + const matchesToGetFromRiot: MatchlistDto = [] + for (let i = 0; i < matchList.length; ++i) { + const matchSaved = await summonerDB + .related('matches') + .query() + .where('matchId', matchList[i].matchId) + .preload('match') + .first() + + if (matchSaved) { + // TODO: Serialize match from DB + put it in Redis + push it in "matches" + } else { + matchesToGetFromRiot.push(matchList[i].matchId) + } + } + + const requests = matchesToGetFromRiot.map((gameId) => Jax.Match.get(gameId, region)) + const matchesFromApi = await Promise.all(requests) + + /* If we have to store some matches in the db */ + if (matchesFromApi.length !== 0) { + // Transform raw matches data + const parsedMatches = await MatchParser.parse(matchesFromApi) + + // TODO: Serialize match from DB + put it in Redis + push it in "matches" + } + + // Todo: Sort and return "matches" + + console.timeEnd('getMatches') + } } export default new MatchService()