diff --git a/server/app/Commands/EditMatchList.js b/server/app/Commands/EditMatchList.js new file mode 100644 index 0000000..129377f --- /dev/null +++ b/server/app/Commands/EditMatchList.js @@ -0,0 +1,77 @@ +'use strict' + +const { Command } = require('@adonisjs/ace') +const Database = use('Database') +const Queue = use('Bee/Queue') +const Summoner = use('App/Models/Summoner') +const { getSeasonNumber } = use('App/helpers') + +class EditMatchList extends Command { + static get signature() { + return 'edit:match:list' + } + + static get description() { + return 'Edit all matchlist of players to add season of the matches' + } + + /** + * Create edit-matchList Queue + */ + createQueue(concurrent) { + Queue.get('edit-matchList').process(concurrent, async (job) => { + // Update matchlist with season + job.data.matchList = job.data.matchList.map(m => { + m.seasonMatch = getSeasonNumber(m.timestamp) + return m + }) + + // Update Summoner in DB + await Summoner.where('puuid', job.data.puuid).update(job.data) + + return + }) + + /** + * Job (edit matchList) finished with success + */ + Queue.get('edit-matchList').on('succeeded', (job, result) => { + console.log(`Job ${job.id} succeeded`) + }) + } + + async handle(args, options) { + console.time('EditMatchList') + this.info('Start EditMatchList Command') + + this.createQueue(10) + + // All sumoners from the db + const summoners = await Summoner.all() + const summonersArray = summoners.toJSON() + + // Create jobs + const jobs = [] + for (const summoner of summonersArray) { + const job = await Queue + .get('edit-matchList') + .createJob(summoner) + .save() + jobs.push(job) + } + + // Wait that all jobs are done + await new Promise((resolve, reject) => { + const lastJob = jobs[jobs.length - 1] + lastJob.on('succeeded', result => { + resolve(`FINAL RESULT for job ${lastJob.id}`) + }) + }) + + Database.close() + console.timeEnd('EditMatchList') + this.success(`${this.icon('success')} Edit ${summonersArray.length} Summoners completed`) + } +} + +module.exports = EditMatchList diff --git a/server/app/Services/MatchService.js b/server/app/Services/MatchService.js index e4f6cdf..a30b1c3 100644 --- a/server/app/Services/MatchService.js +++ b/server/app/Services/MatchService.js @@ -3,6 +3,7 @@ const Logger = use('Logger') const Jax = use('Jax') const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer') +const { getSeasonNumber } = use('App/helpers') class MatchService { /** @@ -18,6 +19,10 @@ class MatchService { let newMatchList = await Jax.Matchlist.accountID(account.accountId, account.region, index) // Error while fetching Riot API if (!newMatchList) { + matchList = matchList.map(m => { + m.seasonMatch = getSeasonNumber(m.timestamp) + return m + }) return matchList } newMatchList = newMatchList.matches @@ -39,6 +44,10 @@ class MatchService { return sameRegion && notATutorialGame }) + .map(m => { + m.seasonMatch = getSeasonNumber(m.timestamp) + return m + }) return matchList } diff --git a/server/app/helpers.js b/server/app/helpers.js index 8448bf8..cdfd1af 100644 --- a/server/app/helpers.js +++ b/server/app/helpers.js @@ -1,9 +1,29 @@ +/** +* League of Legends seasons timestamps +*/ +const seasons = { + 0: 9, + 1578628800000: 10 +} + module.exports = { + seasons, /** - * Sort array of Roles according to a specific order - * @param a first role - * @param b second role - */ + * Get season number for a match + */ + getSeasonNumber(timestamp) { + const arrSeasons = Object.keys(seasons) + arrSeasons.push(timestamp) + arrSeasons.sort() + const indexSeason = arrSeasons.indexOf(timestamp) - 1 + return seasons[arrSeasons[indexSeason]] + }, + /** + * + * 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) diff --git a/server/start/app.js b/server/start/app.js index 5a23729..ebb5353 100644 --- a/server/start/app.js +++ b/server/start/app.js @@ -62,6 +62,7 @@ const commands = [ 'App/Commands/DeleteMatch', 'App/Commands/EditDetailedMatch', 'App/Commands/EditMatch', + 'App/Commands/EditMatchList', ] module.exports = { providers, aceProviders, aliases, commands }