Fixed bug in scheduling with excess matches.

This commit is contained in:
Patrick Fairbank
2014-06-24 23:56:21 -07:00
parent 9600cfa815
commit fc922fe618
3 changed files with 15 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ package main
import (
"encoding/csv"
"fmt"
"math"
"math/rand"
"os"
"strconv"
@@ -29,6 +30,10 @@ func BuildRandomSchedule(teams []Team, scheduleBlocks []ScheduleBlock, matchType
numTeams := len(teams)
numMatches := countMatches(scheduleBlocks)
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))
if err != nil {
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.
matchIndex := 0
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)
matchIndex++
}

View File

@@ -67,6 +67,11 @@ func TestScheduleTeams(t *testing.T) {
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,
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) {

View File

@@ -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.
func ScheduleGeneratePostHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
cachedMatchType = r.PostFormValue("matchType")
scheduleBlocks, err := getScheduleBlocks(r)
if err != nil {
renderSchedule(w, r, "Incomplete or invalid schedule block parameters specified.")
return
}
cachedScheduleBlocks = scheduleBlocks
// Build the schedule.
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()))
return
}
cachedMatches = matches
// Determine each team's first match.
teamFirstMatches := make(map[int]string)
@@ -78,11 +81,8 @@ func ScheduleGeneratePostHandler(w http.ResponseWriter, r *http.Request) {
checkTeam(match.Blue2)
checkTeam(match.Blue3)
}
cachedMatchType = r.PostFormValue("matchType")
cachedScheduleBlocks = scheduleBlocks
cachedMatches = matches
cachedTeamFirstMatches = teamFirstMatches
http.Redirect(w, r, "/setup/schedule", 302)
}