From 35af575af10857fec2c7b3a653024640bc981d99 Mon Sep 17 00:00:00 2001 From: Kalane Date: Wed, 15 Sep 2021 16:22:19 +0200 Subject: [PATCH] feat: championStats query --- server-v2/app/Repositories/MatchRepository.ts | 27 ++++++++++++++++++- .../app/Serializers/BasicMatchSerializer.ts | 2 +- server-v2/app/Services/StatsService.ts | 14 ++++++---- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/server-v2/app/Repositories/MatchRepository.ts b/server-v2/app/Repositories/MatchRepository.ts index 446380a..8e9c91c 100644 --- a/server-v2/app/Repositories/MatchRepository.ts +++ b/server-v2/app/Repositories/MatchRepository.ts @@ -78,7 +78,6 @@ class MatchRepository { COUNT(case when match_teams.result = 'Fail' then 1 else null end) as losses FROM match_players - INNER JOIN matches ON matches.id = match_players.match_id INNER JOIN match_teams ON match_players.match_id = match_teams.match_id AND match_players.team = match_teams.color WHERE summoner_puuid = :puuid @@ -88,6 +87,32 @@ class MatchRepository { const { rows } = await Database.rawQuery(query, { puuid }) return rows } + + public async championStats(puuid: string, limit: number) { + const query = ` + SELECT + match_players.champion_id as id, + SUM(assists) as assists, + SUM(deaths) as deaths, + SUM(kills) as kills, + COUNT(match_players.id) as count, + COUNT(case when match_teams.result = 'Win' then 1 else null end) as wins, + COUNT(case when match_teams.result = 'Fail' then 1 else null end) as losses + FROM + match_players + INNER JOIN match_teams ON match_players.match_id = match_teams.match_id AND match_players.team = match_teams.color + WHERE + summoner_puuid = :puuid + GROUP BY + match_players.champion_id + ORDER BY + count DESC + LIMIT + :limit + ` + const { rows } = await Database.rawQuery(query, { puuid, limit }) + return rows + } } export default new MatchRepository() diff --git a/server-v2/app/Serializers/BasicMatchSerializer.ts b/server-v2/app/Serializers/BasicMatchSerializer.ts index 4e7414a..de87c31 100644 --- a/server-v2/app/Serializers/BasicMatchSerializer.ts +++ b/server-v2/app/Serializers/BasicMatchSerializer.ts @@ -19,7 +19,7 @@ class BasicMatchSerializer extends MatchSerializer { * Get champion specific data * @param id of the champion */ - protected getChampion(id: number): SerializedMatchChampion { + public getChampion(id: number): SerializedMatchChampion { const originalChampionData = CDragonService.champions[id] const icon = CDragonService.BASE_URL + diff --git a/server-v2/app/Services/StatsService.ts b/server-v2/app/Services/StatsService.ts index c9341b0..1b182cc 100644 --- a/server-v2/app/Services/StatsService.ts +++ b/server-v2/app/Services/StatsService.ts @@ -1,6 +1,7 @@ import { sortTeamByRole } from 'App/helpers' import { TeamPosition } from 'App/Parsers/ParsedType' import MatchRepository from 'App/Repositories/MatchRepository' +import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer' class StatsService { public async getSummonerStats(puuid: string, season?: number) { @@ -27,10 +28,13 @@ class StatsService { }) } } - // console.timeEnd('ROLE') - // console.time('CHAMPION') - // const championStats = await MatchRepository.championStats(puuid, 5, season) - // console.timeEnd('CHAMPION') + console.timeEnd('ROLE') + console.time('CHAMPION') + const championStats = await MatchRepository.championStats(puuid, 5) + for (const champ of championStats) { + champ.champion = BasicMatchSerializer.getChampion(champ.id) + } + console.timeEnd('CHAMPION') // console.time('CHAMPION-CLASS') // const championClassStats = await MatchRepository.championClassStats(puuid, season) // console.timeEnd('CHAMPION-CLASS') @@ -44,7 +48,7 @@ class StatsService { role: roleStats.sort(sortTeamByRole), // class: championClassStats, // mates, - // champion: championStats, + champion: championStats, } } }