Skip to content

duysmile/goleaderboard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goleaderboard

From Vietnam with <3 Go Reference

Package to make your own leaderboard in simple way

What is it?

A leaderboard written in Go using Redis database

Features

  • Ranking members by score
  • Members with same score will have the same rank
  • Get around members of a member with specific order
  • Can create multiple leaderboards by name

Installation

Install by using go get

go get github.com/duysmile/goleaderboard

How to use

Create a new leaderboard

rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})
leaderboard := goleaderboard.NewLeaderBoard(rdb, "test", &goleaderboard.Options{
	AllowSameRank: false,
	LifeTime: 0,
})

Add a member with id and score

leaderboard.AddMember(ctx, "P4", 2)

Get rank of a member by id

rank, _ := leaderboard.GetRank(ctx, "P4")
fmt.Println("rank of member:", fmt.Sprintf("#%v", rank))

List members by rank

list, cursor, _ := leaderboard.List(ctx, 0, 10, goleaderboard.OrderDesc)

// you can choose the order you want
// for example: 
// list, cursor, _ := leaderboard.List(ctx, 0, 10, goleaderboard.OrderAsc)

Get around of a member

list, cursor _ := leaderboard.GetAround(ctx, "P4", 4, goleaderboard.OrderDesc)

Contribution

All your contributions to project and make it better, they are welcome. Feel free to start an issue.

License

@2022, DuyN. Released under the MIT License