mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 12:57:28 +00:00
refactor: get mates with a mongo request instead of storing them in db
This commit is contained in:
parent
33ff97a7ac
commit
c92cc760e4
8 changed files with 63 additions and 65 deletions
|
|
@ -19,21 +19,21 @@
|
|||
<ul class="mt-1 text-gray-100">
|
||||
<li
|
||||
v-for="mate in mates.slice(0, maxMates)"
|
||||
:key="mate.name"
|
||||
:key="mate._id"
|
||||
class="flex justify-between items-center"
|
||||
>
|
||||
<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"
|
||||
>{{ mate.name }}</router-link>
|
||||
>{{ mate._id }}</router-link>
|
||||
<div class="w-1/4">{{ mate.wins }} / {{ mate.losses }}</div>
|
||||
<div class="w-1/4">
|
||||
<Dropdown>
|
||||
<template v-slot:trigger>
|
||||
<div class="bg-blue-900 rounded-full h-2 cursor-pointer">
|
||||
<div
|
||||
:class="getWinrateColor(mate.winrate)"
|
||||
:style="{width: mate.winrate}"
|
||||
:class="getWinrateColor(mate.wins, mate.count)"
|
||||
:style="{width: `${winrate(mate.wins, mate.count)}%`}"
|
||||
class="rounded-full h-full"
|
||||
></div>
|
||||
</div>
|
||||
|
|
@ -43,9 +43,9 @@
|
|||
<div>Winrate</div>
|
||||
<div>
|
||||
<span
|
||||
:class="getWinrateColor(mate.winrate, false)"
|
||||
:class="getWinrateColor(mate.wins, mate.count, false)"
|
||||
class="font-bold"
|
||||
>{{ mate.winrate }}</span>
|
||||
>{{ winrate(mate.wins, mate.count)|percent }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
|
@ -82,13 +82,13 @@ export default {
|
|||
return this.mates.length > 0
|
||||
},
|
||||
...mapState({
|
||||
mates: state => state.summoner.infos.mates
|
||||
mates: state => state.summoner.infos.stats.mates
|
||||
}),
|
||||
},
|
||||
|
||||
methods: {
|
||||
getWinrateColor(winrate, background = true) {
|
||||
winrate = winrate.slice(0, -1)
|
||||
getWinrateColor(wins, count, background = true) {
|
||||
const winrate = this.winrate(wins, count)
|
||||
if (winrate >= 70) {
|
||||
return background ? 'bg-yellow-400' : 'text-yellow-400'
|
||||
} else if (winrate >= 60) {
|
||||
|
|
@ -97,6 +97,9 @@ export default {
|
|||
return background ? 'bg-teal-300' : 'text-teal-300'
|
||||
}
|
||||
return background ? 'bg-teal-200' : 'text-teal-200'
|
||||
},
|
||||
winrate(wins, count) {
|
||||
return wins * 100 / count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,7 +99,6 @@ export function createSummonerData(RiotData) {
|
|||
ranked: RiotData.ranked,
|
||||
matchList: RiotData.allMatches,
|
||||
matches: createMatchData(RiotData.matchesDetails),
|
||||
mates: createMatesData(RiotData.mates),
|
||||
playing: RiotData.playing,
|
||||
stats: RiotData.stats,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ export const mutations = {
|
|||
state.infos.matches = infos.matches
|
||||
state.infos.ranked = infos.ranked
|
||||
state.infos.matchIndex = infos.matches.length
|
||||
state.infos.mates = infos.mates
|
||||
state.infos.playing = infos.playing
|
||||
state.infos.stats = infos.stats
|
||||
state.status = 'found'
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
const Jax = use('Jax')
|
||||
const DetailedMatch = use('App/Models/DetailedMatch')
|
||||
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
|
||||
const Match = use('App/Models/Match')
|
||||
const MatchHelper = use('App/Helpers/MatchHelper')
|
||||
const Summoner = use('App/Models/Summoner')
|
||||
|
||||
|
|
@ -22,7 +23,7 @@ class MatchController {
|
|||
|
||||
return response.json({
|
||||
matches,
|
||||
mates: summonerDB.mates.filter(m => m.wins + m.losses > 1)
|
||||
mates: await Match.mates(account.puuid, account.name)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -57,9 +57,6 @@ class SummonerController {
|
|||
const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId)
|
||||
finalJSON.matchesDetails = await MatchHelper.getMatches(account, gameIds, summonerDB)
|
||||
|
||||
// MATES
|
||||
finalJSON.mates = summonerDB.mates.filter(m => m.wins + m.losses > 1)
|
||||
|
||||
// PATCH 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 = {
|
||||
global: globalStats[0],
|
||||
league: gamemodeStats,
|
||||
role: roleStats.sort(MatchHelper.sortTeamByRole),
|
||||
class: championClassStats,
|
||||
mates,
|
||||
}
|
||||
console.timeEnd('STATS')
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
const Logger = use('Logger')
|
||||
const Jax = use('Jax')
|
||||
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
|
||||
const SummonerHelper = use('App/Helpers/SummonerHelper')
|
||||
|
||||
class MatchHelper {
|
||||
/**
|
||||
|
|
@ -118,10 +117,6 @@ class MatchHelper {
|
|||
console.log(matchesFromApi.length)
|
||||
Logger.transport('file').info(matchesFromApi)
|
||||
|
||||
|
||||
// Update teamMates
|
||||
SummonerHelper.updatePlayedWith(account, summonerDB, matchesFromApi)
|
||||
|
||||
matchesDetails = [...matchesDetails, ...matchesFromApi]
|
||||
|
||||
/* Save all matches from Riot Api in db */
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue