Skip to content

Commit

Permalink
新增排名改变事件
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed May 12, 2023
1 parent 9a1e631 commit df4aa30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions game/builtin/ranking_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
8 changes: 8 additions & 0 deletions game/ranking_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

0 comments on commit df4aa30

Please sign in to comment.