Skip to content

Commit

Permalink
Updated scoring display.
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed May 31, 2015
1 parent dedd937 commit 369fadf
Show file tree
Hide file tree
Showing 14 changed files with 1,123 additions and 947 deletions.
4 changes: 3 additions & 1 deletion db/migrations/20140524180123_CreateMatches.sql
Expand Up @@ -5,6 +5,7 @@ CREATE TABLE matches (
displayname VARCHAR(16),
time DATETIME,
elimround int,
elimgroup int,
eliminstance int,
red1 int,
red1issurrogate bool,
Expand All @@ -19,7 +20,8 @@ CREATE TABLE matches (
blue3 int,
blue3issurrogate bool,
status VARCHAR(16),
startedat DATETIME
startedat DATETIME,
winner VARCHAR(16)
);
CREATE UNIQUE INDEX type_displayname ON matches(type, displayname);

Expand Down
29 changes: 21 additions & 8 deletions elimination_schedule.go
Expand Up @@ -44,7 +44,8 @@ func (database *Database) UpdateEliminationSchedule(startTime time.Time) ([]Alli
return winner, err
}

// Recursively traverses the elimination bracket downwards, creating matches as necessary.
// Recursively traverses the elimination bracket downwards, creating matches as necessary. Returns the winner
// of the given round if known.
func (database *Database) buildEliminationMatchSet(round int, group int, numAlliances int) ([]AllianceTeam, error) {
if numAlliances < 2 {
return []AllianceTeam{}, fmt.Errorf("Must have at least 2 alliances")
Expand Down Expand Up @@ -106,7 +107,7 @@ func (database *Database) buildEliminationMatchSet(round int, group int, numAlli
// Check if the match set exists already and if it has been won.
var redWins, blueWins, numIncomplete int
var ties []*Match
matches, err := database.GetMatchesByElimRound(round)
matches, err := database.GetMatchesByElimRoundGroup(round, group)
if err != nil {
return []AllianceTeam{}, err
}
Expand All @@ -128,6 +129,18 @@ func (database *Database) buildEliminationMatchSet(round int, group int, numAlli
numIncomplete += 1
continue
}

// Check who won.
switch match.Winner {
case "R":
redWins += 1
case "B":
blueWins += 1
case "T":
ties = append(ties, &match)
default:
return []AllianceTeam{}, fmt.Errorf("Completed match %d has invalid winner '%s'", match.Id, match.Winner)
}
}

// Delete any superfluous matches if the round is won.
Expand Down Expand Up @@ -161,19 +174,19 @@ func (database *Database) buildEliminationMatchSet(round int, group int, numAlli
return []AllianceTeam{}, fmt.Errorf("Alliances must consist of at least 3 teams")
}
if len(matches) < 1 {
err = database.CreateMatch(createMatch(roundName, round, 1, redAlliance, blueAlliance))
err = database.CreateMatch(createMatch(roundName, round, group, 1, redAlliance, blueAlliance))
if err != nil {
return []AllianceTeam{}, err
}
}
if len(matches) < 2 {
err = database.CreateMatch(createMatch(roundName, round, 2, redAlliance, blueAlliance))
err = database.CreateMatch(createMatch(roundName, round, group, 2, redAlliance, blueAlliance))
if err != nil {
return []AllianceTeam{}, err
}
}
if len(matches) < 3 {
err = database.CreateMatch(createMatch(roundName, round, 3, redAlliance, blueAlliance))
err = database.CreateMatch(createMatch(roundName, round, group, 3, redAlliance, blueAlliance))
if err != nil {
return []AllianceTeam{}, err
}
Expand All @@ -184,7 +197,7 @@ func (database *Database) buildEliminationMatchSet(round int, group int, numAlli
// personnel can reuse any tied matches without having to print new schedules.
if numIncomplete == 0 {
for index, tie := range ties {
match := createMatch(roundName, round, len(matches)+index+1, redAlliance, blueAlliance)
match := createMatch(roundName, round, group, len(matches)+index+1, redAlliance, blueAlliance)
match.Red1, match.Red2, match.Red3 = tie.Red1, tie.Red2, tie.Red3
match.Blue1, match.Blue2, match.Blue3 = tie.Blue1, tie.Blue2, tie.Blue3
err = database.CreateMatch(match)
Expand All @@ -198,9 +211,9 @@ func (database *Database) buildEliminationMatchSet(round int, group int, numAlli
}

// Creates a match at the given point in the elimination bracket and populates the teams.
func createMatch(roundName string, round int, instance int, redAlliance []AllianceTeam, blueAlliance []AllianceTeam) *Match {
func createMatch(roundName string, round int, group int, instance int, redAlliance []AllianceTeam, blueAlliance []AllianceTeam) *Match {
match := Match{Type: "elimination", DisplayName: fmt.Sprintf("%s-%d", roundName, instance),
ElimRound: round, ElimInstance: instance}
ElimRound: round, ElimGroup: group, ElimInstance: instance}
shuffleRedTeams(&match, redAlliance)
shuffleBlueTeams(&match, blueAlliance)
return &match
Expand Down

0 comments on commit 369fadf

Please sign in to comment.