diff --git a/server/app/Controllers/Http/MatchController.js b/server/app/Controllers/Http/MatchController.js index 1baed55..aad5b65 100644 --- a/server/app/Controllers/Http/MatchController.js +++ b/server/app/Controllers/Http/MatchController.js @@ -3,8 +3,8 @@ const Jax = use('Jax') const DetailedMatch = use('App/Models/DetailedMatch') const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer') -const MatchHelper = use('App/Helpers/MatchHelper') -const StatsHelper = use('App/Helpers/StatsHelper') +const MatchService = use('App/Services/MatchService') +const StatsService = use('App/Services/StatsService') const Summoner = use('App/Models/Summoner') class MatchController { @@ -17,11 +17,11 @@ class MatchController { const gameIds = request.input('gameIds') const summonerDB = await Summoner.where({ puuid: account.puuid }).first() - const matches = await MatchHelper.getMatches(account, gameIds, summonerDB) + const matches = await MatchService.getMatches(account, gameIds, summonerDB) await summonerDB.save() - const stats = await StatsHelper.getSummonerStats(account) + const stats = await StatsService.getSummonerStats(account) return response.json({ matches, diff --git a/server/app/Controllers/Http/SummonerController.js b/server/app/Controllers/Http/SummonerController.js index 76eb1d7..06801e8 100644 --- a/server/app/Controllers/Http/SummonerController.js +++ b/server/app/Controllers/Http/SummonerController.js @@ -1,8 +1,8 @@ 'use strict' const Jax = use('Jax') -const MatchHelper = use('App/Helpers/MatchHelper') -const StatsHelper = use('App/Helpers/StatsHelper') +const MatchService = use('App/Services/MatchService') +const StatsService = use('App/Services/StatsService') const Summoner = use('App/Models/Summoner') class SummonerController { @@ -49,20 +49,20 @@ class SummonerController { } // MATCH LIST - await MatchHelper.updateMatchList(account, summonerDB) + await MatchService.updateMatchList(account, summonerDB) const matchList = summonerDB.matchList finalJSON.allMatches = matchList // MATCHES BASIC const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId) - finalJSON.matchesDetails = await MatchHelper.getMatches(account, gameIds, summonerDB) + finalJSON.matchesDetails = await MatchService.getMatches(account, gameIds, summonerDB) // PATCH VERSION finalJSON.version = Jax.DDragon.Version // STATS console.time('STATS') - finalJSON.stats = await StatsHelper.getSummonerStats(account) + finalJSON.stats = await StatsService.getSummonerStats(account) console.timeEnd('STATS') // SAVE IN DB diff --git a/server/app/Helpers/MatchHelper.js b/server/app/Services/MatchService.js similarity index 93% rename from server/app/Helpers/MatchHelper.js rename to server/app/Services/MatchService.js index 5280dc7..9eb625c 100644 --- a/server/app/Helpers/MatchHelper.js +++ b/server/app/Services/MatchService.js @@ -4,13 +4,13 @@ const Logger = use('Logger') const Jax = use('Jax') const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer') -class MatchHelper { +class MatchService { /** * Add 100 matches at a time to MatchList until the stopFetching condition is true * @param account of the summoner * @param stopFetching condition to stop fetching the MatchList */ - async fetchMatchListUntil(account, stopFetching) { + async _fetchMatchListUntil(account, stopFetching) { let matchList = [] let alreadyIn = false let index = 0 @@ -48,7 +48,7 @@ class MatchHelper { // Summoner has already been searched : we already have a MatchList and we need to update it if (summonerDB.matchList) { // Get MatchList - const matchList = await this.fetchMatchListUntil(account, (newMatchList) => { + const matchList = await this._fetchMatchListUntil(account, (newMatchList) => { return summonerDB.matchList.some(m => m.gameId === newMatchList[newMatchList.length - 1].gameId) }) // Update Summoner's MatchList @@ -64,7 +64,7 @@ class MatchHelper { else { const today = Date.now() // Get MatchList - const matchList = await this.fetchMatchListUntil(account, (newMatchList) => { + const matchList = await this._fetchMatchListUntil(account, (newMatchList) => { return (newMatchList.length !== 100 || today - newMatchList[newMatchList.length - 1].timestamp > 10368000000) }) // Create Summoner's MatchList in Database @@ -103,7 +103,6 @@ class MatchHelper { if (matchesFromApi.length !== 0) { const ctx = { account, - MatchHelper: this } // Transform raw matches data await BasicMatchTransformer.transform(matchesFromApi, ctx) @@ -126,4 +125,4 @@ class MatchHelper { } } -module.exports = new MatchHelper() +module.exports = new MatchService() diff --git a/server/app/Helpers/StatsHelper.js b/server/app/Services/StatsService.js similarity index 72% rename from server/app/Helpers/StatsHelper.js rename to server/app/Services/StatsService.js index cea8a81..2d0420f 100644 --- a/server/app/Helpers/StatsHelper.js +++ b/server/app/Services/StatsService.js @@ -1,9 +1,9 @@ 'use strict' -const MatchHelper = use('App/Helpers/MatchHelper') +const Helpers = use('App/helpers') const MatchRepository = make('App/Repositories/MatchRepository') -class StatsHelper { +class StatsService { constructor() { this.matchRepository = MatchRepository } @@ -31,22 +31,12 @@ class StatsHelper { return { global: globalStats[0], league: gamemodeStats, - role: roleStats.sort(this.sortTeamByRole), + role: roleStats.sort(Helpers.sortTeamByRole), class: championClassStats, mates, champion: championStats, } } - - /** - * 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 +module.exports = new StatsService() \ No newline at end of file diff --git a/server/app/Transformers/BasicMatchTransformer.js b/server/app/Transformers/BasicMatchTransformer.js index ef9622f..c7e22d4 100644 --- a/server/app/Transformers/BasicMatchTransformer.js +++ b/server/app/Transformers/BasicMatchTransformer.js @@ -62,8 +62,8 @@ class BasicMatchTransformer extends MatchTransformer { enemyTeam.push(playerInfos) } } - allyTeam.sort(super.sortTeamByRole) - enemyTeam.sort(super.sortTeamByRole) + allyTeam.sort(this.sortTeamByRole) + enemyTeam.sort(this.sortTeamByRole) return { summoner_puuid: account.puuid, diff --git a/server/app/Transformers/DetailedMatchTransformer.js b/server/app/Transformers/DetailedMatchTransformer.js index c5e508a..4cef473 100644 --- a/server/app/Transformers/DetailedMatchTransformer.js +++ b/server/app/Transformers/DetailedMatchTransformer.js @@ -74,7 +74,7 @@ class DetailedMatchTransformer extends MatchTransformer { // Players const players = teamPlayers .map(p => super.getPlayerData(match, p, true, teamStats)) - .sort(super.sortTeamByRole) + .sort(this.sortTeamByRole) return { bans, diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js index 307279e..fda5cf0 100644 --- a/server/app/Transformers/MatchTransformer.js +++ b/server/app/Transformers/MatchTransformer.js @@ -1,6 +1,7 @@ 'use strict' const Jax = use('Jax') +const Helpers = use('App/helpers') /** * MatchTransformer class @@ -16,13 +17,12 @@ class MatchTransformer { 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 + this.sortTeamByRole = Helpers.sortTeamByRole } /** @@ -164,16 +164,6 @@ class MatchTransformer { } 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 diff --git a/server/app/helpers.js b/server/app/helpers.js new file mode 100644 index 0000000..8448bf8 --- /dev/null +++ b/server/app/helpers.js @@ -0,0 +1,11 @@ +module.exports = { + /** + * 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) + }, +}