2019-09-11 07:06:21 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
|
|
const Match = use('App/Models/Match')
|
2019-09-11 18:19:15 +00:00
|
|
|
const Jax = use('Jax')
|
2019-09-29 16:06:54 +00:00
|
|
|
const MatchTransformer = use('App/Transformers/MatchTransformer')
|
2019-09-11 07:06:21 +00:00
|
|
|
|
|
|
|
|
class SummonerController {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST Endpoint : get summoner data
|
|
|
|
|
*/
|
2019-09-29 16:06:54 +00:00
|
|
|
async api({ request, response, transform }) {
|
2019-09-11 07:06:21 +00:00
|
|
|
const summoner = request.input('summoner')
|
|
|
|
|
const region = request.input('region')
|
|
|
|
|
|
2019-09-29 16:06:54 +00:00
|
|
|
const data = await this.getSummonerData(summoner, region, transform)
|
2019-09-11 07:06:21 +00:00
|
|
|
response.json(data)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 16:06:54 +00:00
|
|
|
/**
|
|
|
|
|
* Get the matchlist of all matches made less than 4 months ago by the Summoner
|
|
|
|
|
* @param today
|
|
|
|
|
* @param accountId
|
|
|
|
|
* @param beginIndex
|
|
|
|
|
* @param allMatches
|
|
|
|
|
*/
|
|
|
|
|
async getMatchList(today, accountId, beginIndex, allMatches) {
|
|
|
|
|
const { matches } = await Jax.Matchlist.accountID(accountId, beginIndex)
|
|
|
|
|
|
|
|
|
|
allMatches = [...allMatches, ...matches]
|
|
|
|
|
|
|
|
|
|
const lastMatch = matches[matches.length - 1].timestamp
|
|
|
|
|
const diff = today - lastMatch
|
|
|
|
|
console.log(diff)
|
|
|
|
|
// More matches to get from Riot API if they are younger than 4 months
|
|
|
|
|
if (matches.length === 100 && diff < 10368000000) {
|
|
|
|
|
return this.getMatchList(today, accountId, (beginIndex + 100), allMatches)
|
|
|
|
|
} else {
|
|
|
|
|
console.log('return all matches bro')
|
|
|
|
|
return allMatches
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 07:06:21 +00:00
|
|
|
/**
|
|
|
|
|
* Get all the data about the searched Summoner
|
|
|
|
|
* @param summoner
|
|
|
|
|
* @param region
|
|
|
|
|
*/
|
2019-09-29 16:06:54 +00:00
|
|
|
async getSummonerData(summoner, region, transform) {
|
2019-09-11 07:06:21 +00:00
|
|
|
console.time('all')
|
|
|
|
|
|
|
|
|
|
console.log(summoner, region)
|
|
|
|
|
|
|
|
|
|
const regexSummonerName = new RegExp('^[0-9\\p{L} _\\.]+$', 'u')
|
|
|
|
|
if (!regexSummonerName.exec(summoner)) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const finalJSON = {}
|
|
|
|
|
Jax.regionName = region
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const account = await Jax.Summoner.summonerName(summoner)
|
|
|
|
|
|
|
|
|
|
// Check if the summoner is found
|
|
|
|
|
if (!account) return null
|
|
|
|
|
|
|
|
|
|
finalJSON.account = account
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
console.time('getMatches')
|
2019-09-29 16:06:54 +00:00
|
|
|
const today = Date.now()
|
|
|
|
|
const matches = await this.getMatchList(today, account.accountId, 0, [])
|
2019-09-11 07:06:21 +00:00
|
|
|
const gameIds = matches.slice(0, 10).map(({ gameId }) => gameId)
|
|
|
|
|
|
|
|
|
|
let matchesDetails = []
|
|
|
|
|
const matchesToGetFromRiot = []
|
|
|
|
|
for (let i = 0; i < gameIds.length; ++i) {
|
2019-09-29 16:06:54 +00:00
|
|
|
// const matchSaved = await Match.where({ gameId: gameIds[i] }).first()
|
|
|
|
|
const matchSaved = await Match.where({ gameId: gameIds[i], puuid: account.puuid }).first()
|
2019-09-11 07:06:21 +00:00
|
|
|
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)
|
2019-09-29 16:06:54 +00:00
|
|
|
let matchesFromApi = await Promise.all(requests)
|
|
|
|
|
|
|
|
|
|
/* If we have to same 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()
|
2019-09-11 07:06:21 +00:00
|
|
|
|
2019-09-29 16:06:54 +00:00
|
|
|
matchesDetails = [...matchesDetails, ...matchesFromApi]
|
|
|
|
|
|
|
|
|
|
/* Save all matches in db */
|
|
|
|
|
for (const match of matchesFromApi) {
|
|
|
|
|
await Match.create(match)
|
|
|
|
|
console.log('match saved')
|
|
|
|
|
}
|
2019-09-11 07:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sort 10 matches */
|
2019-09-29 16:06:54 +00:00
|
|
|
matchesDetails.sort((a, b) => (a.gameCreation < b.gameCreation) ? -1 : 1)
|
2019-09-11 07:06:21 +00:00
|
|
|
|
|
|
|
|
finalJSON.matchesDetails = matchesDetails
|
|
|
|
|
finalJSON.allMatches = matches
|
|
|
|
|
|
|
|
|
|
console.timeEnd('getMatches')
|
|
|
|
|
console.timeEnd('all')
|
|
|
|
|
return finalJSON
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('username not found')
|
|
|
|
|
console.log(error)
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = SummonerController
|