2020-10-04 20:05:44 +00:00
|
|
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
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-04 20:05:44 +00:00
|
|
|
import SummonerService from 'App/Services/SummonerService'
|
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-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-05 09:16:21 +00:00
|
|
|
private async getSeasons (puuid: string) {
|
2020-10-05 17:49:03 +00:00
|
|
|
const seasons = await MatchRepository.seasons(puuid)
|
2020-10-05 09:16:21 +00:00
|
|
|
return seasons.length ? seasons.map(s => s._id) : [10]
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST: get champions view summoner data
|
|
|
|
|
* @param ctx
|
|
|
|
|
*/
|
|
|
|
|
public async champions ({ request, response }) {
|
|
|
|
|
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-04 20:05:44 +00:00
|
|
|
}
|