added league table

This commit is contained in:
2026-02-21 23:11:57 +11:00
parent daeb19cb82
commit d011c7acb8
6 changed files with 242 additions and 22 deletions

1
.gitignore vendored
View File

@@ -10,7 +10,6 @@ internal/view/**/*_templ.go
internal/view/**/*_templ.txt internal/view/**/*_templ.txt
cmd/test/* cmd/test/*
.opencode .opencode
Matches/
# Database backups (compressed) # Database backups (compressed)
backups/*.sql.gz backups/*.sql.gz

View File

@@ -127,6 +127,22 @@ func GetFixture(ctx context.Context, tx bun.Tx, id int) (*Fixture, error) {
Get(ctx) Get(ctx)
} }
// GetAllocatedFixtures returns all fixtures with a game_week assigned for a season+league.
func GetAllocatedFixtures(ctx context.Context, tx bun.Tx, seasonID, leagueID int) ([]*Fixture, error) {
fixtures, err := GetList[Fixture](tx).
Where("season_id = ?", seasonID).
Where("league_id = ?", leagueID).
Where("game_week IS NOT NULL").
Order("game_week ASC", "round ASC", "id ASC").
Relation("HomeTeam").
Relation("AwayTeam").
GetAll(ctx)
if err != nil {
return nil, errors.Wrap(err, "GetList")
}
return fixtures, nil
}
func GetFixturesForTeam(ctx context.Context, tx bun.Tx, seasonID, leagueID, teamID int) ([]*Fixture, error) { func GetFixturesForTeam(ctx context.Context, tx bun.Tx, seasonID, leagueID, teamID int) ([]*Fixture, error) {
fixtures, err := GetList[Fixture](tx). fixtures, err := GetList[Fixture](tx).
Where("season_id = ?", seasonID). Where("season_id = ?", seasonID).

View File

@@ -2,6 +2,8 @@ package db
import ( import (
"context" "context"
"sort"
"strings"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -327,15 +329,27 @@ func GetAggregatedPlayerStatsForTeam(
// TeamRecord holds win/loss/draw record and goal totals for a team. // TeamRecord holds win/loss/draw record and goal totals for a team.
type TeamRecord struct { type TeamRecord struct {
Played int Played int
Wins int Wins int
Losses int OvertimeWins int
Draws int OvertimeLosses int
GoalsFor int Losses int
GoalsAgainst int Draws int
GoalsFor int
GoalsAgainst int
Points int
} }
// ComputeTeamRecord calculates W-L-D and GF/GA from fixtures and results. // Point values for the leaderboard scoring system.
const (
PointsWin = 3
PointsOvertimeWin = 2
PointsOvertimeLoss = 1
PointsLoss = 0
)
// ComputeTeamRecord calculates W-OTW-OTL-L, GF/GA, and points from fixtures and results.
// Points: Win=3, OT Win=2, OT Loss=1, Loss=0.
func ComputeTeamRecord(teamID int, fixtures []*Fixture, resultMap map[int]*FixtureResult) *TeamRecord { func ComputeTeamRecord(teamID int, fixtures []*Fixture, resultMap map[int]*FixtureResult) *TeamRecord {
rec := &TeamRecord{} rec := &TeamRecord{}
for _, f := range fixtures { for _, f := range fixtures {
@@ -354,17 +368,80 @@ func ComputeTeamRecord(teamID int, fixtures []*Fixture, resultMap map[int]*Fixtu
} }
won := (isHome && res.Winner == "home") || (!isHome && res.Winner == "away") won := (isHome && res.Winner == "home") || (!isHome && res.Winner == "away")
lost := (isHome && res.Winner == "away") || (!isHome && res.Winner == "home") lost := (isHome && res.Winner == "away") || (!isHome && res.Winner == "home")
if won { isOT := strings.EqualFold(res.EndReason, "Overtime")
switch {
case won && isOT:
rec.OvertimeWins++
rec.Points += PointsOvertimeWin
case won:
rec.Wins++ rec.Wins++
} else if lost { rec.Points += PointsWin
case lost && isOT:
rec.OvertimeLosses++
rec.Points += PointsOvertimeLoss
case lost:
rec.Losses++ rec.Losses++
} else { rec.Points += PointsLoss
default:
rec.Draws++ rec.Draws++
} }
} }
return rec return rec
} }
// LeaderboardEntry represents a single team's standing in the league table.
type LeaderboardEntry struct {
Position int
Team *Team
Record *TeamRecord
}
// ComputeLeaderboard builds a sorted leaderboard from teams, fixtures, and results.
// Teams are sorted by: Points DESC, Goal Differential DESC, Goals For DESC, Name ASC.
func ComputeLeaderboard(teams []*Team, fixtures []*Fixture, resultMap map[int]*FixtureResult) []*LeaderboardEntry {
entries := make([]*LeaderboardEntry, 0, len(teams))
// Build a map of team ID -> fixtures involving that team
teamFixtures := make(map[int][]*Fixture)
for _, f := range fixtures {
teamFixtures[f.HomeTeamID] = append(teamFixtures[f.HomeTeamID], f)
teamFixtures[f.AwayTeamID] = append(teamFixtures[f.AwayTeamID], f)
}
for _, team := range teams {
record := ComputeTeamRecord(team.ID, teamFixtures[team.ID], resultMap)
entries = append(entries, &LeaderboardEntry{
Team: team,
Record: record,
})
}
// Sort: Points DESC, then goal diff DESC, then GF DESC, then name ASC
sort.Slice(entries, func(i, j int) bool {
ri, rj := entries[i].Record, entries[j].Record
if ri.Points != rj.Points {
return ri.Points > rj.Points
}
diffI := ri.GoalsFor - ri.GoalsAgainst
diffJ := rj.GoalsFor - rj.GoalsAgainst
if diffI != diffJ {
return diffI > diffJ
}
if ri.GoalsFor != rj.GoalsFor {
return ri.GoalsFor > rj.GoalsFor
}
return entries[i].Team.Name < entries[j].Team.Name
})
// Assign positions
for i := range entries {
entries[i].Position = i + 1
}
return entries
}
// GetFixtureTeamRosters returns all team players with participation status for a fixture. // GetFixtureTeamRosters returns all team players with participation status for a fixture.
// Returns: map["home"|"away"] -> []*PlayerWithPlayStatus // Returns: map["home"|"away"] -> []*PlayerWithPlayStatus
func GetFixtureTeamRosters( func GetFixtureTeamRosters(

View File

@@ -514,6 +514,9 @@
.w-6 { .w-6 {
width: calc(var(--spacing) * 6); width: calc(var(--spacing) * 6);
} }
.w-10 {
width: calc(var(--spacing) * 10);
}
.w-12 { .w-12 {
width: calc(var(--spacing) * 12); width: calc(var(--spacing) * 12);
} }
@@ -1294,6 +1297,9 @@
.text-subtext1 { .text-subtext1 {
color: var(--subtext1); color: var(--subtext1);
} }
.text-teal {
color: var(--teal);
}
.text-text { .text-text {
color: var(--text); color: var(--text);
} }

View File

@@ -21,26 +21,48 @@ func SeasonLeagueTablePage(
seasonStr := r.PathValue("season_short_name") seasonStr := r.PathValue("season_short_name")
leagueStr := r.PathValue("league_short_name") leagueStr := r.PathValue("league_short_name")
var sl *db.SeasonLeague var season *db.Season
var league *db.League
var leaderboard []*db.LeaderboardEntry
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) { if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
var err error var err error
sl, err = db.GetSeasonLeague(ctx, tx, seasonStr, leagueStr) var teams []*db.Team
season, league, teams, err = db.GetSeasonLeagueWithTeams(ctx, tx, seasonStr, leagueStr)
if err != nil { if err != nil {
if db.IsBadRequest(err) { if db.IsBadRequest(err) {
throw.NotFound(s, w, r, r.URL.Path) throw.NotFound(s, w, r, r.URL.Path)
return false, nil return false, nil
} }
return false, errors.Wrap(err, "db.GetSeasonLeague") return false, errors.Wrap(err, "db.GetSeasonLeagueWithTeams")
} }
fixtures, err := db.GetAllocatedFixtures(ctx, tx, season.ID, league.ID)
if err != nil {
return false, errors.Wrap(err, "db.GetAllocatedFixtures")
}
fixtureIDs := make([]int, len(fixtures))
for i, f := range fixtures {
fixtureIDs[i] = f.ID
}
resultMap, err := db.GetFinalizedResultsForFixtures(ctx, tx, fixtureIDs)
if err != nil {
return false, errors.Wrap(err, "db.GetFinalizedResultsForFixtures")
}
leaderboard = db.ComputeLeaderboard(teams, fixtures, resultMap)
return true, nil return true, nil
}); !ok { }); !ok {
return return
} }
if r.Method == "GET" { if r.Method == "GET" {
renderSafely(seasonsview.SeasonLeagueTablePage(sl.Season, sl.League), s, r, w) renderSafely(seasonsview.SeasonLeagueTablePage(season, league, leaderboard), s, r, w)
} else { } else {
renderSafely(seasonsview.SeasonLeagueTable(), s, r, w) renderSafely(seasonsview.SeasonLeagueTable(leaderboard), s, r, w)
} }
}) })
} }

View File

@@ -1,15 +1,115 @@
package seasonsview package seasonsview
import "git.haelnorr.com/h/oslstats/internal/db" import "git.haelnorr.com/h/oslstats/internal/db"
import "fmt"
templ SeasonLeagueTablePage(season *db.Season, league *db.League) { templ SeasonLeagueTablePage(season *db.Season, league *db.League, leaderboard []*db.LeaderboardEntry) {
@SeasonLeagueLayout("table", season, league) { @SeasonLeagueLayout("table", season, league) {
@SeasonLeagueTable() @SeasonLeagueTable(leaderboard)
} }
} }
templ SeasonLeagueTable() { templ SeasonLeagueTable(leaderboard []*db.LeaderboardEntry) {
<div class="bg-surface0 border border-surface1 rounded-lg p-8 text-center"> if len(leaderboard) == 0 {
<p class="text-subtext0 text-lg">Coming Soon...</p> <div class="bg-surface0 border border-surface1 rounded-lg p-8 text-center">
</div> <p class="text-subtext0 text-lg">No teams in this league yet.</p>
</div>
} else {
<div class="bg-surface0 border border-surface1 rounded-lg overflow-hidden">
<!-- Scoring key -->
<div class="bg-mantle border-b border-surface1 px-4 py-2 flex items-center gap-4 text-xs text-subtext0">
<span class="font-semibold text-subtext1">Points:</span>
<span>W = { fmt.Sprint(db.PointsWin) }</span>
<span>OTW = { fmt.Sprint(db.PointsOvertimeWin) }</span>
<span>OTL = { fmt.Sprint(db.PointsOvertimeLoss) }</span>
<span>L = { fmt.Sprint(db.PointsLoss) }</span>
</div>
<div class="overflow-x-auto">
<table class="w-full">
<thead class="bg-mantle border-b border-surface1">
<tr>
<th class="px-3 py-3 text-center text-xs font-semibold text-subtext0 w-10">#</th>
<th class="px-4 py-3 text-left text-xs font-semibold text-text">Team</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-text" title="Games Played">GP</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-text" title="Wins">W</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-text" title="Overtime Wins">OTW</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-text" title="Overtime Losses">OTL</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-text" title="Losses">L</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-text" title="Goals For">GF</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-text" title="Goals Against">GA</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-text" title="Goal Differential">GD</th>
<th class="px-3 py-3 text-center text-xs font-semibold text-blue" title="Points">PTS</th>
</tr>
</thead>
<tbody class="divide-y divide-surface1">
for _, entry := range leaderboard {
@leaderboardRow(entry)
}
</tbody>
</table>
</div>
</div>
}
}
templ leaderboardRow(entry *db.LeaderboardEntry) {
{{
r := entry.Record
goalDiff := r.GoalsFor - r.GoalsAgainst
var gdStr string
if goalDiff > 0 {
gdStr = fmt.Sprintf("+%d", goalDiff)
} else {
gdStr = fmt.Sprint(goalDiff)
}
}}
<tr class="hover:bg-surface1 transition-colors">
<td class="px-3 py-3 text-center text-sm font-medium text-subtext0">
{ fmt.Sprint(entry.Position) }
</td>
<td class="px-4 py-3">
<div class="flex items-center gap-2">
if entry.Team.Color != "" {
<span
class="w-3 h-3 rounded-full shrink-0"
style={ "background-color: " + templ.SafeCSS(entry.Team.Color) }
></span>
}
<span class="text-sm font-medium text-text">{ entry.Team.Name }</span>
</div>
</td>
<td class="px-3 py-3 text-center text-sm text-subtext0">
{ fmt.Sprint(r.Played) }
</td>
<td class="px-3 py-3 text-center text-sm text-green">
{ fmt.Sprint(r.Wins) }
</td>
<td class="px-3 py-3 text-center text-sm text-teal">
{ fmt.Sprint(r.OvertimeWins) }
</td>
<td class="px-3 py-3 text-center text-sm text-peach">
{ fmt.Sprint(r.OvertimeLosses) }
</td>
<td class="px-3 py-3 text-center text-sm text-red">
{ fmt.Sprint(r.Losses) }
</td>
<td class="px-3 py-3 text-center text-sm text-text">
{ fmt.Sprint(r.GoalsFor) }
</td>
<td class="px-3 py-3 text-center text-sm text-text">
{ fmt.Sprint(r.GoalsAgainst) }
</td>
<td class="px-3 py-3 text-center text-sm">
if goalDiff > 0 {
<span class="text-green">{ gdStr }</span>
} else if goalDiff < 0 {
<span class="text-red">{ gdStr }</span>
} else {
<span class="text-subtext0">{ gdStr }</span>
}
</td>
<td class="px-3 py-3 text-center text-sm font-bold text-blue">
{ fmt.Sprint(r.Points) }
</td>
</tr>
} }