Documentation
¶
Overview ¶
Package leaderboard submits scores to and shows the platform's leaderboards, Game Center on Apple platforms and Play Games Services on Android, through NSGo's gameservices package.
A game declares its boards once in a versioned YAML Catalog that maps each board's name in the game to its ID in every game service, and its code then names boards rather than platform IDs:
version: 1
leaderboards:
- name: high-score
gameCenter: com.example.game.high_score
playGames: CgkIxxxxxxxxxxEAIQAQ
catalog, err := leaderboard.Parse(data)
boards, err := leaderboard.New(cx, catalog)
boards.SignIn(nil)
boards.Submit("high-score", score, func(err error) { ... })
boards.Show("high-score")
Calls complete through callbacks on the engine goroutine. On platforms without a game service (Windows, Linux, the web, watchOS and mini programs) they report ErrUnsupported, and Available and Supports let a view hide its leaderboard button there. A misspelt board name reports ErrUnknownLeaderboard on every platform.
Tests run against nsgotest's per-app fake game service: start the app with nsgotest.Environment(core.Environment{CanUseGameServices: true, OS: core.OSIOS}), sign a player in with App.SetGameServicesPlayer, and read App.GameServicesSubmittedScores and App.GameServicesShownLeaderboards.
Index ¶
- Constants
- Variables
- type Board
- type Catalog
- type Entry
- type Player
- type PlayerScope
- type Provider
- type Query
- type Service
- func (s *Service) Available() bool
- func (s *Service) Catalog() *Catalog
- func (s *Service) ID(name string) (string, error)
- func (s *Service) Player() Player
- func (s *Service) Provider() Provider
- func (s *Service) Scores(name string, q Query, done func([]Entry, error))
- func (s *Service) Show(name string) error
- func (s *Service) SignIn(done func(Player, error))
- func (s *Service) SignedIn() bool
- func (s *Service) Submit(name string, score int64, done func(error))
- func (s *Service) Supports(name string) bool
- type TimeScope
Examples ¶
Constants ¶
const ( Global = gameservices.Global Friends = gameservices.Friends )
Player scopes for Query.Players.
const ( AllTime = gameservices.AllTime Week = gameservices.Week Today = gameservices.Today )
Time scopes for Query.Time.
const CurrentVersion = 1
CurrentVersion is the leaderboard catalog schema version this package reads and writes.
const MaxIDLength = 100
MaxIDLength is the longest platform leaderboard ID a catalog accepts, matching Game Center's limit of 100 characters.
Variables ¶
var ( // ErrUnknownLeaderboard means the name is not declared in the catalog. It is reported on every platform, so a misspelt name shows up while developing on a desktop without a game service. ErrUnknownLeaderboard = errors.New("leaderboard: unknown leaderboard") // ErrNoPlatformID means the board is declared but has no ID for the game service of the platform the game runs on. ErrNoPlatformID = errors.New("leaderboard: no ID for this platform's game service") // ErrUnsupported means the platform has no game service the game can reach (Windows, Linux, the web, watchOS, mini programs, or a device without Game Center or Play Games). ErrUnsupported = gameservices.ErrUnsupported ErrUnavailable = gameservices.ErrUnavailable // ErrUnauthenticated means no player is signed in; call Service.SignIn first. ErrUnauthenticated = gameservices.ErrUnauthenticated // ErrCancelled means the player dismissed the platform's sign-in. ErrCancelled = gameservices.ErrCancelled )
Errors a Service reports. The platform's own errors are NSGo's gameservices sentinels, repeated here so callers can test them with errors.Is without a second import.
Functions ¶
This section is empty.
Types ¶
type Board ¶
type Board struct {
// Name is the game's own key for the board, such as "high-score". It is unique within the catalog.
Name string `yaml:"name"`
// GameCenter is the leaderboard ID registered in App Store Connect, used on iOS, iPadOS, tvOS and macOS.
GameCenter string `yaml:"gameCenter,omitempty"`
// PlayGames is the leaderboard ID the Google Play Console generated, used on Android.
PlayGames string `yaml:"playGames,omitempty"`
}
Board declares one leaderboard: the name the game's code uses and the ID it has in each platform game service. A board needs at least one ID; on a platform whose ID it lacks, Service reports ErrNoPlatformID.
type Catalog ¶
type Catalog struct {
// Version is the schema version; it must equal CurrentVersion.
Version int `yaml:"version"`
// Leaderboards lists the boards in the order the file declares them.
Leaderboards []Board `yaml:"leaderboards"`
}
Catalog is the YAML file that declares a game's leaderboards once: each board's name in the game and its ID in every platform game service it is registered with.
version: 1
leaderboards:
- name: high-score
gameCenter: com.example.game.high_score
playGames: CgkIxxxxxxxxxxEAIQAQ
func Parse ¶
Parse decodes a YAML leaderboard catalog and validates it. Unknown fields, duplicate keys and trailing YAML documents are refused.
Example ¶
A Catalog maps each board's name in the game to its ID in every platform game service; Encode writes a parsed catalog back byte for byte.
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/leaderboard"
)
const exampleCatalog = `version: 1
leaderboards:
- name: high-score
gameCenter: com.example.game.high_score
playGames: CgkIxxxxxxxxxxEAIQAQ
- name: daily
playGames: CgkIxxxxxxxxxxEAIQAw
`
func main() {
c, err := leaderboard.Parse([]byte(exampleCatalog))
if err != nil {
fmt.Println(err)
return
}
b, _ := c.Board("high-score")
fmt.Println(b.ID(leaderboard.ProviderGameCenter), b.ID(leaderboard.ProviderPlayGames))
daily, _ := c.Board("daily")
fmt.Printf("%q\n", daily.ID(leaderboard.ProviderGameCenter))
out, _ := c.Encode()
fmt.Println(string(out) == exampleCatalog)
_, err = leaderboard.Parse([]byte(exampleCatalog + "sort: descending\n"))
fmt.Println(err != nil)
}
Output: com.example.game.high_score CgkIxxxxxxxxxxEAIQAQ "" true true
func (*Catalog) Encode ¶
Encode validates c and writes it as deterministic two-space YAML: fields in schema order, boards in declaration order, and absent IDs left out, so Parse followed by Encode reproduces a file Encode wrote byte for byte.
type Entry ¶
type Entry = gameservices.ScoreEntry
Entry is one ranked row of a leaderboard returned by Service.Scores. Its LeaderboardID is the platform ID, not the catalog name.
type Player ¶
type Player = gameservices.Player
Player is the local player signed into the platform game service.
type PlayerScope ¶
type PlayerScope = gameservices.PlayerScope
PlayerScope selects which players Service.Scores includes: Global or Friends.
type Provider ¶
type Provider uint8
Provider names a platform game service a Board can carry an ID for.
func ProviderFor ¶
ProviderFor returns the game service NSGo's gameservices package talks to on os: Game Center on macOS and iOS (which covers iPadOS and tvOS), Play Games on Android, and ProviderNone elsewhere.
Example ¶
ProviderFor names the game service a platform's leaderboards go through.
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/leaderboard"
"github.com/nsgo-dev/nsgo/core"
)
func main() {
for _, os := range []core.OS{core.OSIOS, core.OSMacOS, core.OSAndroid, core.OSWindows} {
fmt.Println(leaderboard.ProviderFor(os))
}
}
Output: gameCenter gameCenter playGames none
type Query ¶
type Query struct {
// Players chooses everyone (Global, the default) or the local player's friends (Friends).
Players PlayerScope
// Time chooses all-time scores (AllTime, the default), this week's (Week) or today's (Today).
Time TimeScope
// Limit is the most rows to load; 0 or less means 25, and more than 100 is clamped to 100.
Limit int
}
Query selects the rows Service.Scores loads. The zero Query asks for the global all-time board with the platform's default of 25 rows.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service submits scores to and shows the leaderboards a Catalog declares, through NSGo's gameservices package and so through Game Center or Play Games. Every callback runs on the engine goroutine, after the call that started it has returned, so it may update signals and views directly.
Example ¶
A Service names boards by their catalog names; a misspelt name is refused on every platform before the platform is reached.
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/leaderboard"
"github.com/nsgo-dev/nsgo/core"
)
const exampleCatalog = `version: 1
leaderboards:
- name: high-score
gameCenter: com.example.game.high_score
playGames: CgkIxxxxxxxxxxEAIQAQ
- name: daily
playGames: CgkIxxxxxxxxxxEAIQAw
`
func main() {
root := core.NewRoot(core.RootOptions{})
defer root.Dispose()
c, _ := leaderboard.Parse([]byte(exampleCatalog))
boards, err := leaderboard.New(root, c)
if err != nil {
fmt.Println(err)
return
}
_, err = boards.ID("high-scores")
fmt.Println(err)
fmt.Println(boards.Show("high-scores"))
fmt.Println(boards.SignedIn(), len(boards.Catalog().Leaderboards))
}
Output: leaderboard: unknown leaderboard "high-scores" leaderboard: unknown leaderboard "high-scores" false 2
func New ¶
New validates c and returns a Service bound to cx, which keeps its own copy of the catalog. The service starts signed out; call SignIn before submitting scores.
func (*Service) Available ¶
Available reports whether the platform's game service can be used here. It reads the environment reactively, so a view that calls it updates when that changes.
func (*Service) ID ¶
ID returns the platform ID of the board name, or ErrUnknownLeaderboard, ErrUnsupported or ErrNoPlatformID (checked in that order).
func (*Service) Player ¶
Player returns the signed-in player, or the zero Player while signed out. It reads a signal, so a view that calls it updates on sign-in and sign-out.
func (*Service) Provider ¶
Provider returns the game service of the platform the game runs on, whether or not it is reachable.
func (*Service) Scores ¶
Scores loads ranked rows of the board name selected by q, for a game that draws its own board, and calls done with them or an error. done must not be nil.
func (*Service) Show ¶
Show presents the platform's own leaderboard UI for the board name. It reports ErrUnknownLeaderboard, ErrUnsupported or ErrNoPlatformID instead of silently doing nothing; once it returns nil, what the platform shows (including its sign-in prompt) is up to the platform.
func (*Service) SignIn ¶
SignIn signs the local player into the platform game service, which may show the platform's own sign-in UI, and calls done with the player or an error such as ErrUnsupported or ErrCancelled. done may be nil.
func (*Service) Submit ¶
Submit sends score to the board name for the signed-in player and calls done with the result; done may be nil. The platform keeps each player's best score by the board's own sort order, so a game can submit every finished run. A board the catalog does not declare, a platform without a game service or a board without an ID here fail through done without reaching the platform.
type TimeScope ¶
type TimeScope = gameservices.TimeScope
TimeScope selects the time window Service.Scores covers: AllTime, Week or Today.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package selfhost is a self-hostable leaderboard service, for cross-platform games and games on platforms without a game service: one board ranks the scores a player's devices submit from every platform, keyed by the account the accounts package signs in.
|
Package selfhost is a self-hostable leaderboard service, for cross-platform games and games on platforms without a game service: one board ranks the scores a player's devices submit from every platform, keyed by the account the accounts package signs in. |
|
cmd/leaderboardd
command
Command leaderboardd runs the self-hosted leaderboard service as a standalone HTTP server, with its scores in a directory of JSON Lines files.
|
Command leaderboardd runs the self-hosted leaderboard service as a standalone HTTP server, with its scores in a directory of JSON Lines files. |