mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 13:46:44 -04:00
Refactor to merge Match.Status and Match.Winner fields into one.
This commit is contained in:
@@ -33,7 +33,7 @@ func UpdateEliminationSchedule(database *model.Database, startTime time.Time) (b
|
||||
}
|
||||
matchIndex := 0
|
||||
for _, match := range matches {
|
||||
if match.Status == "complete" {
|
||||
if match.IsComplete() {
|
||||
continue
|
||||
}
|
||||
match.Time = startTime.Add(time.Duration(matchIndex*ElimMatchSpacingSec) * time.Second)
|
||||
@@ -152,7 +152,7 @@ func buildEliminationMatchSet(database *model.Database, round int, group int,
|
||||
}
|
||||
var unplayedMatches []*model.Match
|
||||
for _, match := range matches {
|
||||
if match.Status != "complete" {
|
||||
if !match.IsComplete() {
|
||||
// Update the teams in the match if they are not yet set or are incorrect.
|
||||
if len(redAlliance) != 0 && !(match.Red1 == redAlliance[0].TeamId && match.Red2 == redAlliance[1].TeamId &&
|
||||
match.Red3 == redAlliance[2].TeamId) {
|
||||
@@ -183,16 +183,16 @@ func buildEliminationMatchSet(database *model.Database, round int, group int,
|
||||
}
|
||||
|
||||
// Check who won.
|
||||
switch match.Winner {
|
||||
case "R":
|
||||
switch match.Status {
|
||||
case model.RedWonMatch:
|
||||
redWins += 1
|
||||
case "B":
|
||||
case model.BlueWonMatch:
|
||||
blueWins += 1
|
||||
case "T":
|
||||
case model.TieMatch:
|
||||
ties = append(ties, &match)
|
||||
default:
|
||||
return []model.AllianceTeam{}, fmt.Errorf("Completed match %d has invalid winner '%s'", match.Id,
|
||||
match.Winner)
|
||||
match.Status)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -432,8 +432,8 @@ func TestEliminationSchedulePopulatePartialMatch(t *testing.T) {
|
||||
// Final should be updated after semifinal is concluded.
|
||||
CreateTestAlliances(database, 3)
|
||||
UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "SF2-1", "B")
|
||||
scoreMatch(database, "SF2-2", "B")
|
||||
scoreMatch(database, "SF2-1", model.BlueWonMatch)
|
||||
scoreMatch(database, "SF2-2", model.BlueWonMatch)
|
||||
_, err := UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, err := database.GetMatchesByType("elimination")
|
||||
@@ -450,8 +450,8 @@ func TestEliminationSchedulePopulatePartialMatch(t *testing.T) {
|
||||
// Final should be generated and populated as both semifinals conclude.
|
||||
CreateTestAlliances(database, 4)
|
||||
UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "SF2-1", "R")
|
||||
scoreMatch(database, "SF2-2", "R")
|
||||
scoreMatch(database, "SF2-1", model.RedWonMatch)
|
||||
scoreMatch(database, "SF2-2", model.RedWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, err = database.GetMatchesByType("elimination")
|
||||
@@ -461,8 +461,8 @@ func TestEliminationSchedulePopulatePartialMatch(t *testing.T) {
|
||||
assertMatch(t, matches[6], "F-2", 0, 2)
|
||||
assertMatch(t, matches[7], "F-3", 0, 2)
|
||||
}
|
||||
scoreMatch(database, "SF1-1", "R")
|
||||
scoreMatch(database, "SF1-2", "R")
|
||||
scoreMatch(database, "SF1-1", model.RedWonMatch)
|
||||
scoreMatch(database, "SF1-2", model.RedWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, err = database.GetMatchesByType("elimination")
|
||||
@@ -482,22 +482,22 @@ func TestEliminationScheduleCreateNextRound(t *testing.T) {
|
||||
|
||||
CreateTestAlliances(database, 4)
|
||||
UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "SF1-1", "B")
|
||||
scoreMatch(database, "SF1-1", model.BlueWonMatch)
|
||||
_, err := UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, _ := database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 6, len(matches))
|
||||
scoreMatch(database, "SF2-1", "B")
|
||||
scoreMatch(database, "SF2-1", model.BlueWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 6, len(matches))
|
||||
scoreMatch(database, "SF1-2", "B")
|
||||
scoreMatch(database, "SF1-2", model.BlueWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 8, len(matches))
|
||||
scoreMatch(database, "SF2-2", "B")
|
||||
scoreMatch(database, "SF2-2", model.BlueWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
@@ -514,19 +514,19 @@ func TestEliminationScheduleDetermineWinner(t *testing.T) {
|
||||
// Round with one tie and a sweep.
|
||||
CreateTestAlliances(database, 2)
|
||||
UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "F-1", "T")
|
||||
scoreMatch(database, "F-1", model.TieMatch)
|
||||
won, err := UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
matches, _ := database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 3, len(matches))
|
||||
scoreMatch(database, "F-2", "B")
|
||||
scoreMatch(database, "F-2", model.BlueWonMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 3, len(matches))
|
||||
scoreMatch(database, "F-3", "B")
|
||||
scoreMatch(database, "F-3", model.BlueWonMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
if assert.Nil(t, err) {
|
||||
assert.True(t, won)
|
||||
@@ -540,30 +540,30 @@ func TestEliminationScheduleDetermineWinner(t *testing.T) {
|
||||
// Round with one tie and a split.
|
||||
CreateTestAlliances(database, 2)
|
||||
UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "F-1", "R")
|
||||
scoreMatch(database, "F-1", model.RedWonMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 3, len(matches))
|
||||
scoreMatch(database, "F-2", "T")
|
||||
scoreMatch(database, "F-2", model.TieMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 3, len(matches))
|
||||
scoreMatch(database, "F-3", "B")
|
||||
scoreMatch(database, "F-3", model.BlueWonMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 4, len(matches))
|
||||
assert.Equal(t, "F-4", matches[3].DisplayName)
|
||||
scoreMatch(database, "F-4", "T")
|
||||
scoreMatch(database, "F-4", model.TieMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
scoreMatch(database, "F-5", "R")
|
||||
scoreMatch(database, "F-5", model.RedWonMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
if assert.Nil(t, err) {
|
||||
assert.True(t, won)
|
||||
@@ -575,19 +575,19 @@ func TestEliminationScheduleDetermineWinner(t *testing.T) {
|
||||
// Round with two ties.
|
||||
CreateTestAlliances(database, 2)
|
||||
UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "F-1", "T")
|
||||
scoreMatch(database, "F-1", model.TieMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 3, len(matches))
|
||||
scoreMatch(database, "F-2", "B")
|
||||
scoreMatch(database, "F-2", model.BlueWonMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 3, len(matches))
|
||||
scoreMatch(database, "F-3", "T")
|
||||
scoreMatch(database, "F-3", model.TieMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, won)
|
||||
@@ -595,7 +595,7 @@ func TestEliminationScheduleDetermineWinner(t *testing.T) {
|
||||
assert.Equal(t, 5, len(matches))
|
||||
assert.Equal(t, "F-4", matches[3].DisplayName)
|
||||
assert.Equal(t, "F-5", matches[4].DisplayName)
|
||||
scoreMatch(database, "F-4", "B")
|
||||
scoreMatch(database, "F-4", model.BlueWonMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
if assert.Nil(t, err) {
|
||||
assert.True(t, won)
|
||||
@@ -607,17 +607,17 @@ func TestEliminationScheduleDetermineWinner(t *testing.T) {
|
||||
// Round with repeated ties.
|
||||
CreateTestAlliances(database, 2)
|
||||
UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "F-1", "T")
|
||||
scoreMatch(database, "F-2", "T")
|
||||
scoreMatch(database, "F-3", "T")
|
||||
scoreMatch(database, "F-1", model.TieMatch)
|
||||
scoreMatch(database, "F-2", model.TieMatch)
|
||||
scoreMatch(database, "F-3", model.TieMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "F-4", "T")
|
||||
scoreMatch(database, "F-5", "T")
|
||||
scoreMatch(database, "F-6", "T")
|
||||
scoreMatch(database, "F-4", model.TieMatch)
|
||||
scoreMatch(database, "F-5", model.TieMatch)
|
||||
scoreMatch(database, "F-6", model.TieMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "F-7", "R")
|
||||
scoreMatch(database, "F-8", "B")
|
||||
scoreMatch(database, "F-9", "R")
|
||||
scoreMatch(database, "F-7", model.RedWonMatch)
|
||||
scoreMatch(database, "F-8", model.BlueWonMatch)
|
||||
scoreMatch(database, "F-9", model.RedWonMatch)
|
||||
won, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
if assert.Nil(t, err) {
|
||||
assert.True(t, won)
|
||||
@@ -629,15 +629,15 @@ func TestEliminationScheduleRemoveUnneededMatches(t *testing.T) {
|
||||
|
||||
CreateTestAlliances(database, 2)
|
||||
UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
scoreMatch(database, "F-1", "R")
|
||||
scoreMatch(database, "F-2", "R")
|
||||
scoreMatch(database, "F-1", model.RedWonMatch)
|
||||
scoreMatch(database, "F-2", model.RedWonMatch)
|
||||
_, err := UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, _ := database.GetMatchesByType("elimination")
|
||||
assert.Equal(t, 2, len(matches))
|
||||
|
||||
// Check that the deleted match is recreated if the score is changed.
|
||||
scoreMatch(database, "F-2", "B")
|
||||
scoreMatch(database, "F-2", model.BlueWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
@@ -652,12 +652,12 @@ func TestEliminationScheduleChangePreviousRoundResult(t *testing.T) {
|
||||
CreateTestAlliances(database, 4)
|
||||
_, err := UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
scoreMatch(database, "SF2-1", "R")
|
||||
scoreMatch(database, "SF2-2", "B")
|
||||
scoreMatch(database, "SF2-3", "R")
|
||||
scoreMatch(database, "SF2-1", model.RedWonMatch)
|
||||
scoreMatch(database, "SF2-2", model.BlueWonMatch)
|
||||
scoreMatch(database, "SF2-3", model.RedWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
scoreMatch(database, "SF2-3", "B")
|
||||
scoreMatch(database, "SF2-3", model.BlueWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, err := database.GetMatchesByType("elimination")
|
||||
@@ -668,14 +668,14 @@ func TestEliminationScheduleChangePreviousRoundResult(t *testing.T) {
|
||||
assertMatch(t, matches[8], "F-3", 0, 3)
|
||||
}
|
||||
|
||||
scoreMatch(database, "SF1-1", "R")
|
||||
scoreMatch(database, "SF1-2", "R")
|
||||
scoreMatch(database, "SF1-1", model.RedWonMatch)
|
||||
scoreMatch(database, "SF1-2", model.RedWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
scoreMatch(database, "SF1-2", "B")
|
||||
scoreMatch(database, "SF1-2", model.BlueWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
scoreMatch(database, "SF1-3", "B")
|
||||
scoreMatch(database, "SF1-3", model.BlueWonMatch)
|
||||
_, err = UpdateEliminationSchedule(database, time.Unix(0, 0))
|
||||
assert.Nil(t, err)
|
||||
matches, err = database.GetMatchesByType("elimination")
|
||||
@@ -714,8 +714,8 @@ func TestEliminationScheduleTiming(t *testing.T) {
|
||||
assert.True(t, time.Unix(3400, 0).Equal(matches[4].Time))
|
||||
assert.True(t, time.Unix(4000, 0).Equal(matches[5].Time))
|
||||
}
|
||||
scoreMatch(database, "SF1-1", "R")
|
||||
scoreMatch(database, "SF1-3", "B")
|
||||
scoreMatch(database, "SF1-1", model.RedWonMatch)
|
||||
scoreMatch(database, "SF1-3", model.BlueWonMatch)
|
||||
UpdateEliminationSchedule(database, time.Unix(5000, 0))
|
||||
matches, err = database.GetMatchesByType("elimination")
|
||||
assert.Nil(t, err)
|
||||
@@ -749,8 +749,8 @@ func TestEliminationScheduleTeamPositions(t *testing.T) {
|
||||
match2.Blue1, match2.Blue3 = match2.Blue3, match2.Blue1
|
||||
database.SaveMatch(&match1)
|
||||
database.SaveMatch(&match2)
|
||||
scoreMatch(database, "SF1-1", "R")
|
||||
scoreMatch(database, "SF2-1", "B")
|
||||
scoreMatch(database, "SF1-1", model.RedWonMatch)
|
||||
scoreMatch(database, "SF2-1", model.BlueWonMatch)
|
||||
UpdateEliminationSchedule(database, time.Unix(1000, 0))
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
if assert.Equal(t, 6, len(matches)) {
|
||||
@@ -765,8 +765,8 @@ func TestEliminationScheduleTeamPositions(t *testing.T) {
|
||||
}
|
||||
|
||||
// Advance them to the finals and verify that the team position updates have been propagated.
|
||||
scoreMatch(database, "SF1-2", "R")
|
||||
scoreMatch(database, "SF2-2", "B")
|
||||
scoreMatch(database, "SF1-2", model.RedWonMatch)
|
||||
scoreMatch(database, "SF2-2", model.BlueWonMatch)
|
||||
UpdateEliminationSchedule(database, time.Unix(5000, 0))
|
||||
matches, _ = database.GetMatchesByType("elimination")
|
||||
if assert.Equal(t, 7, len(matches)) {
|
||||
@@ -789,9 +789,8 @@ func assertMatch(t *testing.T, match model.Match, displayName string, redAllianc
|
||||
assert.Equal(t, blueAlliance, match.ElimBlueAlliance)
|
||||
}
|
||||
|
||||
func scoreMatch(database *model.Database, displayName string, winner string) {
|
||||
func scoreMatch(database *model.Database, displayName string, winner model.MatchStatus) {
|
||||
match, _ := database.GetMatchByName("elimination", displayName)
|
||||
match.Status = "complete"
|
||||
match.Winner = winner
|
||||
match.Status = winner
|
||||
database.SaveMatch(match)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func CalculateRankings(database *model.Database, preservePreviousRank bool) (gam
|
||||
}
|
||||
rankings := make(map[int]*game.Ranking)
|
||||
for _, match := range matches {
|
||||
if match.Status != "complete" {
|
||||
if !match.IsComplete() {
|
||||
continue
|
||||
}
|
||||
matchResult, err := database.GetMatchResultForMatch(match.Id)
|
||||
@@ -93,7 +93,7 @@ func CalculateTeamCards(database *model.Database, matchType string) error {
|
||||
return err
|
||||
}
|
||||
for _, match := range matches {
|
||||
if match.Status != "complete" {
|
||||
if !match.IsComplete() {
|
||||
continue
|
||||
}
|
||||
matchResult, err := database.GetMatchResultForMatch(match.Id)
|
||||
|
||||
@@ -85,21 +85,21 @@ func TestCalculateRankings(t *testing.T) {
|
||||
// Sets up a schedule and results that touches on all possible variables.
|
||||
func setupMatchResultsForRankings(database *model.Database) {
|
||||
match1 := model.Match{Type: "qualification", DisplayName: "1", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5,
|
||||
Blue3: 6, Status: "complete"}
|
||||
Blue3: 6, Status: model.RedWonMatch}
|
||||
database.CreateMatch(&match1)
|
||||
matchResult1 := model.BuildTestMatchResult(match1.Id, 1)
|
||||
matchResult1.RedCards = map[string]string{"2": "red"}
|
||||
database.CreateMatchResult(matchResult1)
|
||||
|
||||
match2 := model.Match{Type: "qualification", DisplayName: "2", Red1: 1, Red2: 3, Red3: 5, Blue1: 2, Blue2: 4,
|
||||
Blue3: 6, Status: "complete", Red2IsSurrogate: true, Blue3IsSurrogate: true}
|
||||
Blue3: 6, Status: model.BlueWonMatch, Red2IsSurrogate: true, Blue3IsSurrogate: true}
|
||||
database.CreateMatch(&match2)
|
||||
matchResult2 := model.BuildTestMatchResult(match2.Id, 1)
|
||||
matchResult2.BlueScore = matchResult2.RedScore
|
||||
database.CreateMatchResult(matchResult2)
|
||||
|
||||
match3 := model.Match{Type: "qualification", DisplayName: "3", Red1: 6, Red2: 5, Red3: 4, Blue1: 3, Blue2: 2,
|
||||
Blue3: 1, Status: "complete", Red3IsSurrogate: true}
|
||||
Blue3: 1, Status: model.TieMatch, Red3IsSurrogate: true}
|
||||
database.CreateMatch(&match3)
|
||||
matchResult3 := model.BuildTestMatchResult(match3.Id, 1)
|
||||
database.CreateMatchResult(matchResult3)
|
||||
@@ -109,19 +109,19 @@ func setupMatchResultsForRankings(database *model.Database) {
|
||||
database.CreateMatchResult(matchResult3)
|
||||
|
||||
match4 := model.Match{Type: "practice", DisplayName: "1", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5,
|
||||
Blue3: 6, Status: "complete"}
|
||||
Blue3: 6, Status: model.RedWonMatch}
|
||||
database.CreateMatch(&match4)
|
||||
matchResult4 := model.BuildTestMatchResult(match4.Id, 1)
|
||||
database.CreateMatchResult(matchResult4)
|
||||
|
||||
match5 := model.Match{Type: "elimination", DisplayName: "F-1", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5,
|
||||
Blue3: 6, Status: "complete"}
|
||||
Blue3: 6, Status: model.BlueWonMatch}
|
||||
database.CreateMatch(&match5)
|
||||
matchResult5 := model.BuildTestMatchResult(match5.Id, 1)
|
||||
database.CreateMatchResult(matchResult5)
|
||||
|
||||
match6 := model.Match{Type: "qualification", DisplayName: "4", Red1: 7, Red2: 8, Red3: 9, Blue1: 10, Blue2: 11,
|
||||
Blue3: 12, Status: ""}
|
||||
Blue3: 12, Status: model.MatchNotPlayed}
|
||||
database.CreateMatch(&match6)
|
||||
matchResult6 := model.BuildTestMatchResult(match6.Id, 1)
|
||||
database.CreateMatchResult(matchResult6)
|
||||
|
||||
Reference in New Issue
Block a user