feat: add matchlist + current game + ranked to summoner/basic endpoint

This commit is contained in:
Kalane 2021-09-12 19:06:46 +02:00
parent dfc7b64e76
commit 9a3bb6311c
8 changed files with 118 additions and 4 deletions

View file

@ -15,7 +15,15 @@
},
"preloads": [
"./start/routes",
"./start/kernel"
"./start/kernel",
{
"file": "./start/events",
"environment": [
"console",
"repl",
"web"
]
}
],
"providers": [
"./providers/AppProvider",

View file

@ -1,5 +1,7 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Summoner from 'App/Models/Summoner'
import Jax from 'App/Services/Jax'
import MatchService from 'App/Services/MatchService'
import SummonerService from 'App/Services/SummonerService'
import SummonerBasicValidator from 'App/Validators/SummonerBasicValidator'
@ -22,6 +24,17 @@ export default class SummonersController {
// Summoner names
finalJSON.account.names = await SummonerService.getAllSummonerNames(account, summonerDB)
// MATCH LIST
finalJSON.matchList = await MatchService.updateMatchList(account, summonerDB)
// CURRENT GAME
const currentGame = await Jax.Spectator.summonerID(account.id, region)
finalJSON.playing = !!currentGame
finalJSON.current = currentGame
// RANKED STATS
finalJSON.ranked = await SummonerService.getRanked(account, region)
} catch (e) {
console.log(e)
return response.json(null)

View file

@ -20,7 +20,7 @@ export default class Summoner extends BaseModel {
localKey: 'puuid',
foreignKey: 'summonerPuuid',
})
public matchlist: HasMany<typeof SummonerMatchlist>
public matchList: HasMany<typeof SummonerMatchlist>
@hasMany(() => MatchPlayer, {
localKey: 'puuid',

View file

@ -2,6 +2,8 @@ import { BaseModel, BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm'
import Summoner from './Summoner'
export default class SummonerMatchlist extends BaseModel {
public static table = 'summoner_matchlist'
@column({ isPrimary: true })
public id: number

View file

@ -66,7 +66,7 @@ export default class SpectatorEndpoint {
this.limiter = limiter
}
public summonerID(summonerID: string, region: string) {
public summonerID(summonerID: string, region: string): Promise<CurrentGameInfo | undefined> {
return new JaxRequest(
region,
this.config,

View file

@ -0,0 +1,77 @@
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'
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) {
let matchList: MatchlistDto = []
let alreadyIn = false
let index = 0
do {
let newMatchList = await Jax.Matchlist.puuid(account.puuid, account.region as string, index)
// 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
if (
matchList[matchList.length - 1].split('_')[0].toLowerCase() !==
account.region?.toLowerCase()
) {
alreadyIn = true
}
index += 100
} while (!alreadyIn)
return matchList
}
/**
* Update the full MatchList of the summoner
* @param account of the summoner
* @param summonerDB summoner in the database
*/
public async updateMatchList(account: SummonerDTO, summonerDB: Summoner): Promise<MatchlistDto> {
console.time('matchList')
const currentMatchList = await summonerDB
.related('matchList')
.query()
.orderBy('matchId', 'desc')
const currentMatchListIds = currentMatchList.map((m) => m.matchId)
const newMatchList = await this._fetchMatchListUntil(account, (newMatchList: MatchlistDto) => {
return currentMatchListIds.some((id) => id === newMatchList[newMatchList.length - 1])
})
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')
return currentMatchListIds
}
}
export default new MatchService()

View file

@ -46,7 +46,7 @@ const databaseConfig: DatabaseConfig = {
naturalSort: true,
},
healthCheck: true,
debug: false,
debug: true,
},
},
}

14
server-v2/start/events.ts Normal file
View file

@ -0,0 +1,14 @@
/*
|--------------------------------------------------------------------------
| Preloaded File
|--------------------------------------------------------------------------
|
| Any code written inside this file will be executed during the application
| boot.
|
*/
import Event from '@ioc:Adonis/Core/Event'
import Database from '@ioc:Adonis/Lucid/Database'
Event.on('db:query', Database.prettyPrint)