feat: add statistics by role

This commit is contained in:
Valentin Kaelin 2019-11-08 22:04:06 +01:00
parent 115ecdf0d3
commit 1e3e8f13da
2 changed files with 89 additions and 13 deletions

View file

@ -20,7 +20,7 @@
<div class="px-2 text-white text-center text-sm select-none"> <div class="px-2 text-white text-center text-sm select-none">
<div>Stats based on</div> <div>Stats based on</div>
<div> <div>
<span class="text-teal-400 font-bold">{{ globalStats.count }}</span> matches <span class="text-teal-400 font-bold">{{ stats.global.count }}</span> matches
</div> </div>
<div class="mt-2 leading-tight text-xs text-blue-100 font-normal italic"> <div class="mt-2 leading-tight text-xs text-blue-100 font-normal italic">
Load more matches Load more matches
@ -32,6 +32,29 @@
</Dropdown> </Dropdown>
</div> </div>
</div> </div>
<div class="mt-2 flex items-center py-2">
<div
v-for="(role, index) in stats.role"
:key="index"
class="flex flex-col items-center w-1/5"
>
<div class="flex flex-col justify-end w-2 h-12 bg-blue-900 rounded-full">
<div
:style="{height: (role.count * 3 / mostPlayedRole) * role.losses / role.count + 'rem'}"
class="bg-green-400 rounded-t-full"
></div>
<div
:style="{height: (role.count * 3 / mostPlayedRole) * role.wins / role.count + 'rem'}"
class="bg-red-400 rounded-b-full"
></div>
</div>
<div
:style="{backgroundImage: `url(${require('@/assets/img/roles/' + role.role + '.png')})`}"
class="mt-1 w-4 h-4 bg-center bg-cover"
></div>
<div class="text-xs text-blue-200">{{ role.count }}</div>
</div>
</div>
<div class="py-2 text-sm text-center"> <div class="py-2 text-sm text-center">
<div class="px-4 flex items-baseline font-bold text-sm text-blue-300 uppercase"> <div class="px-4 flex items-baseline font-bold text-sm text-blue-300 uppercase">
<div class="w-1/4 text-left text-base text-blue-400">Stat</div> <div class="w-1/4 text-left text-base text-blue-400">Stat</div>
@ -48,16 +71,18 @@
> >
<div class="w-1/4 text-left">{{ name }}</div> <div class="w-1/4 text-left">{{ name }}</div>
<div class="w-1/4">{{ stat }}</div> <div class="w-1/4">{{ stat }}</div>
<div class="w-1/4">{{ (stat / (globalStats.time / 60)).toFixed(2) }}</div> <div class="w-1/4">{{ (stat / (stats.global.time / 60)).toFixed(2) }}</div>
<div class="w-1/4">{{ (stat / globalStats.count).toFixed(2) }}</div> <div class="w-1/4">{{ (stat / stats.global.count).toFixed(2) }}</div>
</li> </li>
<li class="flex justify-between items-center px-4 py-1 bg-blue-760 leading-tight"> <li class="flex justify-between items-center px-4 py-1 bg-blue-760 leading-tight">
<div class="w-1/4 text-left whitespace-no-wrap">kill participation</div> <div class="w-1/4 text-left whitespace-no-wrap">kill participation</div>
<div class="w-1/4">{{ globalStats.kp|percent }}</div> <div class="w-1/4">{{ stats.global.kp|percent }}</div>
</li> </li>
<li class="flex justify-between items-center px-4 py-1 leading-tight"> <li class="flex justify-between items-center px-4 py-1 leading-tight">
<div class="w-1/4 text-left whitespace-no-wrap">kda</div> <div class="w-1/4 text-left whitespace-no-wrap">kda</div>
<div class="w-1/4">{{ (globalStats.kills + globalStats.assists) / globalStats.deaths|round }}</div> <div
class="w-1/4"
>{{ (stats.global.kills + stats.global.assists) / stats.global.deaths|round }}</div>
</li> </li>
</ul> </ul>
<template v-if="leagueStatsByType('Ranked').length"> <template v-if="leagueStatsByType('Ranked').length">
@ -128,17 +153,14 @@ export default {
}, },
computed: { computed: {
globalStats() { mostPlayedRole() {
return this.stats.find(s => s._id === null) return Math.max(...this.stats.role.map(r => r.count), 0)
}, },
globalStatsKeys() { globalStatsKeys() {
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const { _id, wins, losses, count, time, kp, ...rest } = this.globalStats const { _id, wins, losses, count, time, kp, ...rest } = this.stats.global
return rest return rest
}, },
leagueStats() {
return this.stats.filter(s => s._id !== null)
},
...mapState({ ...mapState({
stats: state => state.summoner.infos.stats stats: state => state.summoner.infos.stats
}), }),
@ -154,7 +176,7 @@ export default {
} }
}, },
leagueStatsByType(typeName) { leagueStatsByType(typeName) {
return this.leagueStats return this.stats.league
.map(l => { .map(l => {
return { ...l, ...gameModes[l._id] } return { ...l, ...gameModes[l._id] }
}) })

View file

@ -93,6 +93,56 @@ class SummonerController {
} }
]) ])
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",
}
}
])
// 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 globalStats = await Match.query().aggregate([ const globalStats = await Match.query().aggregate([
{ {
$match: { $match: {
@ -127,7 +177,11 @@ class SummonerController {
} }
} }
]) ])
finalJSON.stats = [...globalStats, ...gamemodeStats] finalJSON.stats = {
global: globalStats[0],
league: gamemodeStats,
role: roleStats.sort(MatchHelper.sortTeamByRole),
}
console.timeEnd('STATS') console.timeEnd('STATS')
// SAVE IN DB // SAVE IN DB