refactor: rename helpers to services and create real helper file

This commit is contained in:
Valentin Kaelin 2019-12-01 15:20:49 +01:00
parent 0a30cbe8d1
commit 7ab6db8ec2
8 changed files with 34 additions and 44 deletions

View file

@ -3,8 +3,8 @@
const Jax = use('Jax') const Jax = use('Jax')
const DetailedMatch = use('App/Models/DetailedMatch') const DetailedMatch = use('App/Models/DetailedMatch')
const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer') const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer')
const MatchHelper = use('App/Helpers/MatchHelper') const MatchService = use('App/Services/MatchService')
const StatsHelper = use('App/Helpers/StatsHelper') const StatsService = use('App/Services/StatsService')
const Summoner = use('App/Models/Summoner') const Summoner = use('App/Models/Summoner')
class MatchController { class MatchController {
@ -17,11 +17,11 @@ class MatchController {
const gameIds = request.input('gameIds') const gameIds = request.input('gameIds')
const summonerDB = await Summoner.where({ puuid: account.puuid }).first() 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() await summonerDB.save()
const stats = await StatsHelper.getSummonerStats(account) const stats = await StatsService.getSummonerStats(account)
return response.json({ return response.json({
matches, matches,

View file

@ -1,8 +1,8 @@
'use strict' 'use strict'
const Jax = use('Jax') const Jax = use('Jax')
const MatchHelper = use('App/Helpers/MatchHelper') const MatchService = use('App/Services/MatchService')
const StatsHelper = use('App/Helpers/StatsHelper') const StatsService = use('App/Services/StatsService')
const Summoner = use('App/Models/Summoner') const Summoner = use('App/Models/Summoner')
class SummonerController { class SummonerController {
@ -49,20 +49,20 @@ class SummonerController {
} }
// MATCH LIST // MATCH LIST
await MatchHelper.updateMatchList(account, summonerDB) await MatchService.updateMatchList(account, summonerDB)
const matchList = summonerDB.matchList const matchList = summonerDB.matchList
finalJSON.allMatches = matchList finalJSON.allMatches = matchList
// MATCHES BASIC // MATCHES BASIC
const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId) 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 // PATCH VERSION
finalJSON.version = Jax.DDragon.Version finalJSON.version = Jax.DDragon.Version
// STATS // STATS
console.time('STATS') console.time('STATS')
finalJSON.stats = await StatsHelper.getSummonerStats(account) finalJSON.stats = await StatsService.getSummonerStats(account)
console.timeEnd('STATS') console.timeEnd('STATS')
// SAVE IN DB // SAVE IN DB

View file

@ -4,13 +4,13 @@ const Logger = use('Logger')
const Jax = use('Jax') const Jax = use('Jax')
const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer') const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer')
class MatchHelper { class MatchService {
/** /**
* Add 100 matches at a time to MatchList until the stopFetching condition is true * Add 100 matches at a time to MatchList until the stopFetching condition is true
* @param account of the summoner * @param account of the summoner
* @param stopFetching condition to stop fetching the MatchList * @param stopFetching condition to stop fetching the MatchList
*/ */
async fetchMatchListUntil(account, stopFetching) { async _fetchMatchListUntil(account, stopFetching) {
let matchList = [] let matchList = []
let alreadyIn = false let alreadyIn = false
let index = 0 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 // Summoner has already been searched : we already have a MatchList and we need to update it
if (summonerDB.matchList) { if (summonerDB.matchList) {
// Get 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) return summonerDB.matchList.some(m => m.gameId === newMatchList[newMatchList.length - 1].gameId)
}) })
// Update Summoner's MatchList // Update Summoner's MatchList
@ -64,7 +64,7 @@ class MatchHelper {
else { else {
const today = Date.now() const today = Date.now()
// Get MatchList // 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) return (newMatchList.length !== 100 || today - newMatchList[newMatchList.length - 1].timestamp > 10368000000)
}) })
// Create Summoner's MatchList in Database // Create Summoner's MatchList in Database
@ -103,7 +103,6 @@ class MatchHelper {
if (matchesFromApi.length !== 0) { if (matchesFromApi.length !== 0) {
const ctx = { const ctx = {
account, account,
MatchHelper: this
} }
// Transform raw matches data // Transform raw matches data
await BasicMatchTransformer.transform(matchesFromApi, ctx) await BasicMatchTransformer.transform(matchesFromApi, ctx)
@ -126,4 +125,4 @@ class MatchHelper {
} }
} }
module.exports = new MatchHelper() module.exports = new MatchService()

View file

@ -1,9 +1,9 @@
'use strict' 'use strict'
const MatchHelper = use('App/Helpers/MatchHelper') const Helpers = use('App/helpers')
const MatchRepository = make('App/Repositories/MatchRepository') const MatchRepository = make('App/Repositories/MatchRepository')
class StatsHelper { class StatsService {
constructor() { constructor() {
this.matchRepository = MatchRepository this.matchRepository = MatchRepository
} }
@ -31,22 +31,12 @@ class StatsHelper {
return { return {
global: globalStats[0], global: globalStats[0],
league: gamemodeStats, league: gamemodeStats,
role: roleStats.sort(this.sortTeamByRole), role: roleStats.sort(Helpers.sortTeamByRole),
class: championClassStats, class: championClassStats,
mates, mates,
champion: championStats, 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() module.exports = new StatsService()

View file

@ -62,8 +62,8 @@ class BasicMatchTransformer extends MatchTransformer {
enemyTeam.push(playerInfos) enemyTeam.push(playerInfos)
} }
} }
allyTeam.sort(super.sortTeamByRole) allyTeam.sort(this.sortTeamByRole)
enemyTeam.sort(super.sortTeamByRole) enemyTeam.sort(this.sortTeamByRole)
return { return {
summoner_puuid: account.puuid, summoner_puuid: account.puuid,

View file

@ -74,7 +74,7 @@ class DetailedMatchTransformer extends MatchTransformer {
// Players // Players
const players = teamPlayers const players = teamPlayers
.map(p => super.getPlayerData(match, p, true, teamStats)) .map(p => super.getPlayerData(match, p, true, teamStats))
.sort(super.sortTeamByRole) .sort(this.sortTeamByRole)
return { return {
bans, bans,

View file

@ -1,6 +1,7 @@
'use strict' 'use strict'
const Jax = use('Jax') const Jax = use('Jax')
const Helpers = use('App/helpers')
/** /**
* MatchTransformer class * MatchTransformer class
@ -16,13 +17,12 @@ class MatchTransformer {
const champions = await Jax.CDragon.champions() const champions = await Jax.CDragon.champions()
const perks = await Jax.CDragon.perks() const perks = await Jax.CDragon.perks()
const perkstyles = await Jax.CDragon.perkstyles() const perkstyles = await Jax.CDragon.perkstyles()
const version = Jax.DDragon.Version
this.champions = champions this.champions = champions
this.items = items this.items = items
this.perks = perks this.perks = perks
this.perkstyles = perkstyles.styles this.perkstyles = perkstyles.styles
this.version = version this.sortTeamByRole = Helpers.sortTeamByRole
} }
/** /**
@ -164,16 +164,6 @@ class MatchTransformer {
} }
return timeline.lane 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 module.exports = MatchTransformer

11
server/app/helpers.js Normal file
View file

@ -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)
},
}