2019-09-11 07:06:21 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
2019-09-11 18:19:15 +00:00
|
|
|
const Jax = use('Jax')
|
2019-10-01 19:39:26 +00:00
|
|
|
const MatchHelper = use('App/Helpers/MatchHelper')
|
2019-10-25 21:09:33 +00:00
|
|
|
const Summoner = use('App/Models/Summoner')
|
2019-09-11 07:06:21 +00:00
|
|
|
|
|
|
|
|
class SummonerController {
|
|
|
|
|
/**
|
|
|
|
|
* POST Endpoint : get summoner data
|
|
|
|
|
*/
|
2019-10-05 18:01:01 +00:00
|
|
|
async api({ request, response }) {
|
|
|
|
|
console.time('all')
|
2019-09-11 07:06:21 +00:00
|
|
|
const summoner = request.input('summoner')
|
|
|
|
|
const region = request.input('region')
|
|
|
|
|
console.log(summoner, region)
|
|
|
|
|
|
|
|
|
|
const regexSummonerName = new RegExp('^[0-9\\p{L} _\\.]+$', 'u')
|
|
|
|
|
if (!regexSummonerName.exec(summoner)) {
|
2019-10-05 18:01:01 +00:00
|
|
|
return response.json(null)
|
2019-09-11 07:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const finalJSON = {}
|
|
|
|
|
Jax.regionName = region
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const account = await Jax.Summoner.summonerName(summoner)
|
|
|
|
|
// Check if the summoner is found
|
2019-10-05 18:01:01 +00:00
|
|
|
if (!account) return response.json(null)
|
2019-10-10 18:56:48 +00:00
|
|
|
account.region = region
|
2019-09-11 07:06:21 +00:00
|
|
|
finalJSON.account = account
|
|
|
|
|
|
2019-10-25 21:09:33 +00:00
|
|
|
// Summoner in DB
|
2019-10-26 20:52:50 +00:00
|
|
|
const summonerDB = await Summoner.findOrCreate(
|
|
|
|
|
{ puuid: account.puuid },
|
|
|
|
|
{ puuid: account.puuid }
|
|
|
|
|
)
|
2019-10-25 21:09:33 +00:00
|
|
|
|
2019-10-22 20:59:22 +00:00
|
|
|
// CURRENT GAME
|
|
|
|
|
const currentGame = await Jax.Spectator.summonerID(account.id)
|
|
|
|
|
finalJSON.playing = !!currentGame
|
2019-10-25 21:09:33 +00:00
|
|
|
|
2019-10-05 18:01:01 +00:00
|
|
|
// RANKED STATS
|
2019-09-11 07:06:21 +00:00
|
|
|
const ranked = await Jax.League.summonerID(account.id)
|
2019-10-08 19:54:32 +00:00
|
|
|
finalJSON.ranked = {
|
|
|
|
|
soloQ: ranked.find(e => e.queueType === 'RANKED_SOLO_5x5') || null,
|
2019-10-21 15:37:48 +00:00
|
|
|
flex5v5: ranked.find(e => e.queueType === 'RANKED_FLEX_SR') || null,
|
|
|
|
|
flex3v3: ranked.find(e => e.queueType === 'RANKED_FLEX_TT') || null
|
2019-10-08 19:54:32 +00:00
|
|
|
}
|
2019-09-11 07:06:21 +00:00
|
|
|
|
2019-10-01 19:39:26 +00:00
|
|
|
// MATCH LIST
|
2019-10-25 21:09:33 +00:00
|
|
|
await MatchHelper.updateMatchList(account, summonerDB)
|
|
|
|
|
const matchList = summonerDB.matchList
|
2019-10-05 18:01:01 +00:00
|
|
|
finalJSON.allMatches = matchList
|
2019-10-01 19:39:26 +00:00
|
|
|
|
|
|
|
|
// MATCHES DETAILS
|
|
|
|
|
const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId)
|
2019-10-25 21:09:33 +00:00
|
|
|
finalJSON.matchesDetails = await MatchHelper.getMatches(account, gameIds, summonerDB)
|
|
|
|
|
|
|
|
|
|
// MATES
|
|
|
|
|
finalJSON.mates = summonerDB.mates.filter(m => m.wins + m.losses > 1)
|
2019-10-21 13:30:48 +00:00
|
|
|
|
|
|
|
|
// PATCH VERSION
|
|
|
|
|
finalJSON.version = Jax.DDragon.Version
|
2019-10-25 21:09:33 +00:00
|
|
|
|
|
|
|
|
// SAVE IN DB
|
|
|
|
|
await summonerDB.save()
|
2019-09-11 07:06:21 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log('username not found')
|
|
|
|
|
console.log(error)
|
2019-10-05 18:01:01 +00:00
|
|
|
return response.json(null)
|
2019-09-11 07:06:21 +00:00
|
|
|
}
|
2019-10-05 18:01:01 +00:00
|
|
|
|
|
|
|
|
console.timeEnd('all')
|
|
|
|
|
return response.json(finalJSON)
|
2019-09-11 07:06:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = SummonerController
|