Skip to content

Commit

Permalink
fix: hint for no votes (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin authored Jul 7, 2024
1 parent 76e2658 commit 9e8356e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/game/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
// Rejection reasons
varietyOfVotesIsTooHigh = "No strong consensus among the players"
maximumDeviationIsTooHigh = "Maximum deviation threshold exceeded"
notEnoughVotes = "Nobody voted 🤷‍"
// Advices
descriptionBingo = "BINGO! 🎉💃"
descriptionGoodJob = "Good job 😎"
Expand All @@ -50,6 +51,14 @@ func GetResultHint(deck protocol.Deck, issueVotes protocol.IssueVotes) (*protoco
return nil, err
}

if len(indexes) == 0 {
return &protocol.Hint{
Acceptable: false,
Value: "",
Description: notEnoughVotes,
}, nil
}

// Calculate measures for the votes
resultMeasures := getMeasures(indexes)
medianValueIndex := resultMeasures.median
Expand Down Expand Up @@ -110,6 +119,12 @@ func getMeasures(values []int) hintMeasurements {
// median value
r.median = median(values)

if r.median < 0 {
r.maxDeviation = 0
r.meanDeviation = 0
return r
}

// Maximum deviation
r.maxDeviation = 0
for _, v := range values {
Expand Down
6 changes: 6 additions & 0 deletions pkg/game/hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ func TestHint(t *testing.T) {
measurements: hintMeasurements{median: 3, meanDeviation: 1, maxDeviation: 2},
expectedHint: protocol.Hint{Acceptable: false, Value: "5", Description: varietyOfVotesIsTooHigh},
},
{
// This also tests round up median when even number of votes
values: []protocol.VoteValue{},
measurements: hintMeasurements{median: -1, meanDeviation: 0, maxDeviation: 0},
expectedHint: protocol.Hint{Acceptable: false, Value: "", Description: notEnoughVotes},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 9e8356e

Please sign in to comment.