leaderboard

package
v0.0.0-...-97bb505 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Leaderboard

type Leaderboard struct {
	// contains filtered or unexported fields
}

Leaderboard knows how to interact with top users

func New

func New(topUserGetter TopUserGetter, topScoreNotifier TopScoreNotifier) *Leaderboard

New creates a new Leaderboard ready to do leaderboard things

Accept interfaces, return implementations!

func (*Leaderboard) NotifyTopPlayers

func (l *Leaderboard) NotifyTopPlayers(ctx context.Context, top int) error

NotifyTopPlayers will send a notification to the top X players

type TopScoreNotifier

type TopScoreNotifier interface {
	NotifyTopScore(ctx context.Context, id string, score int) error
}

TopScoreNotifier notifies users they have a high score

As above, focus on how this feels to read in terms of understanding the code in this package.

type TopUserGetter

type TopUserGetter interface {
	GetTopUsers(ctx context.Context, count int) ([]*db.User, error)
}

TopUserGetter gets top users from somewhere

Notice that we're tying ourselves to the database package here, but we'll live with it. The important part here is that this is just a tiny subsection of the full database functionality, because this is all a leaderboard needs to care about.

Think about how it feels to read this and what it tells you about what the code in this package does compared to what it would be like seeing the full Database functionality at Leaderboard's disposal. When we declare our interface locally like this, it makes the intent MUCH clearer to the reader/maintainer!

Jump to

Keyboard shortcuts

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