refactor: get mates with a mongo request instead of storing them in db

This commit is contained in:
Valentin Kaelin 2019-11-11 18:07:15 +01:00
parent 33ff97a7ac
commit c92cc760e4
8 changed files with 63 additions and 65 deletions

View file

@ -19,21 +19,21 @@
<ul class="mt-1 text-gray-100"> <ul class="mt-1 text-gray-100">
<li <li
v-for="mate in mates.slice(0, maxMates)" v-for="mate in mates.slice(0, maxMates)"
:key="mate.name" :key="mate._id"
class="flex justify-between items-center" class="flex justify-between items-center"
> >
<router-link <router-link
:to="{ name: 'summoner', params: { region: $route.params.region, name: mate.name }}" :to="{ name: 'summoner', params: { region: $route.params.region, name: mate._id }}"
class="w-2/4 hover:text-teal-200" class="w-2/4 hover:text-teal-200"
>{{ mate.name }}</router-link> >{{ mate._id }}</router-link>
<div class="w-1/4">{{ mate.wins }} / {{ mate.losses }}</div> <div class="w-1/4">{{ mate.wins }} / {{ mate.losses }}</div>
<div class="w-1/4"> <div class="w-1/4">
<Dropdown> <Dropdown>
<template v-slot:trigger> <template v-slot:trigger>
<div class="bg-blue-900 rounded-full h-2 cursor-pointer"> <div class="bg-blue-900 rounded-full h-2 cursor-pointer">
<div <div
:class="getWinrateColor(mate.winrate)" :class="getWinrateColor(mate.wins, mate.count)"
:style="{width: mate.winrate}" :style="{width: `${winrate(mate.wins, mate.count)}%`}"
class="rounded-full h-full" class="rounded-full h-full"
></div> ></div>
</div> </div>
@ -43,9 +43,9 @@
<div>Winrate</div> <div>Winrate</div>
<div> <div>
<span <span
:class="getWinrateColor(mate.winrate, false)" :class="getWinrateColor(mate.wins, mate.count, false)"
class="font-bold" class="font-bold"
>{{ mate.winrate }}</span> >{{ winrate(mate.wins, mate.count)|percent }}</span>
</div> </div>
</div> </div>
</template> </template>
@ -82,13 +82,13 @@ export default {
return this.mates.length > 0 return this.mates.length > 0
}, },
...mapState({ ...mapState({
mates: state => state.summoner.infos.mates mates: state => state.summoner.infos.stats.mates
}), }),
}, },
methods: { methods: {
getWinrateColor(winrate, background = true) { getWinrateColor(wins, count, background = true) {
winrate = winrate.slice(0, -1) const winrate = this.winrate(wins, count)
if (winrate >= 70) { if (winrate >= 70) {
return background ? 'bg-yellow-400' : 'text-yellow-400' return background ? 'bg-yellow-400' : 'text-yellow-400'
} else if (winrate >= 60) { } else if (winrate >= 60) {
@ -97,6 +97,9 @@ export default {
return background ? 'bg-teal-300' : 'text-teal-300' return background ? 'bg-teal-300' : 'text-teal-300'
} }
return background ? 'bg-teal-200' : 'text-teal-200' return background ? 'bg-teal-200' : 'text-teal-200'
},
winrate(wins, count) {
return wins * 100 / count
} }
} }
} }

View file

@ -99,7 +99,6 @@ export function createSummonerData(RiotData) {
ranked: RiotData.ranked, ranked: RiotData.ranked,
matchList: RiotData.allMatches, matchList: RiotData.allMatches,
matches: createMatchData(RiotData.matchesDetails), matches: createMatchData(RiotData.matchesDetails),
mates: createMatesData(RiotData.mates),
playing: RiotData.playing, playing: RiotData.playing,
stats: RiotData.stats, stats: RiotData.stats,
} }

View file

@ -39,7 +39,6 @@ export const mutations = {
state.infos.matches = infos.matches state.infos.matches = infos.matches
state.infos.ranked = infos.ranked state.infos.ranked = infos.ranked
state.infos.matchIndex = infos.matches.length state.infos.matchIndex = infos.matches.length
state.infos.mates = infos.mates
state.infos.playing = infos.playing state.infos.playing = infos.playing
state.infos.stats = infos.stats state.infos.stats = infos.stats
state.status = 'found' state.status = 'found'

View file

@ -3,6 +3,7 @@
const Jax = use('Jax') const Jax = use('Jax')
const DetailedMatch = use('App/Models/DetailedMatch') const DetailedMatch = use('App/Models/DetailedMatch')
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer') const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
const Match = use('App/Models/Match')
const MatchHelper = use('App/Helpers/MatchHelper') const MatchHelper = use('App/Helpers/MatchHelper')
const Summoner = use('App/Models/Summoner') const Summoner = use('App/Models/Summoner')
@ -22,7 +23,7 @@ class MatchController {
return response.json({ return response.json({
matches, matches,
mates: summonerDB.mates.filter(m => m.wins + m.losses > 1) mates: await Match.mates(account.puuid, account.name)
}) })
} }

