2021-09-12 14:35:50 +00:00
|
|
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
2021-09-12 17:13:48 +00:00
|
|
|
import { getCurrentSeason } from 'App/helpers'
|
2021-09-12 15:05:30 +00:00
|
|
|
import Summoner from 'App/Models/Summoner'
|
2021-09-12 17:06:46 +00:00
|
|
|
import Jax from 'App/Services/Jax'
|
|
|
|
|
import MatchService from 'App/Services/MatchService'
|
2021-09-15 13:44:53 +00:00
|
|
|
import StatsService from 'App/Services/StatsService'
|
2021-09-12 15:05:30 +00:00
|
|
|
import SummonerService from 'App/Services/SummonerService'
|
2021-09-12 14:35:50 +00:00
|
|
|
import SummonerBasicValidator from 'App/Validators/SummonerBasicValidator'
|
2021-09-12 19:47:48 +00:00
|
|
|
import SummonerOverviewValidator from 'App/Validators/SummonerOverviewValidator'
|
2021-09-12 14:35:50 +00:00
|
|
|
|
|
|
|
|
export default class SummonersController {
|
|
|
|
|
public async basic({ request, response }: HttpContextContract) {
|
2021-09-12 17:13:48 +00:00
|
|
|
console.time('BASIC_REQUEST')
|
2021-09-12 14:35:50 +00:00
|
|
|
const { summoner, region } = await request.validate(SummonerBasicValidator)
|
2021-09-12 15:05:30 +00:00
|
|
|
const finalJSON: any = {}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const account = await SummonerService.getAccount(summoner, region)
|
|
|
|
|
// Check if the summoner is found
|
|
|
|
|
if (!account) {
|
|
|
|
|
return response.json(null)
|
|
|
|
|
}
|
|
|
|
|
account.region = region
|
|
|
|
|
finalJSON.account = account
|
|
|
|
|
|
|
|
|
|
// Summoner in DB
|
|
|
|
|
const summonerDB = await Summoner.firstOrCreate({ puuid: account.puuid })
|
|
|
|
|
|
|
|
|
|
// Summoner names
|
|
|
|
|
finalJSON.account.names = await SummonerService.getAllSummonerNames(account, summonerDB)
|
2021-09-12 17:06:46 +00:00
|
|
|
|
|
|
|
|
// MATCH LIST
|
|
|
|
|
finalJSON.matchList = await MatchService.updateMatchList(account, summonerDB)
|
|
|
|
|
|
2021-09-12 17:13:48 +00:00
|
|
|
// All seasons the summoner has played
|
|
|
|
|
// TODO: check if there is a way to do that with V5...
|
|
|
|
|
finalJSON.seasons = [getCurrentSeason()]
|
|
|
|
|
|
2021-09-12 17:06:46 +00:00
|
|
|
// CURRENT GAME
|
|
|
|
|
const currentGame = await Jax.Spectator.summonerID(account.id, region)
|
|
|
|
|
finalJSON.playing = !!currentGame
|
|
|
|
|
finalJSON.current = currentGame
|
|
|
|
|
|
|
|
|
|
// RANKED STATS
|
|
|
|
|
finalJSON.ranked = await SummonerService.getRanked(account, region)
|
2021-09-15 19:17:52 +00:00
|
|
|
|
|
|
|
|
// RECENT ACTIVITY
|
|
|
|
|
finalJSON.recentActivity = await StatsService.getRecentActivity(account.puuid)
|
2021-09-12 15:05:30 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e)
|
2021-09-12 17:13:48 +00:00
|
|
|
console.timeEnd('BASIC_REQUEST')
|
2021-09-12 15:05:30 +00:00
|
|
|
return response.json(null)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 17:13:48 +00:00
|
|
|
console.timeEnd('BASIC_REQUEST')
|
2021-09-12 15:05:30 +00:00
|
|
|
return response.json(finalJSON)
|
2021-09-12 14:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-12 19:47:48 +00:00
|
|
|
public async overview({ request, response }: HttpContextContract) {
|
|
|
|
|
console.time('OVERVIEW_REQUEST')
|
2021-09-14 11:49:24 +00:00
|
|
|
const { puuid, region, season } = await request.validate(SummonerOverviewValidator)
|
2021-09-12 19:47:48 +00:00
|
|
|
const finalJSON: any = {}
|
|
|
|
|
|
|
|
|
|
// Summoner in DB
|
|
|
|
|
const summonerDB = await Summoner.firstOrCreate({ puuid: puuid })
|
|
|
|
|
|
|
|
|
|
// MATCHES BASIC
|
2021-09-14 11:49:24 +00:00
|
|
|
const matchlist = await summonerDB
|
2021-09-12 19:47:48 +00:00
|
|
|
.related('matchList')
|
|
|
|
|
.query()
|
|
|
|
|
.select('matchId')
|
|
|
|
|
.orderBy('matchId', 'desc')
|
|
|
|
|
.limit(10)
|
2021-09-14 11:49:24 +00:00
|
|
|
const matchIds = matchlist.map((m) => m.matchId)
|
2021-09-12 19:47:48 +00:00
|
|
|
|
2021-09-14 11:49:24 +00:00
|
|
|
finalJSON.matchesDetails = await MatchService.getMatches(region, matchIds, puuid)
|
2021-09-12 19:47:48 +00:00
|
|
|
|
|
|
|
|
// TODO: STATS
|
2021-09-14 15:06:00 +00:00
|
|
|
console.time('STATS')
|
2021-09-15 13:44:53 +00:00
|
|
|
finalJSON.stats = await StatsService.getSummonerStats(puuid, season)
|
2021-09-14 15:06:00 +00:00
|
|
|
console.timeEnd('STATS')
|
2021-09-12 19:47:48 +00:00
|
|
|
|
|
|
|
|
console.timeEnd('OVERVIEW_REQUEST')
|
2021-09-13 21:04:52 +00:00
|
|
|
return response.json(finalJSON)
|
2021-09-12 14:35:50 +00:00
|
|
|
}
|
|
|
|
|
}
|