feat: matchService + serializer

This commit is contained in:
Kalane 2021-09-13 22:00:04 +02:00
parent 55a3fec13d
commit da3eea25f9
3 changed files with 25 additions and 19 deletions

View file

@ -1,4 +1,4 @@
import { getSeasonNumber } from 'App/helpers' import { getSeasonNumber, sortTeamByRole } from 'App/helpers'
import Match from 'App/Models/Match' import Match from 'App/Models/Match'
import MatchPlayer from 'App/Models/MatchPlayer' import MatchPlayer from 'App/Models/MatchPlayer'
import MatchSerializer from './MatchSerializer' import MatchSerializer from './MatchSerializer'
@ -41,7 +41,7 @@ class BasicMatchSerializer extends MatchSerializer {
} }
protected getTeamSummary(players: MatchPlayer[]): SerializedMatchTeamPlayer[] { 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<SerializedMatchItem | null> { protected getItems(player: MatchPlayer): Array<SerializedMatchItem | null> {
@ -104,20 +104,16 @@ class BasicMatchSerializer extends MatchSerializer {
} }
} }
public async serializeOneMatch(match: Match, puuid: string): Promise<SerializedMatch> { public serializeOneMatch(match: Match, puuid: string): SerializedMatch {
const players = await match.related('players').query() const identity = match.players.find((p) => p.summonerPuuid === puuid)!
const identity = players.find((p) => p.summonerPuuid === puuid)!
const allyTeamColor = identity.team === 100 ? 'blueTeam' : 'redTeam' const allyTeamColor = identity.team === 100 ? 'blueTeam' : 'redTeam'
// const enemyTeamColor = allyTeamColor === 'blueTeam' ? 'redTeam' : 'blueTeam' const allyTeam = match[allyTeamColor]
const allyTeam = await match.related(allyTeamColor).query().firstOrFail()
// const enemyTeam = await match.related(enemyTeamColor).query().firstOrFail()
const allyPlayers: MatchPlayer[] = [] const allyPlayers: MatchPlayer[] = []
const enemyPlayers: MatchPlayer[] = [] const enemyPlayers: MatchPlayer[] = []
for (const p of players) { for (const p of match.players) {
// TODO: remove Number() when Lazar push the updated migration // TODO: remove Number() when Lazar push the updated migration
p.team === Number(allyTeam.color) ? allyPlayers.push(p) : enemyPlayers.push(p) 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<SerializedMatch[]> { public async serialize(matches: Match[], puuid: string): Promise<SerializedMatch[]> {
await super.getContext() await super.getContext()
return Promise.all(matches.map((match) => this.serializeOneMatch(match, puuid))) return matches.map((match) => this.serializeOneMatch(match, puuid))
} }
} }

View file

@ -6,6 +6,7 @@ import Database from '@ioc:Adonis/Lucid/Database'
import SummonerMatchlist from 'App/Models/SummonerMatchlist' 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'
class MatchService { class MatchService {
/** /**
@ -77,21 +78,28 @@ class MatchService {
/** /**
* Fetch list of matches for a specific Summoner * 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<SerializedMatch[]> {
console.time('getMatches') console.time('getMatches')
let matches: any[] = [] // Todo: add type of serialized matches here 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 summonerDB
.related('matches') .related('matches')
.query() .query()
.where('matchId', matchList[i].matchId) .where('matchId', matchList[i].matchId)
.preload('match') .preload('match', (preloader) => {
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))
} else { } else {
matchesToGetFromRiot.push(matchList[i].matchId) matchesToGetFromRiot.push(matchList[i].matchId)
} }
@ -110,11 +118,13 @@ class MatchService {
parsedMatches, parsedMatches,
summonerDB.puuid 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') console.timeEnd('getMatches')
return matches
} }
} }

View file

@ -1,4 +1,4 @@
import MatchPlayer from './Models/MatchPlayer' import { SerializedMatchTeamPlayer } from './Serializers/SerializedTypes'
/** /**
* All League of Legends regions used in Riot API * All League of Legends regions used in Riot API
@ -104,7 +104,7 @@ export function getCurrentSeason(): number {
* @param a first player * @param a first player
* @param b second 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'] 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)
} }