2020-10-04 20:05:44 +00:00
|
|
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
2021-09-11 12:09:10 +00:00
|
|
|
import { getCurrentSeason } from 'App/helpers'
|
2020-10-05 10:40:02 +00:00
|
|
|
import Summoner from 'App/Models/Summoner'
|
2020-10-05 09:16:21 +00:00
|
|
|
import MatchRepository from 'App/Repositories/MatchRepository'
|
2020-10-04 20:05:44 +00:00
|
|
|
import Jax from 'App/Services/Jax'
|
2020-10-05 09:16:21 +00:00
|
|
|
import MatchService from 'App/Services/MatchService'
|
2020-10-08 07:51:12 +00:00
|
|
|
import StatsService from 'App/Services/StatsService'
|
2020-10-04 20:05:44 +00:00
|
|
|
import SummonerService from 'App/Services/SummonerService'
|
2020-10-07 20:03:36 +00:00
|
|
|
import LiveMatchTransformer from 'App/Transformers/LiveMatchTransformer'
|
2020-10-05 17:49:03 +00:00
|
|
|
import SummonerBasicValidator from 'App/Validators/SummonerBasicValidator'
|
2020-10-05 19:33:17 +00:00
|
|
|
import SummonerChampionValidator from 'App/Validators/SummonerChampionValidator'
|
2020-10-07 20:03:36 +00:00
|
|
|
import SummonerLiveValidator from 'App/Validators/SummonerLiveValidator'
|
2020-10-08 07:51:12 +00:00
|
|
|
import SummonerOverviewValidator from 'App/Validators/SummonerOverviewValidator'
|
2020-10-07 15:47:27 +00:00
|
|
|
import SummonerRecordValidator from 'App/Validators/SummonerRecordValidator'
|
2020-10-04 20:05:44 +00:00
|
|
|
|
|
|
|
|
export default class SummonersController {
|
2020-10-05 17:49:03 +00:00
|
|
|
/**
|
|
|
|
|
* Get all played seasons for a summoner
|
|
|
|
|
* @param puuid of the summoner
|
|
|
|
|
*/
|
2020-10-08 07:51:12 +00:00
|
|
|
private async getSeasons (puuid: string): Promise<number[]> {
|
2020-10-05 17:49:03 +00:00
|
|
|
const seasons = await MatchRepository.seasons(puuid)
|
2021-09-11 12:09:10 +00:00
|
|
|
return seasons.length ? seasons.map(s => s._id) : [getCurrentSeason()]
|
2020-10-05 09:16:21 +00:00
|
|
|
}
|
2020-10-04 20:05:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST: get basic summoner data
|
2020-10-05 09:16:21 +00:00
|
|
|
* @param ctx
|
2020-10-04 20:05:44 +00:00
|
|
|
*/
|
|
|
|
|
public async basic ({ request, response }: HttpContextContract) {
|
|
|
|
|
console.time('all')
|
2020-10-05 19:33:17 +00:00
|
|
|
const { summoner, region } = await request.validate(SummonerBasicValidator)
|
|
|
|
|
const finalJSON: any = {}
|
2020-10-04 20:05:44 +00:00
|
|
|
|
|
|
|
|
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
|
2020-10-05 10:40:02 +00:00
|
|
|
let summonerDB = await Summoner.findOne({ puuid: account.puuid })
|
2020-10-05 19:33:17 +00:00
|
|
|
if (!summonerDB) {
|
2020-10-05 10:40:02 +00:00
|
|
|
summonerDB = await Summoner.create({ puuid: account.puuid })
|
2020-10-04 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Summoner names
|
|
|
|
|
finalJSON.account.names = SummonerService.getAllSummonerNames(account, summonerDB)
|
|
|
|
|
|
|
|
|
|
// MATCH LIST
|
|
|
|
|
await MatchService.updateMatchList(account, summonerDB)
|
|
|
|
|
finalJSON.matchList = summonerDB.matchList
|
|
|
|
|
|
|
|
|
|
// All seasons the summoner has played
|
|
|
|
|
finalJSON.seasons = await this.getSeasons(account.puuid)
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
|
|
// SAVE IN DB
|
2020-10-05 10:40:02 +00:00
|
|
|
await summonerDB.save()
|
2020-10-04 20:05:44 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log('username not found')
|
|
|
|
|
console.log(error)
|
|
|
|
|
return response.json(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.timeEnd('all')
|
|
|
|
|
return response.json(finalJSON)
|
|
|
|
|
}
|
2020-10-05 19:33:17 +00:00
|
|
|
|
2020-10-08 07:51:12 +00:00
|
|
|
/**
|
|
|
|
|
* POST: get overview view summoner data
|
|
|
|
|
* @param ctx
|
|
|
|
|
*/
|
|
|
|
|
public async overview ({ request, response }: HttpContextContract) {
|
|
|
|
|
console.time('overview')
|
|
|
|
|
const { puuid, accountId, region, season } = await request.validate(SummonerOverviewValidator)
|
|
|
|
|
const finalJSON: any = {}
|
|
|
|
|
|
|
|
|
|
// Summoner in DB
|
|
|
|
|
let summonerDB = await Summoner.findOne({ puuid: puuid })
|
|
|
|
|
if (!summonerDB) {
|
|
|
|
|
summonerDB = await Summoner.create({ puuid: puuid })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MATCHES BASIC
|
|
|
|
|
const gameIds = summonerDB.matchList!.slice(0)
|
2021-08-07 20:52:54 +00:00
|
|
|
// .filter(m => {
|
|
|
|
|
// return season ? m.seasonMatch === season : true // TODO: filter by season
|
|
|
|
|
// })
|
2020-10-08 07:51:12 +00:00
|
|
|
.slice(0, 10)
|
|
|
|
|
finalJSON.matchesDetails = await MatchService.getMatches(puuid, accountId, region, gameIds, summonerDB)
|
|
|
|
|
|
|
|
|
|
// STATS
|
|
|
|
|
console.time('STATS')
|
|
|
|
|
finalJSON.stats = await StatsService.getSummonerStats(puuid, season)
|
|
|
|
|
console.timeEnd('STATS')
|
|
|
|
|
|
|
|
|
|
// SAVE IN DB
|
|
|
|
|
await summonerDB.save()
|
|
|
|
|
|
|
|
|
|
console.timeEnd('overview')
|
|
|
|
|
return response.json(finalJSON)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 19:33:17 +00:00
|
|
|
/**
|
|
|
|
|
* POST: get champions view summoner data
|
|
|
|
|
* @param ctx
|
|
|
|
|
*/
|
2020-10-07 20:03:36 +00:00
|
|
|
public async champions ({ request, response }: HttpContextContract) {
|
2020-10-05 19:33:17 +00:00
|
|
|
console.time('championsRequest')
|
|
|
|
|
const { puuid, queue, season } = await request.validate(SummonerChampionValidator)
|
|
|
|
|
const championStats = await MatchRepository.championCompleteStats(puuid, queue, season)
|
|
|
|
|
console.timeEnd('championsRequest')
|
|
|
|
|
return response.json(championStats)
|
|
|
|
|
}
|
2020-10-07 15:47:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST: get records view summoner data
|
|
|
|
|
* @param ctx
|
|
|
|
|
*/
|
2020-10-07 20:03:36 +00:00
|
|
|
public async records ({ request, response }: HttpContextContract) {
|
2020-10-07 15:47:27 +00:00
|
|
|
console.time('recordsRequest')
|
|
|
|
|
const { puuid, season } = await request.validate(SummonerRecordValidator)
|
|
|
|
|
const records = await MatchRepository.records(puuid, season)
|
|
|
|
|
console.timeEnd('recordsRequest')
|
|
|
|
|
return response.json(records)
|
|
|
|
|
}
|
2020-10-07 20:03:36 +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
|
|
|
|
|
let currentGame = await Jax.Spectator.summonerID(id, region)
|
|
|
|
|
|
|
|
|
|
if (!currentGame) {
|
|
|
|
|
return response.json(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentGame = await LiveMatchTransformer.transform(currentGame, { region })
|
|
|
|
|
console.timeEnd('liveMatchDetails')
|
|
|
|
|
|
|
|
|
|
return response.json(currentGame)
|
|
|
|
|
}
|
2020-10-04 20:05:44 +00:00
|
|
|
}
|