Fixed bug where adding scores would truncate the last score even when there is less than ten highscores (the current maximum).

This commit is contained in:
Sander Schobers 2021-08-15 09:59:36 +02:00
parent 88e6fc4181
commit 99e87cef6e
2 changed files with 23 additions and 1 deletions

View File

@ -23,7 +23,11 @@ func (h Highscores) AddScore(score, difficulty int) (Highscores, bool) {
return append(h, highscore), true
}
if rank < 10 {
return append(h[:rank], append([]Score{highscore}, h[rank:highscores-1]...)...), true
h = append(h[:rank], append([]Score{highscore}, h[rank:highscores]...)...)
if len(h) > 10 {
h = h[:10]
}
return h, true
}
return h, false
}

View File

@ -21,6 +21,16 @@ func newFullHighscore() Highscores {
}
}
func newHalfEmptyHighscore() Highscores {
return Highscores{
NewScore(100, 100),
NewScore(90, 90),
NewScore(80, 80),
NewScore(70, 70),
NewScore(60, 60),
}
}
func TestAddScoreBelowBottom(t *testing.T) {
h := newFullHighscore()
updated, high := h.AddScore(1, 1)
@ -61,6 +71,14 @@ func TestAddScoreMiddle(t *testing.T) {
assert.Equal(t, 51, updated[5].Score)
}
func TestAddScoreMiddleNotFull(t *testing.T) {
h := newHalfEmptyHighscore()
updated, high := h.AddScore(71, 71)
assert.True(t, high)
assert.Len(t, updated, 6)
assert.Equal(t, 71, updated[3].Score)
}
func TestAddScoreTop(t *testing.T) {
h := newFullHighscore()
updated, high := h.AddScore(101, 101)