LeagueStats/server/app/Services/StatsService.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-10-08 07:51:12 +00:00
import { sortTeamByRole } from 'App/helpers'
import { ChampionRoles, TeamPosition } from 'App/Parsers/ParsedType'
2022-01-07 23:16:49 +00:00
import MatchRepository, { SelectFilters } from 'App/Repositories/MatchRepository'
import BasicMatchSerializer from 'App/Serializers/BasicMatchSerializer'
2020-10-08 07:51:12 +00:00
class StatsService {
public async getSummonerStats(puuid: string, season?: number) {
2022-01-07 23:16:49 +00:00
const filters: SelectFilters = { puuid }
if (season) filters.season = season
const [
globalStats,
gamemodeStats,
roleStats,
championStats,
championClassStats,
mates,
recentActivity,
] = await Promise.all([
MatchRepository.globalStats(filters),
MatchRepository.gamemodeStats(filters),
MatchRepository.roleStats(filters),
MatchRepository.championStats({ ...filters, limit: 5 }),
MatchRepository.championClassStats(filters),
MatchRepository.mates(filters),
MatchRepository.recentActivity(puuid),
])
2020-10-08 07:51:12 +00:00
// Check if all roles are in the array
const roles = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'UTILITY']
2020-10-08 07:51:12 +00:00
for (const role of roles) {
const findedRole = roleStats.find((r) => TeamPosition[r.role] === role)
if (findedRole) {
findedRole.role = TeamPosition[findedRole.role]
} else {
2020-10-08 07:51:12 +00:00
roleStats.push({
count: 0,
losses: 0,
role,
wins: 0,
})
}
}
for (const champ of championStats) {
champ.champion = BasicMatchSerializer.getChampion(champ.id)
}
for (const champ of championClassStats) {
champ.id = ChampionRoles[champ.id]
}
2020-10-08 07:51:12 +00:00
return {
global: globalStats,
2020-10-08 07:51:12 +00:00
league: gamemodeStats,
role: roleStats.sort(sortTeamByRole),
champion: championStats,
2020-10-08 07:51:12 +00:00
class: championClassStats,
mates,
recentActivity,
2020-10-08 07:51:12 +00:00
}
}
}
export default new StatsService()