From 8bb7779edbcda1e4d2e5161c0f83c34e47069d52 Mon Sep 17 00:00:00 2001 From: Kalane Date: Mon, 13 Sep 2021 23:04:52 +0200 Subject: [PATCH] fix: remove crash on overview endpoint --- .../Controllers/Http/SummonersController.ts | 2 +- server-v2/app/Parsers/MatchParser.ts | 12 +++++------ .../app/Serializers/BasicMatchSerializer.ts | 21 ++++++++++++++----- server-v2/app/Serializers/MatchSerializer.ts | 4 ++++ server-v2/app/Services/MatchService.ts | 15 +++++++------ 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/server-v2/app/Controllers/Http/SummonersController.ts b/server-v2/app/Controllers/Http/SummonersController.ts index add3518..a7199bc 100644 --- a/server-v2/app/Controllers/Http/SummonersController.ts +++ b/server-v2/app/Controllers/Http/SummonersController.ts @@ -76,6 +76,6 @@ export default class SummonersController { // console.timeEnd('STATS') console.timeEnd('OVERVIEW_REQUEST') - return response.json('OVERVIEW REQUEST') + return response.json(finalJSON) } } diff --git a/server-v2/app/Parsers/MatchParser.ts b/server-v2/app/Parsers/MatchParser.ts index e27bdce..2490d0f 100644 --- a/server-v2/app/Parsers/MatchParser.ts +++ b/server-v2/app/Parsers/MatchParser.ts @@ -19,10 +19,10 @@ class MatchParser { }) // - 2x MatchTeam : Red and Blue - let result = 'Remake' for (const team of match.info.teams) { - if (match.info.gameDuration >= 300) { - result = team.win ? 'Win' : 'Fail' + let result = team.win ? 'Win' : 'Fail' + if (match.info.gameDuration < 300) { + result = 'Remake' } const teamColor = team.teamId === 100 ? 'blueTeam' : 'redTeam' parsedMatch.related(teamColor).create({ @@ -41,9 +41,9 @@ class MatchParser { // - 10x MatchPlayer const matchPlayers: any[] = [] for (const player of match.info.participants) { - const kda: number = + const kda = player.kills + player.assists !== 0 && player.deaths === 0 - ? Infinity + ? player.kills + player.assists : +(player.deaths === 0 ? 0 : (player.kills + player.assists) / player.deaths).toFixed(2) const teamKills = @@ -51,7 +51,7 @@ class MatchParser { ? match.info.teams[0].objectives.champion.kills : match.info.teams[1].objectives.champion.kills - const kp: number = + const kp = teamKills === 0 ? 0 : +(((player.kills + player.assists) * 100) / teamKills).toFixed(1) const primaryStyle = player.perks.styles.find((s) => s.description === 'primaryStyle') diff --git a/server-v2/app/Serializers/BasicMatchSerializer.ts b/server-v2/app/Serializers/BasicMatchSerializer.ts index c2f297d..be6b3c4 100644 --- a/server-v2/app/Serializers/BasicMatchSerializer.ts +++ b/server-v2/app/Serializers/BasicMatchSerializer.ts @@ -104,7 +104,19 @@ class BasicMatchSerializer extends MatchSerializer { } } - public serializeOneMatch(match: Match, puuid: string): SerializedMatch { + public async serializeOneMatch(match: Match, puuid: string): Promise { + // TODO: use a CDragon Service + await super.getContext() + + // TODO: do we really need to... + if (!match.players) { + console.log('NEED TO LOAD') + + await match.load('players') + await match.load('blueTeam') + await match.load('redTeam') + } + const identity = match.players.find((p) => p.summonerPuuid === puuid)! const allyTeamColor = identity.team === 100 ? 'blueTeam' : 'redTeam' @@ -114,8 +126,7 @@ class BasicMatchSerializer extends MatchSerializer { const enemyPlayers: MatchPlayer[] = [] 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) + p.team === allyTeam.color ? allyPlayers.push(p) : enemyPlayers.push(p) } return { @@ -132,7 +143,7 @@ class BasicMatchSerializer extends MatchSerializer { name: identity.summonerName, perks: this.getPerks(identity), region: match.region, - result: allyTeam.result.toString(), // TODO: remove toString() when Lazar push the updated migration + result: allyTeam.result, role: identity.teamPosition, season: getSeasonNumber(match.date), secondSum: identity.summoner2Id, @@ -145,7 +156,7 @@ class BasicMatchSerializer extends MatchSerializer { public async serialize(matches: Match[], puuid: string): Promise { await super.getContext() - return matches.map((match) => this.serializeOneMatch(match, puuid)) + return await Promise.all(matches.map((match) => this.serializeOneMatch(match, puuid))) } } diff --git a/server-v2/app/Serializers/MatchSerializer.ts b/server-v2/app/Serializers/MatchSerializer.ts index 71e7052..abb3327 100644 --- a/server-v2/app/Serializers/MatchSerializer.ts +++ b/server-v2/app/Serializers/MatchSerializer.ts @@ -22,6 +22,10 @@ export default abstract class MatchSerializer { * Get global Context with CDragon Data */ public async getContext() { + if (this.champions) { + return + } + const items = await Jax.CDragon.items() const champions = await Jax.CDragon.champions() const perks = await Jax.CDragon.perks() diff --git a/server-v2/app/Services/MatchService.ts b/server-v2/app/Services/MatchService.ts index acd2f42..cb8a779 100644 --- a/server-v2/app/Services/MatchService.ts +++ b/server-v2/app/Services/MatchService.ts @@ -7,6 +7,7 @@ import SummonerMatchlist from 'App/Models/SummonerMatchlist' import MatchParser from 'App/Parsers/MatchParser' import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer' import { SerializedMatch } from 'App/Serializers/SerializedTypes' +import Match from 'App/Models/Match' class MatchService { /** @@ -88,18 +89,16 @@ class MatchService { 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', (preloader) => { - preloader.preload('blueTeam').preload('redTeam').preload('players') - }) + const matchSaved = await Match.query() + .where('id', matchList[i].matchId) + .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)) + matches.push(await BasicMatchSerializer.serializeOneMatch(matchSaved, summonerDB.puuid)) } else { matchesToGetFromRiot.push(matchList[i].matchId) }