fixtures #2

Merged
h merged 20 commits from fixtures into master 2026-02-23 20:38:26 +11:00
Showing only changes of commit 7d5949af1e - Show all commits

View File

@@ -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
}