diff --git a/server-v2/app/Models/MatchPlayer.ts b/server-v2/app/Models/MatchPlayer.ts index 74b9c8d..a9a2918 100644 --- a/server-v2/app/Models/MatchPlayer.ts +++ b/server-v2/app/Models/MatchPlayer.ts @@ -16,7 +16,7 @@ export default class MatchPlayer extends BaseModel { public participantId: number @column() - public summonerId: number + public summonerId: string @column() public summonerPuuid: string diff --git a/server-v2/app/Models/MatchTeam.ts b/server-v2/app/Models/MatchTeam.ts index d7bf296..7389b49 100644 --- a/server-v2/app/Models/MatchTeam.ts +++ b/server-v2/app/Models/MatchTeam.ts @@ -12,10 +12,10 @@ export default class MatchTeam extends BaseModel { public match: BelongsTo @column() - public color: string + public color: number @column() - public result: number + public result: string @column() public barons: number diff --git a/server-v2/app/Parsers/MatchParser.ts b/server-v2/app/Parsers/MatchParser.ts index dd2694f..e27bdce 100644 --- a/server-v2/app/Parsers/MatchParser.ts +++ b/server-v2/app/Parsers/MatchParser.ts @@ -1,17 +1,128 @@ +import Database from '@ioc:Adonis/Lucid/Database' import { MatchDto } from 'App/Services/Jax/src/Endpoints/MatchEndpoint' - +import Match from 'App/Models/Match' +import { getSeasonNumber } from 'App/helpers' class MatchParser { public async parseOneMatch(match: MatchDto) { - // TODO: parse + store in database - // From the MatchDto, we need these Models in the DB: + // Parse + store in database // - 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(), + 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 + let result = 'Remake' + for (const team of match.info.teams) { + if (match.info.gameDuration >= 300) { + result = team.win ? 'Win' : 'Fail' + } + const teamColor = team.teamId === 100 ? 'blueTeam' : 'redTeam' + parsedMatch.related(teamColor).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.map((ban) => ban.championId), + banOrders: team.bans.map((ban) => ban.pickTurn), + }) + } + // - 10x MatchPlayer - // - 2x MatchTeam + const matchPlayers: any[] = [] + for (const player of match.info.participants) { + const kda: number = + player.kills + player.assists !== 0 && player.deaths === 0 + ? Infinity + : +(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 + + const kp: number = + teamKills === 0 ? 0 : +(((player.kills + player.assists) * 100) / teamKills).toFixed(1) + + const primaryStyle = player.perks.styles.find((s) => s.description === 'primaryStyle') + const secondaryStyle = player.perks.styles.find((s) => s.description === 'subStyle') + + const perksSelected: number[] = [] + for (const styles of player.perks.styles) { + for (const perk of styles.selections) { + perksSelected.push(perk.perk) + } + } + + 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, + kills: player.kills, + deaths: player.deaths, + assists: player.assists, + kda: kda, + kp: kp, + champ_level: player.champLevel, + champion_id: player.championId, + champion_role1: 0, // TODO + champion_role2: 0, // TODO + 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, + perks_selected: perksSelected, + }) + } + await Database.table('match_players').multiInsert(matchPlayers) + return parsedMatch } public async parse(matches: MatchDto[]) { - // TODO - // Loop on all matches and call .parse on it + // 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 } } diff --git a/server-v2/database/migrations/1631392754960_matches.ts b/server-v2/database/migrations/1631392754960_matches.ts index b843eb6..3d36ac2 100644 --- a/server-v2/database/migrations/1631392754960_matches.ts +++ b/server-v2/database/migrations/1631392754960_matches.ts @@ -6,10 +6,10 @@ export default class Matches extends BaseSchema { public async up() { this.schema.createTable(this.tableName, (table) => { table.string('id', 15).primary() - table.integer('game_id').notNullable() + table.bigInteger('game_id').notNullable() table.integer('map').notNullable() table.integer('gamemode').notNullable() - table.integer('date').notNullable() + table.bigInteger('date').notNullable() table.string('region', 4).notNullable() table.integer('result').notNullable() diff --git a/server-v2/database/migrations/1631392766690_match_players.ts b/server-v2/database/migrations/1631392766690_match_players.ts index 360ebd2..8cea6a7 100644 --- a/server-v2/database/migrations/1631392766690_match_players.ts +++ b/server-v2/database/migrations/1631392766690_match_players.ts @@ -9,7 +9,7 @@ export default class MatchPlayers extends BaseSchema { table.string('match_id', 15).notNullable() table.integer('participant_id').notNullable() - table.integer('summoner_id').notNullable() + table.string('summoner_id', 63).notNullable() table.string('summoner_puuid', 78).notNullable() table.string('summoner_name', 16).notNullable() diff --git a/server-v2/database/migrations/1631397498477_match_teams.ts b/server-v2/database/migrations/1631397498477_match_teams.ts index f098539..3dba8cc 100644 --- a/server-v2/database/migrations/1631397498477_match_teams.ts +++ b/server-v2/database/migrations/1631397498477_match_teams.ts @@ -8,8 +8,8 @@ export default class MatchTeams extends BaseSchema { table.increments('id') table.string('match_id', 15) - table.string('color', 4).notNullable() - table.integer('result').notNullable() + table.integer('color').notNullable() // 100 ou 200 + table.string('result', 6) // Win - Remake - Fail table.integer('barons').notNullable() table.integer('dragons').notNullable()