mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 12:57:28 +00:00
feat: add season number in matchlist
This commit is contained in:
parent
faf2ede582
commit
fed9cb6406
4 changed files with 111 additions and 4 deletions
77
server/app/Commands/EditMatchList.js
Normal file
77
server/app/Commands/EditMatchList.js
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ const commands = [
|
|||
'App/Commands/DeleteMatch',
|
||||
'App/Commands/EditDetailedMatch',
|
||||
'App/Commands/EditMatch',
|
||||
'App/Commands/EditMatchList',
|
||||
]
|
||||
|
||||
module.exports = { providers, aceProviders, aliases, commands }
|
||||
|
|
|
|||
Loading…
Reference in a new issue