2020-10-05 09:16:21 +00:00
|
|
|
import Jax from './Jax'
|
2020-10-08 07:51:12 +00:00
|
|
|
import Logger from '@ioc:Adonis/Core/Logger'
|
2021-08-07 20:52:54 +00:00
|
|
|
import { MatchlistDto } from './Jax/src/Endpoints/MatchlistEndpoint'
|
2020-10-05 09:16:21 +00:00
|
|
|
import { SummonerDTO } from './Jax/src/Endpoints/SummonerEndpoint'
|
2020-10-05 10:40:02 +00:00
|
|
|
import { SummonerModel } from 'App/Models/Summoner'
|
2020-10-08 07:51:12 +00:00
|
|
|
import Match, { MatchModel } from 'App/Models/Match'
|
|
|
|
|
import BasicMatchTransformer from 'App/Transformers/BasicMatchTransformer'
|
2020-10-05 09:16:21 +00:00
|
|
|
|
|
|
|
|
class MatchService {
|
|
|
|
|
/**
|
|
|
|
|
* Add 100 matches at a time to MatchList until the stopFetching condition is true
|
|
|
|
|
* @param account of the summoner
|
|
|
|
|
* @param stopFetching condition to stop fetching the MatchList
|
|
|
|
|
*/
|
|
|
|
|
private async _fetchMatchListUntil (account: SummonerDTO, stopFetching: any) {
|
2021-08-07 20:52:54 +00:00
|
|
|
let matchList: MatchlistDto = []
|
2020-10-05 09:16:21 +00:00
|
|
|
let alreadyIn = false
|
|
|
|
|
let index = 0
|
|
|
|
|
do {
|
2021-08-07 20:52:54 +00:00
|
|
|
let newMatchList = await Jax.Matchlist.puuid(account.puuid, account.region as string, index)
|
2020-10-05 09:16:21 +00:00
|
|
|
// Error while fetching Riot API
|
|
|
|
|
if (!newMatchList) {
|
2021-08-07 20:52:54 +00:00
|
|
|
// matchList = matchList.map(m => {
|
|
|
|
|
// m.season = getSeasonNumber(m.timestamp)
|
|
|
|
|
// return m
|
|
|
|
|
// })
|
2020-10-05 09:16:21 +00:00
|
|
|
return matchList
|
|
|
|
|
}
|
2021-08-07 20:52:54 +00:00
|
|
|
matchList = [...matchList, ...newMatchList]
|
|
|
|
|
alreadyIn = newMatchList.length === 0 || stopFetching(newMatchList)
|
2020-10-05 09:16:21 +00:00
|
|
|
// If the match is made in another region : we stop fetching
|
2021-08-10 22:12:17 +00:00
|
|
|
// if (matchList[matchList.length - 1].platformId.toLowerCase() !== account.region) { // TODO: check if region has changed
|
2021-08-07 20:52:54 +00:00
|
|
|
// alreadyIn = true
|
|
|
|
|
// }
|
2020-10-05 09:16:21 +00:00
|
|
|
index += 100
|
|
|
|
|
} while (!alreadyIn)
|
|
|
|
|
|
|
|
|
|
// Remove matches from MatchList made in another region and tutorial games
|
2021-08-07 20:52:54 +00:00
|
|
|
// const tutorialModes = [2000, 2010, 2020]
|
|
|
|
|
// matchList = matchList
|
|
|
|
|
// .filter(m => {
|
|
|
|
|
// const sameRegion = m.platformId.toLowerCase() === account.region
|
|
|
|
|
// const notATutorialGame = !tutorialModes.includes(m.queue)
|
2020-10-05 09:16:21 +00:00
|
|
|
|
2021-08-07 20:52:54 +00:00
|
|
|
// return sameRegion && notATutorialGame
|
|
|
|
|
// })
|
|
|
|
|
// .map(m => {
|
|
|
|
|
// m.seasonMatch = getSeasonNumber(m.timestamp)
|
|
|
|
|
// return m
|
|
|
|
|
// })
|
2020-10-05 09:16:21 +00:00
|
|
|
|
|
|
|
|
return matchList
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Update the full MatchList of the summoner (min. 4 months)
|
|
|
|
|
* @param account of the summoner
|
|
|
|
|
* @param summonerDB summoner in the database
|
|
|
|
|
*/
|
|
|
|
|
public async updateMatchList (account: SummonerDTO, summonerDB: SummonerModel) {
|
|
|
|
|
console.time('matchList')
|
|
|
|
|
|
|
|
|
|
// Summoner has already been searched : we already have a MatchList and we need to update it
|
|
|
|
|
if (summonerDB.matchList) {
|
|
|
|
|
// Get MatchList
|
2021-08-07 20:52:54 +00:00
|
|
|
const matchList = await this._fetchMatchListUntil(account, (newMatchList: MatchlistDto) => {
|
|
|
|
|
return summonerDB.matchList!.some(m => m === newMatchList[newMatchList.length - 1])
|
2020-10-05 09:16:21 +00:00
|
|
|
})
|
|
|
|
|
// Update Summoner's MatchList
|
|
|
|
|
for (const match of matchList.reverse()) {
|
2021-08-07 20:52:54 +00:00
|
|
|
if (!summonerDB.matchList.some(m => m === match)) {
|
2020-10-05 09:16:21 +00:00
|
|
|
summonerDB.matchList.unshift(match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else { // First search of the Summoner
|
2021-08-07 20:52:54 +00:00
|
|
|
// const today = Date.now()
|
2020-10-05 09:16:21 +00:00
|
|
|
// Get MatchList
|
2021-08-07 20:52:54 +00:00
|
|
|
const matchList = await this._fetchMatchListUntil(account, (newMatchList: MatchlistDto) => {
|
|
|
|
|
// return (newMatchList.length !== 100 || today - newMatchList[newMatchList.length - 1].timestamp > 10368000000)
|
|
|
|
|
return newMatchList.length === 0 // TODO: useless
|
2020-10-05 09:16:21 +00:00
|
|
|
})
|
|
|
|
|
// Create Summoner's MatchList in Database
|
|
|
|
|
summonerDB.matchList = matchList
|
|
|
|
|
}
|
|
|
|
|
console.timeEnd('matchList')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch list of matches for a specific Summoner
|
2020-10-08 07:51:12 +00:00
|
|
|
* @param puuid
|
|
|
|
|
* @param accountId
|
|
|
|
|
* @param region
|
|
|
|
|
* @param gameIds
|
|
|
|
|
* @param summonerDB
|
2020-10-05 09:16:21 +00:00
|
|
|
*/
|
2021-08-10 22:12:17 +00:00
|
|
|
public async getMatches (puuid: string, accountId: string, region: string, matchIds: MatchlistDto, summonerDB: SummonerModel) {
|
2020-10-08 07:51:12 +00:00
|
|
|
console.time('getMatches')
|
2020-10-05 09:16:21 +00:00
|
|
|
|
2020-10-08 07:51:12 +00:00
|
|
|
let matchesDetails: MatchModel[] = []
|
2021-08-10 22:12:17 +00:00
|
|
|
const matchesToGetFromRiot: MatchlistDto = []
|
2021-08-07 20:52:54 +00:00
|
|
|
for (let i = 0; i < matchIds.length; ++i) {
|
2021-03-15 20:29:36 +00:00
|
|
|
const matchSaved = await Match.findOne({
|
2020-10-08 07:51:12 +00:00
|
|
|
summoner_puuid: puuid,
|
2021-08-07 20:52:54 +00:00
|
|
|
matchId: matchIds[i],
|
2020-10-08 07:51:12 +00:00
|
|
|
})
|
|
|
|
|
if (matchSaved) {
|
|
|
|
|
matchesDetails.push(matchSaved)
|
|
|
|
|
} else {
|
2021-08-07 20:52:54 +00:00
|
|
|
matchesToGetFromRiot.push(matchIds[i])
|
2020-10-08 07:51:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-05 09:16:21 +00:00
|
|
|
|
2020-10-08 07:51:12 +00:00
|
|
|
const requests = matchesToGetFromRiot.map(gameId => Jax.Match.get(gameId, region))
|
|
|
|
|
let matchesFromApi = await Promise.all(requests)
|
2020-10-05 09:16:21 +00:00
|
|
|
|
2020-10-08 07:51:12 +00:00
|
|
|
/* If we have to store some matches in the db */
|
|
|
|
|
if (matchesFromApi.length !== 0) {
|
|
|
|
|
// Try to see why matches are sometimes undefined
|
|
|
|
|
matchesFromApi.filter(m => {
|
|
|
|
|
if (m === undefined) {
|
|
|
|
|
Logger.info(`Match undefined, summoner: ${summonerDB.puuid}`, m)
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-10-05 09:16:21 +00:00
|
|
|
|
2020-10-08 07:51:12 +00:00
|
|
|
// Transform raw matches data
|
2021-08-10 22:12:17 +00:00
|
|
|
const transformedMatches = await BasicMatchTransformer.transform(matchesFromApi, { puuid, accountId })
|
2020-10-05 09:16:21 +00:00
|
|
|
|
2020-10-08 07:51:12 +00:00
|
|
|
/* Save all matches from Riot Api in db */
|
2021-08-10 22:12:17 +00:00
|
|
|
for (const match of transformedMatches) {
|
|
|
|
|
await Match.create(match)
|
|
|
|
|
match.newMatch = true
|
|
|
|
|
}
|
|
|
|
|
matchesDetails = [...matchesDetails, ...transformedMatches]
|
2020-10-08 07:51:12 +00:00
|
|
|
}
|
2020-10-05 09:16:21 +00:00
|
|
|
|
2020-10-08 07:51:12 +00:00
|
|
|
/* Sort matches */
|
|
|
|
|
matchesDetails.sort((a, b) => (a.date < b.date) ? 1 : -1)
|
|
|
|
|
console.timeEnd('getMatches')
|
2020-10-05 09:16:21 +00:00
|
|
|
|
2020-10-08 07:51:12 +00:00
|
|
|
return matchesDetails
|
|
|
|
|
}
|
2020-10-05 09:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default new MatchService()
|