stores

package
v0.0.0-...-46ed163 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: ISC Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CanUpdateByCreator = "creator"
	CanUpdateByLeader  = "leader"
	CanUpdateByAnyone  = "anyone"
	CanUpdateByNone    = "none"
)

Variables

View Source
var ErrAlreadyInLobby = errors.New("peer already in lobby")
View Source
var ErrInvalidLobbyCode = errors.New("invalid lobby code")
View Source
var ErrInvalidPassword = errors.New("invalid password")
View Source
var ErrInvalidPeerID = errors.New("invalid peer id")
View Source
var ErrLobbyExists = errors.New("lobby already exists")
View Source
var ErrLobbyIsFull = errors.New("lobby is full")
View Source
var ErrNoSuchTopic = errors.New("no such topic")
View Source
var ErrNotFound = errors.New("lobby not found")

Functions

This section is empty.

Types

type ElectionResult

type ElectionResult struct {
	Leader string
	Term   int
}

type Lobby

type Lobby struct {
	Code        string   `json:"code"`
	Peers       []string `json:"peers"`
	PlayerCount int      `json:"playerCount"`
	Creator     string   `json:"creator"`

	Public      bool           `json:"public"`
	MaxPlayers  int            `json:"maxPlayers"`
	HasPassword bool           `json:"hasPassword"`
	CustomData  map[string]any `json:"customData"`
	CanUpdateBy string         `json:"canUpdateBy"`

	Leader string `json:"leader,omitempty"`
	Term   int    `json:"term"`

	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

type LobbyOptions

type LobbyOptions struct {
	Public      *bool
	CustomData  *map[string]any
	CanUpdateBy *string
	Password    *string
	MaxPlayers  *int
}

type PostgresStore

type PostgresStore struct {
	DB *pgxpool.Pool
	// contains filtered or unexported fields
}

func NewPostgresStore

func NewPostgresStore(ctx context.Context, db *pgxpool.Pool) (*PostgresStore, error)

func (*PostgresStore) ClaimNextTimedOutPeer

func (s *PostgresStore) ClaimNextTimedOutPeer(ctx context.Context, threshold time.Duration, callback func(peerID, gameID string, lobbies []string) error) (more bool, err error)

func (*PostgresStore) CleanEmptyLobbies

func (s *PostgresStore) CleanEmptyLobbies(ctx context.Context, olderThan time.Time) error

func (*PostgresStore) CreateLobby

func (s *PostgresStore) CreateLobby(ctx context.Context, game, lobbyCode, peerID string, options LobbyOptions) error

func (*PostgresStore) DoLeaderElection

func (s *PostgresStore) DoLeaderElection(ctx context.Context, gameID, lobbyCode string) (*ElectionResult, error)

DoLeaderElection attempts to elect a leader for the given lobby. If a correct leader already exists it will return nil. If no leader can be elected, it will return an ElectionResult with a nil leader.

func (*PostgresStore) GetLobby

func (s *PostgresStore) GetLobby(ctx context.Context, game, lobbyCode string) (Lobby, error)

func (*PostgresStore) JoinLobby

func (s *PostgresStore) JoinLobby(ctx context.Context, game, lobbyCode, peerID, password string) error

func (*PostgresStore) LeaveLobby

func (s *PostgresStore) LeaveLobby(ctx context.Context, game, lobbyCode, peerID string) error

func (*PostgresStore) ListLobbies

func (s *PostgresStore) ListLobbies(ctx context.Context, game, filter string) ([]Lobby, error)

func (*PostgresStore) Publish

func (s *PostgresStore) Publish(ctx context.Context, topic string, data []byte) error

func (*PostgresStore) ReconnectPeer

func (s *PostgresStore) ReconnectPeer(ctx context.Context, peerID, secret, gameID string) (bool, []string, error)

func (*PostgresStore) Subscribe

func (s *PostgresStore) Subscribe(ctx context.Context, callback SubscriptionCallback, game, lobby, peerID string)

func (*PostgresStore) TimeoutPeer

func (s *PostgresStore) TimeoutPeer(ctx context.Context, peerID, secret, gameID string, lobbies []string) error

func (*PostgresStore) UpdateLobby

func (s *PostgresStore) UpdateLobby(ctx context.Context, game, lobbyCode, peerID string, options LobbyOptions) error

type Store

type Store interface {
	CreateLobby(ctx context.Context, Game, LobbyCode, PeerID string, options LobbyOptions) error
	JoinLobby(ctx context.Context, game, lobby, id, password string) error
	LeaveLobby(ctx context.Context, game, lobby, id string) error
	GetLobby(ctx context.Context, game, lobby string) (Lobby, error)
	ListLobbies(ctx context.Context, game, filter string) ([]Lobby, error)

	Subscribe(ctx context.Context, callback SubscriptionCallback, game, lobby, peerID string)
	Publish(ctx context.Context, topic string, data []byte) error

	TimeoutPeer(ctx context.Context, peerID, secret, gameID string, lobbies []string) error
	ReconnectPeer(ctx context.Context, peerID, secret, gameID string) (bool, []string, error)
	ClaimNextTimedOutPeer(ctx context.Context, threshold time.Duration, callback func(peerID, gameID string, lobbies []string) error) (bool, error)

	CleanEmptyLobbies(ctx context.Context, olderThan time.Time) error

	// DoLeaderElection attempts to elect a leader for the given lobby. If a correct leader already exists it will return nil.
	// If no leader can be elected, it will return an ElectionResult with a nil leader.
	DoLeaderElection(ctx context.Context, gameID, lobbyCode string) (*ElectionResult, error)

	UpdateLobby(ctx context.Context, Game, LobbyCode, PeerID string, options LobbyOptions) error
}

func FromEnv

func FromEnv(ctx context.Context) (Store, chan struct{}, error)

type SubscriptionCallback

type SubscriptionCallback func(context.Context, []byte)

Jump to

Keyboard shortcuts

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