diff --git a/server/app/Commands/EditMatch.js b/server/app/Commands/EditMatch.js new file mode 100644 index 0000000..d168433 --- /dev/null +++ b/server/app/Commands/EditMatch.js @@ -0,0 +1,88 @@ +'use strict' + +const { Command } = require('@adonisjs/ace') +const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer') +const Database = use('Database') +const Jax = use('Jax') +const Match = use('App/Models/Match') +const Queue = use('Bee/Queue') + +class EditMatch extends Command { + static get signature() { + return 'edit:match' + } + + static get description() { + return 'Edit matches in the db with the new Transformer version' + } + + /** + * Create edit-matches Queue + */ + async createQueue() { + Queue.get('edit-matches').process(2, async (job) => { + // Get stats from Riot API + const matchRiot = await Jax.Match.get(job.data.gameId, job.data.region) + const account = { + accountId: (matchRiot.participantIdentities.find(s => s.player.summonerName === job.data.name)).player.currentAccountId, + puuid: job.data.summoner_puuid + } + + // Transform raw matches data + const transformedMatch = await BasicMatchTransformer.transform(matchRiot, { account }) + + // Update match in DB + await Match.where('_id', job.data._id).update(transformedMatch) + + return + }) + + /** + * Job (edit match) finished with success + */ + Queue.get('edit-matches').on('succeeded', (job, result) => { + console.log(`Job ${job.id} succeeded`) + }) + } + + async handle(args, options) { + console.time('EditMatches') + + this.createQueue() + + // All matches from the db + const matches = await Match.all() + const matchesArray = matches.toJSON() + + // Create jobs + const jobs = [] + for (const match of matchesArray) { + const matchInfos = { + _id: match._id, + gameId: match.gameId, + name: match.name, + region: match.region, + summoner_puuid: match.summoner_puuid + } + const job = await Queue + .get('edit-matches') + .createJob(matchInfos) + .save() + jobs.push(job) + } + + // Wait that all jobs are done + const finalResult = 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('EditMatches') + this.success(`${this.icon('success')} Edit ${matchesArray.length} Matches completed`) + } +} + +module.exports = EditMatch diff --git a/server/app/Transformers/BasicMatchTransformer.js b/server/app/Transformers/BasicMatchTransformer.js index c7e22d4..88c04ce 100644 --- a/server/app/Transformers/BasicMatchTransformer.js +++ b/server/app/Transformers/BasicMatchTransformer.js @@ -19,10 +19,12 @@ class BasicMatchTransformer extends MatchTransformer { if (Array.isArray(matches)) { matches.forEach((match, index) => { matches[index] = this.transformOneMatch(match, account) - }); + }) } else { matches = this.transformOneMatch(matches, account) } + + return matches } /** diff --git a/server/start/app.js b/server/start/app.js index 896f202..45468e8 100644 --- a/server/start/app.js +++ b/server/start/app.js @@ -60,6 +60,7 @@ const aliases = {} */ const commands = [ 'App/Commands/DeleteMatch', + 'App/Commands/EditMatch', ] module.exports = { providers, aceProviders, aliases, commands }