LeagueStats/server-v2/app/Serializers/BasicMatchSerializer.ts

81 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-09-13 20:00:04 +00:00
import { getSeasonNumber, sortTeamByRole } from 'App/helpers'
2021-09-12 20:55:46 +00:00
import Match from 'App/Models/Match'
import MatchPlayer from 'App/Models/MatchPlayer'
import { TeamPosition } from 'App/Parsers/ParsedType'
2021-09-12 20:55:46 +00:00
import MatchSerializer from './MatchSerializer'
import { SerializedMatch, SerializedMatchStats, SerializedMatchTeamPlayer } from './SerializedTypes'
2021-09-12 20:55:46 +00:00
class BasicMatchSerializer extends MatchSerializer {
protected getPlayerSummary(player: MatchPlayer): SerializedMatchTeamPlayer {
return {
puuid: player.summonerPuuid,
champion: this.getChampion(player.championId),
name: player.summonerName,
role: TeamPosition[player.teamPosition],
}
}
protected getTeamSummary(players: MatchPlayer[]): SerializedMatchTeamPlayer[] {
2021-09-13 20:00:04 +00:00
return players.map((p) => this.getPlayerSummary(p)).sort(sortTeamByRole)
}
protected getStats(player: MatchPlayer): SerializedMatchStats {
return {
kills: player.kills,
deaths: player.deaths,
assists: player.assists,
minions: player.minions,
vision: player.visionScore,
gold: player.gold,
dmgChamp: player.damageDealtChampions,
dmgObj: player.damageDealtObjectives,
dmgTaken: player.damageTaken,
kp: player.kp,
kda: player.kills + player.assists !== 0 && player.deaths === 0 ? '∞' : player.kda,
realKda: player.kda,
criticalStrike: player.criticalStrike,
killingSpree: player.killingSpree,
doubleKills: player.doubleKills,
tripleKills: player.tripleKills,
quadraKills: player.quadraKills,
pentaKills: player.pentaKills,
heal: player.heal,
towers: player.turretKills,
longestLiving: player.timeSpentLiving,
}
}
2021-09-12 20:55:46 +00:00
public serializeOneMatch(match: Match, puuid: string, newMatch = false): SerializedMatch {
2021-09-13 20:00:04 +00:00
const identity = match.players.find((p) => p.summonerPuuid === puuid)!
const allyTeam = match.teams.find((t) => t.color === identity.team)!
const allyPlayers: MatchPlayer[] = []
const enemyPlayers: MatchPlayer[] = []
2021-09-13 20:00:04 +00:00
for (const p of match.players) {
2021-09-13 22:06:12 +00:00
p.team === identity.team ? allyPlayers.push(p) : enemyPlayers.push(p)
}
2021-09-12 20:55:46 +00:00
return {
allyTeam: this.getTeamSummary(allyPlayers),
2021-09-12 20:55:46 +00:00
date: match.date,
enemyTeam: this.getTeamSummary(enemyPlayers),
2021-09-12 20:55:46 +00:00
matchId: match.id,
gamemode: match.gamemode,
map: match.map,
newMatch,
2021-09-12 20:55:46 +00:00
region: match.region,
2021-09-13 21:04:52 +00:00
result: allyTeam.result,
2021-09-12 20:55:46 +00:00
season: getSeasonNumber(match.date),
stats: this.getStats(identity),
2021-09-12 20:55:46 +00:00
time: match.gameDuration,
...this.getPlayerBase(identity),
2021-09-12 20:55:46 +00:00
}
}
public serialize(matches: Match[], puuid: string, newMatches = false): SerializedMatch[] {
return matches.map((match) => this.serializeOneMatch(match, puuid, newMatches))
2021-09-12 20:55:46 +00:00
}
}
export default new BasicMatchSerializer()