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-16 11:49:32 +00:00
|
|
|
import MatchRepository from 'App/Repositories/MatchRepository'
|
2021-09-16 12:23:09 +00:00
|
|
|
import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer'
|
2021-09-17 20:57:57 +00:00
|
|
|
import LiveMatchSerializer from 'App/Serializers/LiveMatchSerializer'
|
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-17 20:57:57 +00:00
|
|
|
import SummonerLiveValidator from 'App/Validators/SummonerLiveValidator'
|
2021-09-12 19:47:48 +00:00
|
|
|
import SummonerOverviewValidator from 'App/Validators/SummonerOverviewValidator'
|
2021-09-16 11:49:32 +00:00
|
|
|
import SummonerRecordValidator from 'App/Validators/SummonerRecordValidator'
|
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
|
2021-09-16 20:29:20 +00:00
|
|
|
finalJSON.ranked = await SummonerService.getRanked(account.id, 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
|
|
|
|
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
|
|
|
}
|
2021-09-16 11:49:32 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST: get records view summoner data
|
|
|
|
|
* @param ctx
|
|
|
|
|
*/
|
|
|
|
|
public async records({ request, response }: HttpContextContract) {
|
|
|
|
|
console.time('recordsRequest')
|
|
|
|
|
const { puuid, season } = await request.validate(SummonerRecordValidator)
|
|
|
|
|
const records = await MatchRepository.records(puuid)
|
2021-09-16 12:23:09 +00:00
|
|
|
const recordsSerialized = records.map((record) => {
|
|
|
|
|
return {
|
|
|
|
|
...record,
|
|
|
|
|
what: record.what.split('.')[1],
|
|
|
|
|
champion: BasicMatchSerializer.getChampion(record.champion_id),
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-09-16 11:49:32 +00:00
|
|
|
console.timeEnd('recordsRequest')
|
2021-09-16 12:23:09 +00:00
|
|
|
return response.json(recordsSerialized)
|
2021-09-16 11:49:32 +00:00
|
|
|
}
|
2021-09-17 20:57:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST - Return live match detail
|
|
|
|
|
* @param ctx
|
|
|
|
|
*/
|
|
|
|
|
public async liveMatchDetails({ request, response }: HttpContextContract) {
|
|
|
|
|
console.time('liveMatchDetails')
|
|
|
|
|
const { id, region } = await request.validate(SummonerLiveValidator)
|
|
|
|
|
|
|
|
|
|
// CURRENT GAME
|
|
|
|
|
const currentGame = await Jax.Spectator.summonerID(id, region)
|
|
|
|
|
|
|
|
|
|
if (!currentGame) {
|
|
|
|
|
return response.json(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentGameSerialized = await LiveMatchSerializer.serializeOneMatch(currentGame, region)
|
|
|
|
|
console.timeEnd('liveMatchDetails')
|
|
|
|
|
|
|
|
|
|
return response.json(currentGameSerialized)
|
|
|
|
|
}
|
2021-09-12 14:35:50 +00:00
|
|
|
}
|