Skip to content

Commit

Permalink
Add disqualification state distinct from a red card (closes #131).
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed Jun 1, 2024
1 parent 5d283e6 commit aaae019
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
4 changes: 2 additions & 2 deletions model/match_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func (matchResult *MatchResult) BlueScoreSummary() *game.ScoreSummary {
func (matchResult *MatchResult) CorrectPlayoffScore() {
matchResult.RedScore.PlayoffDq = false
for _, card := range matchResult.RedCards {
if card == "red" {
if card == "red" || card == "dq" {
matchResult.RedScore.PlayoffDq = true
}
}
for _, card := range matchResult.BlueCards {
if card == "red" {
if card == "red" || card == "dq" {
matchResult.BlueScore.PlayoffDq = true
}
}
Expand Down
6 changes: 6 additions & 0 deletions templates/edit_match_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ <h6 class="fw-bold mb-2">Notes in Traps</h6>
Red
</label>
</div>
<div class="col-lg-2">
<label>
<input type="radio" name="{{"{{alliance}}"}}Team{{"{{team"}}{{$i}}{{"}}"}}Card" value="dq">
DQ
</label>
</div>
</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions tournament/qualification_rankings.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ func CalculateTeamCards(database *model.Database, matchType model.MatchType) err

// Mark the team as having a yellow card if they got either a yellow or red in a previous match.
for teamId, card := range matchResult.RedCards {
if team, ok := teamsMap[teamId]; ok && card != "" {
if team, ok := teamsMap[teamId]; ok && (card == "red" || card == "yellow") {
team.YellowCard = true
teamsMap[teamId] = team
}
}
for teamId, card := range matchResult.BlueCards {
if team, ok := teamsMap[teamId]; ok && card != "" {
if team, ok := teamsMap[teamId]; ok && (card == "red" || card == "yellow") {
team.YellowCard = true
teamsMap[teamId] = team
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func addMatchResultToRankings(
cards = matchResult.BlueCards
}
disqualified := false
if card, ok := cards[strconv.Itoa(teamId)]; ok && card == "red" {
if card, ok := cards[strconv.Itoa(teamId)]; ok && (card == "red" || card == "dq") {
disqualified = true
}

Expand Down
19 changes: 19 additions & 0 deletions tournament/qualification_rankings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ func TestCalculateRankings(t *testing.T) {

}

func TestAddMatchResultToRankingsHandleCards(t *testing.T) {
rankings := map[int]*game.Ranking{}
matchResult := model.BuildTestMatchResult(1, 1)
matchResult.RedCards = map[string]string{"1": "yellow", "2": "red", "3": "dq"}
matchResult.BlueCards = map[string]string{"4": "red", "5": "dq", "6": "yellow"}
addMatchResultToRankings(rankings, 1, matchResult, true)
addMatchResultToRankings(rankings, 2, matchResult, true)
addMatchResultToRankings(rankings, 3, matchResult, true)
addMatchResultToRankings(rankings, 4, matchResult, false)
addMatchResultToRankings(rankings, 5, matchResult, false)
addMatchResultToRankings(rankings, 6, matchResult, false)
assert.Equal(t, 0, rankings[1].Disqualifications)
assert.Equal(t, 1, rankings[2].Disqualifications)
assert.Equal(t, 1, rankings[3].Disqualifications)
assert.Equal(t, 1, rankings[4].Disqualifications)
assert.Equal(t, 1, rankings[5].Disqualifications)
assert.Equal(t, 0, rankings[6].Disqualifications)
}

// Sets up a schedule and results that touches on all possible variables.
func setupMatchResultsForRankings(database *model.Database) {
match1 := model.Match{Type: model.Qualification, TypeOrder: 1, Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5,
Expand Down
45 changes: 37 additions & 8 deletions web/match_play_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,56 @@ func TestCommitCards(t *testing.T) {
web := setupTestWeb(t)

// Check that a yellow card sticks with a team.
team := &model.Team{Id: 5}
web.arena.Database.CreateTeam(team)
team1 := &model.Team{Id: 3}
team2 := &model.Team{Id: 5}
web.arena.Database.CreateTeam(team1)
web.arena.Database.CreateTeam(team2)
match := &model.Match{Id: 0, Type: model.Qualification, Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5, Blue3: 6}
assert.Nil(t, web.arena.Database.CreateMatch(match))
matchResult := model.NewMatchResult()
matchResult.MatchId = match.Id
matchResult.RedCards = map[string]string{"3": "yellow"}
matchResult.BlueCards = map[string]string{"5": "yellow"}
err := web.commitMatchScore(match, matchResult, true)
assert.Nil(t, err)
team, _ = web.arena.Database.GetTeamById(5)
assert.True(t, team.YellowCard)
team1, _ = web.arena.Database.GetTeamById(3)
assert.True(t, team1.YellowCard)
team2, _ = web.arena.Database.GetTeamById(5)
assert.True(t, team2.YellowCard)

// Check that editing a match result removes a yellow card from a team.
matchResult = model.NewMatchResult()
matchResult.MatchId = match.Id
err = web.commitMatchScore(match, matchResult, true)
assert.Nil(t, err)
team, _ = web.arena.Database.GetTeamById(5)
assert.False(t, team.YellowCard)
team1, _ = web.arena.Database.GetTeamById(3)
assert.False(t, team1.YellowCard)
team2, _ = web.arena.Database.GetTeamById(5)
assert.False(t, team2.YellowCard)

// Check that a red card causes a yellow card to stick with a team.
matchResult = model.NewMatchResult()
matchResult.MatchId = match.Id
matchResult.RedCards = map[string]string{"3": "red"}
matchResult.BlueCards = map[string]string{"5": "red"}
err = web.commitMatchScore(match, matchResult, true)
assert.Nil(t, err)
team, _ = web.arena.Database.GetTeamById(5)
assert.True(t, team.YellowCard)
team1, _ = web.arena.Database.GetTeamById(3)
assert.True(t, team1.YellowCard)
team2, _ = web.arena.Database.GetTeamById(5)
assert.True(t, team2.YellowCard)

// Check that a DQ does not cause a yellow card to stick with a team.
matchResult = model.NewMatchResult()
matchResult.MatchId = match.Id
matchResult.RedCards = map[string]string{"3": "dq"}
matchResult.BlueCards = map[string]string{"5": "dq"}
err = web.commitMatchScore(match, matchResult, true)
assert.Nil(t, err)
team1, _ = web.arena.Database.GetTeamById(3)
assert.False(t, team1.YellowCard)
team2, _ = web.arena.Database.GetTeamById(5)
assert.False(t, team2.YellowCard)

// Check that a red card in playoffs zeroes out the score.
tournament.CreateTestAlliances(web.arena.Database, 2)
Expand All @@ -235,6 +257,13 @@ func TestCommitCards(t *testing.T) {
assert.Nil(t, web.commitMatchScore(match, matchResult, true))
assert.Equal(t, 0, matchResult.RedScoreSummary().Score)
assert.NotEqual(t, 0, matchResult.BlueScoreSummary().Score)

// Check that a DQ in playoffs zeroes out the score.
matchResult.RedCards = map[string]string{}
matchResult.BlueCards = map[string]string{"5": "dq"}
assert.Nil(t, web.commitMatchScore(match, matchResult, true))
assert.NotEqual(t, 0, matchResult.RedScoreSummary().Score)
assert.Equal(t, 0, matchResult.BlueScoreSummary().Score)
}

func TestMatchPlayWebsocketCommands(t *testing.T) {
Expand Down

0 comments on commit aaae019

Please sign in to comment.