fix: remove crash on overview endpoint

This commit is contained in:
Kalane 2021-09-13 23:04:52 +02:00
parent adf08ac779
commit 8bb7779edb
5 changed files with 34 additions and 20 deletions

View file

@ -76,6 +76,6 @@ export default class SummonersController {
// console.timeEnd('STATS') // console.timeEnd('STATS')
console.timeEnd('OVERVIEW_REQUEST') console.timeEnd('OVERVIEW_REQUEST')
return response.json('OVERVIEW REQUEST') return response.json(finalJSON)
} }
} }

View file

@ -19,10 +19,10 @@ class MatchParser {
}) })
// - 2x MatchTeam : Red and Blue // - 2x MatchTeam : Red and Blue
let result = 'Remake'
for (const team of match.info.teams) { for (const team of match.info.teams) {
if (match.info.gameDuration >= 300) { let result = team.win ? 'Win' : 'Fail'
result = team.win ? 'Win' : 'Fail' if (match.info.gameDuration < 300) {
result = 'Remake'
} }
const teamColor = team.teamId === 100 ? 'blueTeam' : 'redTeam' const teamColor = team.teamId === 100 ? 'blueTeam' : 'redTeam'
parsedMatch.related(teamColor).create({ parsedMatch.related(teamColor).create({
@ -41,9 +41,9 @@ class MatchParser {
// - 10x MatchPlayer // - 10x MatchPlayer
const matchPlayers: any[] = [] const matchPlayers: any[] = []
for (const player of match.info.participants) { for (const player of match.info.participants) {
const kda: number = const kda =
player.kills + player.assists !== 0 && player.deaths === 0 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) : +(player.deaths === 0 ? 0 : (player.kills + player.assists) / player.deaths).toFixed(2)
const teamKills = const teamKills =
@ -51,7 +51,7 @@ class MatchParser {
? match.info.teams[0].objectives.champion.kills ? match.info.teams[0].objectives.champion.kills
: match.info.teams[1].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) teamKills === 0 ? 0 : +(((player.kills + player.assists) * 100) / teamKills).toFixed(1)
const primaryStyle = player.perks.styles.find((s) => s.description === 'primaryStyle') const primaryStyle = player.perks.styles.find((s) => s.description === 'primaryStyle')

View file

@ -104,7 +104,19 @@ class BasicMatchSerializer extends MatchSerializer {
} }
} }
public serializeOneMatch(match: Match, puuid: string): SerializedMatch { public async serializeOneMatch(match: Match, puuid: string): Promise<SerializedMatch> {
// 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 identity = match.players.find((p) => p.summonerPuuid === puuid)!
const allyTeamColor = identity.team === 100 ? 'blueTeam' : 'redTeam' const allyTeamColor = identity.team === 100 ? 'blueTeam' : 'redTeam'
@ -114,8 +126,7 @@ class BasicMatchSerializer extends MatchSerializer {
const enemyPlayers: MatchPlayer[] = [] const enemyPlayers: MatchPlayer[] = []
for (const p of match.players) { for (const p of match.players) {
// TODO: remove Number() when Lazar push the updated migration p.team === allyTeam.color ? allyPlayers.push(p) : enemyPlayers.push(p)
p.team === Number(allyTeam.color) ? allyPlayers.push(p) : enemyPlayers.push(p)
} }
return { return {
@ -132,7 +143,7 @@ class BasicMatchSerializer extends MatchSerializer {
name: identity.summonerName, name: identity.summonerName,
perks: this.getPerks(identity), perks: this.getPerks(identity),
region: match.region, region: match.region,
result: allyTeam.result.toString(), // TODO: remove toString() when Lazar push the updated migration result: allyTeam.result,
role: identity.teamPosition, role: identity.teamPosition,
season: getSeasonNumber(match.date), season: getSeasonNumber(match.date),
secondSum: identity.summoner2Id, secondSum: identity.summoner2Id,
@ -145,7 +156,7 @@ class BasicMatchSerializer extends MatchSerializer {
public async serialize(matches: Match[], puuid: string): Promise<SerializedMatch[]> { public async serialize(matches: Match[], puuid: string): Promise<SerializedMatch[]> {
await super.getContext() await super.getContext()
return matches.map((match) => this.serializeOneMatch(match, puuid)) return await Promise.all(matches.map((match) => this.serializeOneMatch(match, puuid)))
} }
} }

View file

@ -22,6 +22,10 @@ export default abstract class MatchSerializer {
* Get global Context with CDragon Data * Get global Context with CDragon Data
*/ */
public async getContext() { public async getContext() {
if (this.champions) {
return
}
const items = await Jax.CDragon.items() const items = await Jax.CDragon.items()
const champions = await Jax.CDragon.champions() const champions = await Jax.CDragon.champions()
const perks = await Jax.CDragon.perks() const perks = await Jax.CDragon.perks()

View file

@ -7,6 +7,7 @@ import SummonerMatchlist from 'App/Models/SummonerMatchlist'
import MatchParser from 'App/Parsers/MatchParser' import MatchParser from 'App/Parsers/MatchParser'
import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer' import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer'
import { SerializedMatch } from 'App/Serializers/SerializedTypes' import { SerializedMatch } from 'App/Serializers/SerializedTypes'
import Match from 'App/Models/Match'
class MatchService { class MatchService {
/** /**
@ -88,18 +89,16 @@ class MatchService {
let matches: SerializedMatch[] = [] let matches: SerializedMatch[] = []
const matchesToGetFromRiot: MatchlistDto = [] const matchesToGetFromRiot: MatchlistDto = []
for (let i = 0; i < matchList.length; ++i) { for (let i = 0; i < matchList.length; ++i) {
const matchSaved = await summonerDB const matchSaved = await Match.query()
.related('matches') .where('id', matchList[i].matchId)
.query() .preload('blueTeam')
.where('matchId', matchList[i].matchId) .preload('redTeam')
.preload('match', (preloader) => { .preload('players')
preloader.preload('blueTeam').preload('redTeam').preload('players')
})
.first() .first()
if (matchSaved) { if (matchSaved) {
// TODO: Serialize match from DB + put it in Redis + push it in "matches" // 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 { } else {
matchesToGetFromRiot.push(matchList[i].matchId) matchesToGetFromRiot.push(matchList[i].matchId)
} }