refactor: clean some Transformers code

This commit is contained in:
Valentin Kaelin 2019-11-03 20:59:21 +01:00
parent cce91efb67
commit 05f635d7d4
4 changed files with 141 additions and 185 deletions

View file

@ -79,21 +79,21 @@
<div class="flex items-center"> <div class="flex items-center">
<img src="@/assets/img/icons/Creep.svg" alt="Minions" /> <img src="@/assets/img/icons/Creep.svg" alt="Minions" />
<div class="ml-1 text-teal-300 text-sm font-bold"> <div class="ml-1 text-teal-300 text-sm font-bold">
{{ data.minions }} {{ data.stats.minions }}
<span class="font-normal">cs</span> <span class="font-normal">cs</span>
</div> </div>
</div> </div>
<div class="flex items-center"> <div class="flex items-center">
<img src="@/assets/img/icons/Gold.svg" alt="Gold" /> <img src="@/assets/img/icons/Gold.svg" alt="Gold" />
<div class="ml-1 gold text-sm font-bold"> <div class="ml-1 gold text-sm font-bold">
{{ data.gold }} {{ data.stats.gold }}
<!-- <span class="font-normal">gold</span> --> <!-- <span class="font-normal">gold</span> -->
</div> </div>
</div> </div>
<div class="flex items-center"> <div class="flex items-center">
<img src="@/assets/img/icons/Damage.svg" alt="Damage" /> <img src="@/assets/img/icons/Damage.svg" alt="Damage" />
<div class="ml-1 damage text-sm font-bold"> <div class="ml-1 damage text-sm font-bold">
{{ data.damage }} {{ data.stats.dmgChamp }}
<!-- <span class="font-normal">dmg</span> --> <!-- <span class="font-normal">dmg</span> -->
</div> </div>
</div> </div>

View file

