feat: roleStats query

This commit is contained in:
Kalane 2021-09-15 16:04:02 +02:00
parent 23b407e5c4
commit 92abb9de0f
2 changed files with 40 additions and 16 deletions

View file

@ -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()

View file

@ -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,