From 9a3bb6311c442b79c0f35b27a5cdc14d2c3ddce7 Mon Sep 17 00:00:00 2001 From: Kalane Date: Sun, 12 Sep 2021 19:06:46 +0200 Subject: [PATCH] feat: add matchlist + current game + ranked to summoner/basic endpoint --- server-v2/.adonisrc.json | 10 ++- .../Controllers/Http/SummonersController.ts | 13 ++++ server-v2/app/Models/Summoner.ts | 2 +- server-v2/app/Models/SummonerMatchlist.ts | 2 + .../Jax/src/Endpoints/SpectatorEndpoint.ts | 2 +- server-v2/app/Services/MatchService.ts | 77 +++++++++++++++++++ server-v2/config/database.ts | 2 +- server-v2/start/events.ts | 14 ++++ 8 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 server-v2/app/Services/MatchService.ts create mode 100644 server-v2/start/events.ts diff --git a/server-v2/.adonisrc.json b/server-v2/.adonisrc.json index 1e026a2..cb7f525 100644 --- a/server-v2/.adonisrc.json +++ b/server-v2/.adonisrc.json @@ -15,7 +15,15 @@ }, "preloads": [ "./start/routes", - "./start/kernel" + "./start/kernel", + { + "file": "./start/events", + "environment": [ + "console", + "repl", + "web" + ] + } ], "providers": [ "./providers/AppProvider", diff --git a/server-v2/app/Controllers/Http/SummonersController.ts b/server-v2/app/Controllers/Http/SummonersController.ts index 954735a..ce71017 100644 --- a/server-v2/app/Controllers/Http/SummonersController.ts +++ b/server-v2/app/Controllers/Http/SummonersController.ts @@ -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) diff --git a/server-v2/app/Models/Summoner.ts b/server-v2/app/Models/Summoner.ts index 0ddb6a6..7a203c1 100644 --- a/server-v2/app/Models/Summoner.ts +++ b/server-v2/app/Models/Summoner.ts @@ -20,7 +20,7 @@ export default class Summoner extends BaseModel { localKey: 'puuid', foreignKey: 'summonerPuuid', }) - public matchlist: HasMany + public matchList: HasMany @hasMany(() => MatchPlayer, { localKey: 'puuid', diff --git a/server-v2/app/Models/SummonerMatchlist.ts b/server-v2/app/Models/SummonerMatchlist.ts index ed6ab10..850039d 100644 --- a/server-v2/app/Models/SummonerMatchlist.ts +++ b/server-v2/app/Models/SummonerMatchlist.ts @@ -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 diff --git a/server-v2/app/Services/Jax/src/Endpoints/SpectatorEndpoint.ts b/server-v2/app/Services/Jax/src/Endpoints/SpectatorEndpoint.ts index 7d6a2da..066c67f 100644 --- a/server-v2/app/Services/Jax/src/Endpoints/SpectatorEndpoint.ts +++ b/server-v2/app/Services/Jax/src/Endpoints/SpectatorEndpoint.ts @@ -66,7 +66,7 @@ export default class SpectatorEndpoint { this.limiter = limiter } - public summonerID(summonerID: string, region: string) { + public summonerID(summonerID: string, region: string): Promise { return new JaxRequest( region, this.config, diff --git a/server-v2/app/Services/MatchService.ts b/server-v2/app/Services/MatchService.ts new file mode 100644 index 0000000..f2c4a97 --- /dev/null +++ b/server-v2/app/Services/MatchService.ts @@ -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 { + 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() diff --git a/server-v2/config/database.ts b/server-v2/config/database.ts index fc59b95..59bf7cf 100644 --- a/server-v2/config/database.ts +++ b/server-v2/config/database.ts @@ -46,7 +46,7 @@ const databaseConfig: DatabaseConfig = { naturalSort: true, }, healthCheck: true, - debug: false, + debug: true, }, }, } diff --git a/server-v2/start/events.ts b/server-v2/start/events.ts new file mode 100644 index 0000000..2b07b86 --- /dev/null +++ b/server-v2/start/events.ts @@ -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)