mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 13:46:44 -04:00
Fixed bug in scheduling with excess matches.
This commit is contained in:
@@ -8,6 +8,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -29,6 +30,10 @@ func BuildRandomSchedule(teams []Team, scheduleBlocks []ScheduleBlock, matchType
|
|||||||
numTeams := len(teams)
|
numTeams := len(teams)
|
||||||
numMatches := countMatches(scheduleBlocks)
|
numMatches := countMatches(scheduleBlocks)
|
||||||
matchesPerTeam := int(float32(numMatches*teamsPerMatch) / float32(numTeams))
|
matchesPerTeam := int(float32(numMatches*teamsPerMatch) / float32(numTeams))
|
||||||
|
|
||||||
|
// Adjust the number of matches to remove any excess from non-perfect block scheduling.
|
||||||
|
numMatches = int(math.Ceil(float64(numTeams) * float64(matchesPerTeam) / teamsPerMatch))
|
||||||
|
|
||||||
file, err := os.Open(fmt.Sprintf("%s/%d_%d.csv", schedulesDir, numTeams, matchesPerTeam))
|
file, err := os.Open(fmt.Sprintf("%s/%d_%d.csv", schedulesDir, numTeams, matchesPerTeam))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("No schedule template exists for %d teams and %d matches", numTeams, matchesPerTeam)
|
return nil, fmt.Errorf("No schedule template exists for %d teams and %d matches", numTeams, matchesPerTeam)
|
||||||
@@ -77,7 +82,7 @@ func BuildRandomSchedule(teams []Team, scheduleBlocks []ScheduleBlock, matchType
|
|||||||
// Fill in the match times.
|
// Fill in the match times.
|
||||||
matchIndex := 0
|
matchIndex := 0
|
||||||
for _, block := range scheduleBlocks {
|
for _, block := range scheduleBlocks {
|
||||||
for i := 0; i < block.NumMatches; i++ {
|
for i := 0; i < block.NumMatches && matchIndex < numMatches; i++ {
|
||||||
matches[matchIndex].Time = block.StartTime.Add(time.Duration(i*block.MatchSpacingSec) * time.Second)
|
matches[matchIndex].Time = block.StartTime.Add(time.Duration(i*block.MatchSpacingSec) * time.Second)
|
||||||
matchIndex++
|
matchIndex++
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,11 @@ func TestScheduleTeams(t *testing.T) {
|
|||||||
Red3: 115, Blue1: 110, Blue2: 114, Blue3: 102}, matches[4])
|
Red3: 115, Blue1: 110, Blue2: 114, Blue3: 102}, matches[4])
|
||||||
assert.Equal(t, Match{Type: "test", DisplayName: "6", Time: time.Unix(300, 0).UTC(), Red1: 118, Red2: 105,
|
assert.Equal(t, Match{Type: "test", DisplayName: "6", Time: time.Unix(300, 0).UTC(), Red1: 118, Red2: 105,
|
||||||
Red3: 106, Blue1: 107, Blue2: 104, Blue3: 116}, matches[5])
|
Red3: 106, Blue1: 107, Blue2: 104, Blue3: 116}, matches[5])
|
||||||
|
|
||||||
|
// Check with excess room for matches in the schedule.
|
||||||
|
scheduleBlocks = []ScheduleBlock{{time.Unix(0, 0).UTC(), 7, 60}}
|
||||||
|
matches, err = BuildRandomSchedule(teams, scheduleBlocks, "test")
|
||||||
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestScheduleTiming(t *testing.T) {
|
func TestScheduleTiming(t *testing.T) {
|
||||||
|
|||||||
@@ -34,11 +34,13 @@ func ScheduleGetHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Generates the schedule and presents it for review without saving it to the database.
|
// Generates the schedule and presents it for review without saving it to the database.
|
||||||
func ScheduleGeneratePostHandler(w http.ResponseWriter, r *http.Request) {
|
func ScheduleGeneratePostHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
cachedMatchType = r.PostFormValue("matchType")
|
||||||
scheduleBlocks, err := getScheduleBlocks(r)
|
scheduleBlocks, err := getScheduleBlocks(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
renderSchedule(w, r, "Incomplete or invalid schedule block parameters specified.")
|
renderSchedule(w, r, "Incomplete or invalid schedule block parameters specified.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
cachedScheduleBlocks = scheduleBlocks
|
||||||
|
|
||||||
// Build the schedule.
|
// Build the schedule.
|
||||||
teams, err := db.GetAllTeams()
|
teams, err := db.GetAllTeams()
|
||||||
@@ -61,6 +63,7 @@ func ScheduleGeneratePostHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
renderSchedule(w, r, fmt.Sprintf("Error generating schedule: %s.", err.Error()))
|
renderSchedule(w, r, fmt.Sprintf("Error generating schedule: %s.", err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
cachedMatches = matches
|
||||||
|
|
||||||
// Determine each team's first match.
|
// Determine each team's first match.
|
||||||
teamFirstMatches := make(map[int]string)
|
teamFirstMatches := make(map[int]string)
|
||||||
@@ -78,11 +81,8 @@ func ScheduleGeneratePostHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
checkTeam(match.Blue2)
|
checkTeam(match.Blue2)
|
||||||
checkTeam(match.Blue3)
|
checkTeam(match.Blue3)
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedMatchType = r.PostFormValue("matchType")
|
|
||||||
cachedScheduleBlocks = scheduleBlocks
|
|
||||||
cachedMatches = matches
|
|
||||||
cachedTeamFirstMatches = teamFirstMatches
|
cachedTeamFirstMatches = teamFirstMatches
|
||||||
|
|
||||||
http.Redirect(w, r, "/setup/schedule", 302)
|
http.Redirect(w, r, "/setup/schedule", 302)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user