refactor: clean code to fetch matches details

This commit is contained in:
Valentin Kaelin 2019-10-05 20:01:01 +02:00
parent a8b4fef901
commit 6f13a76da4
3 changed files with 75 additions and 81 deletions

View file

@ -1,8 +1,6 @@
'use strict' 'use strict'
const Match = use('App/Models/Match')
const Jax = use('Jax') const Jax = use('Jax')
const MatchTransformer = use('App/Transformers/MatchTransformer')
const MatchHelper = use('App/Helpers/MatchHelper') const MatchHelper = use('App/Helpers/MatchHelper')
class SummonerController { class SummonerController {
@ -10,27 +8,15 @@ class SummonerController {
/** /**
* POST Endpoint : get summoner data * POST Endpoint : get summoner data
*/ */
async api({ request, response, transform }) { async api({ request, response }) {
console.time('all')
const summoner = request.input('summoner') const summoner = request.input('summoner')
const region = request.input('region') 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) console.log(summoner, region)
const regexSummonerName = new RegExp('^[0-9\\p{L} _\\.]+$', 'u') const regexSummonerName = new RegExp('^[0-9\\p{L} _\\.]+$', 'u')
if (!regexSummonerName.exec(summoner)) { if (!regexSummonerName.exec(summoner)) {
return null return response.json(null)
} }
const finalJSON = {} const finalJSON = {}
@ -38,77 +24,30 @@ class SummonerController {
try { try {
const account = await Jax.Summoner.summonerName(summoner) const account = await Jax.Summoner.summonerName(summoner)
// Check if the summoner is found // Check if the summoner is found
if (!account) return null if (!account) return response.json(null)
finalJSON.account = account finalJSON.account = account
// RANKED STATS
const ranked = await Jax.League.summonerID(account.id) const ranked = await Jax.League.summonerID(account.id)
const soloQ = ranked.filter(e => e.queueType === 'RANKED_SOLO_5x5') const soloQ = ranked.filter(e => e.queueType === 'RANKED_SOLO_5x5')
finalJSON.soloQ = soloQ.length ? soloQ[0] : null; finalJSON.soloQ = soloQ.length ? soloQ[0] : null;
// MATCH LIST // MATCH LIST
const matchList = await MatchHelper.getFullMatchList(account) 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 finalJSON.allMatches = matchList
console.timeEnd('getMatches') // MATCHES DETAILS
console.timeEnd('all') const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId)
return finalJSON finalJSON.matchesDetails = await MatchHelper.getMatches(account, gameIds)
} catch (error) { } catch (error) {
console.log('username not found') console.log('username not found')
console.log(error) console.log(error)
return null return response.json(null)
} }
console.timeEnd('all')
return response.json(finalJSON)
} }
} }

View file

@ -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 Summoner = use('App/Models/Summoner')
const Jax = use('Jax') const Jax = use('Jax')
const Logger = use('Logger') const MatchTransformer = use('App/Transformers/MatchTransformer')
class MatchHelper { class MatchHelper {
/** /**
@ -61,6 +66,62 @@ class MatchHelper {
return summonerDB.matchList 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 * Return the lane of the summoner according to timeline
* @param timeline from Riot Api * @param timeline from Riot Api

View file

@ -1,8 +1,6 @@
'use strict' 'use strict'
const BumblebeeTransformer = use('Bumblebee/Transformer') const BumblebeeTransformer = use('Bumblebee/Transformer')
const MatchHelper = use('App/Helpers/MatchHelper')
const Logger = use('Logger')
/** /**
* MatchTransformer class * MatchTransformer class
@ -14,11 +12,7 @@ class MatchTransformer extends BumblebeeTransformer {
/** /**
* This method is used to transform the data. * This method is used to transform the data.
*/ */
transform(match, { account, champions, runes }) { transform(match, { account, champions, runes, MatchHelper }) {
// console.log(champions)
// Logger.transport('file').info(champions)
const participantId = match.participantIdentities.find((p) => p.player.currentAccountId === account.accountId).participantId const participantId = match.participantIdentities.find((p) => p.player.currentAccountId === account.accountId).participantId
const player = match.participants[participantId - 1] const player = match.participants[participantId - 1]
const teamId = player.teamId const teamId = player.teamId