mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 21:07:27 +00:00
feat: add MatchList in db (started Summoner Collection)
This commit is contained in:
parent
7e93dcd311
commit
ba8c05fbe8
6 changed files with 134 additions and 55 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
const Match = use('App/Models/Match')
|
const Match = use('App/Models/Match')
|
||||||
const Jax = use('Jax')
|
const Jax = use('Jax')
|
||||||
const MatchTransformer = use('App/Transformers/MatchTransformer')
|
const MatchTransformer = use('App/Transformers/MatchTransformer')
|
||||||
|
const MatchHelper = use('App/Helpers/MatchHelper')
|
||||||
|
|
||||||
class SummonerController {
|
class SummonerController {
|
||||||
|
|
||||||
|
|
@ -17,30 +18,6 @@ class SummonerController {
|
||||||
response.json(data)
|
response.json(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the matchlist of all matches made less than 4 months ago by the Summoner
|
|
||||||
* @param today
|
|
||||||
* @param accountId
|
|
||||||
* @param beginIndex
|
|
||||||
* @param allMatches
|
|
||||||
*/
|
|
||||||
async getMatchList(today, accountId, beginIndex, allMatches) {
|
|
||||||
const { matches } = await Jax.Matchlist.accountID(accountId, beginIndex)
|
|
||||||
|
|
||||||
allMatches = [...allMatches, ...matches]
|
|
||||||
|
|
||||||
const lastMatch = matches[matches.length - 1].timestamp
|
|
||||||
const diff = today - lastMatch
|
|
||||||
console.log(diff)
|
|
||||||
// More matches to get from Riot API if they are younger than 4 months
|
|
||||||
if (matches.length === 100 && diff < 10368000000) {
|
|
||||||
return this.getMatchList(today, accountId, (beginIndex + 100), allMatches)
|
|
||||||
} else {
|
|
||||||
console.log('return all matches bro')
|
|
||||||
return allMatches
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all the data about the searched Summoner
|
* Get all the data about the searched Summoner
|
||||||
* @param summoner
|
* @param summoner
|
||||||
|
|
@ -72,9 +49,12 @@ class SummonerController {
|
||||||
finalJSON.soloQ = soloQ.length ? soloQ[0] : null;
|
finalJSON.soloQ = soloQ.length ? soloQ[0] : null;
|
||||||
|
|
||||||
console.time('getMatches')
|
console.time('getMatches')
|
||||||
const today = Date.now()
|
|
||||||
const matches = await this.getMatchList(today, account.accountId, 0, [])
|
// MATCH LIST
|
||||||
const gameIds = matches.slice(0, 10).map(({ gameId }) => gameId)
|
const matchList = await MatchHelper.getFullMatchList(account)
|
||||||
|
|
||||||
|
// MATCHES DETAILS
|
||||||
|
const gameIds = matchList.slice(0, 10).map(({ gameId }) => gameId)
|
||||||
|
|
||||||
let matchesDetails = []
|
let matchesDetails = []
|
||||||
const matchesToGetFromRiot = []
|
const matchesToGetFromRiot = []
|
||||||
|
|
@ -93,7 +73,7 @@ class SummonerController {
|
||||||
const requests = matchesToGetFromRiot.map(Jax.Match.get)
|
const requests = matchesToGetFromRiot.map(Jax.Match.get)
|
||||||
let matchesFromApi = await Promise.all(requests)
|
let matchesFromApi = await Promise.all(requests)
|
||||||
|
|
||||||
/* If we have to same some matches in the db */
|
/* If we have to store some matches in the db */
|
||||||
if (matchesFromApi.length !== 0) {
|
if (matchesFromApi.length !== 0) {
|
||||||
const champions = await Jax.DDragon.Champion.list()
|
const champions = await Jax.DDragon.Champion.list()
|
||||||
const runes = await Jax.DDragon.Rune.list()
|
const runes = await Jax.DDragon.Rune.list()
|
||||||
|
|
@ -120,7 +100,7 @@ class SummonerController {
|
||||||
matchesDetails.sort((a, b) => (a.date < b.date) ? 1 : -1)
|
matchesDetails.sort((a, b) => (a.date < b.date) ? 1 : -1)
|
||||||
|
|
||||||
finalJSON.matchesDetails = matchesDetails
|
finalJSON.matchesDetails = matchesDetails
|
||||||
finalJSON.allMatches = matches
|
finalJSON.allMatches = matchList
|
||||||
|
|
||||||
console.timeEnd('getMatches')
|
console.timeEnd('getMatches')
|
||||||
console.timeEnd('all')
|
console.timeEnd('all')
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
class MatchHelper {
|
|
||||||
getRoleName(timeline) {
|
|
||||||
if (timeline.lane === 'BOTTOM' && timeline.role.includes('SUPPORT')) {
|
|
||||||
return 'SUPPORT'
|
|
||||||
}
|
|
||||||
return timeline.lane
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return time in a formatted way
|
|
||||||
* @param sec : time in seconds to convert
|
|
||||||
*/
|
|
||||||
secToTime(sec) {
|
|
||||||
const min = Math.floor(sec / 60)
|
|
||||||
const newSec = sec - min * 60
|
|
||||||
return min + 'm' + (newSec < 10 ? '0' + newSec : newSec) + 's'
|
|
||||||
}
|
|
||||||
|
|
||||||
sortTeamByRole(a, b) {
|
|
||||||
const sortingArr = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT']
|
|
||||||
return sortingArr.indexOf(a.role) - sortingArr.indexOf(b.role)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = new MatchHelper()
|
|
||||||
97
server/app/Helpers/MatchHelper.js
Normal file
97
server/app/Helpers/MatchHelper.js
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
const Summoner = use('App/Models/Summoner')
|
||||||
|
const Jax = use('Jax')
|
||||||
|
const Logger = use('Logger')
|
||||||
|
|
||||||
|
class MatchHelper {
|
||||||
|
/**
|
||||||
|
* Get the matchlist of all matches made less than 4 months ago by the Summoner
|
||||||
|
* @param today timestamp of the day
|
||||||
|
* @param accountId id of the Summoner
|
||||||
|
* @param beginIndex index of the first match to fetch
|
||||||
|
* @param allMatches matchList completing
|
||||||
|
*/
|
||||||
|
async getMatchListFourMonths(today, accountId, beginIndex, allMatches) {
|
||||||
|
const { matches } = await Jax.Matchlist.accountID(accountId, beginIndex)
|
||||||
|
|
||||||
|
allMatches = [...allMatches, ...matches]
|
||||||
|
|
||||||
|
const lastMatch = matches[matches.length - 1].timestamp
|
||||||
|
const diff = today - lastMatch
|
||||||
|
console.log(diff)
|
||||||
|
// More matches to get from Riot API if they are younger than 4 months
|
||||||
|
if (matches.length === 100 && diff < 10368000000) {
|
||||||
|
return this.getMatchListFourMonths(today, accountId, (beginIndex + 100), allMatches)
|
||||||
|
} else {
|
||||||
|
return allMatches
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the full MatchList of the summoner (min. 4 months)
|
||||||
|
* @param account of the summoner
|
||||||
|
*/
|
||||||
|
async getFullMatchList(account) {
|
||||||
|
const today = Date.now()
|
||||||
|
let matches
|
||||||
|
|
||||||
|
let summonerDB = await Summoner.where({ puuid: account.puuid }).first()
|
||||||
|
// Summoner has already been searched : we already have a MatchList and we need to update it
|
||||||
|
if (summonerDB) {
|
||||||
|
matches = (await Jax.Matchlist.accountID(account.accountId, 0)).matches
|
||||||
|
let alreadyIn = summonerDB.matchList.some(m => m.gameId === matches[matches.length - 1].gameId)
|
||||||
|
let index = 100
|
||||||
|
while (!alreadyIn) {
|
||||||
|
let newMatchList = (await Jax.Matchlist.accountID(account.accountId, index)).matches
|
||||||
|
matches = [...matches, ...newMatchList]
|
||||||
|
alreadyIn = summonerDB.matchList.some(m => m.gameId === matches[matches.length - 1].gameId)
|
||||||
|
index += 100
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Summoner
|
||||||
|
summonerDB.matchList = [...new Set([...summonerDB.matchList, ...matches])]
|
||||||
|
await summonerDB.save()
|
||||||
|
Logger.transport('file').info(`Summoner ${account.name} has been updated.`)
|
||||||
|
}
|
||||||
|
// First search of the Summoner
|
||||||
|
else {
|
||||||
|
matches = await this.getMatchListFourMonths(today, account.accountId, 0, [])
|
||||||
|
summonerDB = await Summoner.create({ puuid: account.puuid, matchList: matches })
|
||||||
|
Logger.transport('file').info(`Summoner ${account.name} has been created.`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return summonerDB.matchList
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the lane of the summoner according to timeline
|
||||||
|
* @param timeline from Riot Api
|
||||||
|
*/
|
||||||
|
getRoleName(timeline) {
|
||||||
|
if (timeline.lane === 'BOTTOM' && timeline.role.includes('SUPPORT')) {
|
||||||
|
return 'SUPPORT'
|
||||||
|
}
|
||||||
|
return timeline.lane
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return time in a formatted way
|
||||||
|
* @param sec time in seconds to convert
|
||||||
|
*/
|
||||||
|
secToTime(sec) {
|
||||||
|
const min = Math.floor(sec / 60)
|
||||||
|
const newSec = sec - min * 60
|
||||||
|
return min + 'm' + (newSec < 10 ? '0' + newSec : newSec) + 's'
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 MatchHelper()
|
||||||
9
server/app/Models/Summoner.js
Normal file
9
server/app/Models/Summoner.js
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
|
||||||
|
const Model = use('Model')
|
||||||
|
|
||||||
|
class Summoner extends Model {
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Summoner
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const BumblebeeTransformer = use('Bumblebee/Transformer')
|
const BumblebeeTransformer = use('Bumblebee/Transformer')
|
||||||
const MatchHelper = use('App/Helpers/Match')
|
const MatchHelper = use('App/Helpers/MatchHelper')
|
||||||
const Logger = use('Logger')
|
const Logger = use('Logger')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
18
server/database/migrations/1569778458207_summoner_schema.js
Normal file
18
server/database/migrations/1569778458207_summoner_schema.js
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
/** @type {import('@adonisjs/lucid/src/Schema')} */
|
||||||
|
const Schema = use('Schema')
|
||||||
|
|
||||||
|
class SummonerSchema extends Schema {
|
||||||
|
up () {
|
||||||
|
this.create('summoners', (collection) => {
|
||||||
|
collection.index('puuid', {puuid: 1})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
down () {
|
||||||
|
this.drop('summoners')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = SummonerSchema
|
||||||
Loading…
Reference in a new issue