2021-09-12 17:06:46 +00:00
|
|
|
import Jax from './Jax'
|
|
|
|
|
import { MatchlistDto } from './Jax/src/Endpoints/MatchlistEndpoint'
|
|
|
|
|
import { SummonerDTO } from './Jax/src/Endpoints/SummonerEndpoint'
|
|
|
|
|
import Summoner from 'App/Models/Summoner'
|
|
|
|
|
import Database from '@ioc:Adonis/Lucid/Database'
|
2021-09-12 19:47:48 +00:00
|
|
|
import MatchParser from 'App/Parsers/MatchParser'
|
2021-09-12 20:55:46 +00:00
|
|
|
import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer'
|
2021-09-13 20:00:04 +00:00
|
|
|
import { SerializedMatch } from 'App/Serializers/SerializedTypes'
|
2021-09-13 21:04:52 +00:00
|
|
|
import Match from 'App/Models/Match'
|
2021-09-17 22:01:06 +00:00
|
|
|
import { notEmpty, tutorialQueues } from 'App/helpers'
|
2021-09-12 17:06:46 +00:00
|
|
|
|
|
|
|
|
class MatchService {
|
|
|
|
|
/**
|
|
|
|
|
* Add 100 matches at a time to MatchList until the stopFetching condition is true
|
|
|
|
|
* @param account of the summoner
|
2021-09-19 20:41:56 +00:00
|
|
|
* @param region of the summoner
|
2021-09-12 17:06:46 +00:00
|
|
|
* @param stopFetching condition to stop fetching the MatchList
|
|
|
|
|
*/
|
2021-09-19 20:41:56 +00:00
|
|
|
private async _fetchMatchListUntil(account: SummonerDTO, region: string, stopFetching: any) {
|
2021-09-12 17:06:46 +00:00
|
|
|
let matchList: MatchlistDto = []
|
|
|
|
|
let alreadyIn = false
|
|
|
|
|
let index = 0
|
|
|
|
|
do {
|
2021-09-19 20:41:56 +00:00
|
|
|
let newMatchList = await Jax.Matchlist.puuid(account.puuid, region as string, index)
|
2021-09-12 17:06:46 +00:00
|
|
|
// Error while fetching Riot API
|
|
|
|
|
if (!newMatchList) {
|
|
|
|
|
return matchList
|
|
|
|
|
}
|
|
|
|
|
matchList = [...matchList, ...newMatchList]
|
|
|
|
|
alreadyIn = newMatchList.length === 0 || stopFetching(newMatchList)
|
|
|
|
|
// If the match is made in another region : we stop fetching
|
2021-09-19 20:41:56 +00:00
|
|
|
if (matchList[matchList.length - 1].split('_')[0].toLowerCase() !== region.toLowerCase()) {
|
2021-09-12 17:06:46 +00:00
|
|
|
alreadyIn = true
|
|
|
|
|
}
|
|
|
|
|
index += 100
|
|
|
|
|
} while (!alreadyIn)
|
|
|
|
|
return matchList
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Update the full MatchList of the summoner
|
|
|
|
|
*/
|
2021-09-19 20:41:56 +00:00
|
|
|
public async updateMatchList(
|
|
|
|
|
account: SummonerDTO,
|
|
|
|
|
region: string,
|
|
|
|
|
summonerDB: Summoner
|
|
|
|
|
): Promise<MatchlistDto> {
|
2021-09-12 17:06:46 +00:00
|
|
|
console.time('matchList')
|
|
|
|
|
|
2021-09-14 14:39:25 +00:00
|
|
|
const currentMatchList = await summonerDB.related('matchList').query().orderBy('matchId', 'asc')
|
2021-09-12 17:06:46 +00:00
|
|
|
const currentMatchListIds = currentMatchList.map((m) => m.matchId)
|
|
|
|
|
|
2021-09-19 20:41:56 +00:00
|
|
|
const newMatchList = await this._fetchMatchListUntil(
|
|
|
|
|
account,
|
|
|
|
|
region,
|
|
|
|
|
(newMatchList: MatchlistDto) => {
|
|
|
|
|
return currentMatchListIds.some((id) => id === newMatchList[newMatchList.length - 1])
|
|
|
|
|
}
|
|
|
|
|
)
|
2021-09-12 17:06:46 +00:00
|
|
|
|
|
|
|
|
const matchListToSave: MatchlistDto = []
|
|
|
|
|
for (const matchId of newMatchList.reverse()) {
|
|
|
|
|
if (!currentMatchListIds.some((id) => id === matchId)) {
|
|
|
|
|
matchListToSave.push(matchId)
|
|
|
|
|
currentMatchListIds.push(matchId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there is new matchIds to save in database
|
|
|
|
|
if (matchListToSave.length) {
|
|
|
|
|
await Database.table('summoner_matchlist').multiInsert(
|
|
|
|
|
matchListToSave.map((id) => ({
|
|
|
|
|
match_id: id,
|
|
|
|
|
summoner_puuid: summonerDB.puuid,
|
|
|
|
|
}))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.timeEnd('matchList')
|
2021-09-14 14:39:25 +00:00
|
|
|
return currentMatchListIds.reverse()
|
2021-09-12 17:06:46 +00:00
|
|
|
}
|
2021-09-12 19:47:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch list of matches for a specific Summoner
|
|
|
|
|
*/
|
2021-09-13 20:00:04 +00:00
|
|
|
public async getMatches(
|
|
|
|
|
region: string,
|
2021-09-14 11:49:24 +00:00
|
|
|
matchIds: string[],
|
|
|
|
|
puuid: string
|
2021-09-13 20:00:04 +00:00
|
|
|
): Promise<SerializedMatch[]> {
|
2021-09-12 19:47:48 +00:00
|
|
|
console.time('getMatches')
|
|
|
|
|
|
2021-09-14 13:06:42 +00:00
|
|
|
const matches: SerializedMatch[] = []
|
2021-09-12 19:47:48 +00:00
|
|
|
const matchesToGetFromRiot: MatchlistDto = []
|
2021-09-14 11:49:24 +00:00
|
|
|
for (let i = 0; i < matchIds.length; ++i) {
|
2021-09-13 21:04:52 +00:00
|
|
|
const matchSaved = await Match.query()
|
2021-09-14 11:49:24 +00:00
|
|
|
.where('id', matchIds[i])
|
2021-09-14 12:49:03 +00:00
|
|
|
.preload('teams')
|
2021-09-13 21:04:52 +00:00
|
|
|
.preload('players')
|
2021-09-12 19:47:48 +00:00
|
|
|
.first()
|
|
|
|
|
|
|
|
|
|
if (matchSaved) {
|
|
|
|
|
// TODO: Serialize match from DB + put it in Redis + push it in "matches"
|
2021-09-14 11:49:24 +00:00
|
|
|
matches.push(BasicMatchSerializer.serializeOneMatch(matchSaved, puuid))
|
2021-09-12 19:47:48 +00:00
|
|
|
} else {
|
2021-09-14 11:49:24 +00:00
|
|
|
matchesToGetFromRiot.push(matchIds[i])
|
2021-09-12 19:47:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requests = matchesToGetFromRiot.map((gameId) => Jax.Match.get(gameId, region))
|
|
|
|
|
const matchesFromApi = await Promise.all(requests)
|
|
|
|
|
|
|
|
|
|
/* If we have to store some matches in the db */
|
|
|
|
|
if (matchesFromApi.length !== 0) {
|
2021-09-17 22:04:59 +00:00
|
|
|
// Remove bugged matches from the Riot API + tutorial games
|
|
|
|
|
const filteredMatches = matchesFromApi
|
|
|
|
|
.filter(notEmpty)
|
|
|
|
|
.filter(
|
|
|
|
|
(m) =>
|
|
|
|
|
!tutorialQueues.includes(m.info.queueId) &&
|
|
|
|
|
m.info.teams.length > 0 &&
|
|
|
|
|
m.info.participants.length > 0
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-12 19:47:48 +00:00
|
|
|
// Transform raw matches data
|
2021-09-17 22:04:59 +00:00
|
|
|
const parsedMatches: any = await MatchParser.parse(filteredMatches)
|
2021-09-12 19:47:48 +00:00
|
|
|
|
|
|
|
|
// TODO: Serialize match from DB + put it in Redis + push it in "matches"
|
2021-09-14 12:49:03 +00:00
|
|
|
const serializedMatches = BasicMatchSerializer.serialize(parsedMatches, puuid, true)
|
2021-09-14 13:06:42 +00:00
|
|
|
matches.push(...serializedMatches)
|
2021-09-12 19:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-13 20:00:04 +00:00
|
|
|
// Todo: check if we need to sort here
|
|
|
|
|
matches.sort((a, b) => (a.date < b.date ? 1 : -1))
|
2021-09-12 19:47:48 +00:00
|
|
|
console.timeEnd('getMatches')
|
2021-09-13 20:00:04 +00:00
|
|
|
return matches
|
2021-09-12 19:47:48 +00:00
|
|
|
}
|
2021-09-12 17:06:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default new MatchService()
|