From 7f1ba47eb6f34b39e69b20bcf085dfda94d8a78f Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Tue, 3 Dec 2019 22:23:19 +0100 Subject: [PATCH] feat: add endpoint to get full champion stats (in progress) --- .../Controllers/Http/SummonerController.js | 8 +++++ server/app/Repositories/MatchRepository.js | 31 +++++++++++++++++-- server/app/Services/StatsService.js | 7 ++++- server/start/routes.js | 1 + 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/server/app/Controllers/Http/SummonerController.js b/server/app/Controllers/Http/SummonerController.js index 254e5ff..337270f 100644 --- a/server/app/Controllers/Http/SummonerController.js +++ b/server/app/Controllers/Http/SummonerController.js @@ -72,6 +72,14 @@ class SummonerController { console.timeEnd('all') 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 diff --git a/server/app/Repositories/MatchRepository.js b/server/app/Repositories/MatchRepository.js index 42da9ff..8068ece 100644 --- a/server/app/Repositories/MatchRepository.js +++ b/server/app/Repositories/MatchRepository.js @@ -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 limit number of champions to fetch */ - championStats(puuid) { + championStats(puuid, limit = 5) { const groupParams = { champion: { $first: "$champion" }, kills: { $sum: "$stats.kills" }, @@ -63,7 +64,7 @@ class MatchRepository { } const finalSteps = [ { $sort: { 'count': -1 } }, - { $limit: 5 }, + { $limit: limit } ] return this._aggregate(puuid, {}, [], '$champion.id', groupParams, finalSteps) } @@ -77,6 +78,30 @@ class MatchRepository { 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 * @param puuid of the summoner diff --git a/server/app/Services/StatsService.js b/server/app/Services/StatsService.js index 2d0420f..747fe28 100644 --- a/server/app/Services/StatsService.js +++ b/server/app/Services/StatsService.js @@ -8,6 +8,11 @@ class StatsService { this.matchRepository = MatchRepository } + async getChampionStats(puuid) { + const championStats = await this.matchRepository.championCompleteStats(puuid) + return championStats + } + async getSummonerStats(account) { const globalStats = await this.matchRepository.globalStats(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 mates = await this.matchRepository.mates(account.puuid, account.name) diff --git a/server/start/routes.js b/server/start/routes.js index a283df5..4692636 100644 --- a/server/start/routes.js +++ b/server/start/routes.js @@ -26,6 +26,7 @@ Route.get('/', async () => { }) Route.post('/api', 'SummonerController.api') +Route.post('/champions', 'SummonerController.champions') Route.post('/ddragon', 'DDragonController.index') Route.post('/match', 'MatchController.index') Route.post('/match-details', 'MatchController.show')