feat: start summoner/overview endpoint

This commit is contained in:
Kalane 2021-09-12 21:47:48 +02:00
parent c9fc0c7dc1
commit 2bac504b0f
2 changed files with 66 additions and 3 deletions

View file

@ -5,6 +5,7 @@ import Jax from 'App/Services/Jax'
import MatchService from 'App/Services/MatchService'
import SummonerService from 'App/Services/SummonerService'
import SummonerBasicValidator from 'App/Validators/SummonerBasicValidator'
import SummonerOverviewValidator from 'App/Validators/SummonerOverviewValidator'
export default class SummonersController {
public async basic({ request, response }: HttpContextContract) {
@ -51,7 +52,30 @@ export default class SummonersController {
return response.json(finalJSON)
}
public async overview({ response }: HttpContextContract) {
public async overview({ request, response }: HttpContextContract) {
console.time('OVERVIEW_REQUEST')
const { puuid, accountId, region, season } = await request.validate(SummonerOverviewValidator)
const finalJSON: any = {}
// Summoner in DB
const summonerDB = await Summoner.firstOrCreate({ puuid: puuid })
// MATCHES BASIC
const matchIds = await summonerDB
.related('matchList')
.query()
.select('matchId')
.orderBy('matchId', 'desc')
.limit(10)
finalJSON.matchesDetails = await MatchService.getMatches(region, matchIds, summonerDB)
// TODO: STATS
// console.time('STATS')
// finalJSON.stats = 'todo'
// console.timeEnd('STATS')
console.timeEnd('OVERVIEW_REQUEST')
return response.json('OVERVIEW REQUEST')
}
}

View file

@ -3,6 +3,8 @@ import { MatchlistDto } from './Jax/src/Endpoints/MatchlistEndpoint'
import { SummonerDTO } from './Jax/src/Endpoints/SummonerEndpoint'
import Summoner from 'App/Models/Summoner'
import Database from '@ioc:Adonis/Lucid/Database'
import SummonerMatchlist from 'App/Models/SummonerMatchlist'
import MatchParser from 'App/Parsers/MatchParser'
class MatchService {
/**
@ -35,8 +37,6 @@ class MatchService {
}
/**
* Update the full MatchList of the summoner
* @param account of the summoner
* @param summonerDB summoner in the database
*/
public async updateMatchList(account: SummonerDTO, summonerDB: Summoner): Promise<MatchlistDto> {
console.time('matchList')
@ -72,6 +72,45 @@ class MatchService {
console.timeEnd('matchList')
return currentMatchListIds
}
/**
* Fetch list of matches for a specific Summoner
*/
public async getMatches(region: string, matchList: SummonerMatchlist[], summonerDB: Summoner) {
console.time('getMatches')
let matches: any[] = [] // Todo: add type of serialized matches here
const matchesToGetFromRiot: MatchlistDto = []
for (let i = 0; i < matchList.length; ++i) {
const matchSaved = await summonerDB
.related('matches')
.query()
.where('matchId', matchList[i].matchId)
.preload('match')
.first()
if (matchSaved) {
// TODO: Serialize match from DB + put it in Redis + push it in "matches"
} else {
matchesToGetFromRiot.push(matchList[i].matchId)
}
}
const requests = matchesToGetFromRiot.map((gameId) => Jax.Match.get(gameId, region))
const matchesFromApi = await Promise.all(requests)
/* If we have to store some matches in the db */
if (matchesFromApi.length !== 0) {
// Transform raw matches data
const parsedMatches = await MatchParser.parse(matchesFromApi)
// TODO: Serialize match from DB + put it in Redis + push it in "matches"
}
// Todo: Sort and return "matches"
console.timeEnd('getMatches')
}
}
export default new MatchService()