diff --git a/server-v2/app/Repositories/MatchRepository.ts b/server-v2/app/Repositories/MatchRepository.ts index c003a23..446380a 100644 --- a/server-v2/app/Repositories/MatchRepository.ts +++ b/server-v2/app/Repositories/MatchRepository.ts @@ -68,6 +68,26 @@ class MatchRepository { const { rows } = await Database.rawQuery(query, { puuid }) return rows } + + public async roleStats(puuid: string) { + const query = ` + SELECT + match_players.team_position as role, + 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 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 + GROUP BY + role + ` + const { rows } = await Database.rawQuery(query, { puuid }) + return rows + } } export default new MatchRepository() diff --git a/server-v2/app/Services/StatsService.ts b/server-v2/app/Services/StatsService.ts index fcd1c84..c9341b0 100644 --- a/server-v2/app/Services/StatsService.ts +++ b/server-v2/app/Services/StatsService.ts @@ -1,5 +1,6 @@ +import { sortTeamByRole } from 'App/helpers' +import { TeamPosition } from 'App/Parsers/ParsedType' import MatchRepository from 'App/Repositories/MatchRepository' -// import { sortTeamByRole } from 'App/helpers' class StatsService { public async getSummonerStats(puuid: string, season?: number) { @@ -9,20 +10,23 @@ class StatsService { console.time('GAMEMODE') const gamemodeStats = await MatchRepository.gamemodeStats(puuid) console.timeEnd('GAMEMODE') - // console.time('ROLE') - // const roleStats = await MatchRepository.roleStats(puuid, season) - // // Check if all roles are in the array - // const roles = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT'] - // for (const role of roles) { - // if (!roleStats.find((r) => r.role === role)) { - // roleStats.push({ - // count: 0, - // losses: 0, - // role, - // wins: 0, - // }) - // } - // } + console.time('ROLE') + const roleStats = await MatchRepository.roleStats(puuid) + // Check if all roles are in the array + const roles = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'UTILITY'] + for (const role of roles) { + const findedRole = roleStats.find((r) => TeamPosition[r.role] === role) + if (findedRole) { + findedRole.role = TeamPosition[findedRole.role] + } else { + roleStats.push({ + count: 0, + losses: 0, + role, + wins: 0, + }) + } + } // console.timeEnd('ROLE') // console.time('CHAMPION') // const championStats = await MatchRepository.championStats(puuid, 5, season) @@ -37,7 +41,7 @@ class StatsService { return { global: globalStats, league: gamemodeStats, - // role: roleStats.sort(sortTeamByRole), + role: roleStats.sort(sortTeamByRole), // class: championClassStats, // mates, // champion: championStats,