mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-25 12:57:28 +00:00
refactor: clean some more code for MatchList
This commit is contained in:
parent
839b324d64
commit
a8b4fef901
1 changed files with 25 additions and 33 deletions
|
|
@ -4,50 +4,37 @@ const Logger = use('Logger')
|
||||||
|
|
||||||
class MatchHelper {
|
class MatchHelper {
|
||||||
/**
|
/**
|
||||||
* Get the matchlist of all matches made less than 4 months ago by the Summoner
|
* Add 100 matches at a time to MatchList until the stopFetching condition is true
|
||||||
* @param today timestamp of the day
|
* @param account of the summoner
|
||||||
* @param accountId id of the Summoner
|
* @param stopFetching condition to stop fetching the MatchList
|
||||||
* @param beginIndex index of the first match to fetch
|
|
||||||
* @param allMatches matchList completing
|
|
||||||
*/
|
*/
|
||||||
async getMatchListFourMonths(today, accountId, beginIndex, allMatches) {
|
async fetchMatchListUntil(account, stopFetching) {
|
||||||
const { matches } = await Jax.Matchlist.accountID(accountId, beginIndex)
|
let matchList = []
|
||||||
|
let alreadyIn = false
|
||||||
|
let index = 0
|
||||||
|
do {
|
||||||
|
let newMatchList = (await Jax.Matchlist.accountID(account.accountId, index)).matches
|
||||||
|
matchList = [...matchList, ...newMatchList]
|
||||||
|
alreadyIn = stopFetching(newMatchList)
|
||||||
|
index += 100
|
||||||
|
} while (!alreadyIn);
|
||||||
|
|
||||||
allMatches = [...allMatches, ...matches]
|
return matchList
|
||||||
|
|
||||||
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)
|
* Return the full MatchList of the summoner (min. 4 months)
|
||||||
* @param account of the summoner
|
* @param account of the summoner
|
||||||
*/
|
*/
|
||||||
async getFullMatchList(account) {
|
async getFullMatchList(account) {
|
||||||
console.time('matchList')
|
console.time('matchList')
|
||||||
const today = Date.now()
|
|
||||||
let matchList = []
|
|
||||||
|
|
||||||
let summonerDB = await Summoner.where({ puuid: account.puuid }).first()
|
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
|
// Summoner has already been searched : we already have a MatchList and we need to update it
|
||||||
if (summonerDB) {
|
if (summonerDB) {
|
||||||
let alreadyIn = false
|
// Get MatchList
|
||||||
let index = 0
|
const matchList = await this.fetchMatchListUntil(account, (newMatchList) => {
|
||||||
do {
|
return summonerDB.matchList.some(m => m.gameId === newMatchList[newMatchList.length - 1].gameId)
|
||||||
let newMatchList = (await Jax.Matchlist.accountID(account.accountId, index)).matches
|
})
|
||||||
|
|
||||||
matchList = [...matchList, ...newMatchList]
|
|
||||||
alreadyIn = summonerDB.matchList.some(m => m.gameId === matchList[matchList.length - 1].gameId)
|
|
||||||
index += 100
|
|
||||||
} while (!alreadyIn);
|
|
||||||
|
|
||||||
// Update Summoner's MatchList
|
// Update Summoner's MatchList
|
||||||
for (const match of matchList) {
|
for (const match of matchList) {
|
||||||
if (!summonerDB.matchList.some(m => m.gameId === match.gameId)) {
|
if (!summonerDB.matchList.some(m => m.gameId === match.gameId)) {
|
||||||
|
|
@ -60,7 +47,12 @@ class MatchHelper {
|
||||||
}
|
}
|
||||||
// First search of the Summoner
|
// First search of the Summoner
|
||||||
else {
|
else {
|
||||||
matchList = await this.getMatchListFourMonths(today, account.accountId, 0, [])
|
const today = Date.now()
|
||||||
|
// Get MatchList
|
||||||
|
const matchList = await this.fetchMatchListUntil(account, (newMatchList) => {
|
||||||
|
return (newMatchList.length !== 100 || today - newMatchList[newMatchList.length - 1].timestamp > 10368000000)
|
||||||
|
})
|
||||||
|
// Create Summoner in Database
|
||||||
summonerDB = await Summoner.create({ puuid: account.puuid, matchList: matchList })
|
summonerDB = await Summoner.create({ puuid: account.puuid, matchList: matchList })
|
||||||
Logger.transport('file').info(`Summoner ${account.name} has been created.`)
|
Logger.transport('file').info(`Summoner ${account.name} has been created.`)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue