LeagueStats/server/app/Repositories/MatchRepository.js

180 lines
4.5 KiB
JavaScript
Raw Normal View History

'use strict'
class MatchRepository {
static get inject() {
return ['App/Models/Match']
}
constructor(Match) {
this.Match = Match
}
/**
2019-12-01 13:38:08 +00:00
* Build the aggregate mongo query
* @param {Number} puuid
* @param {Object} matchParams
* @param {Array} intermediateSteps
* @param {*} groupId
* @param {Object} groupParams
* @param {Array} finalSteps
*/
2019-12-01 13:38:08 +00:00
_aggregate(puuid, matchParams, intermediateSteps, groupId, groupParams, finalSteps) {
return this.Match.query().aggregate([
{
$match: {
summoner_puuid: puuid,
2019-11-30 21:18:10 +00:00
result: { $not: { $eq: 'Remake' } },
2019-12-01 13:38:08 +00:00
gamemode: { $nin: [800, 810, 820, 830, 840, 850] },
...matchParams
}
},
2019-12-01 13:38:08 +00:00
...intermediateSteps,
{
$group: {
2019-12-01 13:38:08 +00:00
_id: groupId,
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
},
2019-12-01 13:38:08 +00:00
...groupParams
},
},
2019-12-01 13:38:08 +00:00
...finalSteps
])
}
/**
* Get Summoner's statistics for the N most played champions
2019-12-01 13:38:08 +00:00
* @param puuid of the summoner
* @param limit number of champions to fetch
2019-12-01 13:38:08 +00:00
*/
championStats(puuid, limit = 5) {
2019-12-01 13:38:08 +00:00
const groupParams = {
champion: { $first: "$champion" },
kills: { $sum: "$stats.kills" },
deaths: { $sum: "$stats.deaths" },
assists: { $sum: "$stats.assists" },
}
const finalSteps = [
{ $sort: { 'count': -1 } },
{ $limit: limit }
2019-12-01 13:38:08 +00:00
]
return this._aggregate(puuid, {}, [], '$champion.id', groupParams, finalSteps)
}
/**
* Get Summoner's statistics for all played champion classes
* @param puuid of the summoner
*/
championClassStats(puuid) {
2019-12-01 13:38:08 +00:00
const groupId = { "$arrayElemAt": ["$champion.roles", 0] }
return this._aggregate(puuid, {}, [], groupId, {}, [])
}
/**
* Get Summoner's complete statistics for the all played champs
* @param puuid of the summoner
* @param queue of the matches to fetch, if null get all matches
*/
championCompleteStats(puuid, queue) {
const matchParams = queue ? {
gamemode: { $eq: Number(queue) },
} : {}
const groupParams = {
time: { $sum: "$time" },
2019-12-21 16:56:31 +00:00
gameLength: { $avg: "$time" },
date: { $max: "$date" },
champion: { $first: "$champion" },
kills: { $sum: "$stats.kills" },
deaths: { $sum: "$stats.deaths" },
assists: { $sum: "$stats.assists" },
2019-12-21 16:56:31 +00:00
minions: { $avg: "$stats.minions" },
gold: { $avg: "$stats.gold" },
dmgChamp: { $avg: "$stats.dmgChamp" },
dmgTaken: { $avg: "$stats.dmgTaken" },
kp: { $avg: "$stats.kp" },
}
const finalSteps = [
{ $sort: { 'count': -1 } }
]
return this._aggregate(puuid, matchParams, [], '$champion.id', groupParams, finalSteps)
}
/**
* Get Summoner's statistics for all played modes
* @param puuid of the summoner
*/
gamemodeStats(puuid) {
2019-12-01 13:38:08 +00:00
return this._aggregate(puuid, {}, [], '$gamemode', {}, [])
}
/**
* Get global Summoner's statistics
* @param puuid of the summoner
*/
globalStats(puuid) {
2019-12-01 13:38:08 +00:00
const groupParams = {
time: { $sum: "$time" },
kills: { $sum: "$stats.kills" },
deaths: { $sum: "$stats.deaths" },
assists: { $sum: "$stats.assists" },
minions: { $sum: "$stats.minions" },
vision: { $sum: "$stats.vision" },
kp: { $avg: "$stats.kp" },
}
return this._aggregate(puuid, {}, [], null, groupParams, [])
}
/**
* Get Summoner's statistics for the 5 differnt roles
* @param puuid of the summoner
*/
roleStats(puuid) {
2019-12-01 13:38:08 +00:00
const matchParams = {
role: { $not: { $eq: 'NONE' } }
}
const finalSteps = [
{
$project: {
role: "$_id",
count: "$count",
wins: "$wins",
losses: "$losses",
}
}
2019-12-01 13:38:08 +00:00
]
return this._aggregate(puuid, matchParams, [], '$role', {}, finalSteps)
}
/**
* Get Summoner's mates list
* @param puuid of the summoner
* @param summonerName of the summoner
*/
mates(puuid, summonerName) {
2019-12-01 13:38:08 +00:00
const intermediateSteps = [
{ $unwind: "$allyTeam" },
2019-12-01 13:38:08 +00:00
]
const finalSteps = [
{
$match: {
_id: { $not: { $eq: summonerName } },
'count': { $gte: 2 }
}
},
{ $sort: { 'count': -1 } },
{ $limit: 15 },
2019-12-01 13:38:08 +00:00
]
return this._aggregate(puuid, {}, intermediateSteps, '$allyTeam.name', {}, finalSteps)
}
}
module.exports = MatchRepository