From 454316401f831d8a64d0fe441156b3c4254bc6f1 Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Sat, 6 Sep 2014 16:25:09 -0700 Subject: [PATCH] Fixed bug with elimination teams not percolating correctly when the outcome of a round is edited. --- elimination_schedule.go | 17 ++++++++++--- elimination_schedule_test.go | 48 +++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/elimination_schedule.go b/elimination_schedule.go index 90f7955..49957ab 100644 --- a/elimination_schedule.go +++ b/elimination_schedule.go @@ -113,11 +113,13 @@ func (database *Database) buildEliminationMatchSet(round int, group int, numAlli } var unplayedMatches []*Match for _, match := range matches { - // Update the teams in the match if they are not yet set. - if match.Red1 == 0 && len(redAlliance) != 0 { + // Update the teams in the match if they are not yet set or are incorrect. + if len(redAlliance) != 0 && !(teamInAlliance(match.Red1, redAlliance) && + teamInAlliance(match.Red2, redAlliance) && teamInAlliance(match.Red3, redAlliance)) { shuffleRedTeams(&match, redAlliance) database.SaveMatch(&match) - } else if match.Blue1 == 0 && len(blueAlliance) != 0 { + } else if len(blueAlliance) != 0 && !(teamInAlliance(match.Blue1, blueAlliance) && + teamInAlliance(match.Blue2, blueAlliance) && teamInAlliance(match.Blue3, blueAlliance)) { shuffleBlueTeams(&match, blueAlliance) database.SaveMatch(&match) } @@ -229,3 +231,12 @@ func shuffleBlueTeams(match *Match, alliance []AllianceTeam) { match.Blue2 = alliance[shuffle[1]].TeamId match.Blue3 = alliance[shuffle[2]].TeamId } + +func teamInAlliance(teamId int, alliance []AllianceTeam) bool { + for _, allianceTeam := range alliance { + if teamId == allianceTeam.TeamId { + return true + } + } + return false +} diff --git a/elimination_schedule_test.go b/elimination_schedule_test.go index 9fb44ae..9a5d1ce 100644 --- a/elimination_schedule_test.go +++ b/elimination_schedule_test.go @@ -649,9 +649,6 @@ func TestEliminationScheduleDetermineWinner(t *testing.T) { assert.Equal(t, 1, winner[0].TeamId) } } - db.TruncateAllianceTeams() - db.TruncateMatches() - db.TruncateMatchResults() } func TestEliminationScheduleRemoveUnneededMatches(t *testing.T) { @@ -680,6 +677,51 @@ func TestEliminationScheduleRemoveUnneededMatches(t *testing.T) { } } +func TestEliminationScheduleChangePreviousRoundResult(t *testing.T) { + clearDb() + defer clearDb() + db, err := OpenDatabase(testDbPath) + assert.Nil(t, err) + defer db.Close() + + createTestAlliances(db, 4) + _, err = db.UpdateEliminationSchedule(time.Unix(0, 0)) + assert.Nil(t, err) + scoreMatch(db, "SF2-1", "R") + scoreMatch(db, "SF2-2", "B") + scoreMatch(db, "SF2-3", "R") + _, err = db.UpdateEliminationSchedule(time.Unix(0, 0)) + assert.Nil(t, err) + scoreMatch(db, "SF2-3", "B") + _, err = db.UpdateEliminationSchedule(time.Unix(0, 0)) + assert.Nil(t, err) + matches, err := db.GetMatchesByType("elimination") + assert.Nil(t, err) + if assert.Equal(t, 9, len(matches)) { + assertMatch(t, matches[6], "F-1", 0, 3) + assertMatch(t, matches[7], "F-2", 0, 3) + assertMatch(t, matches[8], "F-3", 0, 3) + } + + scoreMatch(db, "SF1-1", "R") + scoreMatch(db, "SF1-2", "R") + _, err = db.UpdateEliminationSchedule(time.Unix(0, 0)) + assert.Nil(t, err) + scoreMatch(db, "SF1-2", "B") + _, err = db.UpdateEliminationSchedule(time.Unix(0, 0)) + assert.Nil(t, err) + scoreMatch(db, "SF1-3", "B") + _, err = db.UpdateEliminationSchedule(time.Unix(0, 0)) + assert.Nil(t, err) + matches, err = db.GetMatchesByType("elimination") + assert.Nil(t, err) + if assert.Equal(t, 9, len(matches)) { + assertMatch(t, matches[6], "F-1", 4, 3) + assertMatch(t, matches[7], "F-2", 4, 3) + assertMatch(t, matches[8], "F-3", 4, 3) + } +} + func TestEliminationScheduleUnscoredMatch(t *testing.T) { clearDb() defer clearDb()