Compare commits
10 Commits
1194d46613
...
ba0844048a
| Author | SHA1 | Date | |
|---|---|---|---|
| ba0844048a | |||
| 1cab39a4f7 | |||
| 26ee81964d | |||
| e0fd3b0a45 | |||
| 34cba6a81f | |||
| 14e10d095e | |||
| dd1ed61adb | |||
| 9ad29586f2 | |||
| 78db8d0324 | |||
| 04389970ac |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
.env
|
||||
*.env
|
||||
*.db*
|
||||
.logs/
|
||||
server.log
|
||||
@@ -10,6 +11,7 @@ internal/view/**/*_templ.go
|
||||
internal/view/**/*_templ.txt
|
||||
cmd/test/*
|
||||
.opencode
|
||||
prod-export.sql
|
||||
|
||||
# Database backups (compressed)
|
||||
backups/*.sql.gz
|
||||
|
||||
@@ -14,6 +14,7 @@ type Flags struct {
|
||||
GenEnv string
|
||||
EnvFile string
|
||||
DevMode bool
|
||||
Staging bool
|
||||
|
||||
// Database reset (destructive)
|
||||
ResetDB bool
|
||||
@@ -36,6 +37,7 @@ func SetupFlags() (*Flags, error) {
|
||||
genEnv := flag.String("genenv", "", "Generate a .env file with all environment variables (specify filename)")
|
||||
envfile := flag.String("envfile", ".env", "Specify a .env file to use for the configuration")
|
||||
devMode := flag.Bool("dev", false, "Run the server in dev mode")
|
||||
staging := flag.Bool("staging", false, "Show a staging banner")
|
||||
|
||||
// Database reset (destructive)
|
||||
resetDB := flag.Bool("reset-db", false, "⚠️ DESTRUCTIVE: Drop and recreate all tables (dev only)")
|
||||
@@ -92,6 +94,7 @@ func SetupFlags() (*Flags, error) {
|
||||
GenEnv: *genEnv,
|
||||
EnvFile: *envfile,
|
||||
DevMode: *devMode,
|
||||
Staging: *staging,
|
||||
ResetDB: *resetDB,
|
||||
MigrateUp: *migrateUp,
|
||||
MigrateRollback: *migrateRollback,
|
||||
|
||||
@@ -13,4 +13,5 @@ func DevMode(ctx context.Context) DevInfo {
|
||||
type DevInfo struct {
|
||||
WebsocketBase string
|
||||
HTMXLog bool
|
||||
StagingBanner bool
|
||||
}
|
||||
|
||||
98
internal/db/migrations/20260308140000_add_playoffs.go
Normal file
98
internal/db/migrations/20260308140000_add_playoffs.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Migrations.MustRegister(
|
||||
// UP migration
|
||||
func(ctx context.Context, conn *bun.DB) error {
|
||||
// Create playoff_brackets table
|
||||
_, err := conn.NewCreateTable().
|
||||
Model((*db.PlayoffBracket)(nil)).
|
||||
IfNotExists().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create playoff_series table
|
||||
_, err = conn.NewCreateTable().
|
||||
Model((*db.PlayoffSeries)(nil)).
|
||||
IfNotExists().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create playoff_matches table
|
||||
_, err = conn.NewCreateTable().
|
||||
Model((*db.PlayoffMatch)(nil)).
|
||||
IfNotExists().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add foreign key for winner_next_series_id
|
||||
_, err = conn.NewRaw(`
|
||||
ALTER TABLE playoff_series
|
||||
ADD CONSTRAINT fk_winner_next_series
|
||||
FOREIGN KEY (winner_next_series_id) REFERENCES playoff_series(id)
|
||||
ON DELETE SET NULL
|
||||
`).Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add foreign key for loser_next_series_id
|
||||
_, err = conn.NewRaw(`
|
||||
ALTER TABLE playoff_series
|
||||
ADD CONSTRAINT fk_loser_next_series
|
||||
FOREIGN KEY (loser_next_series_id) REFERENCES playoff_series(id)
|
||||
ON DELETE SET NULL
|
||||
`).Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
// DOWN migration
|
||||
func(ctx context.Context, conn *bun.DB) error {
|
||||
// Drop tables in reverse order (respecting foreign keys)
|
||||
_, err := conn.NewDropTable().
|
||||
Model((*db.PlayoffMatch)(nil)).
|
||||
IfExists().
|
||||
Cascade().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = conn.NewDropTable().
|
||||
Model((*db.PlayoffSeries)(nil)).
|
||||
IfExists().
|
||||
Cascade().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = conn.NewDropTable().
|
||||
Model((*db.PlayoffBracket)(nil)).
|
||||
IfExists().
|
||||
Cascade().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
326
internal/db/playoff.go
Normal file
326
internal/db/playoff.go
Normal file
@@ -0,0 +1,326 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// PlayoffFormat represents the bracket format based on team count
|
||||
type PlayoffFormat string
|
||||
|
||||
const (
|
||||
// PlayoffFormat5to6 is for 5-6 teams: top 5 qualify, double-elimination style
|
||||
PlayoffFormat5to6 PlayoffFormat = "5-6-teams"
|
||||
// PlayoffFormat7to9 is for 7-9 teams: top 6 qualify, seeded bracket
|
||||
PlayoffFormat7to9 PlayoffFormat = "7-9-teams"
|
||||
// PlayoffFormat10to15 is for 10-15 teams: top 8 qualify
|
||||
PlayoffFormat10to15 PlayoffFormat = "10-15-teams"
|
||||
)
|
||||
|
||||
// PlayoffStatus represents the current state of a playoff bracket
|
||||
type PlayoffStatus string
|
||||
|
||||
const (
|
||||
PlayoffStatusUpcoming PlayoffStatus = "upcoming"
|
||||
PlayoffStatusInProgress PlayoffStatus = "in_progress"
|
||||
PlayoffStatusCompleted PlayoffStatus = "completed"
|
||||
)
|
||||
|
||||
// SeriesStatus represents the current state of a playoff series
|
||||
type SeriesStatus string
|
||||
|
||||
const (
|
||||
SeriesStatusPending SeriesStatus = "pending"
|
||||
SeriesStatusInProgress SeriesStatus = "in_progress"
|
||||
SeriesStatusCompleted SeriesStatus = "completed"
|
||||
SeriesStatusBye SeriesStatus = "bye"
|
||||
)
|
||||
|
||||
// PlayoffBracket is the top-level container for a league's playoff bracket
|
||||
type PlayoffBracket struct {
|
||||
bun.BaseModel `bun:"table:playoff_brackets,alias:pb"`
|
||||
|
||||
ID int `bun:"id,pk,autoincrement"`
|
||||
SeasonID int `bun:",notnull,unique:season_league"`
|
||||
LeagueID int `bun:",notnull,unique:season_league"`
|
||||
Format PlayoffFormat `bun:",notnull"`
|
||||
Status PlayoffStatus `bun:",notnull,default:'upcoming'"`
|
||||
CreatedAt int64 `bun:",notnull"`
|
||||
UpdatedAt *int64 `bun:"updated_at"`
|
||||
|
||||
Season *Season `bun:"rel:belongs-to,join:season_id=id"`
|
||||
League *League `bun:"rel:belongs-to,join:league_id=id"`
|
||||
Series []*PlayoffSeries `bun:"rel:has-many,join:id=bracket_id"`
|
||||
}
|
||||
|
||||
// PlayoffSeries represents a single matchup (potentially best-of-N) in the bracket
|
||||
type PlayoffSeries struct {
|
||||
bun.BaseModel `bun:"table:playoff_series,alias:ps"`
|
||||
|
||||
ID int `bun:"id,pk,autoincrement"`
|
||||
BracketID int `bun:",notnull"`
|
||||
SeriesNumber int `bun:",notnull"` // Display order within bracket
|
||||
Round string `bun:",notnull"` // e.g. "qualifying_final", "semi_final", "grand_final"
|
||||
Label string `bun:",notnull"` // Human-readable label e.g. "QF1", "SF2", "Grand Final"
|
||||
Team1ID *int `bun:"team1_id"`
|
||||
Team2ID *int `bun:"team2_id"`
|
||||
Team1Seed *int `bun:"team1_seed"` // Original seeding position (1st, 2nd, etc.)
|
||||
Team2Seed *int `bun:"team2_seed"` // Original seeding position
|
||||
WinnerTeamID *int `bun:"winner_team_id"` // Set when series is decided
|
||||
LoserTeamID *int `bun:"loser_team_id"` // Set when series is decided
|
||||
MatchesToWin int `bun:",notnull,default:1"` // 1 = single match, 2 = Bo3, 3 = Bo5, etc.
|
||||
Team1Wins int `bun:",notnull,default:0"` // Matches won by team1
|
||||
Team2Wins int `bun:",notnull,default:0"` // Matches won by team2
|
||||
Status SeriesStatus `bun:",notnull,default:'pending'"` // pending, in_progress, completed, bye
|
||||
WinnerNextID *int `bun:"winner_next_series_id"` // Series the winner advances to
|
||||
WinnerNextSlot *string `bun:"winner_next_slot"` // "team1" or "team2" in next series
|
||||
LoserNextID *int `bun:"loser_next_series_id"` // Series the loser drops to (double-elim)
|
||||
LoserNextSlot *string `bun:"loser_next_slot"` // "team1" or "team2" in loser's next series
|
||||
CreatedAt int64 `bun:",notnull"`
|
||||
|
||||
Bracket *PlayoffBracket `bun:"rel:belongs-to,join:bracket_id=id"`
|
||||
Team1 *Team `bun:"rel:belongs-to,join:team1_id=id"`
|
||||
Team2 *Team `bun:"rel:belongs-to,join:team2_id=id"`
|
||||
Winner *Team `bun:"rel:belongs-to,join:winner_team_id=id"`
|
||||
Loser *Team `bun:"rel:belongs-to,join:loser_team_id=id"`
|
||||
Matches []*PlayoffMatch `bun:"rel:has-many,join:id=series_id"`
|
||||
}
|
||||
|
||||
// PlayoffMatch represents a single game within a series
|
||||
type PlayoffMatch struct {
|
||||
bun.BaseModel `bun:"table:playoff_matches,alias:pm"`
|
||||
|
||||
ID int `bun:"id,pk,autoincrement"`
|
||||
SeriesID int `bun:",notnull"`
|
||||
MatchNumber int `bun:",notnull"` // 1-indexed: game 1, game 2, etc.
|
||||
HomeTeamID *int `bun:"home_team_id"`
|
||||
AwayTeamID *int `bun:"away_team_id"`
|
||||
FixtureID *int `bun:"fixture_id"` // Links to existing fixture system
|
||||
Status string `bun:",notnull,default:'pending'"`
|
||||
CreatedAt int64 `bun:",notnull"`
|
||||
|
||||
Series *PlayoffSeries `bun:"rel:belongs-to,join:series_id=id"`
|
||||
Home *Team `bun:"rel:belongs-to,join:home_team_id=id"`
|
||||
Away *Team `bun:"rel:belongs-to,join:away_team_id=id"`
|
||||
Fixture *Fixture `bun:"rel:belongs-to,join:fixture_id=id"`
|
||||
}
|
||||
|
||||
// NewPlayoffBracket creates a new playoff bracket for a season+league
|
||||
func NewPlayoffBracket(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
seasonID, leagueID int,
|
||||
format PlayoffFormat,
|
||||
audit *AuditMeta,
|
||||
) (*PlayoffBracket, error) {
|
||||
bracket := &PlayoffBracket{
|
||||
SeasonID: seasonID,
|
||||
LeagueID: leagueID,
|
||||
Format: format,
|
||||
Status: PlayoffStatusUpcoming,
|
||||
CreatedAt: time.Now().Unix(),
|
||||
}
|
||||
err := Insert(tx, bracket).WithAudit(audit, &AuditInfo{
|
||||
Action: "playoffs.create_bracket",
|
||||
ResourceType: "playoff_bracket",
|
||||
ResourceID: nil,
|
||||
Details: map[string]any{
|
||||
"season_id": seasonID,
|
||||
"league_id": leagueID,
|
||||
"format": string(format),
|
||||
},
|
||||
}).Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Insert")
|
||||
}
|
||||
return bracket, nil
|
||||
}
|
||||
|
||||
// GetPlayoffBracket retrieves a playoff bracket for a season+league with all series and teams
|
||||
func GetPlayoffBracket(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
seasonID, leagueID int,
|
||||
) (*PlayoffBracket, error) {
|
||||
bracket := new(PlayoffBracket)
|
||||
err := tx.NewSelect().
|
||||
Model(bracket).
|
||||
Where("pb.season_id = ?", seasonID).
|
||||
Where("pb.league_id = ?", leagueID).
|
||||
Relation("Season").
|
||||
Relation("League").
|
||||
Relation("Series", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||
return q.Order("ps.series_number ASC")
|
||||
}).
|
||||
Relation("Series.Team1").
|
||||
Relation("Series.Team2").
|
||||
Relation("Series.Winner").
|
||||
Relation("Series.Loser").
|
||||
Relation("Series.Matches", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||
return q.Order("pm.match_number ASC")
|
||||
}).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
if err.Error() == "sql: no rows in result set" {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "tx.NewSelect")
|
||||
}
|
||||
return bracket, nil
|
||||
}
|
||||
|
||||
// GetPlayoffBracketByID retrieves a playoff bracket by ID with all series
|
||||
func GetPlayoffBracketByID(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
bracketID int,
|
||||
) (*PlayoffBracket, error) {
|
||||
return GetByID[PlayoffBracket](tx, bracketID).
|
||||
Relation("Season").
|
||||
Relation("League").
|
||||
Relation("Series", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||
return q.Order("ps.series_number ASC")
|
||||
}).
|
||||
Relation("Series.Team1").
|
||||
Relation("Series.Team2").
|
||||
Relation("Series.Winner").
|
||||
Relation("Series.Loser").
|
||||
Relation("Series.Matches", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||
return q.Order("pm.match_number ASC")
|
||||
}).
|
||||
Get(ctx)
|
||||
}
|
||||
|
||||
// NewPlayoffSeries creates a new series within a bracket
|
||||
func NewPlayoffSeries(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
bracket *PlayoffBracket,
|
||||
seriesNumber int,
|
||||
round, label string,
|
||||
team1ID, team2ID *int,
|
||||
team1Seed, team2Seed *int,
|
||||
matchesToWin int,
|
||||
status SeriesStatus,
|
||||
) (*PlayoffSeries, error) {
|
||||
series := &PlayoffSeries{
|
||||
BracketID: bracket.ID,
|
||||
SeriesNumber: seriesNumber,
|
||||
Round: round,
|
||||
Label: label,
|
||||
Team1ID: team1ID,
|
||||
Team2ID: team2ID,
|
||||
Team1Seed: team1Seed,
|
||||
Team2Seed: team2Seed,
|
||||
MatchesToWin: matchesToWin,
|
||||
Status: status,
|
||||
CreatedAt: time.Now().Unix(),
|
||||
}
|
||||
err := Insert(tx, series).Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Insert")
|
||||
}
|
||||
return series, nil
|
||||
}
|
||||
|
||||
// SetSeriesAdvancement sets the advancement links for a series
|
||||
func SetSeriesAdvancement(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
seriesID int,
|
||||
winnerNextID *int,
|
||||
winnerNextSlot *string,
|
||||
loserNextID *int,
|
||||
loserNextSlot *string,
|
||||
) error {
|
||||
_, err := tx.NewUpdate().
|
||||
Model((*PlayoffSeries)(nil)).
|
||||
Set("winner_next_series_id = ?", winnerNextID).
|
||||
Set("winner_next_slot = ?", winnerNextSlot).
|
||||
Set("loser_next_series_id = ?", loserNextID).
|
||||
Set("loser_next_slot = ?", loserNextSlot).
|
||||
Where("id = ?", seriesID).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "tx.NewUpdate")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountUnplayedFixtures counts fixtures without finalized results for a season+league
|
||||
func CountUnplayedFixtures(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
seasonID, leagueID int,
|
||||
) (int, error) {
|
||||
count, err := tx.NewSelect().
|
||||
Model((*Fixture)(nil)).
|
||||
Where("f.season_id = ?", seasonID).
|
||||
Where("f.league_id = ?", leagueID).
|
||||
Where("f.game_week IS NOT NULL").
|
||||
Where("NOT EXISTS (SELECT 1 FROM fixture_results fr WHERE fr.fixture_id = f.id AND fr.finalized = true)").
|
||||
Count(ctx)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "tx.NewSelect.Count")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// GetUnplayedFixtures returns all fixtures without finalized results for a season+league
|
||||
func GetUnplayedFixtures(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
seasonID, leagueID int,
|
||||
) ([]*Fixture, error) {
|
||||
fixtures, err := GetList[Fixture](tx).
|
||||
Where("f.season_id = ?", seasonID).
|
||||
Where("f.league_id = ?", leagueID).
|
||||
Where("f.game_week IS NOT NULL").
|
||||
Where("NOT EXISTS (SELECT 1 FROM fixture_results fr WHERE fr.fixture_id = f.id AND fr.finalized = true)").
|
||||
Order("f.game_week ASC", "f.round ASC", "f.id ASC").
|
||||
Relation("HomeTeam").
|
||||
Relation("AwayTeam").
|
||||
GetAll(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetList")
|
||||
}
|
||||
return fixtures, nil
|
||||
}
|
||||
|
||||
// AutoForfeitUnplayedFixtures creates mutual forfeit results for all unplayed fixtures
|
||||
func AutoForfeitUnplayedFixtures(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
seasonID, leagueID int,
|
||||
userID int,
|
||||
audit *AuditMeta,
|
||||
) (int, error) {
|
||||
unplayed, err := GetUnplayedFixtures(ctx, tx, seasonID, leagueID)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "GetUnplayedFixtures")
|
||||
}
|
||||
|
||||
reason := "Auto-forfeited: regular season ended for finals"
|
||||
for _, fixture := range unplayed {
|
||||
// Check if a result already exists (non-finalized)
|
||||
existing, err := GetFixtureResult(ctx, tx, fixture.ID)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "GetFixtureResult")
|
||||
}
|
||||
if existing != nil {
|
||||
// Skip fixtures that already have any result
|
||||
continue
|
||||
}
|
||||
|
||||
_, err = CreateForfeitResult(ctx, tx, fixture,
|
||||
ForfeitTypeMutual, "", reason, userID, audit)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "CreateForfeitResult")
|
||||
}
|
||||
}
|
||||
|
||||
return len(unplayed), nil
|
||||
}
|
||||
533
internal/db/playoff_generation.go
Normal file
533
internal/db/playoff_generation.go
Normal file
@@ -0,0 +1,533 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// GeneratePlayoffBracket creates a complete bracket structure from the leaderboard.
|
||||
// It creates the bracket, all series with advancement links, but no individual
|
||||
// matches (those are created when results are recorded).
|
||||
// roundFormats maps round names (e.g. "grand_final") to matches_to_win values
|
||||
// (1 = BO1, 2 = BO3, 3 = BO5). Rounds not in the map default to BO1.
|
||||
func GeneratePlayoffBracket(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
seasonID, leagueID int,
|
||||
format PlayoffFormat,
|
||||
leaderboard []*LeaderboardEntry,
|
||||
roundFormats map[string]int,
|
||||
audit *AuditMeta,
|
||||
) (*PlayoffBracket, error) {
|
||||
// Validate format and team count
|
||||
if err := validateFormatTeamCount(format, len(leaderboard)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check no bracket already exists
|
||||
existing, err := GetPlayoffBracket(ctx, tx, seasonID, leagueID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetPlayoffBracket")
|
||||
}
|
||||
if existing != nil {
|
||||
return nil, BadRequest("playoff bracket already exists for this season and league")
|
||||
}
|
||||
|
||||
// Create the bracket
|
||||
bracket, err := NewPlayoffBracket(ctx, tx, seasonID, leagueID, format, audit)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "NewPlayoffBracket")
|
||||
}
|
||||
|
||||
// Generate series based on format
|
||||
switch format {
|
||||
case PlayoffFormat5to6:
|
||||
err = generate5to6Bracket(ctx, tx, bracket, leaderboard, roundFormats)
|
||||
case PlayoffFormat7to9:
|
||||
err = generate7to9Bracket(ctx, tx, bracket, leaderboard, roundFormats)
|
||||
case PlayoffFormat10to15:
|
||||
err = generate10to15Bracket(ctx, tx, bracket, leaderboard, roundFormats)
|
||||
default:
|
||||
return nil, BadRequest(fmt.Sprintf("unknown playoff format: %s", format))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "generateBracket")
|
||||
}
|
||||
|
||||
return bracket, nil
|
||||
}
|
||||
|
||||
func validateFormatTeamCount(format PlayoffFormat, teamCount int) error {
|
||||
switch format {
|
||||
case PlayoffFormat5to6:
|
||||
if teamCount < 5 {
|
||||
return BadRequest(
|
||||
fmt.Sprintf("5-6 team format requires at least 5 teams, got %d", teamCount))
|
||||
}
|
||||
case PlayoffFormat7to9:
|
||||
if teamCount < 7 {
|
||||
return BadRequest(
|
||||
fmt.Sprintf("7-9 team format requires at least 7 teams, got %d", teamCount))
|
||||
}
|
||||
case PlayoffFormat10to15:
|
||||
if teamCount < 10 {
|
||||
return BadRequest(
|
||||
fmt.Sprintf("10-15 team format requires at least 10 teams, got %d", teamCount))
|
||||
}
|
||||
default:
|
||||
return BadRequest(fmt.Sprintf("unknown playoff format: %s", format))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// intPtr is a helper to create a pointer to an int
|
||||
func intPtr(i int) *int {
|
||||
return &i
|
||||
}
|
||||
|
||||
// strPtr is a helper to create a pointer to a string
|
||||
func strPtr(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
// getMatchesToWin looks up the matches_to_win value for a round from the config map.
|
||||
// Returns 1 (BO1) if the round is not in the map or the value is invalid.
|
||||
func getMatchesToWin(roundFormats map[string]int, round string) int {
|
||||
if roundFormats == nil {
|
||||
return 1
|
||||
}
|
||||
if v, ok := roundFormats[round]; ok && v >= 1 && v <= 3 {
|
||||
return v
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// generate5to6Bracket creates:
|
||||
//
|
||||
// Round 1:
|
||||
// S1 (Upper Bracket): 2nd vs 3rd
|
||||
// S2 (Lower Bracket): 4th vs 5th
|
||||
// Round 2:
|
||||
// S3 (Upper Final): 1st vs Winner(S1) — second chance for both
|
||||
// S4 (Lower Final): Loser(S3) vs Winner(S2)
|
||||
// Round 3:
|
||||
// S5 (Grand Final): Winner(S3) vs Winner(S4)
|
||||
func generate5to6Bracket(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
bracket *PlayoffBracket,
|
||||
leaderboard []*LeaderboardEntry,
|
||||
roundFormats map[string]int,
|
||||
) error {
|
||||
seed1 := leaderboard[0]
|
||||
seed2 := leaderboard[1]
|
||||
seed3 := leaderboard[2]
|
||||
seed4 := leaderboard[3]
|
||||
seed5 := leaderboard[4]
|
||||
|
||||
// S1: Upper Bracket - 2nd vs 3rd
|
||||
s1, err := NewPlayoffSeries(ctx, tx, bracket, 1,
|
||||
"upper_bracket", "Upper Bracket",
|
||||
&seed2.Team.ID, &seed3.Team.ID,
|
||||
intPtr(2), intPtr(3),
|
||||
getMatchesToWin(roundFormats, "upper_bracket"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S1")
|
||||
}
|
||||
|
||||
// S2: Lower Bracket - 4th vs 5th (elimination)
|
||||
s2, err := NewPlayoffSeries(ctx, tx, bracket, 2,
|
||||
"lower_bracket", "Lower Bracket",
|
||||
&seed4.Team.ID, &seed5.Team.ID,
|
||||
intPtr(4), intPtr(5),
|
||||
getMatchesToWin(roundFormats, "lower_bracket"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S2")
|
||||
}
|
||||
|
||||
// S3: Upper Final - 1st vs Winner(S1)
|
||||
s3, err := NewPlayoffSeries(ctx, tx, bracket, 3,
|
||||
"upper_final", "Upper Final",
|
||||
&seed1.Team.ID, nil, // team2 filled by S1 winner
|
||||
intPtr(1), nil,
|
||||
getMatchesToWin(roundFormats, "upper_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S3")
|
||||
}
|
||||
|
||||
// S4: Lower Final - Loser(S3) vs Winner(S2)
|
||||
s4, err := NewPlayoffSeries(ctx, tx, bracket, 4,
|
||||
"lower_final", "Lower Final",
|
||||
nil, nil, // team1 = Loser(S3), team2 = Winner(S2)
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "lower_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S4")
|
||||
}
|
||||
|
||||
// S5: Grand Final - Winner(S3) vs Winner(S4)
|
||||
s5, err := NewPlayoffSeries(ctx, tx, bracket, 5,
|
||||
"grand_final", "Grand Final",
|
||||
nil, nil,
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "grand_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S5")
|
||||
}
|
||||
|
||||
// Wire up advancement
|
||||
// S1: Winner -> S3 (team2), no loser advancement (eliminated)
|
||||
err = SetSeriesAdvancement(ctx, tx, s1.ID,
|
||||
&s3.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire S1")
|
||||
}
|
||||
|
||||
// S2: Winner -> S4 (team2), no loser advancement (eliminated)
|
||||
err = SetSeriesAdvancement(ctx, tx, s2.ID,
|
||||
&s4.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire S2")
|
||||
}
|
||||
|
||||
// S3: Winner -> S5 (team1), Loser -> S4 (team1) — second chance
|
||||
err = SetSeriesAdvancement(ctx, tx, s3.ID,
|
||||
&s5.ID, strPtr("team1"), &s4.ID, strPtr("team1"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire S3")
|
||||
}
|
||||
|
||||
// S4: Winner -> S5 (team2), no loser advancement (eliminated)
|
||||
err = SetSeriesAdvancement(ctx, tx, s4.ID,
|
||||
&s5.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire S4")
|
||||
}
|
||||
|
||||
// S5: Grand Final - no advancement
|
||||
_ = s5
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// generate7to9Bracket creates:
|
||||
//
|
||||
// Quarter Finals:
|
||||
// S1 (QF1): 3rd vs 6th
|
||||
// S2 (QF2): 4th vs 5th
|
||||
// Semi Finals:
|
||||
// S3 (SF1): 1st vs Winner(S1)
|
||||
// S4 (SF2): 2nd vs Winner(S2)
|
||||
// Third Place Playoff:
|
||||
// S5: Loser(S3) vs Loser(S4)
|
||||
// Grand Final:
|
||||
// S6: Winner(S3) vs Winner(S4)
|
||||
func generate7to9Bracket(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
bracket *PlayoffBracket,
|
||||
leaderboard []*LeaderboardEntry,
|
||||
roundFormats map[string]int,
|
||||
) error {
|
||||
seed1 := leaderboard[0]
|
||||
seed2 := leaderboard[1]
|
||||
seed3 := leaderboard[2]
|
||||
seed4 := leaderboard[3]
|
||||
seed5 := leaderboard[4]
|
||||
seed6 := leaderboard[5]
|
||||
|
||||
// S1: QF1 - 3rd vs 6th
|
||||
s1, err := NewPlayoffSeries(ctx, tx, bracket, 1,
|
||||
"quarter_final", "QF1",
|
||||
&seed3.Team.ID, &seed6.Team.ID,
|
||||
intPtr(3), intPtr(6),
|
||||
getMatchesToWin(roundFormats, "quarter_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S1")
|
||||
}
|
||||
|
||||
// S2: QF2 - 4th vs 5th
|
||||
s2, err := NewPlayoffSeries(ctx, tx, bracket, 2,
|
||||
"quarter_final", "QF2",
|
||||
&seed4.Team.ID, &seed5.Team.ID,
|
||||
intPtr(4), intPtr(5),
|
||||
getMatchesToWin(roundFormats, "quarter_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S2")
|
||||
}
|
||||
|
||||
// S3: SF1 - 1st vs Winner(QF1)
|
||||
s3, err := NewPlayoffSeries(ctx, tx, bracket, 3,
|
||||
"semi_final", "SF1",
|
||||
&seed1.Team.ID, nil,
|
||||
intPtr(1), nil,
|
||||
getMatchesToWin(roundFormats, "semi_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S3")
|
||||
}
|
||||
|
||||
// S4: SF2 - 2nd vs Winner(QF2)
|
||||
s4, err := NewPlayoffSeries(ctx, tx, bracket, 4,
|
||||
"semi_final", "SF2",
|
||||
&seed2.Team.ID, nil,
|
||||
intPtr(2), nil,
|
||||
getMatchesToWin(roundFormats, "semi_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S4")
|
||||
}
|
||||
|
||||
// S5: Third Place Playoff - Loser(SF1) vs Loser(SF2)
|
||||
s5, err := NewPlayoffSeries(ctx, tx, bracket, 5,
|
||||
"third_place", "Third Place Playoff",
|
||||
nil, nil,
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "third_place"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S5")
|
||||
}
|
||||
|
||||
// S6: Grand Final - Winner(SF1) vs Winner(SF2)
|
||||
s6, err := NewPlayoffSeries(ctx, tx, bracket, 6,
|
||||
"grand_final", "Grand Final",
|
||||
nil, nil,
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "grand_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create S6")
|
||||
}
|
||||
|
||||
// Wire up advancement
|
||||
// S1 QF1: Winner -> S3 SF1 (team2)
|
||||
err = SetSeriesAdvancement(ctx, tx, s1.ID,
|
||||
&s3.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire S1")
|
||||
}
|
||||
|
||||
// S2 QF2: Winner -> S4 SF2 (team2)
|
||||
err = SetSeriesAdvancement(ctx, tx, s2.ID,
|
||||
&s4.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire S2")
|
||||
}
|
||||
|
||||
// S3 SF1: Winner -> S6 GF (team1), Loser -> S5 3rd Place (team1)
|
||||
err = SetSeriesAdvancement(ctx, tx, s3.ID,
|
||||
&s6.ID, strPtr("team1"), &s5.ID, strPtr("team1"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire S3")
|
||||
}
|
||||
|
||||
// S4 SF2: Winner -> S6 GF (team2), Loser -> S5 3rd Place (team2)
|
||||
err = SetSeriesAdvancement(ctx, tx, s4.ID,
|
||||
&s6.ID, strPtr("team2"), &s5.ID, strPtr("team2"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire S4")
|
||||
}
|
||||
|
||||
// S5 and S6 are terminal — no advancement
|
||||
_ = s5
|
||||
_ = s6
|
||||
return nil
|
||||
}
|
||||
|
||||
// generate10to15Bracket creates a finals bracket for 10-15 teams:
|
||||
//
|
||||
// Qualifying Finals: Top 4 get second chance
|
||||
// QF1: 1st vs 4th
|
||||
// QF2: 2nd vs 3rd
|
||||
//
|
||||
// Elimination Finals: Single elimination
|
||||
// EF1: 5th vs 8th
|
||||
// EF2: 6th vs 7th
|
||||
//
|
||||
// Semi Finals (same-side: QF loser faces same-side EF winner):
|
||||
// SF1: Loser(QF1) vs Winner(EF1) — loser eliminated
|
||||
// SF2: Loser(QF2) vs Winner(EF2) — loser eliminated
|
||||
//
|
||||
// Preliminary Finals (QF winner vs opposite SF winner):
|
||||
// PF1: Winner(QF1) vs Winner(SF2)
|
||||
// PF2: Winner(QF2) vs Winner(SF1)
|
||||
//
|
||||
// Third Place Playoff:
|
||||
// 3rd: Loser(PF1) vs Loser(PF2)
|
||||
//
|
||||
// Grand Final:
|
||||
// GF: Winner(PF1) vs Winner(PF2)
|
||||
func generate10to15Bracket(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
bracket *PlayoffBracket,
|
||||
leaderboard []*LeaderboardEntry,
|
||||
roundFormats map[string]int,
|
||||
) error {
|
||||
seed1 := leaderboard[0]
|
||||
seed2 := leaderboard[1]
|
||||
seed3 := leaderboard[2]
|
||||
seed4 := leaderboard[3]
|
||||
seed5 := leaderboard[4]
|
||||
seed6 := leaderboard[5]
|
||||
seed7 := leaderboard[6]
|
||||
seed8 := leaderboard[7]
|
||||
|
||||
// Qualifying Finals
|
||||
qf1, err := NewPlayoffSeries(ctx, tx, bracket, 1,
|
||||
"qualifying_final", "QF1",
|
||||
&seed1.Team.ID, &seed4.Team.ID,
|
||||
intPtr(1), intPtr(4),
|
||||
getMatchesToWin(roundFormats, "qualifying_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create QF1")
|
||||
}
|
||||
|
||||
qf2, err := NewPlayoffSeries(ctx, tx, bracket, 2,
|
||||
"qualifying_final", "QF2",
|
||||
&seed2.Team.ID, &seed3.Team.ID,
|
||||
intPtr(2), intPtr(3),
|
||||
getMatchesToWin(roundFormats, "qualifying_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create QF2")
|
||||
}
|
||||
|
||||
// Elimination Finals
|
||||
qf3, err := NewPlayoffSeries(ctx, tx, bracket, 3,
|
||||
"elimination_final", "EF1",
|
||||
&seed5.Team.ID, &seed8.Team.ID,
|
||||
intPtr(5), intPtr(8),
|
||||
getMatchesToWin(roundFormats, "elimination_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create EF1")
|
||||
}
|
||||
|
||||
qf4, err := NewPlayoffSeries(ctx, tx, bracket, 4,
|
||||
"elimination_final", "EF2",
|
||||
&seed6.Team.ID, &seed7.Team.ID,
|
||||
intPtr(6), intPtr(7),
|
||||
getMatchesToWin(roundFormats, "elimination_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create EF2")
|
||||
}
|
||||
|
||||
// Semi Finals
|
||||
sf1, err := NewPlayoffSeries(ctx, tx, bracket, 5,
|
||||
"semi_final", "SF1",
|
||||
nil, nil, // Loser(QF1) vs Winner(EF1)
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "semi_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create SF1")
|
||||
}
|
||||
|
||||
sf2, err := NewPlayoffSeries(ctx, tx, bracket, 6,
|
||||
"semi_final", "SF2",
|
||||
nil, nil, // Loser(QF2) vs Winner(EF2)
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "semi_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create SF2")
|
||||
}
|
||||
|
||||
// Preliminary Finals
|
||||
pf1, err := NewPlayoffSeries(ctx, tx, bracket, 7,
|
||||
"preliminary_final", "PF1",
|
||||
nil, nil, // Winner(QF1) vs Winner(SF2)
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "preliminary_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create PF1")
|
||||
}
|
||||
|
||||
pf2, err := NewPlayoffSeries(ctx, tx, bracket, 8,
|
||||
"preliminary_final", "PF2",
|
||||
nil, nil, // Winner(QF2) vs Winner(SF1)
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "preliminary_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create PF2")
|
||||
}
|
||||
|
||||
// Third Place Playoff - Loser(PF1) vs Loser(PF2)
|
||||
tp, err := NewPlayoffSeries(ctx, tx, bracket, 9,
|
||||
"third_place", "Third Place Playoff",
|
||||
nil, nil,
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "third_place"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create 3rd Place")
|
||||
}
|
||||
|
||||
// Grand Final
|
||||
gf, err := NewPlayoffSeries(ctx, tx, bracket, 10,
|
||||
"grand_final", "Grand Final",
|
||||
nil, nil,
|
||||
nil, nil,
|
||||
getMatchesToWin(roundFormats, "grand_final"), SeriesStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create GF")
|
||||
}
|
||||
|
||||
// Wire up advancement
|
||||
// QF1: Winner -> PF1 (team1), Loser -> SF1 (team1)
|
||||
err = SetSeriesAdvancement(ctx, tx, qf1.ID,
|
||||
&pf1.ID, strPtr("team1"), &sf1.ID, strPtr("team1"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire QF1")
|
||||
}
|
||||
|
||||
// QF2: Winner -> PF2 (team1), Loser -> SF2 (team1)
|
||||
err = SetSeriesAdvancement(ctx, tx, qf2.ID,
|
||||
&pf2.ID, strPtr("team1"), &sf2.ID, strPtr("team1"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire QF2")
|
||||
}
|
||||
|
||||
// EF1 (QF3): Winner -> SF1 (team2), Loser eliminated
|
||||
err = SetSeriesAdvancement(ctx, tx, qf3.ID,
|
||||
&sf1.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire EF1")
|
||||
}
|
||||
|
||||
// EF2 (QF4): Winner -> SF2 (team2), Loser eliminated
|
||||
err = SetSeriesAdvancement(ctx, tx, qf4.ID,
|
||||
&sf2.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire EF2")
|
||||
}
|
||||
|
||||
// SF1: Winner -> PF2 (team2), Loser eliminated (crosses to face QF2 winner)
|
||||
err = SetSeriesAdvancement(ctx, tx, sf1.ID,
|
||||
&pf2.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire SF1")
|
||||
}
|
||||
|
||||
// SF2: Winner -> PF1 (team2), Loser eliminated (crosses to face QF1 winner)
|
||||
err = SetSeriesAdvancement(ctx, tx, sf2.ID,
|
||||
&pf1.ID, strPtr("team2"), nil, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire SF2")
|
||||
}
|
||||
|
||||
// PF1: Winner -> GF (team1), Loser -> 3rd Place (team1)
|
||||
err = SetSeriesAdvancement(ctx, tx, pf1.ID,
|
||||
&gf.ID, strPtr("team1"), &tp.ID, strPtr("team1"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire PF1")
|
||||
}
|
||||
|
||||
// PF2: Winner -> GF (team2), Loser -> 3rd Place (team2)
|
||||
err = SetSeriesAdvancement(ctx, tx, pf2.ID,
|
||||
&gf.ID, strPtr("team2"), &tp.ID, strPtr("team2"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "wire PF2")
|
||||
}
|
||||
|
||||
// 3rd Place and Grand Final are terminal — no advancement
|
||||
_ = tp
|
||||
_ = gf
|
||||
return nil
|
||||
}
|
||||
@@ -142,7 +142,12 @@ type LeagueWithTeams struct {
|
||||
Teams []*Team
|
||||
}
|
||||
|
||||
// GetStatus returns the current status of the season based on dates
|
||||
// GetStatus returns the current status of the season based on dates.
|
||||
// Dates are treated as inclusive days:
|
||||
// - StartDate: season is "in progress" from the start of this day
|
||||
// - EndDate: season is "in progress" through the end of this day
|
||||
// - FinalsStartDate: finals are active from the start of this day
|
||||
// - FinalsEndDate: finals are active through the end of this day
|
||||
func (s *Season) GetStatus() SeasonStatus {
|
||||
now := time.Now()
|
||||
|
||||
@@ -150,20 +155,32 @@ func (s *Season) GetStatus() SeasonStatus {
|
||||
return StatusUpcoming
|
||||
}
|
||||
|
||||
// dayPassed returns true if the entire calendar day of t has passed.
|
||||
// e.g., if t is March 8, this returns true starting March 9 00:00:00.
|
||||
dayPassed := func(t time.Time) bool {
|
||||
return now.After(t.Truncate(time.Hour*24).AddDate(0, 0, 1))
|
||||
}
|
||||
|
||||
// dayStarted returns true if the calendar day of t has started.
|
||||
// e.g., if t is March 8, this returns true starting March 8 00:00:00.
|
||||
dayStarted := func(t time.Time) bool {
|
||||
return !now.Before(t.Truncate(time.Hour * 24))
|
||||
}
|
||||
|
||||
if !s.FinalsStartDate.IsZero() {
|
||||
if !s.FinalsEndDate.IsZero() && now.After(s.FinalsEndDate.Time) {
|
||||
if !s.FinalsEndDate.IsZero() && dayPassed(s.FinalsEndDate.Time) {
|
||||
return StatusCompleted
|
||||
}
|
||||
if now.After(s.FinalsStartDate.Time) {
|
||||
if dayStarted(s.FinalsStartDate.Time) {
|
||||
return StatusFinals
|
||||
}
|
||||
if !s.EndDate.IsZero() && now.After(s.EndDate.Time) {
|
||||
if !s.EndDate.IsZero() && dayPassed(s.EndDate.Time) {
|
||||
return StatusFinalsSoon
|
||||
}
|
||||
return StatusInProgress
|
||||
}
|
||||
|
||||
if !s.EndDate.IsZero() && now.After(s.EndDate.Time) {
|
||||
if !s.EndDate.IsZero() && dayPassed(s.EndDate.Time) {
|
||||
return StatusCompleted
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,9 @@ func (db *DB) RegisterModels() []any {
|
||||
(*Player)(nil),
|
||||
(*FixtureResult)(nil),
|
||||
(*FixtureResultPlayerStats)(nil),
|
||||
(*PlayoffBracket)(nil),
|
||||
(*PlayoffSeries)(nil),
|
||||
(*PlayoffMatch)(nil),
|
||||
}
|
||||
db.RegisterModel(models...)
|
||||
return models
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
--container-3xl: 48rem;
|
||||
--container-4xl: 56rem;
|
||||
--container-5xl: 64rem;
|
||||
--container-6xl: 72rem;
|
||||
--container-7xl: 80rem;
|
||||
--text-xs: 0.75rem;
|
||||
--text-xs--line-height: calc(1 / 0.75);
|
||||
@@ -371,9 +372,6 @@
|
||||
.mt-24 {
|
||||
margin-top: calc(var(--spacing) * 24);
|
||||
}
|
||||
.mt-25 {
|
||||
margin-top: calc(var(--spacing) * 25);
|
||||
}
|
||||
.mt-auto {
|
||||
margin-top: auto;
|
||||
}
|
||||
@@ -395,6 +393,9 @@
|
||||
.mb-8 {
|
||||
margin-bottom: calc(var(--spacing) * 8);
|
||||
}
|
||||
.mb-12 {
|
||||
margin-bottom: calc(var(--spacing) * 12);
|
||||
}
|
||||
.mb-auto {
|
||||
margin-bottom: auto;
|
||||
}
|
||||
@@ -444,6 +445,9 @@
|
||||
width: calc(var(--spacing) * 5);
|
||||
height: calc(var(--spacing) * 5);
|
||||
}
|
||||
.h-0 {
|
||||
height: calc(var(--spacing) * 0);
|
||||
}
|
||||
.h-1 {
|
||||
height: calc(var(--spacing) * 1);
|
||||
}
|
||||
@@ -483,6 +487,9 @@
|
||||
.h-screen {
|
||||
height: 100vh;
|
||||
}
|
||||
.max-h-40 {
|
||||
max-height: calc(var(--spacing) * 40);
|
||||
}
|
||||
.max-h-60 {
|
||||
max-height: calc(var(--spacing) * 60);
|
||||
}
|
||||
@@ -549,6 +556,9 @@
|
||||
.w-20 {
|
||||
width: calc(var(--spacing) * 20);
|
||||
}
|
||||
.w-24 {
|
||||
width: calc(var(--spacing) * 24);
|
||||
}
|
||||
.w-26 {
|
||||
width: calc(var(--spacing) * 26);
|
||||
}
|
||||
@@ -582,6 +592,9 @@
|
||||
.max-w-5xl {
|
||||
max-width: var(--container-5xl);
|
||||
}
|
||||
.max-w-6xl {
|
||||
max-width: var(--container-6xl);
|
||||
}
|
||||
.max-w-7xl {
|
||||
max-width: var(--container-7xl);
|
||||
}
|
||||
@@ -618,6 +631,12 @@
|
||||
.min-w-0 {
|
||||
min-width: calc(var(--spacing) * 0);
|
||||
}
|
||||
.min-w-\[500px\] {
|
||||
min-width: 500px;
|
||||
}
|
||||
.min-w-\[700px\] {
|
||||
min-width: 700px;
|
||||
}
|
||||
.flex-1 {
|
||||
flex: 1;
|
||||
}
|
||||
@@ -711,6 +730,9 @@
|
||||
.place-content-center {
|
||||
place-content: center;
|
||||
}
|
||||
.items-baseline {
|
||||
align-items: baseline;
|
||||
}
|
||||
.items-center {
|
||||
align-items: center;
|
||||
}
|
||||
@@ -720,6 +742,9 @@
|
||||
.items-start {
|
||||
align-items: flex-start;
|
||||
}
|
||||
.items-stretch {
|
||||
align-items: stretch;
|
||||
}
|
||||
.justify-between {
|
||||
justify-content: space-between;
|
||||
}
|
||||
@@ -927,6 +952,10 @@
|
||||
border-top-style: var(--tw-border-style);
|
||||
border-top-width: 1px;
|
||||
}
|
||||
.border-t-2 {
|
||||
border-top-style: var(--tw-border-style);
|
||||
border-top-width: 2px;
|
||||
}
|
||||
.border-b {
|
||||
border-bottom-style: var(--tw-border-style);
|
||||
border-bottom-width: 1px;
|
||||
@@ -1053,6 +1082,12 @@
|
||||
.bg-green {
|
||||
background-color: var(--green);
|
||||
}
|
||||
.bg-green\/5 {
|
||||
background-color: var(--green);
|
||||
@supports (color: color-mix(in lab, red, red)) {
|
||||
background-color: color-mix(in oklab, var(--green) 5%, transparent);
|
||||
}
|
||||
}
|
||||
.bg-green\/10 {
|
||||
background-color: var(--green);
|
||||
@supports (color: color-mix(in lab, red, red)) {
|
||||
@@ -1396,6 +1431,9 @@
|
||||
.whitespace-pre-wrap {
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
.text-base {
|
||||
color: var(--base);
|
||||
}
|
||||
.text-blue {
|
||||
color: var(--blue);
|
||||
}
|
||||
@@ -1575,6 +1613,13 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
.group-hover\:text-blue {
|
||||
&:is(:where(.group):hover *) {
|
||||
@media (hover: hover) {
|
||||
color: var(--blue);
|
||||
}
|
||||
}
|
||||
}
|
||||
.group-hover\:opacity-100 {
|
||||
&:is(:where(.group):hover *) {
|
||||
@media (hover: hover) {
|
||||
@@ -1810,6 +1855,16 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
.hover\:bg-surface0\/50 {
|
||||
&:hover {
|
||||
@media (hover: hover) {
|
||||
background-color: var(--surface0);
|
||||
@supports (color: color-mix(in lab, red, red)) {
|
||||
background-color: color-mix(in oklab, var(--surface0) 50%, transparent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.hover\:bg-surface1 {
|
||||
&:hover {
|
||||
@media (hover: hover) {
|
||||
@@ -2316,6 +2371,11 @@
|
||||
display: flex;
|
||||
}
|
||||
}
|
||||
.lg\:w-auto {
|
||||
@media (width >= 64rem) {
|
||||
width: auto;
|
||||
}
|
||||
}
|
||||
.lg\:grid-cols-2 {
|
||||
@media (width >= 64rem) {
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
@@ -2336,11 +2396,6 @@
|
||||
align-items: flex-end;
|
||||
}
|
||||
}
|
||||
.lg\:items-start {
|
||||
@media (width >= 64rem) {
|
||||
align-items: flex-start;
|
||||
}
|
||||
}
|
||||
.lg\:justify-between {
|
||||
@media (width >= 64rem) {
|
||||
justify-content: space-between;
|
||||
|
||||
152
internal/embedfs/web/js/bracket-lines.js
Normal file
152
internal/embedfs/web/js/bracket-lines.js
Normal file
@@ -0,0 +1,152 @@
|
||||
// bracket-lines.js
|
||||
// Draws smooth SVG bezier connector lines between series cards in a playoff bracket.
|
||||
// Lines connect from the bottom-center of a source card to the top-center of a
|
||||
// destination card. Winner paths are solid green, loser paths are dashed red.
|
||||
//
|
||||
// Usage: Add data-bracket-lines to a container element. Inside, series cards
|
||||
// should have data-series="N" attributes. The container needs a data-connections
|
||||
// attribute with a JSON array of connection objects:
|
||||
// [{"from": 1, "to": 3, "type": "winner"}, ...]
|
||||
//
|
||||
// Optional toSide field ("left" or "right") makes the line arrive at the
|
||||
// left or right edge (vertically centered) of the destination card instead
|
||||
// of the top-center.
|
||||
//
|
||||
// An SVG element with data-bracket-svg inside the container is used for drawing.
|
||||
|
||||
(function () {
|
||||
var WINNER_COLOR = "var(--green)";
|
||||
var LOSER_COLOR = "var(--red)";
|
||||
var STROKE_WIDTH = 2;
|
||||
var DASH_ARRAY = "6 3";
|
||||
|
||||
// Curvature control: how far the control points extend
|
||||
// as a fraction of the total distance between cards
|
||||
var CURVE_FACTOR = 0.4;
|
||||
|
||||
function drawBracketLines(container) {
|
||||
var svg = container.querySelector("[data-bracket-svg]");
|
||||
if (!svg) return;
|
||||
|
||||
var connectionsAttr = container.getAttribute("data-connections");
|
||||
if (!connectionsAttr) return;
|
||||
|
||||
var connections;
|
||||
try {
|
||||
connections = JSON.parse(connectionsAttr);
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear existing paths
|
||||
while (svg.firstChild) {
|
||||
svg.removeChild(svg.firstChild);
|
||||
}
|
||||
|
||||
// Get container position for relative coordinates
|
||||
var containerRect = container.getBoundingClientRect();
|
||||
|
||||
// Size SVG to match container
|
||||
svg.setAttribute("width", containerRect.width);
|
||||
svg.setAttribute("height", containerRect.height);
|
||||
|
||||
connections.forEach(function (conn) {
|
||||
var fromCard = container.querySelector(
|
||||
'[data-series="' + conn.from + '"]',
|
||||
);
|
||||
var toCard = container.querySelector('[data-series="' + conn.to + '"]');
|
||||
if (!fromCard || !toCard) return;
|
||||
|
||||
var fromRect = fromCard.getBoundingClientRect();
|
||||
var toRect = toCard.getBoundingClientRect();
|
||||
|
||||
// Start: bottom-center of source card
|
||||
var x1 = fromRect.left + fromRect.width / 2 - containerRect.left;
|
||||
var y1 = fromRect.bottom - containerRect.top;
|
||||
|
||||
var x2, y2, d;
|
||||
|
||||
if (conn.toSide === "left") {
|
||||
// End: left edge, vertically centered
|
||||
x2 = toRect.left - containerRect.left;
|
||||
y2 = toRect.top + toRect.height / 2 - containerRect.top;
|
||||
|
||||
// Bezier: go down first, then curve into the left side
|
||||
var dy = y2 - y1;
|
||||
var dx = x2 - x1;
|
||||
d =
|
||||
"M " + x1 + " " + y1 +
|
||||
" C " + x1 + " " + (y1 + dy * 0.5) +
|
||||
", " + (x2 + dx * 0.2) + " " + y2 +
|
||||
", " + x2 + " " + y2;
|
||||
} else if (conn.toSide === "right") {
|
||||
// End: right edge, vertically centered
|
||||
x2 = toRect.right - containerRect.left;
|
||||
y2 = toRect.top + toRect.height / 2 - containerRect.top;
|
||||
|
||||
// Bezier: go down first, then curve into the right side
|
||||
var dy = y2 - y1;
|
||||
var dx = x2 - x1;
|
||||
d =
|
||||
"M " + x1 + " " + y1 +
|
||||
" C " + x1 + " " + (y1 + dy * 0.5) +
|
||||
", " + (x2 + dx * 0.2) + " " + y2 +
|
||||
", " + x2 + " " + y2;
|
||||
} else {
|
||||
// Default: end at top-center of destination card
|
||||
x2 = toRect.left + toRect.width / 2 - containerRect.left;
|
||||
y2 = toRect.top - containerRect.top;
|
||||
|
||||
var dy = y2 - y1;
|
||||
d =
|
||||
"M " + x1 + " " + y1 +
|
||||
" C " + x1 + " " + (y1 + dy * CURVE_FACTOR) +
|
||||
", " + x2 + " " + (y2 - dy * CURVE_FACTOR) +
|
||||
", " + x2 + " " + y2;
|
||||
}
|
||||
|
||||
var path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
||||
path.setAttribute("d", d);
|
||||
path.setAttribute("fill", "none");
|
||||
path.setAttribute("stroke-width", STROKE_WIDTH);
|
||||
|
||||
if (conn.type === "winner") {
|
||||
path.setAttribute("stroke", WINNER_COLOR);
|
||||
} else {
|
||||
path.setAttribute("stroke", LOSER_COLOR);
|
||||
path.setAttribute("stroke-dasharray", DASH_ARRAY);
|
||||
}
|
||||
|
||||
svg.appendChild(path);
|
||||
});
|
||||
}
|
||||
|
||||
function drawAllBrackets() {
|
||||
var containers = document.querySelectorAll("[data-bracket-lines]");
|
||||
containers.forEach(drawBracketLines);
|
||||
}
|
||||
|
||||
// Draw on initial load
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", drawAllBrackets);
|
||||
} else {
|
||||
// DOM already loaded (e.g. script loaded via HTMX swap)
|
||||
drawAllBrackets();
|
||||
}
|
||||
|
||||
// Redraw on window resize (debounced)
|
||||
var resizeTimer;
|
||||
window.addEventListener("resize", function () {
|
||||
clearTimeout(resizeTimer);
|
||||
resizeTimer = setTimeout(drawAllBrackets, 100);
|
||||
});
|
||||
|
||||
// Redraw after HTMX swaps
|
||||
document.addEventListener("htmx:afterSwap", function () {
|
||||
// Small delay to let the DOM settle
|
||||
setTimeout(drawAllBrackets, 50);
|
||||
});
|
||||
|
||||
// Expose for manual triggering if needed
|
||||
window.drawBracketLines = drawAllBrackets;
|
||||
})();
|
||||
@@ -1,22 +1,85 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"git.haelnorr.com/h/oslstats/internal/throw"
|
||||
homeview "git.haelnorr.com/h/oslstats/internal/view/homeview"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// Index handles responses to the / path. Also serves a 404 Page for paths that
|
||||
// don't have explicit handlers
|
||||
func Index(s *hws.Server) http.Handler {
|
||||
func Index(s *hws.Server, conn *db.DB) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return
|
||||
}
|
||||
renderSafely(homeview.IndexPage(), s, r, w)
|
||||
|
||||
var season *db.Season
|
||||
var standings []homeview.LeagueStandings
|
||||
|
||||
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
// Get the most recent season
|
||||
seasons, err := db.ListSeasons(ctx, tx, &db.PageOpts{
|
||||
Page: 1,
|
||||
PerPage: 1,
|
||||
Order: bun.OrderDesc,
|
||||
OrderBy: "start_date",
|
||||
})
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.ListSeasons")
|
||||
}
|
||||
|
||||
if seasons.Total == 0 || len(seasons.Items) == 0 {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
season = seasons.Items[0]
|
||||
|
||||
// Build leaderboards for each league in this season
|
||||
standings = make([]homeview.LeagueStandings, 0, len(season.Leagues))
|
||||
for _, league := range season.Leagues {
|
||||
_, l, teams, err := db.GetSeasonLeagueWithTeams(ctx, tx, season.ShortName, league.ShortName)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetSeasonLeagueWithTeams")
|
||||
}
|
||||
|
||||
fixtures, err := db.GetAllocatedFixtures(ctx, tx, season.ID, l.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)
|
||||
|
||||
standings = append(standings, homeview.LeagueStandings{
|
||||
League: l,
|
||||
Leaderboard: leaderboard,
|
||||
})
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
renderSafely(homeview.IndexPage(season, standings), s, r, w)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,17 +2,27 @@ package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"strconv"
|
||||
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"git.haelnorr.com/h/oslstats/internal/notify"
|
||||
"git.haelnorr.com/h/oslstats/internal/respond"
|
||||
"git.haelnorr.com/h/oslstats/internal/throw"
|
||||
"git.haelnorr.com/h/oslstats/internal/validation"
|
||||
"git.haelnorr.com/h/oslstats/internal/view/seasonsview"
|
||||
"git.haelnorr.com/h/timefmt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// SeasonLeagueFinalsPage renders the finals tab of a season league page
|
||||
// SeasonLeagueFinalsPage renders the finals tab of a season league page.
|
||||
// Displays different content based on season status:
|
||||
// - In Progress: "Regular Season in Progress" with optional "Begin Finals" button
|
||||
// - Finals Soon/Finals/Completed: The playoff bracket
|
||||
func SeasonLeagueFinalsPage(
|
||||
s *hws.Server,
|
||||
conn *db.DB,
|
||||
@@ -21,11 +31,13 @@ func SeasonLeagueFinalsPage(
|
||||
seasonStr := r.PathValue("season_short_name")
|
||||
leagueStr := r.PathValue("league_short_name")
|
||||
|
||||
var sl *db.SeasonLeague
|
||||
var season *db.Season
|
||||
var league *db.League
|
||||
var bracket *db.PlayoffBracket
|
||||
|
||||
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
sl, err = db.GetSeasonLeague(ctx, tx, seasonStr, leagueStr)
|
||||
sl, err := db.GetSeasonLeague(ctx, tx, seasonStr, leagueStr)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
@@ -33,15 +45,266 @@ func SeasonLeagueFinalsPage(
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GetSeasonLeague")
|
||||
}
|
||||
season = sl.Season
|
||||
league = sl.League
|
||||
|
||||
// Try to load existing bracket
|
||||
bracket, err = db.GetPlayoffBracket(ctx, tx, season.ID, league.ID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetPlayoffBracket")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "GET" {
|
||||
renderSafely(seasonsview.SeasonLeagueFinalsPage(sl.Season, sl.League), s, r, w)
|
||||
renderSafely(seasonsview.SeasonLeagueFinalsPage(season, league, bracket), s, r, w)
|
||||
} else {
|
||||
renderSafely(seasonsview.SeasonLeagueFinals(), s, r, w)
|
||||
renderSafely(seasonsview.SeasonLeagueFinals(season, league, bracket), s, r, w)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// SeasonLeagueFinalsSetupForm renders the finals setup form via HTMX.
|
||||
// Shows date pickers, format selection, unplayed fixture warnings, and standings preview.
|
||||
func SeasonLeagueFinalsSetupForm(
|
||||
s *hws.Server,
|
||||
conn *db.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seasonStr := r.PathValue("season_short_name")
|
||||
leagueStr := r.PathValue("league_short_name")
|
||||
|
||||
var season *db.Season
|
||||
var league *db.League
|
||||
var leaderboard []*db.LeaderboardEntry
|
||||
var unplayedFixtures []*db.Fixture
|
||||
|
||||
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
var teams []*db.Team
|
||||
season, league, teams, err = db.GetSeasonLeagueWithTeams(ctx, tx, seasonStr, leagueStr)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GetSeasonLeagueWithTeams")
|
||||
}
|
||||
|
||||
// Get allocated fixtures and results for leaderboard
|
||||
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)
|
||||
|
||||
// Get unplayed fixtures
|
||||
unplayedFixtures, err = db.GetUnplayedFixtures(ctx, tx, season.ID, league.ID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetUnplayedFixtures")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
renderSafely(seasonsview.FinalsSetupForm(
|
||||
season, league, leaderboard, unplayedFixtures,
|
||||
), s, r, w)
|
||||
})
|
||||
}
|
||||
|
||||
// SeasonLeagueFinalsSetupSubmit processes the finals setup form.
|
||||
// It validates inputs, auto-forfeits unplayed fixtures, updates season dates,
|
||||
// and generates the playoff bracket.
|
||||
func SeasonLeagueFinalsSetupSubmit(
|
||||
s *hws.Server,
|
||||
conn *db.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seasonStr := r.PathValue("season_short_name")
|
||||
leagueStr := r.PathValue("league_short_name")
|
||||
|
||||
getter, ok := validation.ParseFormOrNotify(s, w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
format := timefmt.NewBuilder().
|
||||
DayNumeric2().Slash().
|
||||
MonthNumeric2().Slash().
|
||||
Year4().Build()
|
||||
|
||||
endDate := getter.Time("regular_season_end_date", format).Required().Value
|
||||
finalsStartDate := getter.Time("finals_start_date", format).Required().Value
|
||||
playoffFormat := getter.String("format").TrimSpace().Required().
|
||||
AllowedValues([]string{
|
||||
string(db.PlayoffFormat5to6),
|
||||
string(db.PlayoffFormat7to9),
|
||||
string(db.PlayoffFormat10to15),
|
||||
}).Value
|
||||
|
||||
if !getter.ValidateAndNotify(s, w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
// Validate finals start is after end date
|
||||
if !finalsStartDate.After(endDate) && !finalsStartDate.Equal(endDate) {
|
||||
notify.Warn(s, w, r, "Invalid Dates",
|
||||
"Finals start date must be on or after the regular season end date.", nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse per-round BO configuration from form fields
|
||||
roundFormats := parseRoundFormats(r, db.PlayoffFormat(playoffFormat))
|
||||
|
||||
if ok := conn.WithNotifyTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
// Get season, league, teams
|
||||
var teams []*db.Team
|
||||
season, league, teams, err := db.GetSeasonLeagueWithTeams(ctx, tx, seasonStr, leagueStr)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
respond.NotFound(w, err)
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GetSeasonLeagueWithTeams")
|
||||
}
|
||||
|
||||
// Check no bracket already exists
|
||||
existing, err := db.GetPlayoffBracket(ctx, tx, season.ID, league.ID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetPlayoffBracket")
|
||||
}
|
||||
if existing != nil {
|
||||
notify.Warn(s, w, r, "Already Exists",
|
||||
"A playoff bracket already exists for this league.", nil)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
user := db.CurrentUser(ctx)
|
||||
audit := db.NewAuditFromRequest(r)
|
||||
|
||||
// Auto-forfeit unplayed fixtures
|
||||
forfeitCount, err := db.AutoForfeitUnplayedFixtures(
|
||||
ctx, tx, season.ID, league.ID, user.ID, audit)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.AutoForfeitUnplayedFixtures")
|
||||
}
|
||||
|
||||
// Update season dates
|
||||
err = season.Update(ctx, tx,
|
||||
season.SlapVersion,
|
||||
season.StartDate,
|
||||
endDate,
|
||||
finalsStartDate,
|
||||
season.FinalsEndDate.Time,
|
||||
audit,
|
||||
)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "season.Update")
|
||||
}
|
||||
|
||||
// Compute final leaderboard (after forfeits)
|
||||
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)
|
||||
|
||||
// Generate the bracket
|
||||
_, err = db.GeneratePlayoffBracket(
|
||||
ctx, tx,
|
||||
season.ID, league.ID,
|
||||
db.PlayoffFormat(playoffFormat),
|
||||
leaderboard,
|
||||
roundFormats,
|
||||
audit,
|
||||
)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
notify.Warn(s, w, r, "Cannot Create Bracket", err.Error(), nil)
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GeneratePlayoffBracket")
|
||||
}
|
||||
|
||||
_ = forfeitCount
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("/seasons/%s/leagues/%s/finals", seasonStr, leagueStr)
|
||||
respond.HXRedirect(w, "%s", url)
|
||||
notify.SuccessWithDelay(s, w, r, "Finals Created",
|
||||
"Playoff bracket has been generated successfully.", nil)
|
||||
})
|
||||
}
|
||||
|
||||
// parseRoundFormats reads bo_<round> form fields and returns a map of round name
|
||||
// to matches_to_win value (1 = BO1, 2 = BO3, 3 = BO5).
|
||||
// Form fields are named like "bo_grand_final", "bo_semi_final", etc.
|
||||
func parseRoundFormats(r *http.Request, format db.PlayoffFormat) map[string]int {
|
||||
roundFormats := make(map[string]int)
|
||||
|
||||
var rounds []string
|
||||
switch format {
|
||||
case db.PlayoffFormat5to6:
|
||||
rounds = []string{
|
||||
"upper_bracket", "lower_bracket",
|
||||
"upper_final", "lower_final",
|
||||
"grand_final",
|
||||
}
|
||||
case db.PlayoffFormat7to9:
|
||||
rounds = []string{
|
||||
"quarter_final", "semi_final",
|
||||
"third_place", "grand_final",
|
||||
}
|
||||
case db.PlayoffFormat10to15:
|
||||
rounds = []string{
|
||||
"qualifying_final", "elimination_final",
|
||||
"semi_final", "preliminary_final",
|
||||
"third_place", "grand_final",
|
||||
}
|
||||
}
|
||||
|
||||
for _, round := range rounds {
|
||||
val := r.FormValue("bo_" + round)
|
||||
if val == "" {
|
||||
continue
|
||||
}
|
||||
mtw, err := strconv.Atoi(val)
|
||||
if err != nil || mtw < 1 || mtw > 3 {
|
||||
continue // Invalid values default to BO1 in getMatchesToWin
|
||||
}
|
||||
roundFormats[round] = mtw
|
||||
}
|
||||
|
||||
return roundFormats
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ const (
|
||||
FixturesCreate Permission = "fixtures.create"
|
||||
FixturesDelete Permission = "fixtures.delete"
|
||||
|
||||
// Playoffs permissions
|
||||
PlayoffsManage Permission = "playoffs.manage"
|
||||
|
||||
// Free Agent permissions
|
||||
FreeAgentsAdd Permission = "free_agents.add"
|
||||
FreeAgentsRemove Permission = "free_agents.remove"
|
||||
|
||||
@@ -44,17 +44,21 @@ func addMiddleware(
|
||||
func devMode(cfg *config.Config) hws.Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if cfg.Flags.DevMode {
|
||||
devInfo := contexts.DevInfo{
|
||||
WebsocketBase: "ws://" + cfg.HWS.Host + ":" + strconv.FormatUint(cfg.HWS.Port, 10),
|
||||
HTMXLog: true,
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), contexts.DevModeKey, devInfo)
|
||||
req := r.WithContext(ctx)
|
||||
next.ServeHTTP(w, req)
|
||||
if !cfg.Flags.DevMode && !cfg.Flags.Staging {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
devInfo := contexts.DevInfo{}
|
||||
if cfg.Flags.DevMode {
|
||||
devInfo.WebsocketBase = "ws://" + cfg.HWS.Host + ":" + strconv.FormatUint(cfg.HWS.Port, 10)
|
||||
devInfo.HTMXLog = true
|
||||
}
|
||||
if cfg.Flags.Staging {
|
||||
devInfo.StagingBanner = true
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), contexts.DevModeKey, devInfo)
|
||||
req := r.WithContext(ctx)
|
||||
next.ServeHTTP(w, req)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ func addRoutes(
|
||||
{
|
||||
Path: "/",
|
||||
Method: hws.MethodGET,
|
||||
Handler: handlers.Index(s),
|
||||
Handler: handlers.Index(s, conn),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -137,6 +137,16 @@ func addRoutes(
|
||||
Methods: []hws.Method{hws.MethodGET, hws.MethodPOST},
|
||||
Handler: handlers.SeasonLeagueFinalsPage(s, conn),
|
||||
},
|
||||
{
|
||||
Path: "/seasons/{season_short_name}/leagues/{league_short_name}/finals/setup",
|
||||
Method: hws.MethodGET,
|
||||
Handler: perms.RequirePermission(s, permissions.PlayoffsManage)(handlers.SeasonLeagueFinalsSetupForm(s, conn)),
|
||||
},
|
||||
{
|
||||
Path: "/seasons/{season_short_name}/leagues/{league_short_name}/finals/setup",
|
||||
Method: hws.MethodPOST,
|
||||
Handler: perms.RequirePermission(s, permissions.PlayoffsManage)(handlers.SeasonLeagueFinalsSetupSubmit(s, conn)),
|
||||
},
|
||||
{
|
||||
Path: "/seasons/{season_short_name}/add-league/{league_short_name}",
|
||||
Method: hws.MethodPOST,
|
||||
|
||||
@@ -26,6 +26,15 @@ templ Footer() {
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
<script src="https://storage.ko-fi.com/cdn/scripts/overlay-widget.js"></script>
|
||||
<script>
|
||||
kofiWidgetOverlay.draw('haelnorr', {
|
||||
'type': 'floating-chat',
|
||||
'floating-chat.donateButton.text': 'Support me',
|
||||
'floating-chat.donateButton.background-color': '#313244',
|
||||
'floating-chat.donateButton.text-color': '#ccd5f3'
|
||||
});
|
||||
</script>
|
||||
}
|
||||
|
||||
templ backToTopButton() {
|
||||
@@ -56,12 +65,9 @@ templ backToTopButton() {
|
||||
|
||||
templ footerBranding() {
|
||||
<div>
|
||||
<div class="flex justify-center text-text lg:justify-start">
|
||||
<div class="flex justify-center text-text lg:justify-start pb-4">
|
||||
<span class="text-2xl">OSL Stats</span>
|
||||
</div>
|
||||
<p class="mx-auto max-w-md text-center leading-relaxed text-subtext0">
|
||||
placeholder text
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -86,7 +92,7 @@ templ footerLinks(items []FooterItem) {
|
||||
templ footerCopyright() {
|
||||
<div>
|
||||
<p class="mt-4 text-center text-sm text-overlay0">
|
||||
by Haelnorr | placeholder text
|
||||
by Haelnorr
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ templ Layout(title string) {
|
||||
id="main-content"
|
||||
class="flex flex-col h-screen"
|
||||
>
|
||||
if devInfo.StagingBanner {
|
||||
@stagingBanner()
|
||||
}
|
||||
@Navbar()
|
||||
if previewRole != nil {
|
||||
@previewModeBanner(previewRole)
|
||||
@@ -57,6 +60,12 @@ templ Layout(title string) {
|
||||
</html>
|
||||
}
|
||||
|
||||
templ stagingBanner() {
|
||||
<div class="bg-peach text-crust text-center text-xs font-bold py-1 tracking-wider uppercase">
|
||||
Staging Environment - For Testing Only
|
||||
</div>
|
||||
}
|
||||
|
||||
// Preview mode banner (private helper)
|
||||
templ previewModeBanner(previewRole *db.Role) {
|
||||
<div class="bg-yellow/20 border-b border-yellow/40 px-4 py-3">
|
||||
|
||||
33
internal/view/homeview/external_links.templ
Normal file
33
internal/view/homeview/external_links.templ
Normal file
@@ -0,0 +1,33 @@
|
||||
package homeview
|
||||
|
||||
// ExternalLinks renders card tiles for external community resources
|
||||
templ ExternalLinks() {
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<a
|
||||
href="http://slapshot.gg/osl"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
class="bg-surface0 border border-surface1 rounded-lg p-6 hover:bg-surface1 transition hover:cursor-pointer group"
|
||||
>
|
||||
<h3 class="text-lg font-bold text-text group-hover:text-blue transition mb-2">
|
||||
Join Our Discord
|
||||
</h3>
|
||||
<p class="text-sm text-subtext0">
|
||||
Connect with other players, find teams, and stay up to date with league announcements.
|
||||
</p>
|
||||
</a>
|
||||
<a
|
||||
href="https://slapshot.gg/"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
class="bg-surface0 border border-surface1 rounded-lg p-6 hover:bg-surface1 transition hover:cursor-pointer group"
|
||||
>
|
||||
<h3 class="text-lg font-bold text-text group-hover:text-blue transition mb-2">
|
||||
Official Slapshot
|
||||
</h3>
|
||||
<p class="text-sm text-subtext0">
|
||||
Visit the official Slapshot website to learn more about the game.
|
||||
</p>
|
||||
</a>
|
||||
</div>
|
||||
}
|
||||
@@ -1,13 +1,32 @@
|
||||
package homeview
|
||||
|
||||
import "git.haelnorr.com/h/oslstats/internal/db"
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/baseview"
|
||||
|
||||
// Page content for the index page
|
||||
templ IndexPage() {
|
||||
@baseview.Layout("OSL Stats") {
|
||||
<div class="text-center mt-25">
|
||||
<div class="text-4xl lg:text-6xl">OSL Stats</div>
|
||||
<div>Placeholder text</div>
|
||||
templ IndexPage(season *db.Season, standings []LeagueStandings) {
|
||||
@baseview.Layout("Oceanic Slapshot League") {
|
||||
<div class="max-w-screen-2xl mx-auto px-2">
|
||||
<div class="mt-8 mb-12">
|
||||
<h1 class="text-5xl lg:text-6xl font-bold text-text mb-6 text-center">
|
||||
Oceanic Slapshot League
|
||||
</h1>
|
||||
<div class="max-w-3xl mx-auto bg-surface0 border border-surface1 rounded-lg p-6">
|
||||
<p class="text-base text-subtext0 leading-relaxed">
|
||||
The Oceanic Slapshot League (OSL) is a community for casual and competitive play of Slapshot: Rebound.
|
||||
It is managed by a small group of community members, and aims to provide a place for players in the Oceanic
|
||||
region (primarily Australia and New Zealand) to compete and play in organised League competitions, as well as
|
||||
casual pick-up games (RPUGs) and public matches (in-game matchmaking).
|
||||
The league is open to everyone, regardless of skill level.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="max-w-6xl mx-auto mb-12">
|
||||
@LatestStandings(season, standings)
|
||||
</div>
|
||||
<div class="max-w-6xl mx-auto mb-12">
|
||||
@ExternalLinks()
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
151
internal/view/homeview/latest_standings.templ
Normal file
151
internal/view/homeview/latest_standings.templ
Normal file
@@ -0,0 +1,151 @@
|
||||
package homeview
|
||||
|
||||
import "git.haelnorr.com/h/oslstats/internal/db"
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/component/links"
|
||||
import "fmt"
|
||||
|
||||
// LeagueStandings holds the data needed to render a single league's table
|
||||
type LeagueStandings struct {
|
||||
League *db.League
|
||||
Leaderboard []*db.LeaderboardEntry
|
||||
}
|
||||
|
||||
// LatestStandings renders the latest standings section with tabs to switch
|
||||
// between leagues from the most recent season
|
||||
templ LatestStandings(season *db.Season, standings []LeagueStandings) {
|
||||
<div class="space-y-4">
|
||||
<div class="flex items-baseline gap-3">
|
||||
<h2 class="text-2xl font-bold text-text">Latest Standings</h2>
|
||||
if season != nil {
|
||||
<a
|
||||
href={ templ.SafeURL(fmt.Sprintf("/seasons/%s", season.ShortName)) }
|
||||
class="text-sm text-subtext0 hover:text-blue transition"
|
||||
>
|
||||
{ season.Name }
|
||||
</a>
|
||||
}
|
||||
</div>
|
||||
if season == nil || len(standings) == 0 {
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg p-8 text-center">
|
||||
<p class="text-subtext0 text-lg">No standings available yet.</p>
|
||||
</div>
|
||||
} else {
|
||||
<div x-data={ fmt.Sprintf("{ activeTab: '%s' }", standings[0].League.ShortName) }>
|
||||
if len(standings) > 1 {
|
||||
<div class="flex gap-1 mb-4 border-b border-surface1">
|
||||
for _, s := range standings {
|
||||
<button
|
||||
x-on:click={ fmt.Sprintf("activeTab = '%s'", s.League.ShortName) }
|
||||
class="px-4 py-2 text-sm font-medium transition hover:cursor-pointer"
|
||||
x-bind:class={ fmt.Sprintf("activeTab === '%s' ? 'text-blue border-b-2 border-blue' : 'text-subtext0 hover:text-text'", s.League.ShortName) }
|
||||
>
|
||||
{ s.League.Name }
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
for _, s := range standings {
|
||||
<div x-show={ fmt.Sprintf("activeTab === '%s'", s.League.ShortName) }>
|
||||
@standingsTable(season, s.League, s.Leaderboard)
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ standingsTable(season *db.Season, league *db.League, leaderboard []*db.LeaderboardEntry) {
|
||||
if len(leaderboard) == 0 {
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg p-8 text-center">
|
||||
<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">
|
||||
<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 {
|
||||
@standingsRow(entry, season, league)
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
templ standingsRow(entry *db.LeaderboardEntry, season *db.Season, league *db.League) {
|
||||
{{
|
||||
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">
|
||||
@links.TeamLinkInSeason(entry.Team, season, league)
|
||||
</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>
|
||||
}
|
||||
280
internal/view/seasonsview/finals_setup_form.templ
Normal file
280
internal/view/seasonsview/finals_setup_form.templ
Normal file
@@ -0,0 +1,280 @@
|
||||
package seasonsview
|
||||
|
||||
import "git.haelnorr.com/h/oslstats/internal/db"
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/datepicker"
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/component/links"
|
||||
import "fmt"
|
||||
|
||||
templ FinalsSetupForm(
|
||||
season *db.Season,
|
||||
league *db.League,
|
||||
leaderboard []*db.LeaderboardEntry,
|
||||
unplayedFixtures []*db.Fixture,
|
||||
) {
|
||||
{{
|
||||
// Determine the recommended format value for the default Alpine state
|
||||
defaultFormat := ""
|
||||
if len(leaderboard) >= 10 && len(leaderboard) <= 15 {
|
||||
defaultFormat = string(db.PlayoffFormat10to15)
|
||||
} else if len(leaderboard) >= 7 && len(leaderboard) <= 9 {
|
||||
defaultFormat = string(db.PlayoffFormat7to9)
|
||||
} else if len(leaderboard) >= 5 && len(leaderboard) <= 6 {
|
||||
defaultFormat = string(db.PlayoffFormat5to6)
|
||||
}
|
||||
|
||||
// Prefill dates from existing season values
|
||||
endDateDefault := ""
|
||||
if !season.EndDate.IsZero() {
|
||||
endDateDefault = season.EndDate.Time.Format("02/01/2006")
|
||||
}
|
||||
finalsStartDefault := ""
|
||||
if !season.FinalsStartDate.IsZero() {
|
||||
finalsStartDefault = season.FinalsStartDate.Time.Format("02/01/2006")
|
||||
}
|
||||
}}
|
||||
<div class="max-w-3xl mx-auto">
|
||||
<div
|
||||
class="bg-surface0 border border-surface1 rounded-lg overflow-hidden"
|
||||
x-data={ fmt.Sprintf("{ selectedFormat: '%s' }", defaultFormat) }
|
||||
>
|
||||
<!-- Header -->
|
||||
<div class="bg-mantle border-b border-surface1 px-6 py-4">
|
||||
<h2 class="text-xl font-bold text-text flex items-center gap-2">
|
||||
<span class="text-yellow">★</span>
|
||||
Begin Finals Setup
|
||||
</h2>
|
||||
<p class="text-sm text-subtext0 mt-1">
|
||||
Configure playoff format and dates for { league.Name }
|
||||
</p>
|
||||
</div>
|
||||
<form
|
||||
hx-post={ fmt.Sprintf("/seasons/%s/leagues/%s/finals/setup", season.ShortName, league.ShortName) }
|
||||
hx-swap="none"
|
||||
class="p-6 space-y-6"
|
||||
>
|
||||
<!-- Date Fields -->
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div>
|
||||
@datepicker.DatePickerWithDefault(
|
||||
"regular_season_end_date",
|
||||
"regular_season_end_date",
|
||||
"Regular Season End Date",
|
||||
"DD/MM/YYYY",
|
||||
true,
|
||||
"",
|
||||
endDateDefault,
|
||||
)
|
||||
<p class="text-xs text-subtext0 mt-1">Last day of the regular season (inclusive)</p>
|
||||
</div>
|
||||
<div>
|
||||
@datepicker.DatePickerWithDefault(
|
||||
"finals_start_date",
|
||||
"finals_start_date",
|
||||
"Finals Start Date",
|
||||
"DD/MM/YYYY",
|
||||
true,
|
||||
"",
|
||||
finalsStartDefault,
|
||||
)
|
||||
<p class="text-xs text-subtext0 mt-1">First playoff matches begin on this date</p>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Format Selection -->
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-3">Playoff Format</label>
|
||||
<div class="space-y-3">
|
||||
@formatOption(
|
||||
string(db.PlayoffFormat5to6),
|
||||
"5-6 Teams",
|
||||
"Top 5 qualify. 1st earns a bye, 2nd vs 3rd (upper), 4th vs 5th (lower). Double-chance for top seeds.",
|
||||
len(leaderboard) >= 5 && len(leaderboard) <= 6,
|
||||
len(leaderboard),
|
||||
)
|
||||
@formatOption(
|
||||
string(db.PlayoffFormat7to9),
|
||||
"7-9 Teams",
|
||||
"Top 6 qualify. 1st & 2nd placed into semis. 3rd vs 6th and 4th vs 5th in quarter finals.",
|
||||
len(leaderboard) >= 7 && len(leaderboard) <= 9,
|
||||
len(leaderboard),
|
||||
)
|
||||
@formatOption(
|
||||
string(db.PlayoffFormat10to15),
|
||||
"10-15 Teams",
|
||||
"Top 8 qualify. Top 4 get a second chance in qualifying finals.",
|
||||
len(leaderboard) >= 10 && len(leaderboard) <= 15,
|
||||
len(leaderboard),
|
||||
)
|
||||
</div>
|
||||
</div>
|
||||
<!-- Per-Round Best-of-N Configuration -->
|
||||
<div x-show="selectedFormat !== ''" x-cloak>
|
||||
<label class="block text-sm font-medium mb-3">Series Format (Best-of-N per Round)</label>
|
||||
<div class="bg-mantle border border-surface1 rounded-lg p-4 space-y-3">
|
||||
<!-- 5-6 Teams rounds -->
|
||||
<template x-if={ fmt.Sprintf("selectedFormat === '%s'", string(db.PlayoffFormat5to6)) }>
|
||||
<div class="space-y-3">
|
||||
@boRoundDropdown("bo_upper_bracket", "Upper Bracket", "2nd vs 3rd")
|
||||
@boRoundDropdown("bo_lower_bracket", "Lower Bracket", "4th vs 5th (elimination)")
|
||||
@boRoundDropdown("bo_upper_final", "Upper Final", "1st vs Winner of Upper Bracket")
|
||||
@boRoundDropdown("bo_lower_final", "Lower Final", "Loser of Upper Final vs Winner of Lower Bracket")
|
||||
@boRoundDropdown("bo_grand_final", "Grand Final", "Winner of Upper Final vs Winner of Lower Final")
|
||||
</div>
|
||||
</template>
|
||||
<!-- 7-9 Teams rounds -->
|
||||
<template x-if={ fmt.Sprintf("selectedFormat === '%s'", string(db.PlayoffFormat7to9)) }>
|
||||
<div class="space-y-3">
|
||||
@boRoundDropdown("bo_quarter_final", "Quarter Finals", "3rd vs 6th, 4th vs 5th")
|
||||
@boRoundDropdown("bo_semi_final", "Semi Finals", "1st vs QF winner, 2nd vs QF winner")
|
||||
@boRoundDropdown("bo_third_place", "Third Place Playoff", "SF1 loser vs SF2 loser")
|
||||
@boRoundDropdown("bo_grand_final", "Grand Final", "SF1 winner vs SF2 winner")
|
||||
</div>
|
||||
</template>
|
||||
<!-- 10-15 Teams rounds -->
|
||||
<template x-if={ fmt.Sprintf("selectedFormat === '%s'", string(db.PlayoffFormat10to15)) }>
|
||||
<div class="space-y-3">
|
||||
@boRoundDropdown("bo_qualifying_final", "Qualifying Finals", "1st vs 4th, 2nd vs 3rd (losers get second chance)")
|
||||
@boRoundDropdown("bo_elimination_final", "Elimination Finals", "5th vs 8th, 6th vs 7th (losers eliminated)")
|
||||
@boRoundDropdown("bo_semi_final", "Semi Finals", "QF losers vs EF winners")
|
||||
@boRoundDropdown("bo_preliminary_final", "Preliminary Finals", "QF winners vs SF winners")
|
||||
@boRoundDropdown("bo_third_place", "Third Place Playoff", "PF1 loser vs PF2 loser")
|
||||
@boRoundDropdown("bo_grand_final", "Grand Final", "PF1 winner vs PF2 winner")
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Unplayed Fixtures Warning -->
|
||||
if len(unplayedFixtures) > 0 {
|
||||
<div class="bg-yellow/10 border border-yellow/30 rounded-lg p-4">
|
||||
<div class="flex items-start gap-3">
|
||||
<svg class="w-5 h-5 text-yellow mt-0.5 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z"></path>
|
||||
</svg>
|
||||
<div>
|
||||
<p class="text-sm font-semibold text-yellow mb-1">
|
||||
{ fmt.Sprintf("%d unplayed fixture(s) found", len(unplayedFixtures)) }
|
||||
</p>
|
||||
<p class="text-xs text-subtext0 mb-3">
|
||||
These fixtures will be recorded as a mutual forfeit when you begin finals.
|
||||
This action cannot be undone.
|
||||
</p>
|
||||
<div class="max-h-40 overflow-y-auto space-y-1">
|
||||
for _, fixture := range unplayedFixtures {
|
||||
<div class="text-xs text-subtext1 flex items-center gap-2">
|
||||
<span class="text-subtext0">GW{ fmt.Sprint(*fixture.GameWeek) }</span>
|
||||
<span>{ fixture.HomeTeam.Name }</span>
|
||||
<span class="text-subtext0">vs</span>
|
||||
<span>{ fixture.AwayTeam.Name }</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
<!-- Standings Preview -->
|
||||
if len(leaderboard) > 0 {
|
||||
<div>
|
||||
<label class="block text-sm font-medium mb-3">Current Standings</label>
|
||||
<div class="bg-mantle border border-surface1 rounded-lg overflow-hidden">
|
||||
<table class="w-full">
|
||||
<thead class="bg-surface0 border-b border-surface1">
|
||||
<tr>
|
||||
<th class="px-3 py-2 text-center text-xs font-semibold text-subtext0 w-10">#</th>
|
||||
<th class="px-3 py-2 text-left text-xs font-semibold text-text">Team</th>
|
||||
<th class="px-3 py-2 text-center text-xs font-semibold text-text">GP</th>
|
||||
<th class="px-3 py-2 text-center text-xs font-semibold text-blue">PTS</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-surface1">
|
||||
for _, entry := range leaderboard {
|
||||
@standingsPreviewRow(entry, season, league)
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
<!-- Actions -->
|
||||
<div class="flex justify-end gap-3 pt-4 border-t border-surface1">
|
||||
<button
|
||||
type="button"
|
||||
hx-post={ fmt.Sprintf("/seasons/%s/leagues/%s/finals", season.ShortName, league.ShortName) }
|
||||
hx-target="#finals-content"
|
||||
hx-swap="innerHTML"
|
||||
class="px-4 py-2 bg-surface1 hover:bg-surface2 text-text rounded-lg font-medium transition hover:cursor-pointer"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
class="px-6 py-2 bg-blue hover:bg-blue/80 text-mantle rounded-lg font-semibold transition hover:cursor-pointer"
|
||||
>
|
||||
Begin Finals
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ formatOption(value, label, description string, recommended bool, teamCount int) {
|
||||
<label
|
||||
class="flex items-start gap-3 p-3 bg-mantle border border-surface1 rounded-lg hover:bg-surface0 transition hover:cursor-pointer"
|
||||
x-bind:class={ fmt.Sprintf("selectedFormat === '%s' && 'border-blue/50 bg-blue/5'", value) }
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
name="format"
|
||||
value={ value }
|
||||
if recommended {
|
||||
checked
|
||||
}
|
||||
x-model="selectedFormat"
|
||||
class="mt-1 text-blue focus:ring-blue hover:cursor-pointer"
|
||||
required
|
||||
/>
|
||||
<div>
|
||||
<span class="text-sm font-medium text-text">{ label }</span>
|
||||
if recommended {
|
||||
<span class="ml-2 px-2 py-0.5 bg-green/20 text-green rounded text-xs font-medium">
|
||||
Recommended for { fmt.Sprint(teamCount) } teams
|
||||
</span>
|
||||
}
|
||||
<p class="text-xs text-subtext0 mt-0.5">{ description }</p>
|
||||
</div>
|
||||
</label>
|
||||
}
|
||||
|
||||
templ boRoundDropdown(name, label, description string) {
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<div class="flex-1 min-w-0">
|
||||
<span class="text-sm font-medium text-text">{ label }</span>
|
||||
<p class="text-xs text-subtext0 truncate">{ description }</p>
|
||||
</div>
|
||||
<select
|
||||
name={ name }
|
||||
class="w-24 px-3 py-1.5 bg-surface0 border border-surface1 rounded text-sm text-text focus:border-blue focus:outline-none hover:cursor-pointer"
|
||||
>
|
||||
<option value="1" selected>BO1</option>
|
||||
<option value="2">BO3</option>
|
||||
<option value="3">BO5</option>
|
||||
</select>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ standingsPreviewRow(entry *db.LeaderboardEntry, season *db.Season, league *db.League) {
|
||||
<tr class="hover:bg-surface0/50 transition-colors">
|
||||
<td class="px-3 py-2 text-center text-sm font-medium text-subtext0">
|
||||
{ fmt.Sprint(entry.Position) }
|
||||
</td>
|
||||
<td class="px-3 py-2">
|
||||
@links.TeamLinkInSeason(entry.Team, season, league)
|
||||
</td>
|
||||
<td class="px-3 py-2 text-center text-sm text-subtext0">
|
||||
{ fmt.Sprint(entry.Record.Played) }
|
||||
</td>
|
||||
<td class="px-3 py-2 text-center text-sm font-semibold text-blue">
|
||||
{ fmt.Sprint(entry.Record.Points) }
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
@@ -115,29 +115,28 @@ templ SeasonsList(seasons *db.List[db.Season]) {
|
||||
}
|
||||
</div>
|
||||
}
|
||||
<!-- Date Info -->
|
||||
{{
|
||||
now := time.Now()
|
||||
}}
|
||||
<div class="text-xs text-subtext1 mt-auto">
|
||||
if now.Before(s.StartDate) {
|
||||
<!-- Date Info -->
|
||||
{{
|
||||
listStatus := s.GetStatus()
|
||||
}}
|
||||
<div class="text-xs text-subtext1 mt-auto">
|
||||
switch listStatus {
|
||||
case db.StatusUpcoming:
|
||||
Starts: { formatDate(s.StartDate) }
|
||||
} else if !s.FinalsStartDate.IsZero() {
|
||||
// Finals are scheduled
|
||||
if !s.FinalsEndDate.IsZero() && now.After(s.FinalsEndDate.Time) {
|
||||
case db.StatusCompleted:
|
||||
if !s.FinalsEndDate.IsZero() {
|
||||
Completed: { formatDate(s.FinalsEndDate.Time) }
|
||||
} else if now.After(s.FinalsStartDate.Time) {
|
||||
Finals Started: { formatDate(s.FinalsStartDate.Time) }
|
||||
} else {
|
||||
Finals Start: { formatDate(s.FinalsStartDate.Time) }
|
||||
} else if !s.EndDate.IsZero() {
|
||||
Completed: { formatDate(s.EndDate.Time) }
|
||||
}
|
||||
} else if !s.EndDate.IsZero() && now.After(s.EndDate.Time) {
|
||||
// No finals scheduled and regular season ended
|
||||
Completed: { formatDate(s.EndDate.Time) }
|
||||
} else {
|
||||
case db.StatusFinals:
|
||||
Finals Started: { formatDate(s.FinalsStartDate.Time) }
|
||||
case db.StatusFinalsSoon:
|
||||
Finals Start: { formatDate(s.FinalsStartDate.Time) }
|
||||
default:
|
||||
Started: { formatDate(s.StartDate) }
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</a>
|
||||
}
|
||||
</div>
|
||||
|
||||
306
internal/view/seasonsview/playoff_bracket.templ
Normal file
306
internal/view/seasonsview/playoff_bracket.templ
Normal file
@@ -0,0 +1,306 @@
|
||||
package seasonsview
|
||||
|
||||
import "git.haelnorr.com/h/oslstats/internal/db"
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/component/links"
|
||||
import "fmt"
|
||||
|
||||
// PlayoffBracketView renders the full bracket visualization
|
||||
templ PlayoffBracketView(season *db.Season, league *db.League, bracket *db.PlayoffBracket) {
|
||||
<div class="space-y-6">
|
||||
<!-- Bracket Header -->
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<h2 class="text-xl font-bold text-text flex items-center gap-2">
|
||||
<span class="text-yellow">★</span>
|
||||
Finals Bracket
|
||||
</h2>
|
||||
<p class="text-sm text-subtext0 mt-1">
|
||||
{ formatLabel(bracket.Format) }
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
@playoffStatusBadge(bracket.Status)
|
||||
</div>
|
||||
</div>
|
||||
<!-- Bracket Display -->
|
||||
switch bracket.Format {
|
||||
case db.PlayoffFormat5to6:
|
||||
@bracket5to6(season, league, bracket)
|
||||
case db.PlayoffFormat7to9:
|
||||
@bracket7to9(season, league, bracket)
|
||||
case db.PlayoffFormat10to15:
|
||||
@bracket10to15(season, league, bracket)
|
||||
}
|
||||
<!-- Legend -->
|
||||
<div class="flex items-center gap-6 text-xs text-subtext0">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-8 h-0 border-t-2 border-green"></div>
|
||||
<span>Winner</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="w-8 h-0 border-t-2 border-red border-dashed"></div>
|
||||
<span>Loser</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="/static/js/bracket-lines.js"></script>
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────
|
||||
// 5-6 TEAMS FORMAT
|
||||
// ──────────────────────────────────────────────
|
||||
// Round 1: [Upper Bracket] [Lower Bracket]
|
||||
// Round 2: [Upper Final] [Lower Final]
|
||||
// Round 3: [Grand Final]
|
||||
templ bracket5to6(season *db.Season, league *db.League, bracket *db.PlayoffBracket) {
|
||||
{{
|
||||
s := seriesByNumber(bracket.Series)
|
||||
conns := connectionsJSON(bracket.Series)
|
||||
}}
|
||||
<div class="overflow-x-auto">
|
||||
<div class="relative min-w-[500px]" data-bracket-lines data-connections={ conns }>
|
||||
<svg data-bracket-svg class="absolute inset-0 w-full h-full pointer-events-none" style="z-index: 0;"></svg>
|
||||
<div class="relative" style="z-index: 1;">
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
@seriesCard(season, league, s[1])
|
||||
@seriesCard(season, league, s[2])
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
@seriesCard(season, league, s[3])
|
||||
@seriesCard(season, league, s[4])
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<div class="max-w-md mx-auto">
|
||||
@seriesCard(season, league, s[5])
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────
|
||||
// 7-9 TEAMS FORMAT
|
||||
// ──────────────────────────────────────────────
|
||||
// Round 1 (Quarter Finals): [QF1] [QF2]
|
||||
// Round 2 (Semi Finals): [SF1] [SF2]
|
||||
// Round 3: [3rd Place] [Grand Final]
|
||||
templ bracket7to9(season *db.Season, league *db.League, bracket *db.PlayoffBracket) {
|
||||
{{
|
||||
s := seriesByNumber(bracket.Series)
|
||||
conns := connectionsJSON(bracket.Series)
|
||||
}}
|
||||
<div class="overflow-x-auto">
|
||||
<div class="relative min-w-[500px]" data-bracket-lines data-connections={ conns }>
|
||||
<svg data-bracket-svg class="absolute inset-0 w-full h-full pointer-events-none" style="z-index: 0;"></svg>
|
||||
<div class="relative" style="z-index: 1;">
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
@seriesCard(season, league, s[1])
|
||||
@seriesCard(season, league, s[2])
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
@seriesCard(season, league, s[3])
|
||||
@seriesCard(season, league, s[4])
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
@seriesCard(season, league, s[5])
|
||||
@seriesCard(season, league, s[6])
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────
|
||||
// 10-15 TEAMS FORMAT
|
||||
// ──────────────────────────────────────────────
|
||||
// 4 invisible columns, cards placed into specific cells:
|
||||
// Row 1: EF1(col2) EF2(col3)
|
||||
// Row 2: QF1(col1) QF2(col4)
|
||||
// Row 3: SF1(col2) SF2(col3)
|
||||
// Row 4: PF1(col2) PF2(col3)
|
||||
// Row 5: 3rd(col2)
|
||||
// Row 6: GF(col3)
|
||||
templ bracket10to15(season *db.Season, league *db.League, bracket *db.PlayoffBracket) {
|
||||
{{
|
||||
s := seriesByNumber(bracket.Series)
|
||||
conns := connectionsJSON(bracket.Series)
|
||||
}}
|
||||
<div class="overflow-x-auto">
|
||||
<div class="relative min-w-[700px]" data-bracket-lines data-connections={ conns }>
|
||||
<svg data-bracket-svg class="absolute inset-0 w-full h-full pointer-events-none" style="z-index: 0;"></svg>
|
||||
<div class="relative" style="z-index: 1;">
|
||||
<!-- Row 1: EF1(c2) EF2(c3) -->
|
||||
<div class="grid grid-cols-4 gap-4">
|
||||
<div></div>
|
||||
@seriesCard(season, league, s[3])
|
||||
@seriesCard(season, league, s[4])
|
||||
<div></div>
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<!-- Row 2: QF1(c1) QF2(c4) -->
|
||||
<div class="grid grid-cols-4 gap-4">
|
||||
@seriesCard(season, league, s[1])
|
||||
<div></div>
|
||||
<div></div>
|
||||
@seriesCard(season, league, s[2])
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<!-- Row 3: SF1(c2) SF2(c3) -->
|
||||
<div class="grid grid-cols-4 gap-4">
|
||||
<div></div>
|
||||
@seriesCard(season, league, s[5])
|
||||
@seriesCard(season, league, s[6])
|
||||
<div></div>
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<!-- Row 4: PF1(c2) PF2(c3) -->
|
||||
<div class="grid grid-cols-4 gap-4">
|
||||
<div></div>
|
||||
@seriesCard(season, league, s[7])
|
||||
@seriesCard(season, league, s[8])
|
||||
<div></div>
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<!-- Row 5: 3rd Place(c2) -->
|
||||
<div class="grid grid-cols-4 gap-4">
|
||||
<div></div>
|
||||
@seriesCard(season, league, s[9])
|
||||
<div></div>
|
||||
<div></div>
|
||||
</div>
|
||||
<div class="h-16"></div>
|
||||
<!-- Row 6: Grand Final(c3) -->
|
||||
<div class="grid grid-cols-4 gap-4">
|
||||
<div></div>
|
||||
<div></div>
|
||||
@seriesCard(season, league, s[10])
|
||||
<div></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────
|
||||
// SHARED COMPONENTS
|
||||
// ──────────────────────────────────────────────
|
||||
|
||||
templ seriesCard(season *db.Season, league *db.League, series *db.PlayoffSeries) {
|
||||
<div
|
||||
data-series={ fmt.Sprint(series.SeriesNumber) }
|
||||
class={ "bg-surface0 border rounded-lg overflow-hidden",
|
||||
templ.KV("border-blue/50", series.Status == db.SeriesStatusInProgress),
|
||||
templ.KV("border-surface1", series.Status != db.SeriesStatusInProgress) }
|
||||
>
|
||||
<!-- Series Header -->
|
||||
<div class="bg-mantle px-3 py-1.5 flex items-center justify-between border-b border-surface1">
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="text-xs font-semibold text-subtext0">{ series.Label }</span>
|
||||
@seriesFormatBadge(series.MatchesToWin)
|
||||
</div>
|
||||
@seriesStatusBadge(series.Status)
|
||||
</div>
|
||||
<!-- Teams -->
|
||||
<div class="divide-y divide-surface1">
|
||||
@seriesTeamRow(season, league, series.Team1, series.Team1Seed, series.Team1Wins,
|
||||
series.WinnerTeamID, series.MatchesToWin)
|
||||
@seriesTeamRow(season, league, series.Team2, series.Team2Seed, series.Team2Wins,
|
||||
series.WinnerTeamID, series.MatchesToWin)
|
||||
</div>
|
||||
<!-- Series Score -->
|
||||
if series.MatchesToWin > 1 {
|
||||
<div class="bg-mantle px-3 py-1 text-center text-xs text-subtext0 border-t border-surface1">
|
||||
{ fmt.Sprint(series.Team1Wins) } - { fmt.Sprint(series.Team2Wins) }
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ seriesTeamRow(season *db.Season, league *db.League, team *db.Team, seed *int, wins int, winnerID *int, matchesToWin int) {
|
||||
{{
|
||||
isWinner := false
|
||||
if team != nil && winnerID != nil {
|
||||
isWinner = team.ID == *winnerID
|
||||
}
|
||||
isTBD := team == nil
|
||||
}}
|
||||
<div class={ "flex items-center justify-between px-3 py-2",
|
||||
templ.KV("bg-green/5", isWinner) }>
|
||||
<div class="flex items-center gap-2 min-w-0">
|
||||
if seed != nil {
|
||||
<span class="text-xs font-mono text-subtext0 w-4 text-right flex-shrink-0">
|
||||
{ fmt.Sprint(*seed) }
|
||||
</span>
|
||||
} else {
|
||||
<span class="text-xs font-mono text-subtext0 w-4 text-right flex-shrink-0">-</span>
|
||||
}
|
||||
if isTBD {
|
||||
<span class="text-sm text-subtext1 italic">TBD</span>
|
||||
} else {
|
||||
<div class="truncate">
|
||||
@links.TeamLinkInSeason(team, season, league)
|
||||
</div>
|
||||
if isWinner {
|
||||
<span class="text-green text-xs flex-shrink-0">✓</span>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
if matchesToWin > 1 {
|
||||
<span class={ "text-sm font-mono flex-shrink-0 ml-2",
|
||||
templ.KV("text-text", !isWinner),
|
||||
templ.KV("text-green font-bold", isWinner) }>
|
||||
{ fmt.Sprint(wins) }
|
||||
</span>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ playoffStatusBadge(status db.PlayoffStatus) {
|
||||
switch status {
|
||||
case db.PlayoffStatusUpcoming:
|
||||
<span class="px-2 py-0.5 bg-blue/20 text-blue rounded text-xs font-medium">
|
||||
Upcoming
|
||||
</span>
|
||||
case db.PlayoffStatusInProgress:
|
||||
<span class="px-2 py-0.5 bg-yellow/20 text-yellow rounded text-xs font-medium">
|
||||
In Progress
|
||||
</span>
|
||||
case db.PlayoffStatusCompleted:
|
||||
<span class="px-2 py-0.5 bg-green/20 text-green rounded text-xs font-medium">
|
||||
Completed
|
||||
</span>
|
||||
}
|
||||
}
|
||||
|
||||
templ seriesFormatBadge(matchesToWin int) {
|
||||
{{
|
||||
label := fmt.Sprintf("BO%d", matchesToWin*2-1)
|
||||
}}
|
||||
<span class="px-1.5 py-0.5 bg-surface1 text-subtext1 rounded text-xs font-mono">
|
||||
{ label }
|
||||
</span>
|
||||
}
|
||||
|
||||
templ seriesStatusBadge(status db.SeriesStatus) {
|
||||
switch status {
|
||||
case db.SeriesStatusPending:
|
||||
<span class="px-1.5 py-0.5 bg-surface1 text-subtext0 rounded text-xs">
|
||||
Pending
|
||||
</span>
|
||||
case db.SeriesStatusInProgress:
|
||||
<span class="px-1.5 py-0.5 bg-blue/20 text-blue rounded text-xs">
|
||||
Live
|
||||
</span>
|
||||
case db.SeriesStatusCompleted:
|
||||
<span class="px-1.5 py-0.5 bg-green/20 text-green rounded text-xs">
|
||||
Complete
|
||||
</span>
|
||||
case db.SeriesStatusBye:
|
||||
<span class="px-1.5 py-0.5 bg-surface1 text-subtext0 rounded text-xs">
|
||||
Bye
|
||||
</span>
|
||||
}
|
||||
}
|
||||
86
internal/view/seasonsview/playoff_helpers.go
Normal file
86
internal/view/seasonsview/playoff_helpers.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package seasonsview
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
)
|
||||
|
||||
// seriesByNumber returns a map of series_number -> *PlayoffSeries for quick lookup
|
||||
func seriesByNumber(series []*db.PlayoffSeries) map[int]*db.PlayoffSeries {
|
||||
m := make(map[int]*db.PlayoffSeries, len(series))
|
||||
for _, s := range series {
|
||||
m[s.SeriesNumber] = s
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// formatLabel returns a human-readable format description
|
||||
func formatLabel(format db.PlayoffFormat) string {
|
||||
switch format {
|
||||
case db.PlayoffFormat5to6:
|
||||
return "Top 5 qualify"
|
||||
case db.PlayoffFormat7to9:
|
||||
return "Top 6 qualify"
|
||||
case db.PlayoffFormat10to15:
|
||||
return "Top 8 qualify"
|
||||
default:
|
||||
return string(format)
|
||||
}
|
||||
}
|
||||
|
||||
// bracketConnection represents a line to draw between two series cards
|
||||
type bracketConnection struct {
|
||||
From int `json:"from"`
|
||||
To int `json:"to"`
|
||||
Type string `json:"type"` // "winner" or "loser"
|
||||
ToSide string `json:"toSide,omitempty"` // "left" or "right" — enters side of dest card
|
||||
}
|
||||
|
||||
// connectionsJSON returns a JSON string of connections for the bracket overlay JS.
|
||||
// Connections are derived from the series advancement links stored in the DB.
|
||||
// For the 10-15 format, QF winner lines enter PF cards from the side.
|
||||
func connectionsJSON(series []*db.PlayoffSeries) string {
|
||||
// Build a lookup of series ID → series for resolving advancement targets
|
||||
byID := make(map[int]*db.PlayoffSeries, len(series))
|
||||
for _, s := range series {
|
||||
byID[s.ID] = s
|
||||
}
|
||||
|
||||
var conns []bracketConnection
|
||||
for _, s := range series {
|
||||
if s.WinnerNextID != nil {
|
||||
if target, ok := byID[*s.WinnerNextID]; ok {
|
||||
conn := bracketConnection{
|
||||
From: s.SeriesNumber,
|
||||
To: target.SeriesNumber,
|
||||
Type: "winner",
|
||||
}
|
||||
// QF winners enter PF cards from the side in the 10-15 format
|
||||
if s.Round == "qualifying_final" && target.Round == "preliminary_final" {
|
||||
if s.SeriesNumber == 1 {
|
||||
conn.ToSide = "left"
|
||||
} else if s.SeriesNumber == 2 {
|
||||
conn.ToSide = "right"
|
||||
}
|
||||
}
|
||||
conns = append(conns, conn)
|
||||
}
|
||||
}
|
||||
if s.LoserNextID != nil {
|
||||
if target, ok := byID[*s.LoserNextID]; ok {
|
||||
conns = append(conns, bracketConnection{
|
||||
From: s.SeriesNumber,
|
||||
To: target.SeriesNumber,
|
||||
Type: "loser",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b, err := json.Marshal(conns)
|
||||
if err != nil {
|
||||
return "[]"
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
@@ -1,15 +1,56 @@
|
||||
package seasonsview
|
||||
|
||||
import "git.haelnorr.com/h/oslstats/internal/db"
|
||||
import "git.haelnorr.com/h/oslstats/internal/contexts"
|
||||
import "git.haelnorr.com/h/oslstats/internal/permissions"
|
||||
import "fmt"
|
||||
|
||||
templ SeasonLeagueFinalsPage(season *db.Season, league *db.League) {
|
||||
templ SeasonLeagueFinalsPage(season *db.Season, league *db.League, bracket *db.PlayoffBracket) {
|
||||
@SeasonLeagueLayout("finals", season, league) {
|
||||
@SeasonLeagueFinals()
|
||||
@SeasonLeagueFinals(season, league, bracket)
|
||||
}
|
||||
}
|
||||
|
||||
templ SeasonLeagueFinals() {
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg p-8 text-center">
|
||||
<p class="text-subtext0 text-lg">Coming Soon...</p>
|
||||
templ SeasonLeagueFinals(season *db.Season, league *db.League, bracket *db.PlayoffBracket) {
|
||||
{{
|
||||
permCache := contexts.Permissions(ctx)
|
||||
canManagePlayoffs := permCache != nil && permCache.HasPermission(permissions.PlayoffsManage)
|
||||
}}
|
||||
<div id="finals-content">
|
||||
if bracket != nil {
|
||||
@PlayoffBracketView(season, league, bracket)
|
||||
} else if canManagePlayoffs {
|
||||
@finalsNotYetConfigured(season, league)
|
||||
} else {
|
||||
@finalsNotConfigured()
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ finalsNotYetConfigured(season *db.Season, league *db.League) {
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg p-8 text-center">
|
||||
<div class="mb-4">
|
||||
<svg class="w-12 h-12 mx-auto text-subtext0 mb-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M16.5 18.75h-9m9 0a3 3 0 013 3h-15a3 3 0 013-3m9 0v-3.375c0-.621-.503-1.125-1.125-1.125h-.871M7.5 18.75v-3.375c0-.621.504-1.125 1.125-1.125h.872m5.007 0H9.497m5.007 0a7.454 7.454 0 01-.982-3.172M9.497 14.25a7.454 7.454 0 00.981-3.172M5.25 4.236c-.982.143-1.954.317-2.916.52A6.003 6.003 0 007.73 9.728M5.25 4.236V4.5c0 2.108.966 3.99 2.48 5.228M5.25 4.236V2.721C7.456 2.41 9.71 2.25 12 2.25c2.291 0 4.545.16 6.75.47v1.516M18.75 4.236c.982.143 1.954.317 2.916.52A6.003 6.003 0 0016.27 9.728M18.75 4.236V4.5c0 2.108-.966 3.99-2.48 5.228m0 0a6.003 6.003 0 01-2.77.836 6.003 6.003 0 01-2.77-.836"></path>
|
||||
</svg>
|
||||
</div>
|
||||
<p class="text-text text-lg font-semibold mb-2">No Finals Configured</p>
|
||||
<p class="text-subtext0 mb-6">
|
||||
Set up the playoff bracket for this league.
|
||||
</p>
|
||||
<button
|
||||
hx-get={ fmt.Sprintf("/seasons/%s/leagues/%s/finals/setup", season.ShortName, league.ShortName) }
|
||||
hx-target="#finals-content"
|
||||
hx-swap="innerHTML"
|
||||
class="px-6 py-3 bg-blue hover:bg-blue/80 text-mantle rounded-lg font-semibold transition hover:cursor-pointer"
|
||||
>
|
||||
Begin Finals
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ finalsNotConfigured() {
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg p-8 text-center">
|
||||
<p class="text-subtext0 text-lg">No finals configured for this league.</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -37,16 +37,16 @@ templ SeasonLeagueStats(
|
||||
if len(topGoals) > 0 || len(topAssists) > 0 || len(topSaves) > 0 {
|
||||
<div class="space-y-4">
|
||||
<h2 class="text-xl font-bold text-text text-center">Trophy Leaders</h2>
|
||||
<!-- Triangle layout: two side-by-side on wide screens, saves centered below -->
|
||||
<div class="flex flex-col items-center gap-6">
|
||||
<!-- Top row: Goals and Assists side by side when room allows -->
|
||||
<div class="flex flex-col lg:flex-row gap-6 justify-center items-center lg:items-start">
|
||||
@topGoalScorersTable(season, league, topGoals)
|
||||
@topAssistersTable(season, league, topAssists)
|
||||
</div>
|
||||
<!-- Bottom row: Saves centered -->
|
||||
@topSaversTable(season, league, topSaves)
|
||||
<!-- Triangle layout: two side-by-side on wide screens, saves centered below -->
|
||||
<div class="flex flex-col items-center gap-6 w-full">
|
||||
<!-- Top row: Goals and Assists side by side when room allows -->
|
||||
<div class="flex flex-col lg:flex-row gap-6 justify-center items-stretch w-full lg:w-auto">
|
||||
@topGoalScorersTable(season, league, topGoals)
|
||||
@topAssistersTable(season, league, topAssists)
|
||||
</div>
|
||||
<!-- Bottom row: Saves centered -->
|
||||
@topSaversTable(season, league, topSaves)
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
<!-- All Stats Section -->
|
||||
@@ -61,7 +61,7 @@ templ SeasonLeagueStats(
|
||||
}
|
||||
|
||||
templ topGoalScorersTable(season *db.Season, league *db.League, goals []*db.LeagueTopGoalScorer) {
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg overflow-hidden">
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg overflow-hidden min-w-0 w-full lg:w-auto">
|
||||
<div class="bg-mantle border-b border-surface1 px-4 py-3">
|
||||
<h3 class="text-sm font-semibold text-text">
|
||||
Top Goal Scorers
|
||||
@@ -79,7 +79,8 @@ templ topGoalScorersTable(season *db.Season, league *db.League, goals []*db.Leag
|
||||
<p class="text-subtext0 text-sm">No goal data available yet.</p>
|
||||
</div>
|
||||
} else {
|
||||
<table>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full">
|
||||
<thead class="bg-mantle border-b border-surface1">
|
||||
<tr>
|
||||
<th class="px-3 py-2 text-center text-xs font-semibold text-subtext0 w-10">#</th>
|
||||
@@ -109,12 +110,13 @@ templ topGoalScorersTable(season *db.Season, league *db.League, goals []*db.Leag
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ topAssistersTable(season *db.Season, league *db.League, assists []*db.LeagueTopAssister) {
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg overflow-hidden">
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg overflow-hidden min-w-0 w-full lg:w-auto">
|
||||
<div class="bg-mantle border-b border-surface1 px-4 py-3">
|
||||
<h3 class="text-sm font-semibold text-text">
|
||||
Top Assisters
|
||||
@@ -132,7 +134,8 @@ templ topAssistersTable(season *db.Season, league *db.League, assists []*db.Leag
|
||||
<p class="text-subtext0 text-sm">No assist data available yet.</p>
|
||||
</div>
|
||||
} else {
|
||||
<table>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full">
|
||||
<thead class="bg-mantle border-b border-surface1">
|
||||
<tr>
|
||||
<th class="px-3 py-2 text-center text-xs font-semibold text-subtext0 w-10">#</th>
|
||||
@@ -162,12 +165,13 @@ templ topAssistersTable(season *db.Season, league *db.League, assists []*db.Leag
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ topSaversTable(season *db.Season, league *db.League, saves []*db.LeagueTopSaver) {
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg overflow-hidden">
|
||||
<div class="bg-surface0 border border-surface1 rounded-lg overflow-hidden min-w-0 w-full lg:w-auto">
|
||||
<div class="bg-mantle border-b border-surface1 px-4 py-3">
|
||||
<h3 class="text-sm font-semibold text-text">
|
||||
Top Saves
|
||||
@@ -185,7 +189,8 @@ templ topSaversTable(season *db.Season, league *db.League, saves []*db.LeagueTop
|
||||
<p class="text-subtext0 text-sm">No save data available yet.</p>
|
||||
</div>
|
||||
} else {
|
||||
<table>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full">
|
||||
<thead class="bg-mantle border-b border-surface1">
|
||||
<tr>
|
||||
<th class="px-3 py-2 text-center text-xs font-semibold text-subtext0 w-10">#</th>
|
||||
@@ -215,6 +220,7 @@ templ topSaversTable(season *db.Season, league *db.League, saves []*db.LeagueTop
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package seasonsview
|
||||
|
||||
import "git.haelnorr.com/h/oslstats/internal/db"
|
||||
import "time"
|
||||
|
||||
// StatusBadge renders a season status badge
|
||||
// Parameters:
|
||||
@@ -10,53 +9,34 @@ import "time"
|
||||
// - useShortLabels: bool - true for "Active/Finals", false for "In Progress/Finals in Progress"
|
||||
templ StatusBadge(season *db.Season, compact bool, useShortLabels bool) {
|
||||
{{
|
||||
now := time.Now()
|
||||
seasonStatus := season.GetStatus()
|
||||
status := ""
|
||||
statusBg := ""
|
||||
|
||||
// Determine status based on dates
|
||||
if now.Before(season.StartDate) {
|
||||
|
||||
switch seasonStatus {
|
||||
case db.StatusUpcoming:
|
||||
status = "Upcoming"
|
||||
statusBg = "bg-blue"
|
||||
} else if !season.FinalsStartDate.IsZero() {
|
||||
// Finals are scheduled
|
||||
if !season.FinalsEndDate.IsZero() && now.After(season.FinalsEndDate.Time) {
|
||||
// Finals have ended
|
||||
status = "Completed"
|
||||
statusBg = "bg-teal"
|
||||
} else if now.After(season.FinalsStartDate.Time) {
|
||||
// Finals are in progress
|
||||
if useShortLabels {
|
||||
status = "Finals"
|
||||
} else {
|
||||
status = "Finals in Progress"
|
||||
}
|
||||
statusBg = "bg-yellow"
|
||||
} else if !season.EndDate.IsZero() && now.After(season.EndDate.Time) {
|
||||
// Regular season ended, finals upcoming
|
||||
status = "Finals Soon"
|
||||
statusBg = "bg-peach"
|
||||
} else {
|
||||
// Regular season active, finals scheduled for later
|
||||
if useShortLabels {
|
||||
status = "Active"
|
||||
} else {
|
||||
status = "In Progress"
|
||||
}
|
||||
statusBg = "bg-green"
|
||||
}
|
||||
} else if !season.EndDate.IsZero() && now.After(season.EndDate.Time) {
|
||||
// No finals scheduled and regular season ended
|
||||
status = "Completed"
|
||||
statusBg = "bg-teal"
|
||||
} else {
|
||||
// Regular season active, no finals scheduled
|
||||
case db.StatusInProgress:
|
||||
if useShortLabels {
|
||||
status = "Active"
|
||||
} else {
|
||||
status = "In Progress"
|
||||
}
|
||||
statusBg = "bg-green"
|
||||
case db.StatusFinalsSoon:
|
||||
status = "Finals Soon"
|
||||
statusBg = "bg-peach"
|
||||
case db.StatusFinals:
|
||||
if useShortLabels {
|
||||
status = "Finals"
|
||||
} else {
|
||||
status = "Finals in Progress"
|
||||
}
|
||||
statusBg = "bg-yellow"
|
||||
case db.StatusCompleted:
|
||||
status = "Completed"
|
||||
statusBg = "bg-teal"
|
||||
}
|
||||
}}
|
||||
<span class={ "inline-block px-3 py-1 rounded-full text-sm font-semibold text-mantle " + statusBg }>
|
||||
|
||||
85
justfile
85
justfile
@@ -122,3 +122,88 @@ _migrate-new name: && _build _migrate-status
|
||||
reset-db env='.env': _build
|
||||
echo "⚠️ WARNING - This will DELETE ALL DATA!"
|
||||
{{bin}}/{{entrypoint}} --reset-db --envfile {{env}}
|
||||
|
||||
# Restore database from a production backup (.sql)
|
||||
[group('db')]
|
||||
[confirm("⚠️ This will DELETE ALL DATA in the dev database and replace it with the backup. Continue?")]
|
||||
[script]
|
||||
restore-db backup_file env='.env':
|
||||
set -euo pipefail
|
||||
|
||||
# Source env vars
|
||||
set -a
|
||||
source ./{{env}}
|
||||
set +a
|
||||
|
||||
DB_USER="${DB_USER}"
|
||||
DB_PASSWORD="${DB_PASSWORD}"
|
||||
DB_HOST="${DB_HOST}"
|
||||
DB_PORT="${DB_PORT:-5432}"
|
||||
DB_NAME="${DB_NAME}"
|
||||
PROD_USER="oslstats"
|
||||
|
||||
export PGPASSWORD="$DB_PASSWORD"
|
||||
|
||||
echo "[INFO] Restoring database from: {{backup_file}}"
|
||||
echo "[INFO] Target: $DB_NAME on $DB_HOST:$DB_PORT as $DB_USER"
|
||||
echo ""
|
||||
|
||||
# Step 1: Drop and recreate the database
|
||||
echo "[INFO] Step 1/4: Dropping and recreating database..."
|
||||
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c \
|
||||
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '$DB_NAME' AND pid <> pg_backend_pid();" \
|
||||
> /dev/null 2>&1 || true
|
||||
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c "DROP DATABASE IF EXISTS \"$DB_NAME\";"
|
||||
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c "CREATE DATABASE \"$DB_NAME\" OWNER \"$DB_USER\";"
|
||||
echo "[INFO] Database recreated"
|
||||
|
||||
# Step 2: Preprocess and restore the dump (remap ownership)
|
||||
echo "[INFO] Step 2/4: Restoring backup (remapping owner $PROD_USER → $DB_USER)..."
|
||||
sed \
|
||||
-e "s/OWNER TO ${PROD_USER}/OWNER TO ${DB_USER}/g" \
|
||||
-e "s/Owner: ${PROD_USER}/Owner: ${DB_USER}/g" \
|
||||
-e "/^ALTER DEFAULT PRIVILEGES/d" \
|
||||
-e "s/GRANT ALL ON \(.*\) TO ${PROD_USER}/GRANT ALL ON \1 TO ${DB_USER}/g" \
|
||||
"{{backup_file}}" | psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" --quiet --single-transaction
|
||||
echo "[INFO] Backup restored"
|
||||
|
||||
# Step 3: Reassign all ownership as safety net
|
||||
echo "[INFO] Step 3/4: Reassigning remaining ownership to $DB_USER..."
|
||||
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" <<EOSQL
|
||||
DO \$\$
|
||||
DECLARE
|
||||
r RECORD;
|
||||
BEGIN
|
||||
-- Reassign tables
|
||||
FOR r IN SELECT tablename FROM pg_tables WHERE schemaname = 'public'
|
||||
LOOP
|
||||
EXECUTE format('ALTER TABLE public.%I OWNER TO ${DB_USER}', r.tablename);
|
||||
END LOOP;
|
||||
|
||||
-- Reassign sequences
|
||||
FOR r IN SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema = 'public'
|
||||
LOOP
|
||||
EXECUTE format('ALTER SEQUENCE public.%I OWNER TO ${DB_USER}', r.sequence_name);
|
||||
END LOOP;
|
||||
|
||||
-- Reassign views
|
||||
FOR r IN SELECT viewname FROM pg_views WHERE schemaname = 'public'
|
||||
LOOP
|
||||
EXECUTE format('ALTER VIEW public.%I OWNER TO ${DB_USER}', r.viewname);
|
||||
END LOOP;
|
||||
END \$\$;
|
||||
EOSQL
|
||||
echo "[INFO] Ownership reassigned"
|
||||
|
||||
# Step 4: Summary
|
||||
echo "[INFO] Step 4/4: Verifying table count..."
|
||||
TABLE_COUNT=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c \
|
||||
"SELECT count(*) FROM pg_tables WHERE schemaname = 'public';")
|
||||
echo "[INFO] Found${TABLE_COUNT} tables in restored database"
|
||||
|
||||
echo ""
|
||||
echo "✅ Database restored successfully from: {{backup_file}}"
|
||||
echo ""
|
||||
echo "📋 Next steps:"
|
||||
echo " 1. Run 'just migrate up all' to apply any dev-only migrations"
|
||||
echo " 2. Run 'just dev' to start the development server"
|
||||
|
||||
Reference in New Issue
Block a user