新增排名改变事件
This commit is contained in:
parent
9a1e63193e
commit
df4aa30743
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue