From 99e87cef6ed8243292dd119dd0fad3f9bf971e76 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sun, 15 Aug 2021 09:59:36 +0200 Subject: [PATCH] Fixed bug where adding scores would truncate the last score even when there is less than ten highscores (the current maximum). --- score.go | 6 +++++- score_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/score.go b/score.go index 5d2ca83..824ce27 100644 --- a/score.go +++ b/score.go @@ -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 } diff --git a/score_test.go b/score_test.go index e8f8159..a0f66fa 100644 --- a/score_test.go +++ b/score_test.go @@ -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)