diff --git a/server/app/Controllers/Http/SummonerController.js b/server/app/Controllers/Http/SummonerController.js index 5cdb72f..abe2744 100644 --- a/server/app/Controllers/Http/SummonerController.js +++ b/server/app/Controllers/Http/SummonerController.js @@ -1,8 +1,6 @@ 'use strict' -const Match = use('App/Models/Match') const Jax = use('Jax') -const MatchTransformer = use('App/Transformers/MatchTransformer') const MatchHelper = use('App/Helpers/MatchHelper') class SummonerController { @@ -10,27 +8,15 @@ class SummonerController { /** * POST Endpoint : get summoner data */ - async api({ request, response, transform }) { + async api({ request, response }) { + console.time('all') const summoner = request.input('summoner') const region = request.input('region') - - const data = await this.getSummonerData(summoner, region, transform) - response.json(data) - } - - /** - * Get all the data about the searched Summoner - * @param summoner - * @param region - */ - async getSummonerData(summoner, region, transform) { - console.time('all') - console.log(summoner, region) const regexSummonerName = new RegExp('^[0-9\\p{L} _\\.]+$', 'u') if (!regexSummonerName.exec(summoner)) { - return null + return response.json(null) } const finalJSON = {} @@ -38,77 +24,30 @@ class SummonerController { try { const account = await Jax.Summoner.summonerName(summoner) - // Check if the summoner is found - if (!account) return null - + if (!account) return response.json(null) finalJSON.account = account + // RANKED STATS const ranked = await Jax.League.summonerID(account.id) const soloQ = ranked.filter(e => e.queueType === 'RANKED_SOLO_5x5') finalJSON.soloQ = soloQ.length ? soloQ[0] : null; // MATCH LIST const matchList = await MatchHelper.getFullMatchList(account) - - // MATCHES DETAILS - console.time('getMatches') - const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId) - - let matchesDetails = [] - const matchesToGetFromRiot = [] - for (let i = 0; i < gameIds.length; ++i) { - // const matchSaved = await Match.where({ gameId: gameIds[i] }).first() - const matchSaved = await Match.where({ gameId: gameIds[i], puuid: account.puuid }).first() - if (matchSaved) { - console.log('match in mongodb') - matchesDetails.push(matchSaved) - } else { - console.log('match to get from api') - matchesToGetFromRiot.push(gameIds[i]) - } - } - - const requests = matchesToGetFromRiot.map(Jax.Match.get) - let matchesFromApi = await Promise.all(requests) - - /* If we have to store some matches in the db */ - if (matchesFromApi.length !== 0) { - const champions = await Jax.DDragon.Champion.list() - const runes = await Jax.DDragon.Rune.list() - const ctx = { - account, - champions: champions.data, - runes - } - matchesFromApi = await transform.collection(matchesFromApi) - .transformWith(MatchTransformer) - .withContext(ctx) - .toJSON() - - matchesDetails = [...matchesDetails, ...matchesFromApi] - - /* Save all matches in db */ - for (const match of matchesFromApi) { - await Match.create(match) - console.log('match saved') - } - } - - /* Sort 10 matches */ - matchesDetails.sort((a, b) => (a.date < b.date) ? 1 : -1) - - finalJSON.matchesDetails = matchesDetails finalJSON.allMatches = matchList - console.timeEnd('getMatches') - console.timeEnd('all') - return finalJSON + // MATCHES DETAILS + const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId) + finalJSON.matchesDetails = await MatchHelper.getMatches(account, gameIds) } catch (error) { console.log('username not found') console.log(error) - return null + return response.json(null) } + + console.timeEnd('all') + return response.json(finalJSON) } } diff --git a/server/app/Helpers/MatchHelper.js b/server/app/Helpers/MatchHelper.js index 685f7a6..53f994a 100644 --- a/server/app/Helpers/MatchHelper.js +++ b/server/app/Helpers/MatchHelper.js @@ -1,6 +1,11 @@ +'use strict' + +const Bumblebee = use('Adonis/Addons/Bumblebee') +const Logger = use('Logger') +const Match = use('App/Models/Match') const Summoner = use('App/Models/Summoner') const Jax = use('Jax') -const Logger = use('Logger') +const MatchTransformer = use('App/Transformers/MatchTransformer') class MatchHelper { /** @@ -61,6 +66,62 @@ class MatchHelper { return summonerDB.matchList } + /** + * Fetch list of matches for a specific Summoner + * @param account of the summoner + * @param gameIds of the matches to fetch + */ + async getMatches(account, gameIds) { + console.time('getMatches') + + let matchesDetails = [] + const matchesToGetFromRiot = [] + for (let i = 0; i < gameIds.length; ++i) { + const matchSaved = await Match.where({ gameId: gameIds[i], puuid: account.puuid }).first() + if (matchSaved) { + console.log('match in mongodb') + matchesDetails.push(matchSaved) + } else { + console.log('match to get from api') + matchesToGetFromRiot.push(gameIds[i]) + } + } + + const requests = matchesToGetFromRiot.map(Jax.Match.get) + let matchesFromApi = await Promise.all(requests) + + /* If we have to store some matches in the db */ + if (matchesFromApi.length !== 0) { + const champions = await Jax.DDragon.Champion.list() + const runes = await Jax.DDragon.Rune.list() + const ctx = { + account, + champions: champions.data, + runes, + MatchHelper: this + } + + matchesFromApi = await Bumblebee.create().collection(matchesFromApi) + .transformWith(MatchTransformer) + .withContext(ctx) + .toJSON() + + matchesDetails = [...matchesDetails, ...matchesFromApi] + + /* Save all matches from Riot Api in db */ + for (const match of matchesFromApi) { + await Match.create(match) + console.log('match saved') + } + } + + /* Sort matches */ + matchesDetails.sort((a, b) => (a.date < b.date) ? 1 : -1) + console.timeEnd('getMatches') + + return matchesDetails + } + /** * Return the lane of the summoner according to timeline * @param timeline from Riot Api diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js index f82e20b..d90e7f8 100644 --- a/server/app/Transformers/MatchTransformer.js +++ b/server/app/Transformers/MatchTransformer.js @@ -1,8 +1,6 @@ 'use strict' const BumblebeeTransformer = use('Bumblebee/Transformer') -const MatchHelper = use('App/Helpers/MatchHelper') -const Logger = use('Logger') /** * MatchTransformer class @@ -14,11 +12,7 @@ class MatchTransformer extends BumblebeeTransformer { /** * This method is used to transform the data. */ - transform(match, { account, champions, runes }) { - - // console.log(champions) - // Logger.transport('file').info(champions) - + transform(match, { account, champions, runes, MatchHelper }) { const participantId = match.participantIdentities.find((p) => p.player.currentAccountId === account.accountId).participantId const player = match.participants[participantId - 1] const teamId = player.teamId