diff --git a/server-v2/app/Serializers/BasicMatchSerializer.ts b/server-v2/app/Serializers/BasicMatchSerializer.ts index 1ceea4e..c2f297d 100644 --- a/server-v2/app/Serializers/BasicMatchSerializer.ts +++ b/server-v2/app/Serializers/BasicMatchSerializer.ts @@ -1,4 +1,4 @@ -import { getSeasonNumber } from 'App/helpers' +import { getSeasonNumber, sortTeamByRole } from 'App/helpers' import Match from 'App/Models/Match' import MatchPlayer from 'App/Models/MatchPlayer' import MatchSerializer from './MatchSerializer' @@ -41,7 +41,7 @@ class BasicMatchSerializer extends MatchSerializer { } protected getTeamSummary(players: MatchPlayer[]): SerializedMatchTeamPlayer[] { - return players.map((p) => this.getPlayerSummary(p)) + return players.map((p) => this.getPlayerSummary(p)).sort(sortTeamByRole) } protected getItems(player: MatchPlayer): Array { @@ -104,20 +104,16 @@ class BasicMatchSerializer extends MatchSerializer { } } - public async serializeOneMatch(match: Match, puuid: string): Promise { - const players = await match.related('players').query() - const identity = players.find((p) => p.summonerPuuid === puuid)! + public serializeOneMatch(match: Match, puuid: string): SerializedMatch { + const identity = match.players.find((p) => p.summonerPuuid === puuid)! const allyTeamColor = identity.team === 100 ? 'blueTeam' : 'redTeam' - // const enemyTeamColor = allyTeamColor === 'blueTeam' ? 'redTeam' : 'blueTeam' - - const allyTeam = await match.related(allyTeamColor).query().firstOrFail() - // const enemyTeam = await match.related(enemyTeamColor).query().firstOrFail() + const allyTeam = match[allyTeamColor] const allyPlayers: MatchPlayer[] = [] const enemyPlayers: MatchPlayer[] = [] - for (const p of players) { + for (const p of match.players) { // TODO: remove Number() when Lazar push the updated migration p.team === Number(allyTeam.color) ? allyPlayers.push(p) : enemyPlayers.push(p) } @@ -149,7 +145,7 @@ class BasicMatchSerializer extends MatchSerializer { public async serialize(matches: Match[], puuid: string): Promise { await super.getContext() - return Promise.all(matches.map((match) => this.serializeOneMatch(match, puuid))) + return matches.map((match) => this.serializeOneMatch(match, puuid)) } } diff --git a/server-v2/app/Services/MatchService.ts b/server-v2/app/Services/MatchService.ts index 8b733bb..acd2f42 100644 --- a/server-v2/app/Services/MatchService.ts +++ b/server-v2/app/Services/MatchService.ts @@ -6,6 +6,7 @@ import Database from '@ioc:Adonis/Lucid/Database' import SummonerMatchlist from 'App/Models/SummonerMatchlist' import MatchParser from 'App/Parsers/MatchParser' import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer' +import { SerializedMatch } from 'App/Serializers/SerializedTypes' class MatchService { /** @@ -77,21 +78,28 @@ class MatchService { /** * Fetch list of matches for a specific Summoner */ - public async getMatches(region: string, matchList: SummonerMatchlist[], summonerDB: Summoner) { + public async getMatches( + region: string, + matchList: SummonerMatchlist[], + summonerDB: Summoner + ): Promise { console.time('getMatches') - let matches: any[] = [] // Todo: add type of serialized matches here + let matches: SerializedMatch[] = [] const matchesToGetFromRiot: MatchlistDto = [] for (let i = 0; i < matchList.length; ++i) { const matchSaved = await summonerDB .related('matches') .query() .where('matchId', matchList[i].matchId) - .preload('match') + .preload('match', (preloader) => { + preloader.preload('blueTeam').preload('redTeam').preload('players') + }) .first() if (matchSaved) { // TODO: Serialize match from DB + put it in Redis + push it in "matches" + matches.push(BasicMatchSerializer.serializeOneMatch(matchSaved.match, summonerDB.puuid)) } else { matchesToGetFromRiot.push(matchList[i].matchId) } @@ -110,11 +118,13 @@ class MatchService { parsedMatches, summonerDB.puuid ) + matches = [...matches, ...serializedMatches] } - // Todo: Sort and return "matches" - + // Todo: check if we need to sort here + matches.sort((a, b) => (a.date < b.date ? 1 : -1)) console.timeEnd('getMatches') + return matches } } diff --git a/server-v2/app/helpers.ts b/server-v2/app/helpers.ts index 1a637f8..e6602b5 100644 --- a/server-v2/app/helpers.ts +++ b/server-v2/app/helpers.ts @@ -1,4 +1,4 @@ -import MatchPlayer from './Models/MatchPlayer' +import { SerializedMatchTeamPlayer } from './Serializers/SerializedTypes' /** * All League of Legends regions used in Riot API @@ -104,7 +104,7 @@ export function getCurrentSeason(): number { * @param a first player * @param b second player */ -export function sortTeamByRole(a: MatchPlayer, b: MatchPlayer) { +export function sortTeamByRole(a: SerializedMatchTeamPlayer, b: SerializedMatchTeamPlayer) { const sortingArr = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT'] - return sortingArr.indexOf(a.teamPosition) - sortingArr.indexOf(b.teamPosition) + return sortingArr.indexOf(a.role) - sortingArr.indexOf(b.role) }