2019-09-11 07:06:21 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
2019-09-11 18:19:15 +00:00
|
|
|
const Jax = use('Jax')
|
2019-12-01 14:20:49 +00:00
|
|
|
const MatchService = use('App/Services/MatchService')
|
2019-12-01 15:20:27 +00:00
|
|
|
const SummonerService = use('App/Services/SummonerService')
|
2019-12-01 14:20:49 +00:00
|
|
|
const StatsService = use('App/Services/StatsService')
|
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 {
|
2019-12-01 18:17:30 +00:00
|
|
|
const account = await SummonerService.getAccount(summoner, region)
|
2019-09-11 07:06:21 +00:00
|
|
|
// 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
|
2019-12-02 19:41:05 +00:00
|
|
|
const currentGame = await Jax.Spectator.summonerID(account.id, region)
|
2019-10-22 20:59:22 +00:00
|
|
|
finalJSON.playing = !!currentGame
|
2019-10-25 21:09:33 +00:00
|
|
|
|
2019-10-05 18:01:01 +00:00
|
|
|
// RANKED STATS
|
2019-12-02 19:41:05 +00:00
|
|
|
finalJSON.ranked = await SummonerService.getRanked(account, region)
|
2019-09-11 07:06:21 +00:00
|
|
|
|
2019-10-01 19:39:26 +00:00
|
|
|
// MATCH LIST
|
2019-12-01 14:20:49 +00:00
|
|
|
await MatchService.updateMatchList(account, summonerDB)
|
2019-10-25 21:09:33 +00:00
|
|
|
const matchList = summonerDB.matchList
|
2019-10-05 18:01:01 +00:00
|
|
|
finalJSON.allMatches = matchList
|
2019-10-01 19:39:26 +00:00
|
|
|
|
2019-11-10 19:19:04 +00:00
|
|
|
// MATCHES BASIC
|
2019-10-01 19:39:26 +00:00
|
|
|
const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId)
|
2019-12-01 14:20:49 +00:00
|
|
|
finalJSON.matchesDetails = await MatchService.getMatches(account, gameIds, summonerDB)
|
2019-10-25 21:09:33 +00:00
|
|
|
|
2019-10-21 13:30:48 +00:00
|
|
|
// PATCH VERSION
|
|
|
|
|
finalJSON.version = Jax.DDragon.Version
|
2019-10-25 21:09:33 +00:00
|
|
|
|
2019-11-08 14:39:56 +00:00
|
|
|
// STATS
|
|
|
|
|
console.time('STATS')
|
2019-12-01 14:20:49 +00:00
|
|
|
finalJSON.stats = await StatsService.getSummonerStats(account)
|
2019-11-08 14:39:56 +00:00
|
|
|
console.timeEnd('STATS')
|
|
|
|
|
|
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
|
|
|
}
|
2019-12-03 21:23:19 +00:00
|
|
|
|
|
|
|
|
async champions({ request, response }) {
|
|
|
|
|
const puuid = request.input('puuid')
|
|
|
|
|
console.log('champions request', puuid)
|
|
|
|
|
const championStats = await StatsService.getChampionStats(puuid)
|
|
|
|
|
|
|
|
|
|
return response.json(championStats)
|
|
|
|
|
}
|
2019-09-11 07:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = SummonerController
|