LeagueStats/server/app/Transformers/LiveMatchTransformer.js

79 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-01-14 21:04:45 +00:00
'use strict'
const MatchTransformer = use('App/Transformers/MatchTransformer')
const RoleIdentificationService = use('App/Services/RoleIdentificationService')
2020-01-14 21:04:45 +00:00
const SummonerService = use('App/Services/SummonerService')
const { queuesWithRole } = use('App/helpers')
2020-01-14 21:04:45 +00:00
/**
* LiveMatchTransformer class
*
* @class LiveMatchTransformer
*/
class LiveMatchTransformer extends MatchTransformer {
async _getPlayerRank(participant, region) {
2020-01-14 21:04:45 +00:00
const account = await SummonerService.getAccount(participant.summonerName, region)
if (account) {
participant.level = account.summonerLevel
const ranked = await SummonerService.getRanked(account, region)
participant.rank = ranked
} else {
participant.rank = null
}
return participant
}
_getTeamRoles(team) {
const teamJunglers = team.filter(p => p.jungle)
const jungle = teamJunglers.length === 1 ? teamJunglers[0].champion : null
return RoleIdentificationService.getRoles(this.championRoles, team.map(p => p.champion), jungle)
}
2020-01-14 21:04:45 +00:00
/**
* Transform raw data from Riot API
* @param match data from Riot API, one live match
*/
async transform(match, { region }) {
await super.getContext()
// Roles
const blueTeam = [] // 100
const redTeam = [] // 200
let blueRoles = []
let redRoles = []
if (this.championRoles && queuesWithRole.includes(match.gameQueueConfigId)) {
match.participants.map(p => {
const playerRole = { champion: p.championId, jungle: p.spell1Id === 11 || p.spell2Id === 11 }
p.teamId === 100 ? blueTeam.push(playerRole) : redTeam.push(playerRole)
})
blueRoles = this._getTeamRoles(blueTeam)
redRoles = this._getTeamRoles(redTeam)
}
2020-01-14 21:04:45 +00:00
for (const participant of match.participants) {
// Perks
participant.runes = participant.perks ? super.getPerksImages(participant.perks.perkIds[0], participant.perks.perkSubStyle) : {}
// Roles
if (this.championRoles) {
const roles = participant.teamId === 100 ? blueRoles : redRoles
participant.role = Object.entries(roles).find(([, champion]) => participant.championId === champion)[0]
if (participant.role === 'UTILITY') {
participant.role = 'SUPPORT'
}
}
2020-01-14 21:04:45 +00:00
}
// Ranks
const requestsParticipants = match.participants.map(p => this._getPlayerRank(p, region))
2020-01-14 21:04:45 +00:00
match.participants = await Promise.all(requestsParticipants)
return match
}
}
module.exports = new LiveMatchTransformer()