From 05f635d7d44e289e49310109e14740d9caf85e5f Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Sun, 3 Nov 2019 20:59:21 +0100 Subject: [PATCH] refactor: clean some Transformers code --- client/src/components/Match/Match.vue | 6 +- .../app/Transformers/BasicMatchTransformer.js | 100 +++------------ .../Transformers/DetailedMatchTransformer.js | 103 +-------------- server/app/Transformers/MatchTransformer.js | 117 +++++++++++++++++- 4 files changed, 141 insertions(+), 185 deletions(-) diff --git a/client/src/components/Match/Match.vue b/client/src/components/Match/Match.vue index 36f6969..7387071 100644 --- a/client/src/components/Match/Match.vue +++ b/client/src/components/Match/Match.vue @@ -79,21 +79,21 @@
Minions
- {{ data.minions }} + {{ data.stats.minions }} cs
Gold
- {{ data.gold }} + {{ data.stats.gold }}
Damage
- {{ data.damage }} + {{ data.stats.dmgChamp }}
diff --git a/server/app/Transformers/BasicMatchTransformer.js b/server/app/Transformers/BasicMatchTransformer.js index dec2ed3..c2be401 100644 --- a/server/app/Transformers/BasicMatchTransformer.js +++ b/server/app/Transformers/BasicMatchTransformer.js @@ -9,77 +9,33 @@ const MatchTransformer = use('App/Transformers/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 }) { + 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 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 if (match.gameDuration < 300) { win = 'Remake' } - const map = match.mapId - const mode = match.queueId - - 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 + // Player data + const playerData = super.getPlayerData(player, false) + // Teams data const allyTeam = [] const enemyTeam = [] 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]) } - if (allData.teamId === teamId) { + if (allData.teamId === player.teamId) { allyTeam.push(playerInfos) } else { enemyTeam.push(playerInfos) @@ -103,28 +59,10 @@ class BasicMatchTransformer extends MatchTransformer { summoner_puuid: account.puuid, gameId: match.gameId, 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, - enemyTeam + enemyTeam, + ...globalInfos, + ...playerData } } } diff --git a/server/app/Transformers/DetailedMatchTransformer.js b/server/app/Transformers/DetailedMatchTransformer.js index 7e23707..02dcc0c 100644 --- a/server/app/Transformers/DetailedMatchTransformer.js +++ b/server/app/Transformers/DetailedMatchTransformer.js @@ -9,7 +9,9 @@ const MatchTransformer = use('App/Transformers/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 }) { this.match = match @@ -18,10 +20,7 @@ class DetailedMatchTransformer extends MatchTransformer { this.MatchHelper = MatchHelper // Global data - const map = match.mapId - const mode = match.queueId - const gameCreation = match.gameCreation - const time = this.MatchHelper.secToTime(match.gameDuration) + const globalInfos = super.getGameInfos() // Teams const firstTeam = this.getTeamData(match.teams[0]) @@ -29,13 +28,10 @@ class DetailedMatchTransformer extends MatchTransformer { return { gameId: match.gameId, - map, - gamemode: mode, - date: gameCreation, season: match.seasonId, - time, blueTeam: firstTeam.color === 'Blue' ? firstTeam : secondTeam, redTeam: firstTeam.color === 'Blue' ? secondTeam : firstTeam, + ...globalInfos } } @@ -78,10 +74,9 @@ class DetailedMatchTransformer extends MatchTransformer { }) } - // Players const players = teamPlayers - .map(p => this.getPlayerData(p, teamStats)) + .map(p => super.getPlayerData(p, true, teamStats)) .sort(this.MatchHelper.sortTeamByRole) return { @@ -97,92 +92,6 @@ class DetailedMatchTransformer extends MatchTransformer { 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() diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js index 244b465..1c04e53 100644 --- a/server/app/Transformers/MatchTransformer.js +++ b/server/app/Transformers/MatchTransformer.js @@ -7,12 +7,121 @@ */ class MatchTransformer { /** - * This method is used to transform the data. + * Get global data about the match */ - transform (match) { - // TODO : implement this method + getGameInfos() { + const map = this.match.mapId + const gamemode = this.match.queueId + const date = this.match.gameCreation + const time = this.MatchHelper.secToTime(this.match.gameDuration) + 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, } } }