feat: add endpoint to get full champion stats (in progress)

This commit is contained in:
Valentin Kaelin 2019-12-03 22:23:19 +01:00
parent 3d60937996
commit 7f1ba47eb6
4 changed files with 43 additions and 4 deletions

View file

@ -72,6 +72,14 @@ class SummonerController {
console.timeEnd('all') console.timeEnd('all')
return response.json(finalJSON) return response.json(finalJSON)
} }
async champions({ request, response }) {
const puuid = request.input('puuid')
console.log('champions request', puuid)
const championStats = await StatsService.getChampionStats(puuid)
return response.json(championStats)
}
} }
module.exports = SummonerController module.exports = SummonerController

View file

@ -51,10 +51,11 @@ class MatchRepository {
} }
/** /**
* Get Summoner's statistics for the 5 most played champions * Get Summoner's statistics for the N most played champions
* @param puuid of the summoner * @param puuid of the summoner
* @param limit number of champions to fetch
*/ */
championStats(puuid) { championStats(puuid, limit = 5) {
const groupParams = { const groupParams = {
champion: { $first: "$champion" }, champion: { $first: "$champion" },
kills: { $sum: "$stats.kills" }, kills: { $sum: "$stats.kills" },
@ -63,7 +64,7 @@ class MatchRepository {
} }
const finalSteps = [ const finalSteps = [
{ $sort: { 'count': -1 } }, { $sort: { 'count': -1 } },
{ $limit: 5 }, { $limit: limit }
] ]
return this._aggregate(puuid, {}, [], '$champion.id', groupParams, finalSteps) return this._aggregate(puuid, {}, [], '$champion.id', groupParams, finalSteps)
} }
@ -77,6 +78,30 @@ class MatchRepository {
return this._aggregate(puuid, {}, [], groupId, {}, []) return this._aggregate(puuid, {}, [], groupId, {}, [])
} }
/**
* Get Summoner's complete statistics for the all played champs
* @param puuid of the summoner
*/
championCompleteStats(puuid) {
const groupParams = {
time: { $sum: "$time" },
champion: { $first: "$champion" },
kills: { $sum: "$stats.kills" },
deaths: { $sum: "$stats.deaths" },
assists: { $sum: "$stats.assists" },
minions: { $sum: "$stats.minions" },
vision: { $sum: "$stats.vision" },
gold: { $sum: "$stats.gold" },
dmgChamp: { $sum: "$stats.dmgChamp" },
dmgTaken: { $sum: "$stats.dmgTaken" },
kp: { $avg: "$stats.kp" },
}
const finalSteps = [
{ $sort: { 'count': -1 } }
]
return this._aggregate(puuid, {}, [], '$champion.id', groupParams, finalSteps)
}
/** /**
* Get Summoner's statistics for all played modes * Get Summoner's statistics for all played modes
* @param puuid of the summoner * @param puuid of the summoner

View file

@ -8,6 +8,11 @@ class StatsService {
this.matchRepository = MatchRepository this.matchRepository = MatchRepository
} }
async getChampionStats(puuid) {
const championStats = await this.matchRepository.championCompleteStats(puuid)
return championStats
}
async getSummonerStats(account) { async getSummonerStats(account) {
const globalStats = await this.matchRepository.globalStats(account.puuid) const globalStats = await this.matchRepository.globalStats(account.puuid)
const gamemodeStats = await this.matchRepository.gamemodeStats(account.puuid) const gamemodeStats = await this.matchRepository.gamemodeStats(account.puuid)
@ -24,7 +29,7 @@ class StatsService {
}) })
} }
} }
const championStats = await this.matchRepository.championStats(account.puuid) const championStats = await this.matchRepository.championStats(account.puuid, 5)
const championClassStats = await this.matchRepository.championClassStats(account.puuid) const championClassStats = await this.matchRepository.championClassStats(account.puuid)
const mates = await this.matchRepository.mates(account.puuid, account.name) const mates = await this.matchRepository.mates(account.puuid, account.name)

View file

@ -26,6 +26,7 @@ Route.get('/', async () => {
}) })
Route.post('/api', 'SummonerController.api') Route.post('/api', 'SummonerController.api')
Route.post('/champions', 'SummonerController.champions')
Route.post('/ddragon', 'DDragonController.index') Route.post('/ddragon', 'DDragonController.index')
Route.post('/match', 'MatchController.index') Route.post('/match', 'MatchController.index')
Route.post('/match-details', 'MatchController.show') Route.post('/match-details', 'MatchController.show')