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

111 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-10-11 14:35:23 +00:00
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
2020-10-11 15:31:16 +00:00
import DetailedMatch, { DetailedMatchModel } from 'App/Models/DetailedMatch'
import { ParticipantDetails } from 'App/Models/Match'
2020-10-11 14:35:23 +00:00
import Summoner from 'App/Models/Summoner'
2020-10-11 15:31:16 +00:00
import Jax from 'App/Services/Jax'
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 SummonerService from 'App/Services/SummonerService'
import DetailedMatchTransformer from 'App/Transformers/DetailedMatchTransformer'
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
/**
* Get the soloQ rank of all the players of the team
* @param summoner all the data of the summoner
* @param region of the match
*/
private async getPlayerRank (summoner: ParticipantDetails, region: string) {
const account = await Jax.Summoner.summonerId(summoner.summonerId, region)
2020-10-11 15:31:16 +00:00
if (account) {
const ranked = await SummonerService.getRanked(account, region)
summoner.rank = ranked.soloQ ? (({ tier, shortName }) => ({ tier, shortName }))(ranked.soloQ) : null
} else {
summoner.rank = null
}
return summoner
}
2020-10-11 14:35:23 +00:00
/**
* POST - Return data from matches searched by gameIds
* @param ctx
*/
public async index ({ request, response }: HttpContextContract) {
console.log('More Matches Request')
const { puuid, accountId, region, gameIds, season } = await request.validate(MatchesIndexValidator)
2020-10-11 14:35:23 +00:00
const summonerDB = await Summoner.findOne({ puuid })
if (!summonerDB) {
return response.json(null)
}
const matches = await MatchService.getMatches(puuid, accountId, region, gameIds, summonerDB)
await summonerDB.save()
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
*/
public async show ({ request, response }: HttpContextContract) {
console.time('MatchDetails')
const { gameId, region } = await request.validate(DetailedMatchValidator)
let matchDetails: DetailedMatchModel
2021-03-15 20:29:36 +00:00
const alreadySaved = await DetailedMatch.findOne({ gameId, region })
2020-10-11 15:31:16 +00:00
if (alreadySaved) {
console.log('MATCH DETAILS ALREADY SAVED')
matchDetails = alreadySaved
} else {
const match = await Jax.Match.get(gameId, region)
matchDetails = await DetailedMatchTransformer.transform(match)
await DetailedMatch.create(matchDetails)
}
console.timeEnd('MatchDetails')
return response.json({
matchDetails,
})
}
/**
* POST - Return ranks of players for a specific game
* @param ctx
*/
public async showRanks ({ request, response }: HttpContextContract) {
console.time('Ranks')
const { gameId, region } = await request.validate(DetailedMatchValidator)
let matchDetails = await DetailedMatch.findOne({ gameId, region })
if (!matchDetails) {
return response.json(null)
}
const requestsBlue = matchDetails.blueTeam.players.map(p => this.getPlayerRank(p, region))
matchDetails.blueTeam.players = await Promise.all(requestsBlue)
const requestsRed = matchDetails.redTeam.players.map(p => this.getPlayerRank(p, region))
matchDetails.redTeam.players = await Promise.all(requestsRed)
matchDetails.save()
console.timeEnd('Ranks')
return response.json({
blueTeam: matchDetails.blueTeam.players,
redTeam: matchDetails.redTeam.players,
})
}
2020-10-04 20:05:44 +00:00
}