feat: get stats by champion class from api

This commit is contained in:
Valentin Kaelin 2019-11-10 20:19:04 +01:00
parent 48d2fade07
commit 33ff97a7ac
3 changed files with 158 additions and 101 deletions

View file

@ -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')

View file

@ -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

View file

@ -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