From df4aa3074321d6f0aa192c0b7a4c083f6e5b5265 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Fri, 12 May 2023 20:20:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8E=92=E5=90=8D=E6=94=B9?= =?UTF-8?q?=E5=8F=98=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/builtin/ranking_list.go | 13 +++++++++++++ game/ranking_list.go | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/game/builtin/ranking_list.go b/game/builtin/ranking_list.go index 63738d4..dcf0b38 100644 --- a/game/builtin/ranking_list.go +++ b/game/builtin/ranking_list.go @@ -2,6 +2,7 @@ package builtin import ( "encoding/json" + "github.com/kercylan98/minotaur/game" "github.com/kercylan98/minotaur/utils/generic" "github.com/kercylan98/minotaur/utils/synchronization" ) @@ -23,6 +24,8 @@ type RankingList[CompetitorID comparable, Score generic.Ordered] struct { rankCount int competitors *synchronization.Map[CompetitorID, Score] scores []*scoreItem[CompetitorID, Score] // CompetitorID, Score + + rankChangeEventHandles []game.RankChangeEventHandle[CompetitorID, Score] } type scoreItem[CompetitorID comparable, Score generic.Ordered] struct { @@ -257,3 +260,13 @@ func (slf *RankingList[CompetitorID, Score]) MarshalJSON() ([]byte, error) { return json.Marshal(&t) } + +func (slf *RankingList[CompetitorID, Score]) RegRankChangeEvent(handle game.RankChangeEventHandle[CompetitorID, Score]) { + slf.rankChangeEventHandles = append(slf.rankChangeEventHandles, handle) +} + +func (slf *RankingList[CompetitorID, Score]) OnRankChangeEvent(competitorId CompetitorID, oldRank, newRank int, oldScore, newScore Score) { + for _, handle := range slf.rankChangeEventHandles { + handle(competitorId, oldRank, newRank, oldScore, newScore) + } +} diff --git a/game/ranking_list.go b/game/ranking_list.go index 8489db7..3841567 100644 --- a/game/ranking_list.go +++ b/game/ranking_list.go @@ -27,4 +27,12 @@ type RankingList[CompetitorID comparable, Score generic.Ordered] interface { GetAllCompetitor() []CompetitorID // Clear 清空排行榜 Clear() + + // RegRankChangeEvent 排名改变时将立即执行注册的事件处理函数 + RegRankChangeEvent(handle RankChangeEventHandle[CompetitorID, Score]) + OnRankChangeEvent(competitorId CompetitorID, oldRank, newRank int, oldScore, newScore Score) } + +type ( + RankChangeEventHandle[CompetitorID comparable, Score generic.Ordered] func(competitorId CompetitorID, oldRank, newRank int, oldScore, newScore Score) +)