@ -9,77 +9,33 @@ const MatchTransformer = use('App/Transformers/MatchTransformer')
*/ */
class BasicMatchTransformer extends MatchTransformer { class BasicMatchTransformer extends MatchTransformer {
/** /**
* This method is used to transform the data. * Transform raw data from Riot API
* @param match data from Riot API
* @param ctx context
*/ */
transform(match, { account, champions, runes, MatchHelper }) { transform(match, { account, champions, runes, MatchHelper }) {
this.match = match
this.champions = champions
this.runes = runes
this.MatchHelper = MatchHelper
// Global data about the match
const globalInfos = super.getGameInfos()
const participantId = match.participantIdentities.find((p) => p.player.currentAccountId === account.accountId).participantId const participantId = match.participantIdentities.find((p) => p.player.currentAccountId === account.accountId).participantId
const player = match.participants[participantId - 1] const player = match.participants[participantId - 1]
const teamId = player.teamId
let win = match.teams.find((t) => t.teamId === teamId).win let win = match.teams.find((t) => t.teamId === player.teamId).win
// Match less than 5min // Match less than 5min
if (match.gameDuration < 300) { if (match.gameDuration < 300) {
win = 'Remake' win = 'Remake'
} }
const map = match.mapId // Player data
const mode = match.queueId const playerData = super.getPlayerData(player, false)
const champion = (({ id, name }) => ({ id, name }))(Object.entries(champions).find(([, champion]) => Number(champion.key) === player.championId)[1])
const role = MatchHelper.getRoleName(player.timeline)
const gameCreation = match.gameCreation
const time = MatchHelper.secToTime(match.gameDuration)
const kills = player.stats.kills
const deaths = player.stats.deaths
const assists = player.stats.assists
let kda
if (kills + assists !== 0 && deaths === 0) {
kda = '∞'
} else {
kda = +(deaths === 0 ? 0 : ((kills + assists) / deaths)).toFixed(2)
}
const level = player.stats.champLevel
const damage = +(player.stats.totalDamageDealtToChampions / 1000).toFixed(1) + 'k'
const totalKills = match.participants.reduce((prev, current) => {
if (current.teamId !== teamId) {
return prev
}
return prev + current.stats.kills
}, 0)
const kp = totalKills === 0 ? '0%' : +((kills + assists) * 100 / totalKills).toFixed(1) + '%'
let primaryRune = null
let secondaryRune = null
if (player.stats.perkPrimaryStyle) {
const primaryRuneCategory = runes.find(r => r.id === player.stats.perkPrimaryStyle)
for (const subCat of primaryRuneCategory.slots) {
primaryRune = subCat.runes.find(r => r.id === player.stats.perk0)
if (primaryRune) {
break
}
}
primaryRune = `https://ddragon.leagueoflegends.com/cdn/img/${primaryRune.icon}`
secondaryRune = runes.find(r => r.id === player.stats.perkSubStyle)
secondaryRune = `https://ddragon.leagueoflegends.com/cdn/img/${secondaryRune.icon}`
}
const items = []
for (let i = 0; i < 6; i++) {
const currentItem = 'item' + i
items.push(player.stats[currentItem])
}
const gold = +(player.stats.goldEarned / 1000).toFixed(1) + 'k'
const minions = player.stats.totalMinionsKilled + player.stats.neutralMinionsKilled
const firstSum = player.spell1Id
const secondSum = player.spell2Id
// Teams data
const allyTeam = [] const allyTeam = []
const enemyTeam = [] const enemyTeam = []
for (let summoner of match.participantIdentities) { for (let summoner of match.participantIdentities) {
@ -90,7 +46,7 @@ class BasicMatchTransformer extends MatchTransformer {
champion: (({ id, name }) => ({ id, name }))(Object.entries(champions).find(([, champion]) => Number(champion.key) === allData.championId)[1]) champion: (({ id, name }) => ({ id, name }))(Object.entries(champions).find(([, champion]) => Number(champion.key) === allData.championId)[1])
} }
if (allData.teamId === teamId) { if (allData.teamId === player.teamId) {
allyTeam.push(playerInfos) allyTeam.push(playerInfos)
} else { } else {
enemyTeam.push(playerInfos) enemyTeam.push(playerInfos)
@ -103,28 +59,10 @@ class BasicMatchTransformer extends MatchTransformer {
summoner_puuid: account.puuid, summoner_puuid: account.puuid,
gameId: match.gameId, gameId: match.gameId,
result: win, result: win,
map,
gamemode: mode,
champion,
role,
primaryRune,
secondaryRune,
date: gameCreation,
time,
kills,
deaths,
assists,
kda,
level,
damage,
kp,
items,
gold,
minions,
firstSum,
secondSum,
allyTeam, allyTeam,
enemyTeam enemyTeam,
...globalInfos,
...playerData
} }
} }
} }

View file

@ -9,7 +9,9 @@ const MatchTransformer = use('App/Transformers/MatchTransformer')
*/ */
class DetailedMatchTransformer extends MatchTransformer { class DetailedMatchTransformer extends MatchTransformer {
/** /**
* This method is used to transform the data. * Transform raw data from Riot API
* @param match data from Riot API
* @param ctx context
*/ */
transform(match, { champions, runes, MatchHelper }) { transform(match, { champions, runes, MatchHelper }) {
this.match = match this.match = match
@ -18,10 +20,7 @@ class DetailedMatchTransformer extends MatchTransformer {
this.MatchHelper = MatchHelper this.MatchHelper = MatchHelper
// Global data // Global data
const map = match.mapId const globalInfos = super.getGameInfos()
const mode = match.queueId
const gameCreation = match.gameCreation
const time = this.MatchHelper.secToTime(match.gameDuration)
// Teams // Teams
const firstTeam = this.getTeamData(match.teams[0]) const firstTeam = this.getTeamData(match.teams[0])
@ -29,13 +28,10 @@ class DetailedMatchTransformer extends MatchTransformer {
return { return {
gameId: match.gameId, gameId: match.gameId,
map,
gamemode: mode,
date: gameCreation,
season: match.seasonId, season: match.seasonId,
time,
blueTeam: firstTeam.color === 'Blue' ? firstTeam : secondTeam, blueTeam: firstTeam.color === 'Blue' ? firstTeam : secondTeam,
redTeam: firstTeam.color === 'Blue' ? secondTeam : firstTeam, redTeam: firstTeam.color === 'Blue' ? secondTeam : firstTeam,
...globalInfos
} }
} }
@ -78,10 +74,9 @@ class DetailedMatchTransformer extends MatchTransformer {
}) })
} }
// Players // Players
const players = teamPlayers const players = teamPlayers
.map(p => this.getPlayerData(p, teamStats)) .map(p => super.getPlayerData(p, true, teamStats))
.sort(this.MatchHelper.sortTeamByRole) .sort(this.MatchHelper.sortTeamByRole)
return { return {
@ -97,92 +92,6 @@ class DetailedMatchTransformer extends MatchTransformer {
towers: team.towerKills, towers: team.towerKills,
} }
} }
/**
* Get all data for one player
* @param player raw player data from Riot API
* @param teamStats global stats of the team
*/
getPlayerData(player, teamStats) {
const identity = this.match.participantIdentities.find(p => p.participantId === player.participantId)
const name = identity.player.summonerName
const champion = (({ id, name }) => ({ id, name }))(Object.entries(this.champions).find(([, champion]) => Number(champion.key) === player.championId)[1])
const role = this.MatchHelper.getRoleName(player.timeline)
const kills = player.stats.kills
const deaths = player.stats.deaths
const assists = player.stats.assists
let kda
if (kills + assists !== 0 && deaths === 0) {
kda = '∞'
} else {
kda = +(deaths === 0 ? 0 : ((kills + assists) / deaths)).toFixed(2)
}
const level = player.stats.champLevel
// Regular stats / Full match stats
const stats = {
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',
}
// Percent stats / Per minute stats
const percentStats = {
minions: +(stats.minions / (this.match.gameDuration / 60)).toFixed(2),
vision: +(stats.vision / (this.match.gameDuration / 60)).toFixed(2),
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) + '%',
}
const kp = teamStats.kills === 0 ? '0%' : +((kills + assists) * 100 / teamStats.kills).toFixed(1) + '%'
let primaryRune = null
let secondaryRune = null
if (player.stats.perkPrimaryStyle) {
const primaryRuneCategory = this.runes.find(r => r.id === player.stats.perkPrimaryStyle)
for (const subCat of primaryRuneCategory.slots) {
primaryRune = subCat.runes.find(r => r.id === player.stats.perk0)
if (primaryRune) {
break
}
}
primaryRune = `https://ddragon.leagueoflegends.com/cdn/img/${primaryRune.icon}`
secondaryRune = this.runes.find(r => r.id === player.stats.perkSubStyle)
secondaryRune = `https://ddragon.leagueoflegends.com/cdn/img/${secondaryRune.icon}`
}
const items = []
for (let i = 0; i < 6; i++) {
const currentItem = 'item' + i
items.push(player.stats[currentItem])
}
const firstSum = player.spell1Id
const secondSum = player.spell2Id
return {
name,
champion,
role,
primaryRune,
secondaryRune,
kills,
deaths,
assists,
kda,
level,
kp,
items,
firstSum,
secondSum,
stats,
percentStats,
}
}
} }
module.exports = new DetailedMatchTransformer() module.exports = new DetailedMatchTransformer()

