diff --git a/game/builtin/ranking_list.go b/game/builtin/ranking_list.go index 299eec6..92ec9e7 100644 --- a/game/builtin/ranking_list.go +++ b/game/builtin/ranking_list.go @@ -44,7 +44,7 @@ func (slf *RankingList[CompetitorID, Score]) Competitor(competitorId CompetitorI } else { if slf.rankCount > 0 && len(slf.scores) >= slf.rankCount { last := slf.scores[len(slf.scores)-1] - if slf.Cmp(score, last[1]) <= 0 { + if slf.Cmp(score, last[1].(Score)) <= 0 { return } } @@ -74,6 +74,10 @@ func (slf *RankingList[CompetitorID, Score]) RemoveCompetitor(competitorId Compe slf.competitors.Delete(competitorId) } +func (slf *RankingList[CompetitorID, Score]) Size() int { + return slf.competitors.Size() +} + func (slf *RankingList[CompetitorID, Score]) GetRank(competitorId CompetitorID) (int, error) { competitorScore, exist := slf.competitors.GetExist(competitorId) if !exist { @@ -113,7 +117,7 @@ func (slf *RankingList[CompetitorID, Score]) GetCompetitor(rank int) (competitor if rank < 0 || rank >= len(slf.scores) { return competitorId, ErrRankingListNonexistentRanking } - return slf.scores[rank][0], nil + return slf.scores[rank][0].(CompetitorID), nil } func (slf *RankingList[CompetitorID, Score]) GetCompetitorWithRange(start, end int) ([]CompetitorID, error) { @@ -129,7 +133,7 @@ func (slf *RankingList[CompetitorID, Score]) GetCompetitorWithRange(start, end i } var ids []CompetitorID for _, data := range slf.scores[start-1 : end] { - ids = append(ids, data[0]) + ids = append(ids, data[0].(CompetitorID)) } return ids, nil } @@ -145,7 +149,7 @@ func (slf *RankingList[CompetitorID, Score]) GetScore(competitorId CompetitorID) func (slf *RankingList[CompetitorID, Score]) GetAllCompetitor() []CompetitorID { var result []CompetitorID for _, data := range slf.scores { - result = append(result, data[0]) + result = append(result, data[0].(CompetitorID)) } return result } @@ -175,13 +179,13 @@ func (slf *RankingList[CompetitorID, Score]) competitor(competitorId CompetitorI for low <= high { mid := (low + high) / 2 data := slf.scores[mid] - if slf.Cmp(data[1], score) == 0 { + if slf.Cmp(data[1].(Score), score) == 0 { for low = mid + 1; low <= high; low++ { - if slf.Cmp(slf.scores[low][1], score) != 0 { + if slf.Cmp(slf.scores[low][1].(Score), score) != 0 { break } } - } else if slf.Cmp(data[1], score) < 0 { + } else if slf.Cmp(data[1].(Score), score) < 0 { high = mid - 1 } else { low = mid + 1 @@ -215,6 +219,6 @@ func (slf *RankingList[CompetitorID, Score]) competitor(competitorId CompetitorI count = len(slf.scores) - 1 scoreItem = slf.scores[count] - slf.competitors.Delete(scoreItem[0]) + slf.competitors.Delete(scoreItem[0].(CompetitorID)) slf.scores = slf.scores[0:count] } diff --git a/game/builtin/ranking_list_test.go b/game/builtin/ranking_list_test.go new file mode 100644 index 0000000..d77b6c8 --- /dev/null +++ b/game/builtin/ranking_list_test.go @@ -0,0 +1,53 @@ +package builtin + +import ( + "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestNewRankingList(t *testing.T) { + convey.Convey("NewRankingList", t, func() { + convey.So(NewRankingList[string, int](), convey.ShouldNotEqual, nil) + convey.So(NewRankingList[string, int](WithRankingListCount[string, int](50)).rankCount, convey.ShouldEqual, 50) + convey.So(NewRankingList[string, int](WithRankingListASC[string, int]()).asc, convey.ShouldEqual, true) + }) +} + +func TestRankingList_Competitor(t *testing.T) { + convey.Convey("TestRankingList_Competitor", t, func() { + rankingList := NewRankingList[string, int]() + rankingList.Competitor("angle", 63) + rankingList.Competitor("jacky", 59) + rankingList.Competitor("dave", 86) + rankingList.Competitor("jacky", 42) + convey.So(rankingList.Size(), convey.ShouldEqual, 3) + }) + convey.Convey("TestRankingList_Competitor", t, func() { + rankingList := NewRankingList[string, int](WithRankingListCount[string, int](2)) + rankingList.Competitor("angle", 63) + rankingList.Competitor("jacky", 59) + rankingList.Competitor("dave", 86) + rankingList.Competitor("jacky", 42) + convey.So(rankingList.Size(), convey.ShouldEqual, 2) + one, err := rankingList.GetCompetitor(0) + convey.So(one, convey.ShouldEqual, "dave") + convey.So(err, convey.ShouldEqual, nil) + two, err := rankingList.GetCompetitor(1) + convey.So(two, convey.ShouldEqual, "angle") + convey.So(err, convey.ShouldEqual, nil) + }) +} + +func TestRankingList_CompetitorIncrease(t *testing.T) { + convey.Convey("TestRankingList_Competitor", t, func() { + rankingList := NewRankingList[string, int]() + rankingList.Competitor("angle", 63) + rankingList.Competitor("dave", 86) + rankingList.Competitor("jacky", 42) + rankingList.CompetitorIncrease("jacky", 100) + convey.So(rankingList.Size(), convey.ShouldEqual, 3) + one, err := rankingList.GetCompetitor(0) + convey.So(one, convey.ShouldEqual, "jacky") + convey.So(err, convey.ShouldEqual, nil) + }) +} diff --git a/game/ranking_list.go b/game/ranking_list.go index ba90076..d46bfd7 100644 --- a/game/ranking_list.go +++ b/game/ranking_list.go @@ -10,6 +10,8 @@ type RankingList[CompetitorID comparable, Score comparable] interface { CompetitorIncrease(competitorId CompetitorID, score Score) // RemoveCompetitor 删除特定竞争者 RemoveCompetitor(competitorId CompetitorID) + // Size 获取竞争者数量 + Size() int // GetRank 获取竞争者排名 GetRank(competitorId CompetitorID) (int, error) // GetCompetitor 获取特定排名的竞争者