added league stats
This commit is contained in:
@@ -437,6 +437,99 @@ func GetAggregatedPlayerStatsForTeam(
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// LeaguePlayerStats holds all aggregated stats for a player in a season-league.
|
||||
type LeaguePlayerStats struct {
|
||||
PlayerID int `bun:"player_id"`
|
||||
PlayerName string `bun:"player_name"`
|
||||
TeamID int `bun:"team_id"`
|
||||
TeamName string `bun:"team_name"`
|
||||
TeamColor string `bun:"team_color"`
|
||||
GamesPlayed int `bun:"games_played"`
|
||||
PeriodsPlayed int `bun:"total_periods_played"`
|
||||
Goals int `bun:"total_goals"`
|
||||
Assists int `bun:"total_assists"`
|
||||
PrimaryAssists int `bun:"total_primary_assists"`
|
||||
SecondaryAssists int `bun:"total_secondary_assists"`
|
||||
Saves int `bun:"total_saves"`
|
||||
Shots int `bun:"total_shots"`
|
||||
Blocks int `bun:"total_blocks"`
|
||||
Passes int `bun:"total_passes"`
|
||||
Score int `bun:"total_score"`
|
||||
}
|
||||
|
||||
// GetAllLeaguePlayerStats returns aggregated stats for all players in a season-league.
|
||||
// Stats are combined across all teams a player may have played on,
|
||||
// and the player's current roster team is shown.
|
||||
func GetAllLeaguePlayerStats(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
seasonID, leagueID int,
|
||||
) ([]*LeaguePlayerStats, error) {
|
||||
if seasonID == 0 {
|
||||
return nil, errors.New("seasonID not provided")
|
||||
}
|
||||
if leagueID == 0 {
|
||||
return nil, errors.New("leagueID not provided")
|
||||
}
|
||||
|
||||
var stats []*LeaguePlayerStats
|
||||
err := tx.NewRaw(`
|
||||
SELECT
|
||||
agg.player_id,
|
||||
agg.player_name,
|
||||
COALESCE(tr.team_id, 0) AS team_id,
|
||||
COALESCE(t.name, '') AS team_name,
|
||||
COALESCE(t.color, '') AS team_color,
|
||||
agg.games_played,
|
||||
agg.total_periods_played,
|
||||
agg.total_goals,
|
||||
agg.total_assists,
|
||||
agg.total_primary_assists,
|
||||
agg.total_secondary_assists,
|
||||
agg.total_saves,
|
||||
agg.total_shots,
|
||||
agg.total_blocks,
|
||||
agg.total_passes,
|
||||
agg.total_score
|
||||
FROM (
|
||||
SELECT
|
||||
frps.player_id AS player_id,
|
||||
COALESCE(p.name, frps.player_username) AS player_name,
|
||||
COUNT(DISTINCT frps.fixture_result_id) AS games_played,
|
||||
COALESCE(SUM(frps.periods_played), 0) AS total_periods_played,
|
||||
COALESCE(SUM(frps.goals), 0) AS total_goals,
|
||||
COALESCE(SUM(frps.assists), 0) AS total_assists,
|
||||
COALESCE(SUM(frps.primary_assists), 0) AS total_primary_assists,
|
||||
COALESCE(SUM(frps.secondary_assists), 0) AS total_secondary_assists,
|
||||
COALESCE(SUM(frps.saves), 0) AS total_saves,
|
||||
COALESCE(SUM(frps.shots), 0) AS total_shots,
|
||||
COALESCE(SUM(frps.blocks), 0) AS total_blocks,
|
||||
COALESCE(SUM(frps.passes), 0) AS total_passes,
|
||||
COALESCE(SUM(frps.score), 0) AS total_score
|
||||
FROM fixture_result_player_stats frps
|
||||
JOIN fixture_results fr ON fr.id = frps.fixture_result_id
|
||||
JOIN fixtures f ON f.id = fr.fixture_id
|
||||
LEFT JOIN players p ON p.id = frps.player_id
|
||||
WHERE fr.finalized = true
|
||||
AND f.season_id = ?
|
||||
AND f.league_id = ?
|
||||
AND frps.period_num = 3
|
||||
AND frps.player_id IS NOT NULL
|
||||
GROUP BY frps.player_id, COALESCE(p.name, frps.player_username)
|
||||
) agg
|
||||
LEFT JOIN team_rosters tr
|
||||
ON tr.player_id = agg.player_id
|
||||
AND tr.season_id = ?
|
||||
AND tr.league_id = ?
|
||||
LEFT JOIN teams t ON t.id = tr.team_id
|
||||
ORDER BY agg.total_score DESC
|
||||
`, seasonID, leagueID, seasonID, leagueID).Scan(ctx, &stats)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "tx.NewRaw")
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// LeagueTopGoalScorer holds aggregated goal scoring stats for a player in a season-league.
|
||||
type LeagueTopGoalScorer struct {
|
||||
PlayerID int `bun:"player_id"`
|
||||
|
||||
Reference in New Issue
Block a user