mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 12:57:28 +00:00
refactor: fetch soloQ ranks in a 2nd request in match details
This commit is contained in:
parent
c6d0cd5d4e
commit
00e2f1f481
8 changed files with 93 additions and 47 deletions
|
|
@ -66,11 +66,11 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="ml-1 flex flex-col justify-around">
|
<div class="ml-1 flex flex-col justify-around">
|
||||||
<div
|
<div
|
||||||
:style="{backgroundImage: `url(${player.firstSum})`}"
|
:style="{backgroundImage: `url(${player.firstSum.icon})`}"
|
||||||
class="w-4 h-4 bg-blue-1000 rounded-md bg-center bg-cover"
|
class="w-4 h-4 bg-blue-1000 rounded-md bg-center bg-cover"
|
||||||
></div>
|
></div>
|
||||||
<div
|
<div
|
||||||
:style="{backgroundImage: `url(${player.secondSum})`}"
|
:style="{backgroundImage: `url(${player.secondSum.icon})`}"
|
||||||
class="w-4 h-4 bg-blue-1000 rounded-md bg-center bg-cover"
|
class="w-4 h-4 bg-blue-1000 rounded-md bg-center bg-cover"
|
||||||
></div>
|
></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,6 @@ import summonersJSON from '@/data/summoner.json'
|
||||||
|
|
||||||
const leaguesNumbers = { 'I': 1, 'II': 2, 'III': 3, 'IV': 4 }
|
const leaguesNumbers = { 'I': 1, 'II': 2, 'III': 3, 'IV': 4 }
|
||||||
|
|
||||||
/**
|
|
||||||
* Return all the infos about a detailed match
|
|
||||||
* @param detailedMatch : all data about the match from the Riot API
|
|
||||||
*/
|
|
||||||
export function createDetailedMatchData(detailedMatch) {
|
|
||||||
detailedMatch.blueTeam.players = detailedMatch.blueTeam.players.map(p => getPlayerData(p))
|
|
||||||
detailedMatch.redTeam.players = detailedMatch.redTeam.players.map(p => getPlayerData(p))
|
|
||||||
|
|
||||||
function getPlayerData(p) {
|
|
||||||
// Summoner Spells
|
|
||||||
p.firstSum = getSummonerLink(p.firstSum)
|
|
||||||
p.secondSum = getSummonerLink(p.secondSum)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
return detailedMatch
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all the infos about a list of matches built with the Riot API data
|
* Return all the infos about a list of matches built with the Riot API data
|
||||||
* @param {Object} RiotData : all data from the Riot API
|
* @param {Object} RiotData : all data from the Riot API
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import { axios } from '@/plugins/axios'
|
import { axios } from '@/plugins/axios'
|
||||||
import { createDetailedMatchData } from '@/helpers/summoner'
|
|
||||||
|
|
||||||
export const namespaced = true
|
export const namespaced = true
|
||||||
|
|
||||||
|
|
@ -21,6 +20,11 @@ export const mutations = {
|
||||||
const index = state.matches.findIndex(m => m.gameId === matchDetails.gameId)
|
const index = state.matches.findIndex(m => m.gameId === matchDetails.gameId)
|
||||||
Vue.set(state.matches, index, matchDetails)
|
Vue.set(state.matches, index, matchDetails)
|
||||||
},
|
},
|
||||||
|
MATCHE_RANKS_FOUND(state, { gameId, blueTeam, redTeam }) {
|
||||||
|
const match = state.matches.find(m => m.gameId === gameId)
|
||||||
|
match.blueTeam.players = blueTeam
|
||||||
|
match.redTeam.players = redTeam
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
|
|
@ -31,8 +35,16 @@ export const actions = {
|
||||||
const resp = await axios(({ url: 'match-details', data: { gameId, region: rootState.currentRegion }, method: 'POST' })).catch(() => { })
|
const resp = await axios(({ url: 'match-details', data: { gameId, region: rootState.currentRegion }, method: 'POST' })).catch(() => { })
|
||||||
console.log('--- DETAILS INFOS ---')
|
console.log('--- DETAILS INFOS ---')
|
||||||
console.log(resp.data)
|
console.log(resp.data)
|
||||||
const detailedMatch = createDetailedMatchData(resp.data.matchDetails)
|
// const detailedMatch = createDetailedMatchData(resp.data.matchDetails)
|
||||||
commit('MATCHE_FOUND', detailedMatch)
|
commit('MATCHE_FOUND', resp.data.matchDetails)
|
||||||
|
|
||||||
|
// If the ranks of the players are not yet known
|
||||||
|
if (resp.data.matchDetails.blueTeam.players[0].rank === undefined) {
|
||||||
|
const ranks = await axios(({ url: 'match-details-ranks', data: { gameId, region: rootState.currentRegion }, method: 'POST' })).catch(() => { })
|
||||||
|
console.log('--- RANK OF MATCH DETAILS ---')
|
||||||
|
console.log(ranks.data)
|
||||||
|
commit('MATCHE_RANKS_FOUND', { gameId, ...ranks.data })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,27 @@ const DetailedMatch = use('App/Models/DetailedMatch')
|
||||||
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
|
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
|
||||||
const MatchService = use('App/Services/MatchService')
|
const MatchService = use('App/Services/MatchService')
|
||||||
const StatsService = use('App/Services/StatsService')
|
const StatsService = use('App/Services/StatsService')
|
||||||
|
const SummonerService = use('App/Services/SummonerService')
|
||||||
const Summoner = use('App/Models/Summoner')
|
const Summoner = use('App/Models/Summoner')
|
||||||
|
|
||||||
class MatchController {
|
class MatchController {
|
||||||
|
/**
|
||||||
|
* Get the soloQ rank of all the players of the team
|
||||||
|
* @param summoner all the data of the summoner
|
||||||
|
* @param region of the match
|
||||||
|
*/
|
||||||
|
async _getPlayerRank(summoner, region) {
|
||||||
|
const account = await SummonerService.getAccount(summoner.name, region)
|
||||||
|
if (account) {
|
||||||
|
const ranked = await SummonerService.getRanked(account)
|
||||||
|
summoner.rank = ranked.soloQ ? (({ tier, shortName }) => ({ tier, shortName }))(ranked.soloQ) : null
|
||||||
|
} else {
|
||||||
|
summoner.rank = null
|
||||||
|
}
|
||||||
|
|
||||||
|
return summoner
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POST - Return data from matches searched by gameIds
|
* POST - Return data from matches searched by gameIds
|
||||||
*/
|
*/
|
||||||
|
|
@ -50,13 +68,40 @@ class MatchController {
|
||||||
await DetailedMatch.create(matchDetails)
|
await DetailedMatch.create(matchDetails)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
console.timeEnd('MatchDetails')
|
console.timeEnd('MatchDetails')
|
||||||
|
|
||||||
return response.json({
|
return response.json({
|
||||||
matchDetails
|
matchDetails
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST - Return ranks of players for a specific game
|
||||||
|
*/
|
||||||
|
async showRanks({ request, response }) {
|
||||||
|
console.time('Ranks')
|
||||||
|
const gameId = request.input('gameId')
|
||||||
|
const region = request.input('region')
|
||||||
|
|
||||||
|
let matchDetails = await DetailedMatch.where({ gameId, region }).first()
|
||||||
|
if (!matchDetails) {
|
||||||
|
return response.json(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestsBlue = matchDetails.blueTeam.players.map(p => this._getPlayerRank(p, region))
|
||||||
|
matchDetails.blueTeam.players = await Promise.all(requestsBlue)
|
||||||
|
|
||||||
|
const requestsRed = matchDetails.redTeam.players.map(p => this._getPlayerRank(p, region))
|
||||||
|
matchDetails.redTeam.players = await Promise.all(requestsRed)
|
||||||
|
|
||||||
|
matchDetails.save()
|
||||||
|
console.timeEnd('Ranks')
|
||||||
|
|
||||||
|
return response.json({
|
||||||
|
blueTeam: matchDetails.blueTeam.players,
|
||||||
|
redTeam: matchDetails.redTeam.players,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = MatchController
|
module.exports = MatchController
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,8 @@ class DetailedMatchTransformer extends MatchTransformer {
|
||||||
const globalInfos = super.getGameInfos(match)
|
const globalInfos = super.getGameInfos(match)
|
||||||
|
|
||||||
// Teams
|
// Teams
|
||||||
const firstTeam = await this.getTeamData(match, match.teams[0])
|
const firstTeam = this.getTeamData(match, match.teams[0])
|
||||||
const secondTeam = await this.getTeamData(match, match.teams[1])
|
const secondTeam = this.getTeamData(match, match.teams[1])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
gameId: match.gameId,
|
gameId: match.gameId,
|
||||||
|
|
@ -37,7 +37,7 @@ class DetailedMatchTransformer extends MatchTransformer {
|
||||||
* @param match raw match data from Riot API
|
* @param match raw match data from Riot API
|
||||||
* @param team raw team data from Riot API
|
* @param team raw team data from Riot API
|
||||||
*/
|
*/
|
||||||
async getTeamData(match, team) {
|
getTeamData(match, team) {
|
||||||
let win = team.win
|
let win = team.win
|
||||||
if (match.gameDuration < 300) {
|
if (match.gameDuration < 300) {
|
||||||
win = 'Remake'
|
win = 'Remake'
|
||||||
|
|
@ -75,11 +75,13 @@ class DetailedMatchTransformer extends MatchTransformer {
|
||||||
// Players
|
// Players
|
||||||
let players = teamPlayers
|
let players = teamPlayers
|
||||||
.map(p => super.getPlayerData(match, p, true, teamStats))
|
.map(p => super.getPlayerData(match, p, true, teamStats))
|
||||||
|
.map(p => {
|
||||||
|
p.firstSum = super.getSummonerSpell(p.firstSum)
|
||||||
|
p.secondSum = super.getSummonerSpell(p.secondSum)
|
||||||
|
return p
|
||||||
|
})
|
||||||
.sort(this.sortTeamByRole)
|
.sort(this.sortTeamByRole)
|
||||||
|
|
||||||
const requests = players.map(p => this.getPlayerRank(p, match.platformId))
|
|
||||||
players = await Promise.all(requests)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bans,
|
bans,
|
||||||
barons: team.baronKills,
|
barons: team.baronKills,
|
||||||
|
|
@ -93,23 +95,6 @@ class DetailedMatchTransformer extends MatchTransformer {
|
||||||
towers: team.towerKills,
|
towers: team.towerKills,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the soloQ rank of all the players of the team
|
|
||||||
* @param summoner all the data of the summoner
|
|
||||||
* @param region of the match
|
|
||||||
*/
|
|
||||||
async getPlayerRank(summoner, region) {
|
|
||||||
const account = await SummonerService.getAccount(summoner.name, region)
|
|
||||||
if (account) {
|
|
||||||
const ranked = await SummonerService.getRanked(account)
|
|
||||||
summoner.rank = ranked.soloQ ? (({ tier, shortName }) => ({ tier, shortName }))(ranked.soloQ) : null
|
|
||||||
} else {
|
|
||||||
summoner.rank = null
|
|
||||||
}
|
|
||||||
|
|
||||||
return summoner
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new DetailedMatchTransformer()
|
module.exports = new DetailedMatchTransformer()
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,13 @@ class MatchTransformer {
|
||||||
const champions = await Jax.CDragon.champions()
|
const champions = await Jax.CDragon.champions()
|
||||||
const perks = await Jax.CDragon.perks()
|
const perks = await Jax.CDragon.perks()
|
||||||
const perkstyles = await Jax.CDragon.perkstyles()
|
const perkstyles = await Jax.CDragon.perkstyles()
|
||||||
|
const summonerSpells = await Jax.CDragon.summonerSpells()
|
||||||
|
|
||||||
this.champions = champions
|
this.champions = champions
|
||||||
this.items = items
|
this.items = items
|
||||||
this.perks = perks
|
this.perks = perks
|
||||||
this.perkstyles = perkstyles.styles
|
this.perkstyles = perkstyles.styles
|
||||||
|
this.summonerSpells = summonerSpells
|
||||||
this.sortTeamByRole = Helpers.sortTeamByRole
|
this.sortTeamByRole = Helpers.sortTeamByRole
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -165,6 +167,21 @@ class MatchTransformer {
|
||||||
}
|
}
|
||||||
return timeline.lane
|
return timeline.lane
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Summoner Spell Data from CDragon
|
||||||
|
* @param id of the summonerSpell
|
||||||
|
*/
|
||||||
|
getSummonerSpell(id) {
|
||||||
|
if (id === 0) return null
|
||||||
|
const spell = this.summonerSpells.find(s => s.id === id)
|
||||||
|
const spellName = spell.iconPath.split('/assets/')[1].toLowerCase()
|
||||||
|
return {
|
||||||
|
name: spell.name,
|
||||||
|
description: spell.description,
|
||||||
|
icon: `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${spellName}`
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = MatchTransformer
|
module.exports = MatchTransformer
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,10 @@ class CDragonEndpoint {
|
||||||
perkstyles() {
|
perkstyles() {
|
||||||
return new CDragonRequest('perkstyles.json').execute()
|
return new CDragonRequest('perkstyles.json').execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
summonerSpells() {
|
||||||
|
return new CDragonRequest('summoner-spells.json').execute()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = CDragonEndpoint
|
module.exports = CDragonEndpoint
|
||||||
|
|
|
||||||
|
|
@ -29,3 +29,4 @@ Route.post('/api', 'SummonerController.api')
|
||||||
Route.post('/ddragon', 'DDragonController.index')
|
Route.post('/ddragon', 'DDragonController.index')
|
||||||
Route.post('/match', 'MatchController.index')
|
Route.post('/match', 'MatchController.index')
|
||||||
Route.post('/match-details', 'MatchController.show')
|
Route.post('/match-details', 'MatchController.show')
|
||||||
|
Route.post('/match-details-ranks', 'MatchController.showRanks')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue