added an autoallocate fixtures function
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user