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 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,

View file

@ -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

View file

@ -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()

View file

@ -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()
module.exports = new StatsService()

View file

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

View file

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

View file

@ -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

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