ranking

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrListNotExistCompetitor = errors.New("ranking list not exist competitor")
	ErrListIndexErr           = errors.New("ranking list index error")
	ErrListNonexistentRanking = errors.New("nonexistent ranking")
)

Functions

This section is empty.

Types

type List

type List[CompetitorID comparable, Score generic.Ordered] struct {
	// contains filtered or unexported fields
}

func NewList

func NewList[CompetitorID comparable, Score generic.Ordered](options ...ListOption[CompetitorID, Score]) *List[CompetitorID, Score]

NewList 创建一个排名从 0 开始的排行榜

Example
package main

import (
	"fmt"
	"github.com/kercylan98/minotaur/game/ranking"
)

func main() {
	ranklingList := ranking.NewList[string, int](ranking.WithListCount[string, int](10))

	fmt.Println(ranklingList != nil)
}
Output:

true

func (*List[CompetitorID, Score]) Clear

func (slf *List[CompetitorID, Score]) Clear()

Clear 清空排行榜

func (*List[CompetitorID, Score]) Cmp

func (slf *List[CompetitorID, Score]) Cmp(s1, s2 Score) int

func (*List[CompetitorID, Score]) Competitor

func (slf *List[CompetitorID, Score]) Competitor(competitorId CompetitorID, score Score)

Competitor 声明排行榜竞争者

  • 如果竞争者存在的情况下,会更新已有成绩,否则新增竞争者
Example
package main

import (
	"fmt"
	"github.com/kercylan98/minotaur/game/ranking"
)

func main() {
	ranklingList := ranking.NewList[string, int](ranking.WithListCount[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++ {
		ranklingList.Competitor(fmt.Sprintf("competitor_%2d", i), scores[i-1])
	}

	for rank, competitor := range ranklingList.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 (*List[CompetitorID, Score]) GetAllCompetitor

func (slf *List[CompetitorID, Score]) GetAllCompetitor() []CompetitorID

GetAllCompetitor 获取所有竞争者ID

  • 结果为名次有序的

func (*List[CompetitorID, Score]) GetCompetitor

func (slf *List[CompetitorID, Score]) GetCompetitor(rank int) (competitorId CompetitorID, err error)

GetCompetitor 获取特定排名的竞争者

func (*List[CompetitorID, Score]) GetCompetitorWithRange

func (slf *List[CompetitorID, Score]) GetCompetitorWithRange(start, end int) ([]CompetitorID, error)

GetCompetitorWithRange 获取第start名到第end名竞争者

func (*List[CompetitorID, Score]) GetRank

func (slf *List[CompetitorID, Score]) GetRank(competitorId CompetitorID) (int, error)

GetRank 获取竞争者排名

  • 排名从 0 开始
Example
package main

import (
	"fmt"
	"github.com/kercylan98/minotaur/game/ranking"
)

func main() {
	ranklingList := ranking.NewList[string, int](ranking.WithListCount[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++ {
		ranklingList.Competitor(fmt.Sprintf("competitor_%2d", i), scores[i-1])
	}

	fmt.Println(ranklingList.GetRank("competitor_ 1"))

}
Output:

0 <nil>

func (*List[CompetitorID, Score]) GetRankDefault added in v0.0.27

func (slf *List[CompetitorID, Score]) GetRankDefault(competitorId CompetitorID, defaultValue int) int

GetRankDefault 获取竞争者排名,如果竞争者不存在则返回默认值

  • 排名从 0 开始

func (*List[CompetitorID, Score]) GetScore

func (slf *List[CompetitorID, Score]) GetScore(competitorId CompetitorID) (score Score, err error)

GetScore 获取竞争者成绩

func (*List[CompetitorID, Score]) GetScoreDefault added in v0.0.27

func (slf *List[CompetitorID, Score]) GetScoreDefault(competitorId CompetitorID, defaultValue Score) Score

GetScoreDefault 获取竞争者成绩,不存在时返回默认值

func (*List[CompetitorID, Score]) MarshalJSON

func (slf *List[CompetitorID, Score]) MarshalJSON() ([]byte, error)

func (*List[CompetitorID, Score]) OnRankChangeEvent

func (slf *List[CompetitorID, Score]) OnRankChangeEvent(competitorId CompetitorID, oldRank, newRank int, oldScore, newScore Score)

func (*List[CompetitorID, Score]) OnRankClearBeforeEvent

func (slf *List[CompetitorID, Score]) OnRankClearBeforeEvent()

func (*List[CompetitorID, Score]) RegRankChangeEvent

func (slf *List[CompetitorID, Score]) RegRankChangeEvent(handle RankChangeEventHandle[CompetitorID, Score])

func (List) RegRankChangeEventHandle

func (slf List) RegRankChangeEventHandle(handle RankChangeEventHandle[CompetitorID, Score])

RegRankChangeEventHandle 注册排行榜变更事件

func (*List[CompetitorID, Score]) RegRankClearBeforeEvent

func (slf *List[CompetitorID, Score]) RegRankClearBeforeEvent(handle RankClearBeforeEventHandle[CompetitorID, Score])

func (List) RegRankClearBeforeEventHandle

func (slf List) RegRankClearBeforeEventHandle(handle RankClearBeforeEventHandle[CompetitorID, Score])

RegRankClearBeforeEventHandle 注册排行榜清空前事件

func (*List[CompetitorID, Score]) RemoveCompetitor

func (slf *List[CompetitorID, Score]) RemoveCompetitor(competitorId CompetitorID)

RemoveCompetitor 删除特定竞争者

Example
package main

import (
	"fmt"
	"github.com/kercylan98/minotaur/game/ranking"
)

func main() {
	ranklingList := ranking.NewList[string, int](ranking.WithListCount[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++ {
		ranklingList.Competitor(fmt.Sprintf("competitor_%2d", i), scores[i-1])
	}
	ranklingList.RemoveCompetitor("competitor_ 1")
	for rank, competitor := range ranklingList.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 (*List[CompetitorID, Score]) Size

func (slf *List[CompetitorID, Score]) Size() int

Size 获取竞争者数量

func (*List[CompetitorID, Score]) UnmarshalJSON

func (slf *List[CompetitorID, Score]) UnmarshalJSON(bytes []byte) error

type ListOption

type ListOption[CompetitorID comparable, Score generic.Ordered] func(list *List[CompetitorID, Score])

func WithListASC

func WithListASC[CompetitorID comparable, Score generic.Ordered]() ListOption[CompetitorID, Score]

WithListASC 通过升序的方式创建排行榜

  • 默认情况下为降序

func WithListCount

func WithListCount[CompetitorID comparable, Score generic.Ordered](rankCount int) ListOption[CompetitorID, Score]

WithListCount 通过限制排行榜竞争者数量来创建排行榜

  • 默认情况下允许100位竞争者

type RankChangeEventHandle

type RankChangeEventHandle[CompetitorID comparable, Score generic.Ordered] func(list *List[CompetitorID, Score], competitorId CompetitorID, oldRank, newRank int, oldScore, newScore Score)

type RankClearBeforeEventHandle

type RankClearBeforeEventHandle[CompetitorID comparable, Score generic.Ordered] func(list *List[CompetitorID, Score])

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL