From 33ff97a7acd2c50bc0de6a9eaaf5945049cab9a2 Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Sun, 10 Nov 2019 20:19:04 +0100 Subject: [PATCH] feat: get stats by champion class from api --- .../Controllers/Http/SummonerController.js | 106 +----------- server/app/Models/Match.js | 151 ++++++++++++++++++ server/app/Transformers/MatchTransformer.js | 2 +- 3 files changed, 158 insertions(+), 101 deletions(-) diff --git a/server/app/Controllers/Http/SummonerController.js b/server/app/Controllers/Http/SummonerController.js index 7042b9f..7afa260 100644 --- a/server/app/Controllers/Http/SummonerController.js +++ b/server/app/Controllers/Http/SummonerController.js @@ -53,7 +53,7 @@ class SummonerController { const matchList = summonerDB.matchList finalJSON.allMatches = matchList - // MATCHES DETAILS + // MATCHES BASIC const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId) finalJSON.matchesDetails = await MatchHelper.getMatches(account, gameIds, summonerDB) @@ -65,71 +65,9 @@ class SummonerController { // STATS console.time('STATS') - const gamemodeStats = await Match.query().aggregate([ - { - $match: { - summoner_puuid: account.puuid - } - }, - { - $group: { - _id: "$gamemode", - count: { $sum: 1 }, - wins: { - $sum: { - $cond: [ - { $eq: ["$result", "Win"] }, 1, 0 - ] - } - }, - losses: { - $sum: { - $cond: [ - { $eq: ["$result", "Fail"] }, 1, 0 - ] - } - } - } - } - ]) - - const roleStats = await Match.query().aggregate([ - { - $match: { - summoner_puuid: account.puuid, - role: { $not: { $eq: 'NONE' } } - } - }, - { - $group: { - _id: "$role", - count: { $sum: 1 }, - wins: { - $sum: { - $cond: [ - { $eq: ["$result", "Win"] }, 1, 0 - ] - } - }, - losses: { - $sum: { - $cond: [ - { $eq: ["$result", "Fail"] }, 1, 0 - ] - } - } - }, - }, - { - $project: { - role: "$_id", - count: "$count", - wins: "$wins", - losses: "$losses", - } - } - ]) - + const globalStats = await Match.globalStats(account.puuid) + const gamemodeStats = await Match.gamemodeStats(account.puuid) + const roleStats = await Match.roleStats(account.puuid) // Check if all roles are in the array const roles = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT'] for (const role of roles) { @@ -142,45 +80,13 @@ class SummonerController { }) } } + const championClassStats = await Match.championClassStats(account.puuid); - const globalStats = await Match.query().aggregate([ - { - $match: { - summoner_puuid: account.puuid - } - }, - { - $group: { - _id: null, - count: { $sum: 1 }, - time: { $sum: "$time" }, - wins: { - $sum: { - $cond: [ - { $eq: ["$result", "Win"] }, 1, 0 - ] - } - }, - losses: { - $sum: { - $cond: [ - { $eq: ["$result", "Fail"] }, 1, 0 - ] - } - }, - kills: { $sum: "$stats.kills" }, - deaths: { $sum: "$stats.deaths" }, - assists: { $sum: "$stats.assists" }, - minions: { $sum: "$stats.minions" }, - vision: { $sum: "$stats.vision" }, - kp: { $avg: "$stats.kp" }, - } - } - ]) finalJSON.stats = { global: globalStats[0], league: gamemodeStats, role: roleStats.sort(MatchHelper.sortTeamByRole), + class: championClassStats, } console.timeEnd('STATS') diff --git a/server/app/Models/Match.js b/server/app/Models/Match.js index d21c115..301f05d 100644 --- a/server/app/Models/Match.js +++ b/server/app/Models/Match.js @@ -4,6 +4,157 @@ const Model = use('Model') class Match extends Model { + /** + * Get Summoner's statistics for all played champion classes + * @param puuid of the summoner + */ + static championClassStats(puuid) { + return Match.query().aggregate([ + { + $match: { + summoner_puuid: puuid + } + }, + { + $group: { + _id: { "$arrayElemAt": [ "$champion.tags", 0 ] }, + count: { $sum: 1 }, + wins: { + $sum: { + $cond: [ + { $eq: ["$result", "Win"] }, 1, 0 + ] + } + }, + losses: { + $sum: { + $cond: [ + { $eq: ["$result", "Fail"] }, 1, 0 + ] + } + } + } + } + ]) + } + + /** + * Get Summoner's statistics for all played modes + * @param puuid of the summoner + */ + static gamemodeStats(puuid) { + return Match.query().aggregate([ + { + $match: { + summoner_puuid: puuid + } + }, + { + $group: { + _id: "$gamemode", + count: { $sum: 1 }, + wins: { + $sum: { + $cond: [ + { $eq: ["$result", "Win"] }, 1, 0 + ] + } + }, + losses: { + $sum: { + $cond: [ + { $eq: ["$result", "Fail"] }, 1, 0 + ] + } + } + } + } + ]) + } + + /** + * Get global Summoner's statistics + * @param puuid of the summoner + */ + static globalStats(puuid) { + return Match.query().aggregate([ + { + $match: { + summoner_puuid: puuid + } + }, + { + $group: { + _id: null, + count: { $sum: 1 }, + time: { $sum: "$time" }, + wins: { + $sum: { + $cond: [ + { $eq: ["$result", "Win"] }, 1, 0 + ] + } + }, + losses: { + $sum: { + $cond: [ + { $eq: ["$result", "Fail"] }, 1, 0 + ] + } + }, + kills: { $sum: "$stats.kills" }, + deaths: { $sum: "$stats.deaths" }, + assists: { $sum: "$stats.assists" }, + minions: { $sum: "$stats.minions" }, + vision: { $sum: "$stats.vision" }, + kp: { $avg: "$stats.kp" }, + } + } + ]) + } + + /** + * Get Summoner's statistics for the 5 differnt roles + * @param puuid of the summoner + */ + static roleStats(puuid) { + return Match.query().aggregate([ + { + $match: { + summoner_puuid: puuid, + role: { $not: { $eq: 'NONE' } } + } + }, + { + $group: { + _id: "$role", + count: { $sum: 1 }, + wins: { + $sum: { + $cond: [ + { $eq: ["$result", "Win"] }, 1, 0 + ] + } + }, + losses: { + $sum: { + $cond: [ + { $eq: ["$result", "Fail"] }, 1, 0 + ] + } + } + }, + }, + { + $project: { + role: "$_id", + count: "$count", + wins: "$wins", + losses: "$losses", + } + } + ]) + } } module.exports = Match diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js index 65cfb96..7a26e87 100644 --- a/server/app/Transformers/MatchTransformer.js +++ b/server/app/Transformers/MatchTransformer.js @@ -33,7 +33,7 @@ class MatchTransformer { getPlayerData(player, detailed, teamStats = {}) { const identity = this.match.participantIdentities.find(p => p.participantId === player.participantId) const name = identity.player.summonerName - const champion = (({ id, name }) => ({ id, name }))(Object.entries(this.champions).find(([, champion]) => Number(champion.key) === player.championId)[1]) + const champion = (({ id, name, tags }) => ({ id, name, tags }))(Object.entries(this.champions).find(([, champion]) => Number(champion.key) === player.championId)[1]) const role = this.MatchHelper.getRoleName(player.timeline) const level = player.stats.champLevel