Documentation ¶
Index ¶
- Variables
- type BinarySearch
- func (slf *BinarySearch[CompetitorID, Score]) Clear()
- func (slf *BinarySearch[CompetitorID, Score]) Cmp(s1, s2 Score) int
- func (slf *BinarySearch[CompetitorID, Score]) Competitor(competitorId CompetitorID, score Score)
- func (slf *BinarySearch[CompetitorID, Score]) GetAllCompetitor() []CompetitorID
- func (slf *BinarySearch[CompetitorID, Score]) GetCompetitor(rank int) (competitorId CompetitorID, err error)
- func (slf *BinarySearch[CompetitorID, Score]) GetCompetitorWithRange(start, end int) ([]CompetitorID, error)
- func (slf *BinarySearch[CompetitorID, Score]) GetRank(competitorId CompetitorID) (int, error)
- func (slf *BinarySearch[CompetitorID, Score]) GetRankDefault(competitorId CompetitorID, defaultValue int) int
- func (slf *BinarySearch[CompetitorID, Score]) GetScore(competitorId CompetitorID) (score Score, err error)
- func (slf *BinarySearch[CompetitorID, Score]) GetScoreDefault(competitorId CompetitorID, defaultValue Score) Score
- func (slf *BinarySearch[CompetitorID, Score]) MarshalJSON() ([]byte, error)
- func (slf *BinarySearch[CompetitorID, Score]) OnRankChangeEvent(competitorId CompetitorID, oldRank, newRank int, oldScore, newScore Score)
- func (slf *BinarySearch[CompetitorID, Score]) OnRankClearBeforeEvent()
- func (slf *BinarySearch[CompetitorID, Score]) RegRankChangeEvent(handle BinarySearchRankChangeEventHandle[CompetitorID, Score])
- func (slf BinarySearch) RegRankChangeEventHandle(handle BinarySearchRankChangeEventHandle[CompetitorID, Score])
- func (slf *BinarySearch[CompetitorID, Score]) RegRankClearBeforeEvent(handle BinarySearchRankClearBeforeEventHandle[CompetitorID, Score])
- func (slf BinarySearch) RegRankClearBeforeEventHandle(handle BinarySearchRankClearBeforeEventHandle[CompetitorID, Score])
- func (slf *BinarySearch[CompetitorID, Score]) RemoveCompetitor(competitorId CompetitorID)
- func (slf *BinarySearch[CompetitorID, Score]) Size() int
- func (slf *BinarySearch[CompetitorID, Score]) UnmarshalJSON(bytes []byte) error
- type BinarySearchOption
- type BinarySearchRankChangeEventHandle
- type BinarySearchRankClearBeforeEventHandle
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type BinarySearch ¶
type BinarySearch[CompetitorID comparable, Score generic.Ordered] struct { // contains filtered or unexported fields }
func NewBinarySearch ¶
func NewBinarySearch[CompetitorID comparable, Score generic.Ordered](options ...BinarySearchOption[CompetitorID, Score]) *BinarySearch[CompetitorID, Score]
NewBinarySearch 创建一个基于内存的二分查找排行榜
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/game/leaderboard" ) func main() { bs := leaderboard.NewBinarySearch[string, int](leaderboard.WithBinarySearchCount[string, int](10)) fmt.Println(bs != nil) }
Output: true
func (*BinarySearch[CompetitorID, Score]) Clear ¶
func (slf *BinarySearch[CompetitorID, Score]) Clear()
Clear 清空排行榜
func (*BinarySearch[CompetitorID, Score]) Cmp ¶
func (slf *BinarySearch[CompetitorID, Score]) Cmp(s1, s2 Score) int
func (*BinarySearch[CompetitorID, Score]) Competitor ¶
func (slf *BinarySearch[CompetitorID, Score]) Competitor(competitorId CompetitorID, score Score)
Competitor 声明排行榜竞争者
- 如果竞争者存在的情况下,会更新已有成绩,否则新增竞争者
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/game/leaderboard" ) func main() { bs := leaderboard.NewBinarySearch[string, int](leaderboard.WithBinarySearchCount[string, int](10)) scores := []int{6131, 132, 5133, 134, 135, 136, 137, 138, 139, 140, 222, 333, 444, 555, 666} for i := 1; i <= 15; i++ { bs.Competitor(fmt.Sprintf("competitor_%2d", i), scores[i-1]) } for rank, competitor := range bs.GetAllCompetitor() { fmt.Println(rank, competitor) } }
Output: 0 competitor_ 1 1 competitor_ 3 2 competitor_15 3 competitor_14 4 competitor_13 5 competitor_12 6 competitor_11 7 competitor_10 8 competitor_ 9 9 competitor_ 8
func (*BinarySearch[CompetitorID, Score]) GetAllCompetitor ¶
func (slf *BinarySearch[CompetitorID, Score]) GetAllCompetitor() []CompetitorID
GetAllCompetitor 获取所有竞争者ID
- 结果为名次有序的
func (*BinarySearch[CompetitorID, Score]) GetCompetitor ¶
func (slf *BinarySearch[CompetitorID, Score]) GetCompetitor(rank int) (competitorId CompetitorID, err error)
GetCompetitor 获取特定排名的竞争者
func (*BinarySearch[CompetitorID, Score]) GetCompetitorWithRange ¶
func (slf *BinarySearch[CompetitorID, Score]) GetCompetitorWithRange(start, end int) ([]CompetitorID, error)
GetCompetitorWithRange 获取第start名到第end名竞争者
func (*BinarySearch[CompetitorID, Score]) GetRank ¶
func (slf *BinarySearch[CompetitorID, Score]) GetRank(competitorId CompetitorID) (int, error)
GetRank 获取竞争者排名
- 排名从 0 开始
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/game/leaderboard" ) func main() { bs := leaderboard.NewBinarySearch[string, int](leaderboard.WithBinarySearchCount[string, int](10)) scores := []int{6131, 132, 5133, 134, 135, 136, 137, 138, 139, 140, 222, 333, 444, 555, 666} for i := 1; i <= 15; i++ { bs.Competitor(fmt.Sprintf("competitor_%2d", i), scores[i-1]) } fmt.Println(bs.GetRank("competitor_ 1")) }
Output: 0 <nil>
func (*BinarySearch[CompetitorID, Score]) GetRankDefault ¶
func (slf *BinarySearch[CompetitorID, Score]) GetRankDefault(competitorId CompetitorID, defaultValue int) int
GetRankDefault 获取竞争者排名,如果竞争者不存在则返回默认值
- 排名从 0 开始
func (*BinarySearch[CompetitorID, Score]) GetScore ¶
func (slf *BinarySearch[CompetitorID, Score]) GetScore(competitorId CompetitorID) (score Score, err error)
GetScore 获取竞争者成绩
func (*BinarySearch[CompetitorID, Score]) GetScoreDefault ¶
func (slf *BinarySearch[CompetitorID, Score]) GetScoreDefault(competitorId CompetitorID, defaultValue Score) Score
GetScoreDefault 获取竞争者成绩,不存在时返回默认值
func (*BinarySearch[CompetitorID, Score]) MarshalJSON ¶
func (slf *BinarySearch[CompetitorID, Score]) MarshalJSON() ([]byte, error)
func (*BinarySearch[CompetitorID, Score]) OnRankChangeEvent ¶
func (slf *BinarySearch[CompetitorID, Score]) OnRankChangeEvent(competitorId CompetitorID, oldRank, newRank int, oldScore, newScore Score)
func (*BinarySearch[CompetitorID, Score]) OnRankClearBeforeEvent ¶
func (slf *BinarySearch[CompetitorID, Score]) OnRankClearBeforeEvent()
func (*BinarySearch[CompetitorID, Score]) RegRankChangeEvent ¶
func (slf *BinarySearch[CompetitorID, Score]) RegRankChangeEvent(handle BinarySearchRankChangeEventHandle[CompetitorID, Score])
func (BinarySearch) RegRankChangeEventHandle ¶
func (slf BinarySearch) RegRankChangeEventHandle(handle BinarySearchRankChangeEventHandle[CompetitorID, Score])
RegRankChangeEventHandle 注册排行榜变更事件
func (*BinarySearch[CompetitorID, Score]) RegRankClearBeforeEvent ¶
func (slf *BinarySearch[CompetitorID, Score]) RegRankClearBeforeEvent(handle BinarySearchRankClearBeforeEventHandle[CompetitorID, Score])
func (BinarySearch) RegRankClearBeforeEventHandle ¶
func (slf BinarySearch) RegRankClearBeforeEventHandle(handle BinarySearchRankClearBeforeEventHandle[CompetitorID, Score])
RegRankClearBeforeEventHandle 注册排行榜清空前事件
func (*BinarySearch[CompetitorID, Score]) RemoveCompetitor ¶
func (slf *BinarySearch[CompetitorID, Score]) RemoveCompetitor(competitorId CompetitorID)
RemoveCompetitor 删除特定竞争者
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/game/leaderboard" ) func main() { bs := leaderboard.NewBinarySearch[string, int](leaderboard.WithBinarySearchCount[string, int](10)) scores := []int{6131, 132, 5133, 134, 135, 136, 137, 138, 139, 140, 222, 333, 444, 555, 666} for i := 1; i <= 15; i++ { bs.Competitor(fmt.Sprintf("competitor_%2d", i), scores[i-1]) } bs.RemoveCompetitor("competitor_ 1") for rank, competitor := range bs.GetAllCompetitor() { fmt.Println(rank, competitor) } }
Output: 0 competitor_ 3 1 competitor_15 2 competitor_14 3 competitor_13 4 competitor_12 5 competitor_11 6 competitor_10 7 competitor_ 9 8 competitor_ 8
func (*BinarySearch[CompetitorID, Score]) Size ¶
func (slf *BinarySearch[CompetitorID, Score]) Size() int
Size 获取竞争者数量
func (*BinarySearch[CompetitorID, Score]) UnmarshalJSON ¶
func (slf *BinarySearch[CompetitorID, Score]) UnmarshalJSON(bytes []byte) error
type BinarySearchOption ¶
type BinarySearchOption[CompetitorID comparable, Score generic.Ordered] func(list *BinarySearch[CompetitorID, Score])
func WithBinarySearchASC ¶
func WithBinarySearchASC[CompetitorID comparable, Score generic.Ordered]() BinarySearchOption[CompetitorID, Score]
WithBinarySearchASC 通过升序的方式创建排行榜
- 默认情况下为降序
func WithBinarySearchCount ¶
func WithBinarySearchCount[CompetitorID comparable, Score generic.Ordered](rankCount int) BinarySearchOption[CompetitorID, Score]
WithBinarySearchCount 通过限制排行榜竞争者数量来创建排行榜
- 默认情况下允许100位竞争者
type BinarySearchRankChangeEventHandle ¶
type BinarySearchRankChangeEventHandle[CompetitorID comparable, Score generic.Ordered] func(leaderboard *BinarySearch[CompetitorID, Score], competitorId CompetitorID, oldRank, newRank int, oldScore, newScore Score)
type BinarySearchRankClearBeforeEventHandle ¶
type BinarySearchRankClearBeforeEventHandle[CompetitorID comparable, Score generic.Ordered] func(leaderboard *BinarySearch[CompetitorID, Score])
Click to show internal directories.
Click to hide internal directories.