LeagueStats/server/app/Transformers/MatchTransformer.js

180 lines
5.7 KiB
JavaScript
Raw Normal View History

'use strict'
const Jax = use('Jax')
/**
* MatchTransformer class
*
* @class MatchTransformer
*/
class MatchTransformer {
/**
* Get global Context with DDragon Data
*/
async getContext() {
const items = await Jax.CDragon.items()
const champions = await Jax.CDragon.champions()
const perks = await Jax.CDragon.perks()
const perkstyles = await Jax.CDragon.perkstyles()
const version = Jax.DDragon.Version
this.champions = champions
this.items = items
this.perks = perks
this.perkstyles = perkstyles.styles
this.version = version
}
2019-11-03 19:59:21 +00:00
/**
* Get champion specific data
* @param id of the champion
*/
getChampion(id) {
const champion = { ...this.champions.find(c => c.id === id) }
champion.icon = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${champion.squarePortraitPath.split('/assets/')[1].toLowerCase()}`
delete champion.squarePortraitPath
return champion
}
/**
* Get global data about the match
*/
getGameInfos(match) {
2019-11-03 19:59:21 +00:00
return {
map: match.mapId,
gamemode: match.queueId,
date: match.gameCreation,
time: match.gameDuration
2019-11-03 19:59:21 +00:00
}
}
/**
* Get player specific data during the match
* @param match
2019-11-03 19:59:21 +00:00
* @param player
* @param detailed : detailed or not stats
* @param teamStats : if detailed, the teamStats argument is mandatory
*/
getPlayerData(match, player, detailed, teamStats = {}) {
const identity = match.participantIdentities.find(p => p.participantId === player.participantId)
2019-11-03 19:59:21 +00:00
const name = identity.player.summonerName
const champion = this.getChampion(player.championId)
const role = this.getRoleName(player.timeline)
2019-11-03 19:59:21 +00:00
const level = player.stats.champLevel
// Regular stats / Full match stats
const stats = {
kills: player.stats.kills,
deaths: player.stats.deaths,
assists: player.stats.assists,
2019-11-03 19:59:21 +00:00
minions: player.stats.totalMinionsKilled + player.stats.neutralMinionsKilled,
vision: player.stats.visionScore,
gold: +(player.stats.goldEarned / 1000).toFixed(1) + 'k',
dmgChamp: +(player.stats.totalDamageDealtToChampions / 1000).toFixed(1) + 'k',
dmgObj: +(player.stats.damageDealtToObjectives / 1000).toFixed(1) + 'k',
dmgTaken: +(player.stats.totalDamageTaken / 1000).toFixed(1) + 'k',
}
if (stats.kills + stats.assists !== 0 && stats.deaths === 0) {
stats.kda = '∞'
} else {
stats.kda = +(stats.deaths === 0 ? 0 : ((stats.kills + stats.assists) / stats.deaths)).toFixed(2)
}
2019-11-03 19:59:21 +00:00
// Percent stats / Per minute stats : only for detailed match
let percentStats
if (detailed) {
percentStats = {
minions: +(stats.minions / (match.gameDuration / 60)).toFixed(2),
vision: +(stats.vision / (match.gameDuration / 60)).toFixed(2),
gold: +(player.stats.goldEarned * 100 / teamStats.gold).toFixed(1) + '%',
2019-11-03 19:59:21 +00:00
dmgChamp: +(player.stats.totalDamageDealtToChampions * 100 / teamStats.dmgChamp).toFixed(1) + '%',
dmgObj: +(player.stats.damageDealtToObjectives * 100 / teamStats.dmgObj).toFixed(1) + '%',
dmgTaken: +(player.stats.totalDamageTaken * 100 / teamStats.dmgTaken).toFixed(1) + '%',
}
stats.kp = teamStats.kills === 0 ? '0%' : +((stats.kills + stats.assists) * 100 / teamStats.kills).toFixed(1) + '%'
2019-11-03 19:59:21 +00:00
} else {
const totalKills = match.participants.reduce((prev, current) => {
2019-11-03 19:59:21 +00:00
if (current.teamId !== player.teamId) {
return prev
}
return prev + current.stats.kills
}, 0)
stats.kp = totalKills === 0 ? 0 : +((stats.kills + stats.assists) * 100 / totalKills).toFixed(1)
2019-11-03 19:59:21 +00:00
}
let primaryRune = null
let secondaryRune = null
if (player.stats.perkPrimaryStyle) {
const firstRune = this.perks.find(p => p.id === player.stats.perk0)
const firstRuneUrl = firstRune.iconPath.split('/assets/')[1].toLowerCase()
primaryRune = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${firstRuneUrl}`
const secondRuneStyle = this.perkstyles.find(p => p.id === player.stats.perkSubStyle)
const secondRuneStyleUrl = secondRuneStyle.iconPath.split('/assets/')[1].toLowerCase()
secondaryRune = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${secondRuneStyleUrl}`
2019-11-03 19:59:21 +00:00
}
const items = []
for (let i = 0; i < 6; i++) {
const id = player.stats['item' + i]
if (id === 0) {
items.push(null)
continue
}
const item = this.items.find(i => i.id === id)
const itemUrl = item.iconPath.split('/assets/')[1].toLowerCase()
items.push({
image: `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${itemUrl}`,
name: item.name,
description: item.description,
price: item.priceTotal
})
2019-11-03 19:59:21 +00:00
}
const firstSum = player.spell1Id
const secondSum = player.spell2Id
return {
2019-11-03 19:59:21 +00:00
name,
champion,
role,
primaryRune,
secondaryRune,
level,
items,
firstSum,
secondSum,
stats,
percentStats,
}
}
/**
* Return the lane of the summoner according to timeline
* @param timeline from Riot Api
*/
getRoleName(timeline) {
if (timeline.lane === 'BOTTOM' && timeline.role.includes('SUPPORT')) {
return 'SUPPORT'
}
return timeline.lane
}
/**
* Sort array of Roles according to a specific order
* @param a first role
* @param b second role
*/
sortTeamByRole(a, b) {
const sortingArr = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT']
return sortingArr.indexOf(a.role) - sortingArr.indexOf(b.role)
}
}
module.exports = MatchTransformer