From a200da1a058efff843fe090915eb6dcf692f8ec5 Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Sat, 16 Nov 2019 17:43:55 +0100 Subject: [PATCH] refactor: clean code of the Transformers --- .../app/Controllers/Http/MatchController.js | 14 +--- server/app/Helpers/MatchHelper.js | 47 +------------ server/app/Helpers/StatsHelper.js | 12 +++- .../app/Transformers/BasicMatchTransformer.js | 36 ++++++---- .../Transformers/DetailedMatchTransformer.js | 27 +++----- server/app/Transformers/MatchTransformer.js | 69 ++++++++++++++----- 6 files changed, 98 insertions(+), 107 deletions(-) diff --git a/server/app/Controllers/Http/MatchController.js b/server/app/Controllers/Http/MatchController.js index e4a65c1..1baed55 100644 --- a/server/app/Controllers/Http/MatchController.js +++ b/server/app/Controllers/Http/MatchController.js @@ -40,19 +40,7 @@ class MatchController { let matchFromRiot = await Jax.Match.get(gameId) - const champions = await Jax.DDragon.Champion.list() - const items = await Jax.DDragon.Item.list() - const runes = await Jax.DDragon.Rune.list() - const version = Jax.DDragon.Version - const ctx = { - champions: champions.data, - items: items.data, - runes, - version, - MatchHelper - } - - matchFromRiot = DetailedMatchTransformer.transform(matchFromRiot, ctx) + matchFromRiot = await DetailedMatchTransformer.transform(matchFromRiot) // DetailedMatch.save(matchFromRiot) diff --git a/server/app/Helpers/MatchHelper.js b/server/app/Helpers/MatchHelper.js index 187eb5f..5280dc7 100644 --- a/server/app/Helpers/MatchHelper.js +++ b/server/app/Helpers/MatchHelper.js @@ -101,24 +101,12 @@ class MatchHelper { /* If we have to store some matches in the db */ if (matchesFromApi.length !== 0) { - const items = await Jax.DDragon.Item.list() - const champions = await Jax.DDragon.Champion.list() - const runes = await Jax.DDragon.Rune.list() - const version = Jax.DDragon.Version const ctx = { account, - champions: champions.data, - items: items.data, - runes, - version, MatchHelper: this } - - matchesFromApi = matchesFromApi.map(m => { - if (m) return BasicMatchTransformer.transform(m, ctx) - }) - - console.log(matchesFromApi.length) + // Transform raw matches data + await BasicMatchTransformer.transform(matchesFromApi, ctx) Logger.transport('file').info(matchesFromApi) matchesDetails = [...matchesDetails, ...matchesFromApi] @@ -136,37 +124,6 @@ class MatchHelper { return matchesDetails } - - /** - * 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 - } - - /** - * Return time in a formatted way - * @param sec time in seconds to convert - */ - secToTime(sec) { - const min = Math.floor(sec / 60) - const newSec = sec - min * 60 - return min + 'm' + (newSec < 10 ? '0' + newSec : newSec) + 's' - } - - /** - * 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 = new MatchHelper() diff --git a/server/app/Helpers/StatsHelper.js b/server/app/Helpers/StatsHelper.js index c25b74e..f4975a2 100644 --- a/server/app/Helpers/StatsHelper.js +++ b/server/app/Helpers/StatsHelper.js @@ -30,11 +30,21 @@ class StatsHelper { return { global: globalStats[0], league: gamemodeStats, - role: roleStats.sort(MatchHelper.sortTeamByRole), + role: roleStats.sort(this.sortTeamByRole), class: championClassStats, mates, } } + + /** + * 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 = new StatsHelper() \ No newline at end of file diff --git a/server/app/Transformers/BasicMatchTransformer.js b/server/app/Transformers/BasicMatchTransformer.js index 0cdcf81..4d0195a 100644 --- a/server/app/Transformers/BasicMatchTransformer.js +++ b/server/app/Transformers/BasicMatchTransformer.js @@ -10,19 +10,27 @@ const MatchTransformer = use('App/Transformers/MatchTransformer') class BasicMatchTransformer extends MatchTransformer { /** * Transform raw data from Riot API - * @param match data from Riot API + * @param matches data from Riot API, Array of match or a single match * @param ctx context */ - transform(match, { account, champions, items, runes, version, MatchHelper }) { - this.match = match - this.champions = champions - this.items = items - this.runes = runes - this.version = version - this.MatchHelper = MatchHelper + async transform(matches, { account }) { + await super.getContext() + if (Array.isArray(matches)) { + matches.forEach((match, index) => { + matches[index] = this.transformOneMatch(match, account) + }); + } else { + matches = this.transformOneMatch(matches, account) + } + } + + /** + * Transform raw data for 1 match + */ + transformOneMatch(match, account) { // Global data about the match - const globalInfos = super.getGameInfos() + const globalInfos = super.getGameInfos(match) const participantId = match.participantIdentities.find((p) => p.player.currentAccountId === account.accountId).participantId const player = match.participants[participantId - 1] @@ -35,7 +43,7 @@ class BasicMatchTransformer extends MatchTransformer { } // Player data - const playerData = super.getPlayerData(player, false) + const playerData = super.getPlayerData(match, player, false) // Teams data const allyTeam = [] @@ -44,8 +52,8 @@ class BasicMatchTransformer extends MatchTransformer { const allData = match.participants[summoner.participantId - 1] const playerInfos = { name: summoner.player.summonerName, - role: MatchHelper.getRoleName(allData.timeline), - champion: (({ id, name }) => ({ id, name }))(Object.entries(champions).find(([, champion]) => Number(champion.key) === allData.championId)[1]) + role: super.getRoleName(allData.timeline), + champion: (({ id, name }) => ({ id, name }))(Object.entries(this.champions).find(([, champion]) => Number(champion.key) === allData.championId)[1]) } if (allData.teamId === player.teamId) { @@ -54,8 +62,8 @@ class BasicMatchTransformer extends MatchTransformer { enemyTeam.push(playerInfos) } } - allyTeam.sort(MatchHelper.sortTeamByRole) - enemyTeam.sort(MatchHelper.sortTeamByRole) + allyTeam.sort(super.sortTeamByRole) + enemyTeam.sort(super.sortTeamByRole) return { summoner_puuid: account.puuid, diff --git a/server/app/Transformers/DetailedMatchTransformer.js b/server/app/Transformers/DetailedMatchTransformer.js index c8270c7..5baae52 100644 --- a/server/app/Transformers/DetailedMatchTransformer.js +++ b/server/app/Transformers/DetailedMatchTransformer.js @@ -11,22 +11,16 @@ class DetailedMatchTransformer extends MatchTransformer { /** * Transform raw data from Riot API * @param match data from Riot API - * @param ctx context */ - transform(match, { champions, items, runes, version, MatchHelper }) { - this.match = match - this.champions = champions - this.items = items - this.runes = runes - this.version = version - this.MatchHelper = MatchHelper + async transform(match) { + await super.getContext() // Global data - const globalInfos = super.getGameInfos() + const globalInfos = super.getGameInfos(match) // Teams - const firstTeam = this.getTeamData(match.teams[0]) - const secondTeam = this.getTeamData(match.teams[1]) + const firstTeam = this.getTeamData(match, match.teams[0]) + const secondTeam = this.getTeamData(match, match.teams[1]) return { gameId: match.gameId, @@ -39,16 +33,17 @@ class DetailedMatchTransformer extends MatchTransformer { /** * Get all data of one team + * @param match raw match data from Riot API * @param team raw team data from Riot API */ - getTeamData(team) { + getTeamData(match, team) { let win = team.win - if (this.match.gameDuration < 300) { + if (match.gameDuration < 300) { win = 'Remake' } // Global stats of the team - const teamPlayers = this.match.participants.filter(p => p.teamId === team.teamId) + const teamPlayers = match.participants.filter(p => p.teamId === team.teamId) const teamStats = teamPlayers.reduce((prev, cur) => { prev.kills += cur.stats.kills prev.deaths += cur.stats.deaths @@ -78,8 +73,8 @@ class DetailedMatchTransformer extends MatchTransformer { // Players const players = teamPlayers - .map(p => super.getPlayerData(p, true, teamStats)) - .sort(this.MatchHelper.sortTeamByRole) + .map(p => super.getPlayerData(match, p, true, teamStats)) + .sort(super.sortTeamByRole) return { bans, diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js index 326b1ec..4fe2469 100644 --- a/server/app/Transformers/MatchTransformer.js +++ b/server/app/Transformers/MatchTransformer.js @@ -1,40 +1,52 @@ 'use strict' +const Jax = use('Jax') + /** * MatchTransformer class * * @class MatchTransformer */ class MatchTransformer { + /** + * Get global Context with DDragon Data + */ + async getContext() { + const items = await Jax.DDragon.Item.list() + const champions = await Jax.DDragon.Champion.list() + const runes = await Jax.DDragon.Rune.list() + const version = Jax.DDragon.Version + + this.champions = champions.data + this.items = items.data + this.runes = runes + this.version = version + } + /** * Get global data about the match */ - getGameInfos() { - const map = this.match.mapId - const gamemode = this.match.queueId - const date = this.match.gameCreation - // const time = this.MatchHelper.secToTime(this.match.gameDuration) - const time = this.match.gameDuration - + getGameInfos(match) { return { - map, - gamemode, - date, - time + map: match.mapId, + gamemode: match.queueId, + date: match.gameCreation, + time: match.gameDuration } } /** * Get player specific data during the match + * @param 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) + getPlayerData(match, player, detailed, teamStats = {}) { + const identity = match.participantIdentities.find(p => p.participantId === player.participantId) const name = identity.player.summonerName const champion = (({ id, name, tags }) => ({ id, name, tags }))(Object.entries(this.champions).find(([, champion]) => Number(champion.key) === player.championId)[1]) - const role = this.MatchHelper.getRoleName(player.timeline) + const role = this.getRoleName(player.timeline) const level = player.stats.champLevel // Regular stats / Full match stats @@ -60,8 +72,8 @@ class MatchTransformer { let percentStats if (detailed) { percentStats = { - minions: +(stats.minions / (this.match.gameDuration / 60)).toFixed(2), - vision: +(stats.vision / (this.match.gameDuration / 60)).toFixed(2), + 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) + '%', dmgChamp: +(player.stats.totalDamageDealtToChampions * 100 / teamStats.dmgChamp).toFixed(1) + '%', dmgObj: +(player.stats.damageDealtToObjectives * 100 / teamStats.dmgObj).toFixed(1) + '%', @@ -70,7 +82,7 @@ class MatchTransformer { stats.kp = teamStats.kills === 0 ? '0%' : +((stats.kills + stats.assists) * 100 / teamStats.kills).toFixed(1) + '%' } else { - const totalKills = this.match.participants.reduce((prev, current) => { + const totalKills = match.participants.reduce((prev, current) => { if (current.teamId !== player.teamId) { return prev } @@ -99,7 +111,7 @@ class MatchTransformer { const items = [] for (let i = 0; i < 6; i++) { const id = player.stats['item' + i] - if(id === 0) { + if (id === 0) { items.push(null) continue } @@ -128,6 +140,27 @@ class MatchTransformer { 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