refactor: match repository queries

This commit is contained in:
Valentin Kaelin 2019-12-01 14:38:08 +01:00
parent 351ce80c77
commit 0a30cbe8d1

View file

@ -10,22 +10,28 @@ class MatchRepository {
} }
/** /**
* Get Summoner's statistics for the 5 most played champions * Build the aggregate mongo query
* @param puuid of the summoner * @param {Number} puuid
* @param {Object} matchParams
* @param {Array} intermediateSteps
* @param {*} groupId
* @param {Object} groupParams
* @param {Array} finalSteps
*/ */
championStats(puuid) { _aggregate(puuid, matchParams, intermediateSteps, groupId, groupParams, finalSteps) {
return this.Match.query().aggregate([ return this.Match.query().aggregate([
{ {
$match: { $match: {
summoner_puuid: puuid, summoner_puuid: puuid,
result: { $not: { $eq: 'Remake' } }, result: { $not: { $eq: 'Remake' } },
gamemode: { $nin: [800, 810, 820, 830, 840, 850] } gamemode: { $nin: [800, 810, 820, 830, 840, 850] },
...matchParams
} }
}, },
...intermediateSteps,
{ {
$group: { $group: {
_id: "$champion.id", _id: groupId,
champion: { $first: "$champion" },
count: { $sum: 1 }, count: { $sum: 1 },
wins: { wins: {
$sum: { $sum: {
@ -37,14 +43,29 @@ class MatchRepository {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0] $cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
} }
}, },
kills: { $sum: "$stats.kills" }, ...groupParams
deaths: { $sum: "$stats.deaths" }, },
assists: { $sum: "$stats.assists" },
}
}, },
...finalSteps
])
}
/**
* Get Summoner's statistics for the 5 most played champions
* @param puuid of the summoner
*/
championStats(puuid) {
const groupParams = {
champion: { $first: "$champion" },
kills: { $sum: "$stats.kills" },
deaths: { $sum: "$stats.deaths" },
assists: { $sum: "$stats.assists" },
}
const finalSteps = [
{ $sort: { 'count': -1 } }, { $sort: { 'count': -1 } },
{ $limit: 5 }, { $limit: 5 },
]) ]
return this._aggregate(puuid, {}, [], '$champion.id', groupParams, finalSteps)
} }
/** /**
@ -52,31 +73,8 @@ class MatchRepository {
* @param puuid of the summoner * @param puuid of the summoner
*/ */
championClassStats(puuid) { championClassStats(puuid) {
return this.Match.query().aggregate([ const groupId = { "$arrayElemAt": ["$champion.roles", 0] }
{ return this._aggregate(puuid, {}, [], groupId, {}, [])
$match: {
summoner_puuid: puuid,
result: { $not: { $eq: 'Remake' } },
gamemode: { $nin: [800, 810, 820, 830, 840, 850] }
}
},
{
$group: {
_id: { "$arrayElemAt": ["$champion.roles", 0] },
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
}
}
}
])
} }
/** /**
@ -84,31 +82,7 @@ class MatchRepository {
* @param puuid of the summoner * @param puuid of the summoner
*/ */
gamemodeStats(puuid) { gamemodeStats(puuid) {
return this.Match.query().aggregate([ return this._aggregate(puuid, {}, [], '$gamemode', {}, [])
{
$match: {
summoner_puuid: puuid,
result: { $not: { $eq: 'Remake' } },
gamemode: { $nin: [800, 810, 820, 830, 840, 850] }
}
},
{
$group: {
_id: "$gamemode",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
}
}
}
])
} }
/** /**
@ -116,38 +90,16 @@ class MatchRepository {
* @param puuid of the summoner * @param puuid of the summoner
*/ */
globalStats(puuid) { globalStats(puuid) {
return this.Match.query().aggregate([ const groupParams = {
{ time: { $sum: "$time" },
$match: { kills: { $sum: "$stats.kills" },
summoner_puuid: puuid, deaths: { $sum: "$stats.deaths" },
result: { $not: { $eq: 'Remake' } }, assists: { $sum: "$stats.assists" },
gamemode: { $nin: [800, 810, 820, 830, 840, 850] } minions: { $sum: "$stats.minions" },
} vision: { $sum: "$stats.vision" },
}, kp: { $avg: "$stats.kp" },
{ }
$group: { return this._aggregate(puuid, {}, [], null, groupParams, [])
_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" },
}
}
])
} }
/** /**
@ -155,31 +107,10 @@ class MatchRepository {
* @param puuid of the summoner * @param puuid of the summoner
*/ */
roleStats(puuid) { roleStats(puuid) {
return this.Match.query().aggregate([ const matchParams = {
{ role: { $not: { $eq: 'NONE' } }
$match: { }
summoner_puuid: puuid, const finalSteps = [
role: { $not: { $eq: 'NONE' } },
result: { $not: { $eq: 'Remake' } },
gamemode: { $nin: [800, 810, 820, 830, 840, 850] }
}
},
{
$group: {
_id: "$role",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
}
},
},
{ {
$project: { $project: {
role: "$_id", role: "$_id",
@ -188,7 +119,8 @@ class MatchRepository {
losses: "$losses", losses: "$losses",
} }
} }
]) ]
return this._aggregate(puuid, matchParams, [], '$role', {}, finalSteps)
} }
/** /**
@ -197,31 +129,10 @@ class MatchRepository {
* @param summonerName of the summoner * @param summonerName of the summoner
*/ */
mates(puuid, summonerName) { mates(puuid, summonerName) {
return this.Match.query().aggregate([ const intermediateSteps = [
{
$match: {
summoner_puuid: puuid,
result: { $not: { $eq: 'Remake' } },
gamemode: { $nin: [800, 810, 820, 830, 840, 850] }
}
},
{ $unwind: "$allyTeam" }, { $unwind: "$allyTeam" },
{ ]
$group: { const finalSteps = [
_id: "$allyTeam.name",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [{ $eq: ["$result", "Win"] }, 1, 0]
}
},
losses: {
$sum: {
$cond: [{ $eq: ["$result", "Fail"] }, 1, 0]
}
}
},
},
{ {
$match: { $match: {
_id: { $not: { $eq: summonerName } }, _id: { $not: { $eq: summonerName } },
@ -230,7 +141,8 @@ class MatchRepository {
}, },
{ $sort: { 'count': -1 } }, { $sort: { 'count': -1 } },
{ $limit: 15 }, { $limit: 15 },
]) ]
return this._aggregate(puuid, {}, intermediateSteps, '$allyTeam.name', {}, finalSteps)
} }
} }