scores

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: May 24, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HighScore

type HighScore struct {
	// GameID is the identifier of the game that we're tracking the score for.
	GameID string `json:"gameID"`
	// PlayerName is the handle of the player who got the score. In a *real* system, this would
	// likely be a PlayerID instead, but to keep this example a bit more simple, I didn't want to
	// add a third service into the mix. But you would treat it exactly like the game service if
	// you wanted to track player data separately.
	PlayerName string `json:"playerName"`
	// Score is the numeric score that the player attained. This score may mean different things for
	// different games. For Tetris, it is the actual score, but for something like Dark Souls maybe it
	// is how many New Game + runs did you complete before dying.
	Score uint64 `json:"score"`
	// Date is the date/time that the player attained this score.
	Date time.Time `json:"date"`
}

HighScore tracks a single high score that a player achieved for a specific game. A single player can have multiple instances of this for the same game since they can continue to beat their scores.

type HighScoreList

type HighScoreList []HighScore

HighScoreList is a slice of high scores with sorting and sub-slicing behavior baked in.

func (HighScoreList) Len

func (scores HighScoreList) Len() int

Len returns the number of scores in this list.

func (HighScoreList) Less

func (scores HighScoreList) Less(i, j int) bool

Less returns true when the score at index 'i' is actually greater than the one at index 'j' since we sort the list from highest score to lowest.

func (HighScoreList) Swap

func (scores HighScoreList) Swap(i, j int)

Swap provides 'sort' package support by switching the values at indices i and j.

func (HighScoreList) Top

func (scores HighScoreList) Top(howMany int) HighScoreList

Top returns the first "n" high scores in the list. If the list is shorter than 'howMany' then this will return the entire list.

type HighScoresForGameRequest

type HighScoresForGameRequest struct {
	// GameID is the identifier of the game that we're looking up scores for.
	GameID string `json:"gameID"`
	// HowMany limits the results to just the top N scores. (Default=5)
	HowMany int `json:"howMany"`
}

HighScoresForGameRequest contains the attributes used to filter high scores for a specific game.

func (HighScoresForGameRequest) Limit

func (req HighScoresForGameRequest) Limit() int

Limit decides if we should use the default list size of 5 or if we should use the 'HowMany' you specified in the request to cap the resulting high score list.

type HighScoresForGameResponse

type HighScoresForGameResponse struct {
	Scores []HighScore `json:"scores"`
}

HighScoresForGameResponse contains the list of high scores for the given game.

type NewHighScoreRequest

type NewHighScoreRequest struct {
	// GameID is the identifier of the game that we're tracking the score for.
	GameID string `json:"gameID"`
	// PlayerName is the handle of the player who got the score. In a *real* system, this would
	// likely be a PlayerID instead, but to keep this example a bit more simple, I didn't want to
	// add a third service into the mix. But you would treat it exactly like the game service if
	// you wanted to track player data separately.
	PlayerName string `json:"playerName"`
	// Score is the numeric score that the player attained. This score may mean different things for
	// different games. For Tetris, it is the actual score, but for something like Dark Souls maybe it
	// is how many New Game + runs did you complete before dying.
	Score uint64 `json:"score"`
}

NewHighScoreRequest captures all of the data fields required to post a new high score.

type NewHighScoreResponse

type NewHighScoreResponse HighScore

NewHighScoreResponse is the complete record of the high score that you just posted.

type Repo

type Repo interface {
	// Create adds a high score record to the store.
	Create(ctx context.Context, score HighScore) (HighScore, error)
	// ByGame searches for a list of all scores posted for the given game.
	ByGame(ctx context.Context, gameID string) (HighScoreList, error)
}

Repo manages access to the data store where we keep high score data.

func NewRepo

func NewRepo() Repo

NewRepo constructs a new mock repo that stores high score data in-memory. Not what you'd use in production, but it serves us well for this example code.

type ScoreService

type ScoreService interface {
	// NewHighScore captures a player's high score for the given game.
	//
	// HTTP 201
	// POST /game/:GameID/highscore
	NewHighScore(context.Context, *NewHighScoreRequest) (*NewHighScoreResponse, error)

	// HighScoresForGame fetches the top "N" high scores achieved by any player
	// for the specified game. If you don't specify the HowMany value, this will default
	// to returning the top 5 scores.
	//
	// Frodo Notes: The request has 2 attributes. The GameID field will be populated via the
	// path parameters, but HowMany will be specified via the query string. The auto-generated
	// clients will do this by default under the hood, but if you use curl/Postman, that's how
	// you would supply that (e.g. "/v2/game/2/highscore?howMany=3" for the top 3).
	//
	// GET /game/:GameID/highscore
	HighScoresForGame(context.Context, *HighScoresForGameRequest) (*HighScoresForGameResponse, error)
}

ScoreService is a shared leaderboard service that tracks the high scores that people have achieved while playing various games.

PREFIX /v2

type ScoreServiceHandler

type ScoreServiceHandler struct {
	// Games is our client to interact with the GameService.
	//
	// Frodo Notes: In cmd/main.go we'll pass "games.NewGameServiceClient()" in for this value. Both the
	// gateway and the client implement the GameService interface, so we don't actually care which
	// we are getting here. This makes it easy for you to run everything in the same process if you want
	// or distribute them across different processes/servers.
	Games games.GameService
	// Repo manages access to the underlying data store. Even when working with Frodo-powered services
	// you can still use standard Go dependency injection to get your services running.
	Repo Repo
}

ScoreServiceHandler implements all of the "real" functionality for the ScoreService.

func (ScoreServiceHandler) HighScoresForGame

HighScoresForGame fetches the top "N" high scores achieved by any player

func (*ScoreServiceHandler) NewHighScore

NewHighScore captures a player's high score for the given game.

Directories

Path Synopsis
Code generated by Frodo - DO NOT EDIT.
Code generated by Frodo - DO NOT EDIT.

Jump to

Keyboard shortcuts

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