updated team stats
This commit is contained in:
@@ -35,6 +35,7 @@ func SeasonLeagueTeamDetailPage(
|
||||
var scheduleMap map[int]*db.FixtureSchedule
|
||||
var resultMap map[int]*db.FixtureResult
|
||||
var playerStats []*db.AggregatedPlayerStats
|
||||
var leaderboard []*db.LeaderboardEntry
|
||||
|
||||
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
@@ -72,12 +73,51 @@ func SeasonLeagueTeamDetailPage(
|
||||
return false, errors.Wrap(err, "db.GetPlayersNotOnTeam")
|
||||
}
|
||||
|
||||
// Get all teams and all fixtures for the league to compute leaderboard
|
||||
var allTeams []*db.Team
|
||||
err = tx.NewSelect().
|
||||
Model(&allTeams).
|
||||
Join("INNER JOIN team_participations AS tp ON tp.team_id = t.id").
|
||||
Where("tp.season_id = ? AND tp.league_id = ?", twr.Season.ID, twr.League.ID).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "tx.NewSelect allTeams")
|
||||
}
|
||||
|
||||
allFixtures, err := db.GetAllocatedFixtures(ctx, tx, twr.Season.ID, twr.League.ID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetAllocatedFixtures")
|
||||
}
|
||||
allFixtureIDs := make([]int, len(allFixtures))
|
||||
for i, f := range allFixtures {
|
||||
allFixtureIDs[i] = f.ID
|
||||
}
|
||||
allResultMap, err := db.GetFinalizedResultsForFixtures(ctx, tx, allFixtureIDs)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetFinalizedResultsForFixtures allFixtures")
|
||||
}
|
||||
|
||||
leaderboard = db.ComputeLeaderboard(allTeams, allFixtures, allResultMap)
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
record := db.ComputeTeamRecord(teamID, fixtures, resultMap)
|
||||
renderSafely(seasonsview.SeasonLeagueTeamDetailPage(twr, fixtures, available, scheduleMap, resultMap, record, playerStats), s, r, w)
|
||||
// Find this team's position and record from the leaderboard
|
||||
var position int
|
||||
var record *db.TeamRecord
|
||||
for _, entry := range leaderboard {
|
||||
if entry.Team.ID == teamID {
|
||||
position = entry.Position
|
||||
record = entry.Record
|
||||
break
|
||||
}
|
||||
}
|
||||
if record == nil {
|
||||
record = &db.TeamRecord{}
|
||||
}
|
||||
|
||||
renderSafely(seasonsview.SeasonLeagueTeamDetailPage(twr, fixtures, available, scheduleMap, resultMap, record, playerStats, position, len(leaderboard)), s, r, w)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user