View file

@ -7,12 +7,121 @@
*/ */
class MatchTransformer { class MatchTransformer {
/** /**
* This method is used to transform the data. * Get global data about the match
*/ */
transform (match) { getGameInfos() {
// TODO : implement this method const map = this.match.mapId
const gamemode = this.match.queueId
const date = this.match.gameCreation
const time = this.MatchHelper.secToTime(this.match.gameDuration)
return { return {
// add your transformation object here map,
gamemode,
date,
time
}
}
/**
* Get player specific data during the match
* @param player
* @param detailed : detailed or not stats
* @param teamStats : if detailed, the teamStats argument is mandatory
*/
getPlayerData(player, detailed, teamStats = {}) {
const identity = this.match.participantIdentities.find(p => p.participantId === player.participantId)
const name = identity.player.summonerName
const champion = (({ id, name }) => ({ id, name }))(Object.entries(this.champions).find(([, champion]) => Number(champion.key) === player.championId)[1])
const role = this.MatchHelper.getRoleName(player.timeline)
const kills = player.stats.kills
const deaths = player.stats.deaths
const assists = player.stats.assists
let kda
if (kills + assists !== 0 && deaths === 0) {
kda = '∞'
} else {
kda = +(deaths === 0 ? 0 : ((kills + assists) / deaths)).toFixed(2)
}
const level = player.stats.champLevel
// Regular stats / Full match stats
const stats = {
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',
}
// Percent stats / Per minute stats : only for detailed match
let percentStats
let kp
if (detailed) {
percentStats = {
minions: +(stats.minions / (this.match.gameDuration / 60)).toFixed(2),
vision: +(stats.vision / (this.match.gameDuration / 60)).toFixed(2),
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) + '%',
}
kp = teamStats.kills === 0 ? '0%' : +((kills + assists) * 100 / teamStats.kills).toFixed(1) + '%'
} else {
const totalKills = this.match.participants.reduce((prev, current) => {
if (current.teamId !== player.teamId) {
return prev
}
return prev + current.stats.kills
}, 0)
kp = totalKills === 0 ? '0%' : +((kills + assists) * 100 / totalKills).toFixed(1) + '%'
}
let primaryRune = null
let secondaryRune = null
if (player.stats.perkPrimaryStyle) {
const primaryRuneCategory = this.runes.find(r => r.id === player.stats.perkPrimaryStyle)
for (const subCat of primaryRuneCategory.slots) {
primaryRune = subCat.runes.find(r => r.id === player.stats.perk0)
if (primaryRune) {
break
}
}
primaryRune = `https://ddragon.leagueoflegends.com/cdn/img/${primaryRune.icon}`
secondaryRune = this.runes.find(r => r.id === player.stats.perkSubStyle)
secondaryRune = `https://ddragon.leagueoflegends.com/cdn/img/${secondaryRune.icon}`
}
const items = []
for (let i = 0; i < 6; i++) {
const currentItem = 'item' + i
items.push(player.stats[currentItem])
}
const firstSum = player.spell1Id
const secondSum = player.spell2Id
return {
name,
champion,
role,
primaryRune,
secondaryRune,
kills,
deaths,
assists,
kda,
level,
kp,
items,
firstSum,
secondSum,
stats,
percentStats,
} }
} }
} }