LeagueStats/server-v2/app/Parsers/MatchParser.ts

153 lines
5.7 KiB
TypeScript
Raw Normal View History

2021-09-13 20:06:12 +00:00
import Database from '@ioc:Adonis/Lucid/Database'
2021-09-12 18:18:19 +00:00
import { MatchDto } from 'App/Services/Jax/src/Endpoints/MatchEndpoint'
import Match from 'App/Models/Match'
import { getSeasonNumber, queuesWithRole } from 'App/helpers'
2021-09-13 22:06:12 +00:00
import CDragonService from 'App/Services/CDragonService'
import { ChampionRoles, TeamPosition } from './ParsedType'
2021-09-12 18:18:19 +00:00
class MatchParser {
public async parseOneMatch(match: MatchDto) {
2021-09-13 20:06:12 +00:00
// Parse + store in database
2021-09-12 18:18:19 +00:00
// - 1x Match
const parsedMatch = await Match.create({
id: match.metadata.matchId,
gameId: match.info.gameId,
map: match.info.mapId,
gamemode: match.info.queueId,
date: match.info.gameCreation,
region: match.info.platformId.toLowerCase(),
2021-09-13 20:06:12 +00:00
result: match.info.teams[0].win ? match.info.teams[0].teamId : match.info.teams[1].teamId,
season: getSeasonNumber(match.info.gameCreation),
gameDuration: match.info.gameDuration,
})
// - 2x MatchTeam : Red and Blue
for (const team of match.info.teams) {
2021-09-13 21:04:52 +00:00
let result = team.win ? 'Win' : 'Fail'
if (match.info.gameDuration < 300) {
result = 'Remake'
}
await parsedMatch.related('teams').create({
matchId: match.metadata.matchId,
color: team.teamId,
result: result,
barons: team.objectives.baron.kills,
dragons: team.objectives.dragon.kills,
inhibitors: team.objectives.inhibitor.kills,
riftHeralds: team.objectives.riftHerald.kills,
bans: team.bans.length ? team.bans.map((ban) => ban.championId) : undefined,
banOrders: team.bans.length ? team.bans.map((ban) => ban.pickTurn) : undefined,
})
}
2021-09-13 20:06:12 +00:00
// - 10x MatchPlayer
const matchPlayers: any[] = []
for (const player of match.info.participants) {
2021-09-13 21:04:52 +00:00
const kda =
2021-09-13 20:06:12 +00:00
player.kills + player.assists !== 0 && player.deaths === 0
2021-09-13 21:04:52 +00:00
? player.kills + player.assists
2021-09-13 20:06:12 +00:00
: +(player.deaths === 0 ? 0 : (player.kills + player.assists) / player.deaths).toFixed(2)
const teamKills =
match.info.teams[0].teamId === player.teamId
? match.info.teams[0].objectives.champion.kills
: match.info.teams[1].objectives.champion.kills
2021-09-13 21:04:52 +00:00
const kp =
2021-09-13 20:06:12 +00:00
teamKills === 0 ? 0 : +(((player.kills + player.assists) * 100) / teamKills).toFixed(1)
2021-09-13 20:06:12 +00:00
const primaryStyle = player.perks.styles.find((s) => s.description === 'primaryStyle')
const secondaryStyle = player.perks.styles.find((s) => s.description === 'subStyle')
2021-09-13 20:06:12 +00:00
const perksSelected: number[] = []
for (const styles of player.perks.styles) {
for (const perk of styles.selections) {
perksSelected.push(perk.perk)
}
}
// Fix championId bug in older matches
if (player.championId > 1000) {
console.log('CHAMPION ID NOT FOUND: ' + player.championId)
console.log('FROM MATCH ' + match.metadata.matchId)
const championId = Object.keys(CDragonService.champions).find(
(key) => CDragonService.champions[key].name === player.championName
)
player.championId = championId ? Number(championId) : 1
console.log('CHAMPION ID FROM NAME : ' + championId)
}
const originalChampionData = CDragonService.champions[player.championId]
2021-09-13 22:06:12 +00:00
const champRoles = originalChampionData.roles
2021-09-13 20:06:12 +00:00
matchPlayers.push({
match_id: match.metadata.matchId,
participant_id: player.participantId,
summoner_id: player.summonerId,
summoner_puuid: player.puuid,
summoner_name: player.summonerName,
team: player.teamId,
team_position:
player.teamPosition.length && queuesWithRole.includes(match.info.queueId)
? TeamPosition[player.teamPosition]
: TeamPosition.NONE,
2021-09-13 20:06:12 +00:00
kills: player.kills,
deaths: player.deaths,
assists: player.assists,
kda: kda,
kp: kp,
champ_level: player.champLevel,
champion_id: player.championId,
2021-09-13 22:06:12 +00:00
champion_role1: ChampionRoles[champRoles[0]],
champion_role2: ChampionRoles[champRoles[1]],
2021-09-13 20:06:12 +00:00
double_kills: player.doubleKills,
triple_kills: player.tripleKills,
quadra_kills: player.quadraKills,
penta_kills: player.pentaKills,
baron_kills: player.baronKills,
dragon_kills: player.dragonKills,
turret_kills: player.turretKills,
vision_score: player.visionScore,
gold: player.goldEarned,
summoner1_id: player.summoner1Id,
summoner2_id: player.summoner2Id,
item0: player.item0,
item1: player.item1,
item2: player.item2,
item3: player.item3,
item4: player.item4,
item5: player.item5,
item6: player.item6,
damage_dealt_objectives: player.damageDealtToObjectives,
damage_dealt_champions: player.totalDamageDealtToChampions,
damage_taken: player.totalDamageTaken,
heal: player.totalHeal,
minions: player.totalMinionsKilled,
critical_strike: player.largestCriticalStrike,
killing_spree: player.killingSprees,
time_spent_living: player.longestTimeSpentLiving,
perks_primary_style: primaryStyle!.style,
perks_secondary_style: secondaryStyle!.style,
2021-09-13 23:19:47 +00:00
perks_selected: perksSelected.concat(Object.values(player.perks.statPerks)),
2021-09-13 20:06:12 +00:00
})
}
await Database.table('match_players').multiInsert(matchPlayers)
2021-09-13 22:06:12 +00:00
// Load Match relations
await parsedMatch.load((loader) => {
loader.load('teams').load('players')
2021-09-13 22:06:12 +00:00
})
return parsedMatch
2021-09-12 18:18:19 +00:00
}
public async parse(matches: MatchDto[]) {
// Loop on all matches and call .parseOneMatch on it
const parsedMatches: Match[] = []
for (const match of matches) {
parsedMatches.push(await this.parseOneMatch(match))
}
return parsedMatches
2021-09-12 18:18:19 +00:00
}
}
export default new MatchParser()