2019-11-12 18:47:23 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
|
|
class MatchRepository {
|
|
|
|
|
static get inject() {
|
|
|
|
|
return ['App/Models/Match']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(Match) {
|
|
|
|
|
this.Match = Match
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 13:26:27 +00:00
|
|
|
/**
|
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-11-24 13:26:27 +00:00
|
|
|
*/
|
2019-12-01 13:38:08 +00:00
|
|
|
_aggregate(puuid, matchParams, intermediateSteps, groupId, groupParams, finalSteps) {
|
2019-11-24 13:26:27 +00:00
|
|
|
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-11-24 13:26:27 +00:00
|
|
|
}
|
|
|
|
|
},
|
2019-12-01 13:38:08 +00:00
|
|
|
...intermediateSteps,
|
2019-11-24 13:26:27 +00:00
|
|
|
{
|
|
|
|
|
$group: {
|
2019-12-01 13:38:08 +00:00
|
|
|
_id: groupId,
|
2019-11-24 13:26:27 +00:00
|
|
|
count: { $sum: 1 },
|
|
|
|
|
wins: {
|
|
|
|
|
$sum: {
|
2020-01-12 00:31:28 +00:00
|
|
|
$cond: [{ $eq: ['$result', 'Win'] }, 1, 0]
|
2019-11-24 13:26:27 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
losses: {
|
|
|
|
|
$sum: {
|
2020-01-12 00:31:28 +00:00
|
|
|
$cond: [{ $eq: ['$result', 'Fail'] }, 1, 0]
|
2019-11-24 13:26:27 +00:00
|
|
|
}
|
|
|
|
|
},
|
2019-12-01 13:38:08 +00:00
|
|
|
...groupParams
|
|
|
|
|
},
|
2019-11-24 13:26:27 +00:00
|
|
|
},
|
2019-12-01 13:38:08 +00:00
|
|
|
...finalSteps
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-12-03 21:23:19 +00:00
|
|
|
* Get Summoner's statistics for the N most played champions
|
2019-12-01 13:38:08 +00:00
|
|
|
* @param puuid of the summoner
|
2019-12-03 21:23:19 +00:00
|
|
|
* @param limit number of champions to fetch
|
2019-12-01 13:38:08 +00:00
|
|
|
*/
|
2019-12-03 21:23:19 +00:00
|
|
|
championStats(puuid, limit = 5) {
|
2019-12-01 13:38:08 +00:00
|
|
|
const groupParams = {
|
2020-01-12 00:31:28 +00:00
|
|
|
champion: { $first: '$champion' },
|
|
|
|
|
kills: { $sum: '$stats.kills' },
|
|
|
|
|
deaths: { $sum: '$stats.deaths' },
|
|
|
|
|
assists: { $sum: '$stats.assists' },
|
2019-12-01 13:38:08 +00:00
|
|
|
}
|
|
|
|
|
const finalSteps = [
|
2019-11-24 13:26:27 +00:00
|
|
|
{ $sort: { 'count': -1 } },
|
2019-12-03 21:23:19 +00:00
|
|
|
{ $limit: limit }
|
2019-12-01 13:38:08 +00:00
|
|
|
]
|
|
|
|
|
return this._aggregate(puuid, {}, [], '$champion.id', groupParams, finalSteps)
|
2019-11-24 13:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-12 18:47:23 +00:00
|
|
|
/**
|
|
|
|
|
* Get Summoner's statistics for all played champion classes
|
|
|
|
|
* @param puuid of the summoner
|
|
|
|
|
*/
|
|
|
|
|
championClassStats(puuid) {
|
2020-01-12 00:31:28 +00:00
|
|
|
const groupId = { '$arrayElemAt': ['$champion.roles', 0] }
|
2019-12-01 13:38:08 +00:00
|
|
|
return this._aggregate(puuid, {}, [], groupId, {}, [])
|
2019-11-12 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-03 21:23:19 +00:00
|
|
|
/**
|
|
|
|
|
* Get Summoner's complete statistics for the all played champs
|
|
|
|
|
* @param puuid of the summoner
|
2019-12-23 21:13:42 +00:00
|
|
|
* @param queue of the matches to fetch, if null get all matches
|
2019-12-03 21:23:19 +00:00
|
|
|
*/
|
2019-12-23 21:13:42 +00:00
|
|
|
championCompleteStats(puuid, queue) {
|
|
|
|
|
const matchParams = queue ? {
|
|
|
|
|
gamemode: { $eq: Number(queue) },
|
|
|
|
|
} : {}
|
2019-12-03 21:23:19 +00:00
|
|
|
const groupParams = {
|
2020-01-12 00:31:28 +00:00
|
|
|
time: { $sum: '$time' },
|
|
|
|
|
gameLength: { $avg: '$time' },
|
|
|
|
|
date: { $max: '$date' },
|
|
|
|
|
champion: { $first: '$champion' },
|
|
|
|
|
kills: { $sum: '$stats.kills' },
|
|
|
|
|
deaths: { $sum: '$stats.deaths' },
|
|
|
|
|
assists: { $sum: '$stats.assists' },
|
|
|
|
|
minions: { $avg: '$stats.minions' },
|
|
|
|
|
gold: { $avg: '$stats.gold' },
|
|
|
|
|
dmgChamp: { $avg: '$stats.dmgChamp' },
|
|
|
|
|
dmgTaken: { $avg: '$stats.dmgTaken' },
|
|
|
|
|
kp: { $avg: '$stats.kp' },
|
2019-12-03 21:23:19 +00:00
|
|
|
}
|
|
|
|
|
const finalSteps = [
|
|
|
|
|
{ $sort: { 'count': -1 } }
|
|
|
|
|
]
|
2019-12-23 21:13:42 +00:00
|
|
|
return this._aggregate(puuid, matchParams, [], '$champion.id', groupParams, finalSteps)
|
2019-12-03 21:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-12 18:47:23 +00:00
|
|
|
/**
|
|
|
|
|
* 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', {}, [])
|
2019-11-12 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get global Summoner's statistics
|
|
|
|
|
* @param puuid of the summoner
|
|
|
|
|
*/
|
|
|
|
|
globalStats(puuid) {
|
2019-12-01 13:38:08 +00:00
|
|
|
const groupParams = {
|
2020-01-12 00:31:28 +00:00
|
|
|
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' },
|
2019-12-01 13:38:08 +00:00
|
|
|
}
|
|
|
|
|
return this._aggregate(puuid, {}, [], null, groupParams, [])
|
2019-11-12 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-12 00:31:28 +00:00
|
|
|
/**
|
|
|
|
|
* Get Summoner's all records
|
|
|
|
|
* @param puuid of the summoner
|
|
|
|
|
*/
|
|
|
|
|
records(puuid) {
|
|
|
|
|
return this.Match.query().aggregate([
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
summoner_puuid: puuid,
|
|
|
|
|
result: { $not: { $eq: 'Remake' } },
|
|
|
|
|
gamemode: { $nin: [800, 810, 820, 830, 840, 850] },
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: null,
|
|
|
|
|
maxKills: { $max: '$stats.kills' },
|
|
|
|
|
maxDeaths: { $max: '$stats.deaths' },
|
|
|
|
|
maxAssists: { $max: '$stats.assists' },
|
|
|
|
|
maxGold: { $max: '$stats.gold' },
|
|
|
|
|
maxTime: { $max: '$time' },
|
|
|
|
|
maxMinions: { $max: '$stats.minions' },
|
2020-01-25 11:33:28 +00:00
|
|
|
maxKda: { $max: '$stats.realKda' },
|
2020-01-12 00:31:28 +00:00
|
|
|
maxDmgTaken: { $max: '$stats.dmgTaken' },
|
|
|
|
|
maxDmgChamp: { $max: '$stats.dmgChamp' },
|
|
|
|
|
maxDmgObj: { $max: '$stats.dmgObj' },
|
|
|
|
|
maxKp: { $max: '$stats.kp' },
|
|
|
|
|
maxVision: { $max: '$stats.vision' },
|
|
|
|
|
docs: {
|
|
|
|
|
'$push': {
|
|
|
|
|
'champion': '$champion',
|
|
|
|
|
'gameId': '$gameId',
|
|
|
|
|
'kills': '$stats.kills',
|
|
|
|
|
'deaths': '$stats.deaths',
|
|
|
|
|
'assists': '$stats.assists',
|
|
|
|
|
'gold': '$stats.gold',
|
|
|
|
|
'time': '$time',
|
|
|
|
|
'minions': '$stats.minions',
|
2020-01-25 11:33:28 +00:00
|
|
|
'kda': '$stats.realKda',
|
2020-01-12 00:31:28 +00:00
|
|
|
'dmgTaken': '$stats.dmgTaken',
|
|
|
|
|
'dmgChamp': '$stats.dmgChamp',
|
|
|
|
|
'dmgObj': '$stats.dmgObj',
|
|
|
|
|
'kp': '$stats.kp',
|
|
|
|
|
'vision': '$stats.vision',
|
|
|
|
|
'result': '$result',
|
|
|
|
|
'date': '$date',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0,
|
|
|
|
|
maxKills: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.kills', '$maxKills'] } } }, 0] },
|
|
|
|
|
maxDeaths: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.deaths', '$maxDeaths'] } } }, 0] },
|
|
|
|
|
maxAssists: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.assists', '$maxAssists'] } } }, 0] },
|
|
|
|
|
maxGold: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.gold', '$maxGold'] } } }, 0] },
|
|
|
|
|
maxTime: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.time', '$maxTime'] } } }, 0] },
|
|
|
|
|
maxMinions: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.minions', '$maxMinions'] } } }, 0] },
|
|
|
|
|
maxKda: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.kda', '$maxKda'] } } }, 0] },
|
|
|
|
|
maxDmgTaken: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.dmgTaken', '$maxDmgTaken'] } } }, 0] },
|
|
|
|
|
maxDmgChamp: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.dmgChamp', '$maxDmgChamp'] } } }, 0] },
|
|
|
|
|
maxDmgObj: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.dmgObj', '$maxDmgObj'] } } }, 0] },
|
|
|
|
|
maxKp: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.kp', '$maxKp'] } } }, 0] },
|
|
|
|
|
maxVision: { $arrayElemAt: [{ $filter: { input: '$docs', cond: { $eq: ['$$this.vision', '$maxVision'] } } }, 0] },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 18:47:23 +00:00
|
|
|
/**
|
|
|
|
|
* 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 = [
|
2019-11-12 18:47:23 +00:00
|
|
|
{
|
|
|
|
|
$project: {
|
2020-01-12 00:31:28 +00:00
|
|
|
role: '$_id',
|
|
|
|
|
count: '$count',
|
|
|
|
|
wins: '$wins',
|
|
|
|
|
losses: '$losses',
|
2019-11-12 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-12-01 13:38:08 +00:00
|
|
|
]
|
|
|
|
|
return this._aggregate(puuid, matchParams, [], '$role', {}, finalSteps)
|
2019-11-12 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Summoner's mates list
|
|
|
|
|
* @param puuid of the summoner
|
|
|
|
|
*/
|
2020-01-08 20:16:21 +00:00
|
|
|
mates(puuid) {
|
2019-12-01 13:38:08 +00:00
|
|
|
const intermediateSteps = [
|
2020-01-12 00:31:28 +00:00
|
|
|
{ $unwind: '$allyTeam' },
|
2019-12-01 13:38:08 +00:00
|
|
|
]
|
2020-01-08 20:16:21 +00:00
|
|
|
const groupParams = {
|
2020-01-12 00:31:28 +00:00
|
|
|
account_id: { $first: '$account_id' },
|
|
|
|
|
name: { $first: '$allyTeam.name' },
|
|
|
|
|
mateId: { $first: '$allyTeam.account_id' },
|
2020-01-08 20:16:21 +00:00
|
|
|
}
|
2019-12-01 13:38:08 +00:00
|
|
|
const finalSteps = [
|
2020-01-08 20:16:21 +00:00
|
|
|
{
|
2020-01-12 00:31:28 +00:00
|
|
|
'$addFields': {
|
|
|
|
|
'idEq': { '$eq': ['$mateId', '$account_id'] }
|
2020-01-08 20:16:21 +00:00
|
|
|
}
|
|
|
|
|
},
|
2019-11-12 18:47:23 +00:00
|
|
|
{
|
|
|
|
|
$match: {
|
2020-01-08 20:16:21 +00:00
|
|
|
'idEq': false,
|
2019-11-12 18:47:23 +00:00
|
|
|
'count': { $gte: 2 }
|
2020-01-08 20:16:21 +00:00
|
|
|
},
|
2019-11-12 18:47:23 +00:00
|
|
|
},
|
|
|
|
|
{ $sort: { 'count': -1 } },
|
|
|
|
|
{ $limit: 15 },
|
2019-12-01 13:38:08 +00:00
|
|
|
]
|
2020-01-08 20:16:21 +00:00
|
|
|
return this._aggregate(puuid, {}, intermediateSteps, '$allyTeam.account_id', groupParams, finalSteps)
|
2019-11-12 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = MatchRepository
|