Fix bug with committing playoff matches after a team substitution.

This commit is contained in:
Patrick Fairbank
2019-10-07 21:41:43 -07:00
parent 6f6fa9ba78
commit c0e3f16144
4 changed files with 41 additions and 2 deletions

View File

@@ -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,

View File

@@ -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))

View File

@@ -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

View File

@@ -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)