From 037df786b7f4a7e35b77ec934333a5ac09a5de06 Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Tue, 16 Aug 2022 20:03:51 -0700 Subject: [PATCH] Make playoff round ordering sequential and increasing. --- bracket/bracket.go | 8 +-- bracket/bracket_test.go | 40 ++++++------ bracket/single_elimination.go | 114 +++++++++++++++++----------------- model/match.go | 2 +- 4 files changed, 80 insertions(+), 84 deletions(-) diff --git a/bracket/bracket.go b/bracket/bracket.go index 33b66d6..4dec370 100644 --- a/bracket/bracket.go +++ b/bracket/bracket.go @@ -18,7 +18,7 @@ type Bracket struct { const ElimMatchSpacingSec = 600 // Creates an unpopulated bracket with a format that is defined by the given matchup templates and number of alliances. -func newBracket(matchupTemplates []matchupTemplate, numAlliances int) (*Bracket, error) { +func newBracket(matchupTemplates []matchupTemplate, finalsMatchupKey matchupKey, numAlliances int) (*Bracket, error) { // Create a map of matchup templates by key for easy lookup while creating the bracket. matchupTemplateMap := make(map[matchupKey]matchupTemplate, len(matchupTemplates)) for _, matchupTemplate := range matchupTemplates { @@ -27,11 +27,7 @@ func newBracket(matchupTemplates []matchupTemplate, numAlliances int) (*Bracket, // Recursively build the bracket, starting with the finals matchup. finalsMatchup, _, err := createMatchupGraph( - newMatchupKey(1, 1), - true, - matchupTemplateMap, - numAlliances, - make(map[matchupKey]*Matchup), + finalsMatchupKey, true, matchupTemplateMap, numAlliances, make(map[matchupKey]*Matchup), ) if err != nil { return nil, err diff --git a/bracket/bracket_test.go b/bracket/bracket_test.go index 1ed7ae6..e35f99e 100644 --- a/bracket/bracket_test.go +++ b/bracket/bracket_test.go @@ -12,9 +12,9 @@ import ( ) func TestNewBracketErrors(t *testing.T) { - _, err := newBracket([]matchupTemplate{}, 8) + _, err := newBracket([]matchupTemplate{}, newMatchupKey(33, 12), 8) if assert.NotNil(t, err) { - assert.Equal(t, "could not find template for matchup {round:1 group:1} in the list of templates", err.Error()) + assert.Equal(t, "could not find template for matchup {round:33 group:12} in the list of templates", err.Error()) } matchTemplate := matchupTemplate{ @@ -22,7 +22,7 @@ func TestNewBracketErrors(t *testing.T) { redAllianceSource: allianceSource{allianceId: 1}, blueAllianceSource: newWinnerAllianceSource(2, 2), } - _, err = newBracket([]matchupTemplate{matchTemplate}, 8) + _, err = newBracket([]matchupTemplate{matchTemplate}, newMatchupKey(1, 1), 8) if assert.NotNil(t, err) { assert.Equal(t, "both alliances must be populated either from selection or a lower round", err.Error()) } @@ -33,17 +33,24 @@ func TestNewBracketInverseSeeding(t *testing.T) { matchupTemplates := []matchupTemplate{ { matchupKey: newMatchupKey(1, 1), - displayNameFormat: "F-${instance}", + displayNameFormat: "QF${group}-${instance}", numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(2, 1), - blueAllianceSource: newWinnerAllianceSource(2, 2), + redAllianceSource: allianceSource{allianceId: 8}, + blueAllianceSource: allianceSource{allianceId: 1}, + }, + { + matchupKey: newMatchupKey(1, 2), + displayNameFormat: "QF${group}-${instance}", + numWinsToAdvance: 2, + redAllianceSource: allianceSource{allianceId: 5}, + blueAllianceSource: allianceSource{allianceId: 4}, }, { matchupKey: newMatchupKey(2, 1), displayNameFormat: "SF${group}-${instance}", numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(4, 2), - blueAllianceSource: newWinnerAllianceSource(4, 1), + redAllianceSource: newWinnerAllianceSource(1, 2), + blueAllianceSource: newWinnerAllianceSource(1, 1), }, { matchupKey: newMatchupKey(2, 2), @@ -53,23 +60,16 @@ func TestNewBracketInverseSeeding(t *testing.T) { blueAllianceSource: allianceSource{allianceId: 2}, }, { - matchupKey: newMatchupKey(4, 1), - displayNameFormat: "SF${group}-${instance}", + matchupKey: newMatchupKey(3, 1), + displayNameFormat: "F-${instance}", numWinsToAdvance: 2, - redAllianceSource: allianceSource{allianceId: 8}, - blueAllianceSource: allianceSource{allianceId: 1}, - }, - { - matchupKey: newMatchupKey(4, 2), - displayNameFormat: "SF${group}-${instance}", - numWinsToAdvance: 2, - redAllianceSource: allianceSource{allianceId: 5}, - blueAllianceSource: allianceSource{allianceId: 4}, + redAllianceSource: newWinnerAllianceSource(2, 1), + blueAllianceSource: newWinnerAllianceSource(2, 2), }, } tournament.CreateTestAlliances(database, 2) - bracket, err := newBracket(matchupTemplates, 2) + bracket, err := newBracket(matchupTemplates, newMatchupKey(3, 1), 2) assert.Nil(t, err) assert.Nil(t, bracket.Update(database, &dummyStartTime)) matches, err := database.GetMatchesByType("elimination") diff --git a/bracket/single_elimination.go b/bracket/single_elimination.go index b5b32af..01616d2 100644 --- a/bracket/single_elimination.go +++ b/bracket/single_elimination.go @@ -16,113 +16,113 @@ func NewSingleEliminationBracket(numAlliances int) (*Bracket, error) { if numAlliances > 16 { return nil, fmt.Errorf("Must have at most 16 alliances") } - return newBracket(singleEliminationBracketMatchupTemplates, numAlliances) + return newBracket(singleEliminationBracketMatchupTemplates, newMatchupKey(4, 1), numAlliances) } var singleEliminationBracketMatchupTemplates = []matchupTemplate{ { matchupKey: newMatchupKey(1, 1), - displayNameFormat: "F-${instance}", - numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(2, 1), - blueAllianceSource: newWinnerAllianceSource(2, 2), - }, - { - matchupKey: newMatchupKey(2, 1), - displayNameFormat: "SF${group}-${instance}", - numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(4, 1), - blueAllianceSource: newWinnerAllianceSource(4, 2), - }, - { - matchupKey: newMatchupKey(2, 2), - displayNameFormat: "SF${group}-${instance}", - numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(4, 3), - blueAllianceSource: newWinnerAllianceSource(4, 4), - }, - { - matchupKey: newMatchupKey(4, 1), - displayNameFormat: "QF${group}-${instance}", - numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(8, 1), - blueAllianceSource: newWinnerAllianceSource(8, 2), - }, - { - matchupKey: newMatchupKey(4, 2), - displayNameFormat: "QF${group}-${instance}", - numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(8, 3), - blueAllianceSource: newWinnerAllianceSource(8, 4), - }, - { - matchupKey: newMatchupKey(4, 3), - displayNameFormat: "QF${group}-${instance}", - numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(8, 5), - blueAllianceSource: newWinnerAllianceSource(8, 6), - }, - { - matchupKey: newMatchupKey(4, 4), - displayNameFormat: "QF${group}-${instance}", - numWinsToAdvance: 2, - redAllianceSource: newWinnerAllianceSource(8, 7), - blueAllianceSource: newWinnerAllianceSource(8, 8), - }, - { - matchupKey: newMatchupKey(8, 1), displayNameFormat: "EF${group}-${instance}", numWinsToAdvance: 2, redAllianceSource: allianceSource{allianceId: 1}, blueAllianceSource: allianceSource{allianceId: 16}, }, { - matchupKey: newMatchupKey(8, 2), + matchupKey: newMatchupKey(1, 2), displayNameFormat: "EF${group}-${instance}", numWinsToAdvance: 2, redAllianceSource: allianceSource{allianceId: 8}, blueAllianceSource: allianceSource{allianceId: 9}, }, { - matchupKey: newMatchupKey(8, 3), + matchupKey: newMatchupKey(1, 3), displayNameFormat: "EF${group}-${instance}", numWinsToAdvance: 2, redAllianceSource: allianceSource{allianceId: 4}, blueAllianceSource: allianceSource{allianceId: 13}, }, { - matchupKey: newMatchupKey(8, 4), + matchupKey: newMatchupKey(1, 4), displayNameFormat: "EF${group}-${instance}", numWinsToAdvance: 2, redAllianceSource: allianceSource{allianceId: 5}, blueAllianceSource: allianceSource{allianceId: 12}, }, { - matchupKey: newMatchupKey(8, 5), + matchupKey: newMatchupKey(1, 5), displayNameFormat: "EF${group}-${instance}", numWinsToAdvance: 2, redAllianceSource: allianceSource{allianceId: 2}, blueAllianceSource: allianceSource{allianceId: 15}, }, { - matchupKey: newMatchupKey(8, 6), + matchupKey: newMatchupKey(1, 6), displayNameFormat: "EF${group}-${instance}", numWinsToAdvance: 2, redAllianceSource: allianceSource{allianceId: 7}, blueAllianceSource: allianceSource{allianceId: 10}, }, { - matchupKey: newMatchupKey(8, 7), + matchupKey: newMatchupKey(1, 7), displayNameFormat: "EF${group}-${instance}", numWinsToAdvance: 2, redAllianceSource: allianceSource{allianceId: 3}, blueAllianceSource: allianceSource{allianceId: 14}, }, { - matchupKey: newMatchupKey(8, 8), + matchupKey: newMatchupKey(1, 8), displayNameFormat: "EF${group}-${instance}", numWinsToAdvance: 2, redAllianceSource: allianceSource{allianceId: 6}, blueAllianceSource: allianceSource{allianceId: 11}, }, + { + matchupKey: newMatchupKey(2, 1), + displayNameFormat: "QF${group}-${instance}", + numWinsToAdvance: 2, + redAllianceSource: newWinnerAllianceSource(1, 1), + blueAllianceSource: newWinnerAllianceSource(1, 2), + }, + { + matchupKey: newMatchupKey(2, 2), + displayNameFormat: "QF${group}-${instance}", + numWinsToAdvance: 2, + redAllianceSource: newWinnerAllianceSource(1, 3), + blueAllianceSource: newWinnerAllianceSource(1, 4), + }, + { + matchupKey: newMatchupKey(2, 3), + displayNameFormat: "QF${group}-${instance}", + numWinsToAdvance: 2, + redAllianceSource: newWinnerAllianceSource(1, 5), + blueAllianceSource: newWinnerAllianceSource(1, 6), + }, + { + matchupKey: newMatchupKey(2, 4), + displayNameFormat: "QF${group}-${instance}", + numWinsToAdvance: 2, + redAllianceSource: newWinnerAllianceSource(1, 7), + blueAllianceSource: newWinnerAllianceSource(1, 8), + }, + { + matchupKey: newMatchupKey(3, 1), + displayNameFormat: "SF${group}-${instance}", + numWinsToAdvance: 2, + redAllianceSource: newWinnerAllianceSource(2, 1), + blueAllianceSource: newWinnerAllianceSource(2, 2), + }, + { + matchupKey: newMatchupKey(3, 2), + displayNameFormat: "SF${group}-${instance}", + numWinsToAdvance: 2, + redAllianceSource: newWinnerAllianceSource(2, 3), + blueAllianceSource: newWinnerAllianceSource(2, 4), + }, + { + matchupKey: newMatchupKey(4, 1), + displayNameFormat: "F-${instance}", + numWinsToAdvance: 2, + redAllianceSource: newWinnerAllianceSource(3, 1), + blueAllianceSource: newWinnerAllianceSource(3, 2), + }, } diff --git a/model/match.go b/model/match.go index 6cc7819..93a9aaf 100644 --- a/model/match.go +++ b/model/match.go @@ -122,7 +122,7 @@ func (database *Database) GetMatchesByType(matchType string) ([]Match, error) { } return matchingMatches[i].ElimInstance < matchingMatches[j].ElimInstance } - return matchingMatches[i].ElimRound > matchingMatches[j].ElimRound + return matchingMatches[i].ElimRound < matchingMatches[j].ElimRound }) return matchingMatches, nil }