From 7d5949af1e6c0ba2594cb28eb1dbf8c3fccce3c0 Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Wed, 18 Feb 2026 21:15:16 +1100 Subject: [PATCH] added an autoallocate fixtures function --- internal/db/fixture.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/internal/db/fixture.go b/internal/db/fixture.go index 15b0f71..f16caa5 100644 --- a/internal/db/fixture.go +++ b/internal/db/fixture.go @@ -280,3 +280,40 @@ func playOtherTeams(team *Team, teams []*Team, round int) []*versus { } return matchups } + +func AutoAllocateFixtures(fixtures []*Fixture, gamesPerWeek, startingWeek int) []*Fixture { + gameWeek := startingWeek + teamPlays := map[int]int{} + // Work on a copy so we can track what's remaining + remaining := make([]*Fixture, len(fixtures)) + copy(remaining, fixtures) + for len(remaining) > 0 { + madeProgress := false + nextRemaining := make([]*Fixture, 0, len(remaining)) + for _, fixture := range remaining { + if teamPlays[fixture.HomeTeamID] < gamesPerWeek && + teamPlays[fixture.AwayTeamID] < gamesPerWeek { + gw := gameWeek + fixture.GameWeek = &gw + teamPlays[fixture.HomeTeamID]++ + teamPlays[fixture.AwayTeamID]++ + madeProgress = true + } else { + nextRemaining = append(nextRemaining, fixture) + } + } + if !madeProgress { + // No fixture could be placed this week — advance to avoid infinite loop + // (shouldn't happen with valid fixture data, but guards against edge cases) + gameWeek++ + teamPlays = map[int]int{} + continue + } + remaining = nextRemaining + if len(remaining) > 0 { + gameWeek++ + teamPlays = map[int]int{} + } + } + return fixtures +}