From c0e3f16144bbc2ba7f35788e18d8f7fc4c40d264 Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Mon, 7 Oct 2019 21:41:43 -0700 Subject: [PATCH] Fix bug with committing playoff matches after a team substitution. --- tournament/elimination_schedule.go | 28 ++++++++++++++++++++++++++++ web/match_play.go | 9 +++++++++ web/match_play_test.go | 2 ++ web/match_review_test.go | 4 ++-- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/tournament/elimination_schedule.go b/tournament/elimination_schedule.go index c120085..35a9ec1 100644 --- a/tournament/elimination_schedule.go +++ b/tournament/elimination_schedule.go @@ -44,6 +44,34 @@ func UpdateEliminationSchedule(database *model.Database, startTime time.Time) (b return len(winner) > 0, err } +// Updates the alliance, if necessary, to include whoever played in the match, in case there was a substitute. +func UpdateAlliance(database *model.Database, matchTeamIds [3]int, allianceId int) error { + allianceTeams, err := database.GetTeamsByAlliance(allianceId) + if err != nil { + return err + } + + for _, teamId := range matchTeamIds { + found := false + for _, allianceTeam := range allianceTeams { + if teamId == allianceTeam.TeamId { + found = true + break + } + } + if !found { + newAllianceTeam := model.AllianceTeam{AllianceId: allianceId, PickPosition: len(allianceTeams), + TeamId: teamId} + allianceTeams = append(allianceTeams, newAllianceTeam) + if err := database.CreateAllianceTeam(&newAllianceTeam); err != nil { + return err + } + } + } + + return nil +} + // Recursively traverses the elimination bracket downwards, creating matches as necessary. Returns the winner // of the given round if known. func buildEliminationMatchSet(database *model.Database, round int, group int, diff --git a/web/match_play.go b/web/match_play.go index d5c7ed6..23df217 100644 --- a/web/match_play.go +++ b/web/match_play.go @@ -376,6 +376,15 @@ func (web *Web) commitMatchScore(match *model.Match, matchResult *model.MatchRes } if match.ShouldUpdateEliminationMatches() { + if err = tournament.UpdateAlliance(web.arena.Database, [3]int{match.Red1, match.Red2, match.Red3}, + match.ElimRedAlliance); err != nil { + return err + } + if err = tournament.UpdateAlliance(web.arena.Database, [3]int{match.Blue1, match.Blue2, match.Blue3}, + match.ElimBlueAlliance); err != nil { + return err + } + // Generate any subsequent elimination matches. isTournamentWon, err := tournament.UpdateEliminationSchedule(web.arena.Database, time.Now().Add(time.Second*tournament.ElimMatchSpacingSec)) diff --git a/web/match_play_test.go b/web/match_play_test.go index 301cc98..56eeeaa 100644 --- a/web/match_play_test.go +++ b/web/match_play_test.go @@ -228,6 +228,8 @@ func TestCommitCards(t *testing.T) { // Check that a red card in eliminations zeroes out the score. tournament.CreateTestAlliances(web.arena.Database, 2) match.Type = "elimination" + match.ElimRedAlliance = 1 + match.ElimBlueAlliance = 2 web.arena.Database.SaveMatch(match) matchResult = model.BuildTestMatchResult(match.Id, 10) matchResult.MatchType = match.Type diff --git a/web/match_review_test.go b/web/match_review_test.go index 4ae6b1c..e3f1260 100644 --- a/web/match_review_test.go +++ b/web/match_review_test.go @@ -39,7 +39,7 @@ func TestMatchReviewEditExistingResult(t *testing.T) { web := setupTestWeb(t) match := model.Match{Type: "elimination", DisplayName: "QF4-3", Status: "complete", Winner: "R", Red1: 1001, - Red2: 1002, Red3: 1003, Blue1: 1004, Blue2: 1005, Blue3: 1006} + Red2: 1002, Red3: 1003, Blue1: 1004, Blue2: 1005, Blue3: 1006, ElimRedAlliance: 1, ElimBlueAlliance: 2} web.arena.Database.CreateMatch(&match) matchResult := model.BuildTestMatchResult(match.Id, 1) matchResult.MatchType = match.Type @@ -79,7 +79,7 @@ func TestMatchReviewCreateNewResult(t *testing.T) { web := setupTestWeb(t) match := model.Match{Type: "elimination", DisplayName: "QF4-3", Status: "complete", Winner: "R", Red1: 1001, - Red2: 1002, Red3: 1003, Blue1: 1004, Blue2: 1005, Blue3: 1006} + Red2: 1002, Red3: 1003, Blue1: 1004, Blue2: 1005, Blue3: 1006, ElimRedAlliance: 1, ElimBlueAlliance: 2} web.arena.Database.CreateMatch(&match) tournament.CreateTestAlliances(web.arena.Database, 2)