2020-10-04 20:05:44 +00:00
|
|
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
2020-10-05 09:16:21 +00:00
|
|
|
import { SummonerModel } from '@ioc:Adonis/League'
|
2020-10-04 20:05:44 +00:00
|
|
|
import mongodb from '@ioc:Mongodb/Database'
|
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'
|
|
|
|
|
|
|
|
|
|
export default class SummonersController {
|
2020-10-05 09:16:21 +00:00
|
|
|
private async getSeasons (puuid: string) {
|
|
|
|
|
let seasons = await MatchRepository.seasons(puuid)
|
|
|
|
|
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')
|
|
|
|
|
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)) {
|
|
|
|
|
return response.json(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.findOrCreate(
|
|
|
|
|
// { puuid: account.puuid },
|
|
|
|
|
// { puuid: account.puuid }
|
|
|
|
|
// )
|
|
|
|
|
|
|
|
|
|
const summonersCollection = await mongodb.connection().collection('summoners')
|
2020-10-05 09:16:21 +00:00
|
|
|
let summonerDB:SummonerModel|null = await summonersCollection.findOne({ puuid: account.puuid })
|
2020-10-04 20:05:44 +00:00
|
|
|
if(!summonerDB) {
|
|
|
|
|
await summonersCollection.insertOne({ puuid: account.puuid })
|
2020-10-05 09:16:21 +00:00
|
|
|
summonerDB = {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
|
|
|
|
|
// await summonerDB.save()
|
|
|
|
|
await summonersCollection.updateOne({ puuid: account.puuid }, summonerDB)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('username not found')
|
|
|
|
|
console.log(error)
|
|
|
|
|
return response.json(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.timeEnd('all')
|
|
|
|
|
return response.json(finalJSON)
|
|
|
|
|
}
|
|
|
|
|
}
|