View file

@ -57,9 +57,6 @@ class SummonerController {
const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId) const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId)
finalJSON.matchesDetails = await MatchHelper.getMatches(account, gameIds, summonerDB) finalJSON.matchesDetails = await MatchHelper.getMatches(account, gameIds, summonerDB)
// MATES
finalJSON.mates = summonerDB.mates.filter(m => m.wins + m.losses > 1)
// PATCH VERSION // PATCH VERSION
finalJSON.version = Jax.DDragon.Version finalJSON.version = Jax.DDragon.Version
@ -80,13 +77,15 @@ class SummonerController {
}) })
} }
} }
const championClassStats = await Match.championClassStats(account.puuid); const championClassStats = await Match.championClassStats(account.puuid)
const mates = await Match.mates(account.puuid, account.name)
finalJSON.stats = { finalJSON.stats = {
global: globalStats[0], global: globalStats[0],
league: gamemodeStats, league: gamemodeStats,
role: roleStats.sort(MatchHelper.sortTeamByRole), role: roleStats.sort(MatchHelper.sortTeamByRole),
class: championClassStats, class: championClassStats,
mates,
} }
console.timeEnd('STATS') console.timeEnd('STATS')

View file

@ -3,7 +3,6 @@
const Logger = use('Logger') const Logger = use('Logger')
const Jax = use('Jax') const Jax = use('Jax')
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer') const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
const SummonerHelper = use('App/Helpers/SummonerHelper')
class MatchHelper { class MatchHelper {
/** /**
@ -118,10 +117,6 @@ class MatchHelper {
console.log(matchesFromApi.length) console.log(matchesFromApi.length)
Logger.transport('file').info(matchesFromApi) Logger.transport('file').info(matchesFromApi)
// Update teamMates
SummonerHelper.updatePlayedWith(account, summonerDB, matchesFromApi)
matchesDetails = [...matchesDetails, ...matchesFromApi] matchesDetails = [...matchesDetails, ...matchesFromApi]
/* Save all matches from Riot Api in db */ /* Save all matches from Riot Api in db */

View file

@ -1,42 +0,0 @@
'use strict'
const Logger = use('Logger')
const Summoner = use('App/Models/Summoner')
class SummonerHelper {
/**
* Update the mates' list of the Summoner
* @param account of the summoner
* @param summonerDB summoner in the database
* @param matches all new matches
*/
updatePlayedWith(account, summonerDB, matches) {
let teamMates = summonerDB.mates || []
for (const match of matches) {
if(match.result === 'Remake') continue
const win = match.result === 'Win'
for (const ally of match.allyTeam) {
if (ally.name === account.name) continue
const mate = teamMates.find(m => m.name === ally.name)
if (mate) {
win ? mate.wins++ : mate.losses++
} else {
teamMates.push({
name: ally.name,
wins: win ? 1 : 0,
losses: win ? 0 : 1,
creation: Date.now()
})
}
}
}
// Save new mates'list
summonerDB.mates = teamMates
}
}
module.exports = new SummonerHelper()

View file

@ -155,6 +155,50 @@ class Match extends Model {
} }
]) ])
} }
/**
* Get Summoner's mates list
* @param puuid of the summoner
* @param summonerName of the summoner
*/
static mates(puuid, summonerName) {
return Match.query().aggregate([
{
$match: {
summoner_puuid: puuid
}
},
{ $unwind: "$allyTeam" },
{
$group: {
_id: "$allyTeam.name",
count: { $sum: 1 },
wins: {
$sum: {
$cond: [
{ $eq: ["$result", "Win"] }, 1, 0
]
}
},
losses: {
$sum: {
$cond: [
{ $eq: ["$result", "Fail"] }, 1, 0
]
}
}
},
},
{
$match: {
_id: { $not: { $eq: summonerName } },
'count': { $gte: 2 }
}
},
{ $sort: { 'count': -1 } },
{ $limit: 15 },
])
}
} }
module.exports = Match module.exports = Match