From 57ee7ff3ef634f754945c701fd4ae7336290d53b Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Sat, 12 Aug 2023 10:39:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ranking.List=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=BB=98=E8=AE=A4=E5=80=BC=E7=9A=84=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=8E=92=E5=90=8D=E5=92=8C=E5=88=86=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/ranking/list.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/game/ranking/list.go b/game/ranking/list.go index 346aea5..9693687 100644 --- a/game/ranking/list.go +++ b/game/ranking/list.go @@ -87,6 +87,16 @@ func (slf *List[CompetitorID, Score]) Size() int { return slf.competitors.Size() } +// GetRankDefault 获取竞争者排名,如果竞争者不存在则返回默认值 +// - 排名从 0 开始 +func (slf *List[CompetitorID, Score]) GetRankDefault(competitorId CompetitorID, defaultValue int) int { + rank, err := slf.GetRank(competitorId) + if err != nil { + return defaultValue + } + return rank +} + // GetRank 获取竞争者排名 // - 排名从 0 开始 func (slf *List[CompetitorID, Score]) GetRank(competitorId CompetitorID) (int, error) { @@ -160,6 +170,15 @@ func (slf *List[CompetitorID, Score]) GetScore(competitorId CompetitorID) (score return data, nil } +// GetScoreDefault 获取竞争者成绩,不存在时返回默认值 +func (slf *List[CompetitorID, Score]) GetScoreDefault(competitorId CompetitorID, defaultValue Score) Score { + score, err := slf.GetScore(competitorId) + if err != nil { + return defaultValue + } + return score +} + // GetAllCompetitor 获取所有竞争者ID // - 结果为名次有序的 func (slf *List[CompetitorID, Score]) GetAllCompetitor() []CompetitorID {