LeagueStats/server/app/Controllers/Http/MatchesController.ts

74 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-10-11 14:35:23 +00:00
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Match from 'App/Models/Match'
import MatchPlayerRankParser from 'App/Parsers/MatchPlayerRankParser'
import DetailedMatchSerializer from 'App/Serializers/DetailedMatchSerializer'
import MatchPlayerRankSerializer from 'App/Serializers/MatchPlayerRankSerializer'
import { SerializedMatch } from 'App/Serializers/SerializedTypes'
2020-10-11 14:35:23 +00:00
import MatchService from 'App/Services/MatchService'
import StatsService from 'App/Services/StatsService'
2020-10-11 15:31:16 +00:00
import DetailedMatchValidator from 'App/Validators/DetailedMatchValidator'
2020-10-11 14:35:23 +00:00
import MatchesIndexValidator from 'App/Validators/MatchesIndexValidator'
2020-10-04 20:05:44 +00:00
export default class MatchesController {
2020-10-11 15:31:16 +00:00
/**
* POST - Return data from matches searched by matchIds
* @param ctx
2020-10-11 15:31:16 +00:00
*/
public async index({ request, response }: HttpContextContract) {
const { puuid, region, lastMatchId, season } = await request.validate(MatchesIndexValidator)
const matchIds = await MatchService.getMatchIdsToFetch(lastMatchId, puuid, season)
let matches: SerializedMatch[] = []
if (matchIds.length > 0) {
matches = await MatchService.getMatches(region, matchIds, puuid)
}
2020-10-11 14:35:23 +00:00
const stats = await StatsService.getSummonerStats(puuid, season)
2020-10-11 14:35:23 +00:00
return response.json({
matches,
stats,
})
}
2020-10-11 15:31:16 +00:00
/**
* POST - Return details data for one specific match
* @param ctx
2020-10-11 15:31:16 +00:00
*/
public async show({ request, response }: HttpContextContract) {
2020-10-11 15:31:16 +00:00
console.time('MatchDetails')
const { matchId } = await request.validate(DetailedMatchValidator)
2020-10-11 15:31:16 +00:00
const match = await Match.query()
.where('id', matchId)
.preload('teams')
.preload('players', (playersQuery) => {
playersQuery.preload('ranks')
})
.firstOrFail()
2021-03-15 20:29:36 +00:00
const { match: matchDetails, ranksLoaded } = DetailedMatchSerializer.serializeOneMatch(match)
2020-10-11 15:31:16 +00:00
console.timeEnd('MatchDetails')
return response.json({
matchDetails,
ranksLoaded,
2020-10-11 15:31:16 +00:00
})
}
/**
* POST - Return ranks of players for a specific game
* @param ctx
2020-10-11 15:31:16 +00:00
*/
public async showRanks({ request, response }: HttpContextContract) {
2020-10-11 15:31:16 +00:00
console.time('Ranks')
const { matchId } = await request.validate(DetailedMatchValidator)
const match = await Match.query().where('id', matchId).preload('players').firstOrFail()
const parsedRanks = await MatchPlayerRankParser.parse(match)
const serializedRanks = MatchPlayerRankSerializer.serialize(parsedRanks)
2020-10-11 15:31:16 +00:00
console.timeEnd('Ranks')
return response.json(serializedRanks)
2020-10-11 15:31:16 +00:00
}
2020-10-04 20:05:44 +00:00
}