From 3cb2366c00bfb63fd32ca5863478bf0eeeda24a2 Mon Sep 17 00:00:00 2001 From: Kalane Date: Wed, 15 Sep 2021 15:44:53 +0200 Subject: [PATCH] feat: globalStats query --- .../Controllers/Http/SummonersController.ts | 6 +- server-v2/app/Repositories/MatchRepository.ts | 62 ++++++++++++++----- server-v2/app/Services/StatsService.ts | 48 ++++++++++++++ 3 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 server-v2/app/Services/StatsService.ts diff --git a/server-v2/app/Controllers/Http/SummonersController.ts b/server-v2/app/Controllers/Http/SummonersController.ts index af29495..f7920ca 100644 --- a/server-v2/app/Controllers/Http/SummonersController.ts +++ b/server-v2/app/Controllers/Http/SummonersController.ts @@ -1,9 +1,9 @@ import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' import { getCurrentSeason } from 'App/helpers' import Summoner from 'App/Models/Summoner' -import MatchRepository from 'App/Repositories/MatchRepository' import Jax from 'App/Services/Jax' import MatchService from 'App/Services/MatchService' +import StatsService from 'App/Services/StatsService' import SummonerService from 'App/Services/SummonerService' import SummonerBasicValidator from 'App/Validators/SummonerBasicValidator' import SummonerOverviewValidator from 'App/Validators/SummonerOverviewValidator' @@ -74,9 +74,7 @@ export default class SummonersController { // TODO: STATS console.time('STATS') - finalJSON.stats = { - global: await MatchRepository.globalStats(puuid), - } + finalJSON.stats = await StatsService.getSummonerStats(puuid, season) console.timeEnd('STATS') console.timeEnd('OVERVIEW_REQUEST') diff --git a/server-v2/app/Repositories/MatchRepository.ts b/server-v2/app/Repositories/MatchRepository.ts index 6276cbd..c4bf141 100644 --- a/server-v2/app/Repositories/MatchRepository.ts +++ b/server-v2/app/Repositories/MatchRepository.ts @@ -2,23 +2,51 @@ import Database from '@ioc:Adonis/Lucid/Database' class MatchRepository { public async globalStats(puuid: string, season?: number) { - // TODO: add wins/losses - return Database.from('match_players') - .where('summoner_puuid', puuid) - .join('matches', 'match_players.match_id', 'matches.id') - .sum({ - assists: 'assists', - deaths: 'deaths', - kills: 'kills', - minions: 'minions', - time: 'matches.game_duration', - vision: 'vision_score', - }) - .count({ - count: 'assists', - }) - .avg('kp') - .first() + const query = ` + SELECT + SUM(assists) as assists, + SUM(deaths) as deaths, + SUM(kills) as kills, + SUM(minions) as minions, + SUM(matches.game_duration) as time, + SUM(vision_score) as vision, + COUNT(match_players.id) as count, + AVG(kp) as kp, + 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 matches.id = match_teams.match_id AND match_players.team = match_teams.color + WHERE + summoner_puuid = :puuid + LIMIT + 1 + ` + const { rows } = await Database.rawQuery(query, { puuid }) + return rows[0] + // return Database.from('match_players') + // .where('summoner_puuid', puuid) + // .join('matches', 'match_players.match_id', 'matches.id') + // .join('match_teams', (query) => { + // query + // .on('match_teams.match_id', '=', 'match_players.match_id') + // .andOn('match_teams.color', '=', 'match_players.team') + // }) + // .sum({ + // assists: 'assists', + // deaths: 'deaths', + // kills: 'kills', + // minions: 'minions', + // time: 'matches.game_duration', + // vision: 'vision_score', + // result: 'match_teams.result', + // }) + // .count({ + // count: 'assists', + // }) + // .avg('kp') + // .first() } } diff --git a/server-v2/app/Services/StatsService.ts b/server-v2/app/Services/StatsService.ts new file mode 100644 index 0000000..93eb3c5 --- /dev/null +++ b/server-v2/app/Services/StatsService.ts @@ -0,0 +1,48 @@ +import MatchRepository from 'App/Repositories/MatchRepository' +// import { sortTeamByRole } from 'App/helpers' + +class StatsService { + public async getSummonerStats(puuid: string, season?: number) { + console.time('GLOBAL') + const globalStats = await MatchRepository.globalStats(puuid, season) + console.timeEnd('GLOBAL') + // console.time('GAMEMODE') + // const gamemodeStats = await MatchRepository.gamemodeStats(puuid, season) + // 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.timeEnd('ROLE') + // console.time('CHAMPION') + // const championStats = await MatchRepository.championStats(puuid, 5, season) + // console.timeEnd('CHAMPION') + // console.time('CHAMPION-CLASS') + // const championClassStats = await MatchRepository.championClassStats(puuid, season) + // console.timeEnd('CHAMPION-CLASS') + // console.time('MATES') + // const mates = await MatchRepository.mates(puuid, season) + // console.timeEnd('MATES') + + return { + global: globalStats, + // league: gamemodeStats, + // role: roleStats.sort(sortTeamByRole), + // class: championClassStats, + // mates, + // champion: championStats, + } + } +} + +export default new StatsService()