From b4a2edfd323951cbc9ac67237d7d2f4317c374b3 Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Tue, 12 Nov 2019 19:47:23 +0100 Subject: [PATCH] refactor: create a Repository to store mongo requests --- .../app/Controllers/Http/MatchController.js | 29 +-- .../Controllers/Http/SummonerController.js | 28 +-- server/app/Helpers/StatsHelper.js | 40 ++++ server/app/Models/Match.js | 195 ------------------ server/app/Repositories/MatchRepository.js | 189 +++++++++++++++++ 5 files changed, 233 insertions(+), 248 deletions(-) create mode 100644 server/app/Helpers/StatsHelper.js create mode 100644 server/app/Repositories/MatchRepository.js diff --git a/server/app/Controllers/Http/MatchController.js b/server/app/Controllers/Http/MatchController.js index 4d91435..7b7a2f0 100644 --- a/server/app/Controllers/Http/MatchController.js +++ b/server/app/Controllers/Http/MatchController.js @@ -3,8 +3,8 @@ const Jax = use('Jax') const DetailedMatch = use('App/Models/DetailedMatch') const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer') -const Match = use('App/Models/Match') const MatchHelper = use('App/Helpers/MatchHelper') +const StatsHelper = use('App/Helpers/StatsHelper') const Summoner = use('App/Models/Summoner') class MatchController { @@ -21,32 +21,7 @@ class MatchController { await summonerDB.save() - // Stats - 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) { - if (!roleStats.find(r => r.role === role)) { - roleStats.push({ - count: 0, - losses: 0, - role, - wins: 0 - }) - } - } - const championClassStats = await Match.championClassStats(account.puuid) - const mates = await Match.mates(account.puuid, account.name) - - const stats = { - global: globalStats[0], - league: gamemodeStats, - role: roleStats.sort(MatchHelper.sortTeamByRole), - class: championClassStats, - mates, - } + const stats = await StatsHelper.getSummonerStats(account) return response.json({ matches, diff --git a/server/app/Controllers/Http/SummonerController.js b/server/app/Controllers/Http/SummonerController.js index 3fb751a..76eb1d7 100644 --- a/server/app/Controllers/Http/SummonerController.js +++ b/server/app/Controllers/Http/SummonerController.js @@ -2,8 +2,8 @@ const Jax = use('Jax') const MatchHelper = use('App/Helpers/MatchHelper') +const StatsHelper = use('App/Helpers/StatsHelper') const Summoner = use('App/Models/Summoner') -const Match = use('App/Models/Match') class SummonerController { /** @@ -62,31 +62,7 @@ class SummonerController { // STATS console.time('STATS') - 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) { - if (!roleStats.find(r => r.role === role)) { - roleStats.push({ - count: 0, - losses: 0, - role, - wins: 0 - }) - } - } - const championClassStats = await Match.championClassStats(account.puuid) - const mates = await Match.mates(account.puuid, account.name) - - finalJSON.stats = { - global: globalStats[0], - league: gamemodeStats, - role: roleStats.sort(MatchHelper.sortTeamByRole), - class: championClassStats, - mates, - } + finalJSON.stats = await StatsHelper.getSummonerStats(account) console.timeEnd('STATS') // SAVE IN DB diff --git a/server/app/Helpers/StatsHelper.js b/server/app/Helpers/StatsHelper.js new file mode 100644 index 0000000..c25b74e --- /dev/null +++ b/server/app/Helpers/StatsHelper.js @@ -0,0 +1,40 @@ +'use strict' + +const MatchHelper = use('App/Helpers/MatchHelper') +const MatchRepository = make('App/Repositories/MatchRepository') + +class StatsHelper { + constructor() { + this.matchRepository = MatchRepository + } + + async getSummonerStats(account) { + const globalStats = await this.matchRepository.globalStats(account.puuid) + const gamemodeStats = await this.matchRepository.gamemodeStats(account.puuid) + const roleStats = await this.matchRepository.roleStats(account.puuid) + // Check if all roles are in the array + const roles = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT'] + for (const role of roles) { + if (!roleStats.find(r => r.role === role)) { + roleStats.push({ + count: 0, + losses: 0, + role, + wins: 0 + }) + } + } + const championClassStats = await this.matchRepository.championClassStats(account.puuid) + const mates = await this.matchRepository.mates(account.puuid, account.name) + + return { + global: globalStats[0], + league: gamemodeStats, + role: roleStats.sort(MatchHelper.sortTeamByRole), + class: championClassStats, + mates, + } + } +} + +module.exports = new StatsHelper() \ No newline at end of file diff --git a/server/app/Models/Match.js b/server/app/Models/Match.js index 419d145..d21c115 100644 --- a/server/app/Models/Match.js +++ b/server/app/Models/Match.js @@ -4,201 +4,6 @@ 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", - } - } - ]) - } - - /** - * Get Summoner's mates list - * @param puuid of the summoner - * @param summonerName of the summoner - */ - static mates(puuid, summonerName) { - return Match.query().aggregate([ - { - $match: { - summoner_puuid: puuid - } - }, - { $unwind: "$allyTeam" }, - { - $group: { - _id: "$allyTeam.name", - count: { $sum: 1 }, - wins: { - $sum: { - $cond: [ - { $eq: ["$result", "Win"] }, 1, 0 - ] - } - }, - losses: { - $sum: { - $cond: [ - { $eq: ["$result", "Fail"] }, 1, 0 - ] - } - } - }, - }, - { - $match: { - _id: { $not: { $eq: summonerName } }, - 'count': { $gte: 2 } - } - }, - { $sort: { 'count': -1 } }, - { $limit: 15 }, - ]) - } } module.exports = Match diff --git a/server/app/Repositories/MatchRepository.js b/server/app/Repositories/MatchRepository.js new file mode 100644 index 0000000..f3d24dc --- /dev/null +++ b/server/app/Repositories/MatchRepository.js @@ -0,0 +1,189 @@ +'use strict' + +class MatchRepository { + static get inject() { + return ['App/Models/Match'] + } + + constructor(Match) { + this.Match = Match + } + + /** + * Get Summoner's statistics for all played champion classes + * @param puuid of the summoner + */ + championClassStats(puuid) { + return this.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 + */ + gamemodeStats(puuid) { + return this.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 + */ + globalStats(puuid) { + return this.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 + */ + roleStats(puuid) { + return this.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", + } + } + ]) + } + + /** + * Get Summoner's mates list + * @param puuid of the summoner + * @param summonerName of the summoner + */ + mates(puuid, summonerName) { + return this.Match.query().aggregate([ + { + $match: { + summoner_puuid: puuid + } + }, + { $unwind: "$allyTeam" }, + { + $group: { + _id: "$allyTeam.name", + count: { $sum: 1 }, + wins: { + $sum: { + $cond: [{ $eq: ["$result", "Win"] }, 1, 0] + } + }, + losses: { + $sum: { + $cond: [{ $eq: ["$result", "Fail"] }, 1, 0] + } + } + }, + }, + { + $match: { + _id: { $not: { $eq: summonerName } }, + 'count': { $gte: 2 } + } + }, + { $sort: { 'count': -1 } }, + { $limit: 15 }, + ]) + } +} + +module.exports = MatchRepository