Documentation ¶
Index ¶
- Constants
- Variables
- type CreateGameOptions
- type Duration
- type GameEndEvent
- type GameEvent
- type GameID
- type GameManager
- func (m *GameManager) BeginGC() (stop func())
- func (m *GameManager) CreateGame(user user.Authorization, metadata CreateGameOptions) (GameID, error)
- func (m *GameManager) JoinGame(user user.Authorization, id GameID) error
- func (m *GameManager) MakeMove(user user.Authorization, id GameID, move scouts.Move) error
- func (m *GameManager) QueryGame(id GameID) (GameState, error)
- func (m *GameManager) SubscribeGame(user user.Authorization, id GameID) (<-chan GameEvent, func(), error)
- type GameState
- type GoingAwayEvent
- type MoveMadeEvent
- type MoveSnapshot
- type PlayerConnectedEvent
- type PlayerDisconnectedEvent
- type PlayerJoinedEvent
- type PlayerLeftEvent
- type TurnBeginEvent
Constants ¶
const InfiniteDuration = Duration(-1)
InfiniteDuration is a duration that represents infinity.
Variables ¶
var ErrGameFull = hrt.NewHTTPError(400, "game already has two players")
ErrGameFull is an error that is returned when a game is full.
var ErrInvalidGameState = hrt.NewHTTPError(400, "invalid game state")
ErrInvalidMove is an error that is returned when a move is invalid.
var ErrInvalidMove = hrt.NewHTTPError(400, "invalid move")
ErrInvalidMove is an error that is returned when a move is invalid.
var ErrNotFound = hrt.NewHTTPError(404, "not found")
ErrNotFound is an error that is returned when an item is not found.
var InfiniteDurationPair = [2]Duration{ InfiniteDuration, InfiniteDuration, }
InfiniteDurationPair is a pair of infinite durations.
Functions ¶
This section is empty.
Types ¶
type CreateGameOptions ¶
type CreateGameOptions struct { // TimeLimit is the time limit per side. // If this is zero, then there is no time limit. TimeLimit Duration // Increment is the time increment per move. Increment Duration }
CreateGameOptions is a struct that contains options for creating a game. All fields are optional.
type Duration ¶
Duration is equivalent to time.Duration, but it marshals to and from JSON as a float64 representing seconds. A negative duration is considered infinite.
func (Duration) MarshalText ¶
MarshalText marshals the duration into text.
func (Duration) ToDuration ¶
ToDuration converts the duration to a time.Duration.
func (*Duration) UnmarshalText ¶
UnmarshalText unmarshals the duration from text.
type GameEndEvent ¶
type GameEndEvent struct { // Winner is the side that won the game. Winner scouts.Player `json:"winner"` // TimeRemaining is the time remaining for both sides. TimeRemaining [2]Duration `json:"time_remaining"` }
GameEndEvent is an event that is emitted when the game ends.
func (GameEndEvent) Type ¶
func (GameEndEvent) Type() string
type GameEvent ¶
type GameEvent interface { // Type returns the type of the game event. Type() string }
GameEvent is an interface that represents a game event. An event is emitted when the game state changes.
type GameID ¶
type GameID ulid.ULID
GameID is a type that represents a game ID.
func (GameID) MarshalText ¶
MarshalText marshals the game ID into text.
func (*GameID) UnmarshalText ¶
UnmarshalText unmarshals the game ID from text.
type GameManager ¶
type GameManager struct {
// contains filtered or unexported fields
}
GameManager is in charge of managing games. Games managed here may or may not be persisted in a database.
func NewGameManager ¶
func NewGameManager(logger *slog.Logger) *GameManager
NewGameManager creates a new game manager.
func (*GameManager) BeginGC ¶
func (m *GameManager) BeginGC() (stop func())
BeginGC starts a background goroutine that will periodically garbage collect games that have been inactive for a certain amount of time.
func (*GameManager) CreateGame ¶
func (m *GameManager) CreateGame(user user.Authorization, metadata CreateGameOptions) (GameID, error)
CreateGame creates a game with the given game ID and game metadata.
func (*GameManager) JoinGame ¶
func (m *GameManager) JoinGame(user user.Authorization, id GameID) error
JoinGame joins the game with the given game ID. If the game is full, then this returns an error, otherwise the player automatically takes the next available side.
func (*GameManager) MakeMove ¶
func (m *GameManager) MakeMove(user user.Authorization, id GameID, move scouts.Move) error
MakeMove makes a move in the game with the given game ID.
func (*GameManager) QueryGame ¶
func (m *GameManager) QueryGame(id GameID) (GameState, error)
QueryGame queries the game with the given game ID.
func (*GameManager) SubscribeGame ¶
func (m *GameManager) SubscribeGame(user user.Authorization, id GameID) (<-chan GameEvent, func(), error)
SubscribeGame returns a new channel that will receive game events. The channel will first receive playbacks of all moves that have been made in the game, and then it will receive new moves as they are made.
type GameState ¶
type GameState struct { // GameID is the GameID of the game. GameID GameID `json:"game_id"` // BeganAt is the time that the game began. // If nil, then the game has not begun yet. BeganAt *time.Time `json:"began_at"` // PlayerA is the first player. // If nil, then the player has not joined yet. PlayerA *user.Authorization `json:"player_a"` // PlayerB is the second player. // If nil, then the player has not joined yet. PlayerB *user.Authorization `json:"player_b"` // Moves is the list of moves that have been made in the game. Moves []MoveSnapshot `json:"moves"` // Metadata is the metadata of the game. Metadata CreateGameOptions `json:"metadata"` // CreatedAt is the time that the game was created. CreatedAt time.Time `json:"created_at"` // SnapshotAt is the time that the snapshot was taken. SnapshotAt time.Time `json:"snapshot_at"` }
GameState is a struct that contains metadata about a game.
type GoingAwayEvent ¶
type GoingAwayEvent struct { }
GoingAwayEvent is an event that is emitted when the server is about to disconnect the client.
func (GoingAwayEvent) Type ¶
func (GoingAwayEvent) Type() string
type MoveMadeEvent ¶
type MoveMadeEvent struct { // Move is the move that was made. Move scouts.Move `json:"move"` // PlayerSide is the side that made the move. PlayerSide scouts.Player `json:"player_side"` // PlaysRemaining is the number of plays remaining for the player. PlaysRemaining int `json:"plays_remaining"` // TimeRemaining is the time remaining for both sides. TimeRemaining [2]Duration `json:"time_remaining"` }
MoveMadeEvent is an event that is emitted when a move is made.
func (MoveMadeEvent) Type ¶
func (MoveMadeEvent) Type() string
type MoveSnapshot ¶
MoveSnapshot contains a single move that a player made and the time that the move was made.
type PlayerConnectedEvent ¶
type PlayerConnectedEvent struct { // PlayerSide is the side that the user connected. PlayerSide scouts.Player `json:"player_side"` }
PlayerConnectedEvent is an event that is emitted when a player connects to the game. It can only be emitted after a PlayerJoinedEvent but before a PlayerLeftEvent.
func (PlayerConnectedEvent) Type ¶
func (PlayerConnectedEvent) Type() string
type PlayerDisconnectedEvent ¶
type PlayerDisconnectedEvent struct { // PlayerSide is the side that the user disconnected. PlayerSide scouts.Player `json:"player_side"` }
PlayerDisconnectedEvent is an event that is emitted when a player disconnects from the game. It can only be emitted after a PlayerJoinedEvent but before a PlayerLeftEvent. A disconnect implies that the player might still choose to rejoin the game, after which a PlayerConnectedEvent will be emitted again.
func (PlayerDisconnectedEvent) Type ¶
func (PlayerDisconnectedEvent) Type() string
type PlayerJoinedEvent ¶
type PlayerJoinedEvent struct { // PlayerSide is the side that the user joined. PlayerSide scouts.Player `json:"player_side"` // UserID is the ID of the user that joined the game. // If this is nil, then the user is anonymous. UserID *user.UserID `json:"user_id"` }
PlayerJoinedEvent is an event that is emitted when a player joins the game.
func (PlayerJoinedEvent) Type ¶
func (PlayerJoinedEvent) Type() string
type PlayerLeftEvent ¶
type PlayerLeftEvent struct { // PlayerSide is the side that the user left. PlayerSide scouts.Player `json:"player_side"` // UserID is the ID of the user that left the game. // If this is nil, then the user is anonymous. UserID *user.UserID `json:"user_id"` }
PlayerLeftEvent is an event that is emitted when a player leaves the game.
func (PlayerLeftEvent) Type ¶
func (PlayerLeftEvent) Type() string
type TurnBeginEvent ¶
type TurnBeginEvent struct { // PlayerSide is the side that is about to make a move. PlayerSide scouts.Player `json:"player_side"` // PlaysRemaining is the number of plays remaining for the player. PlaysRemaining int `json:"plays_remaining"` // TimeRemaining is the time remaining for both sides. TimeRemaining [2]Duration `json:"time_remaining"` }
TurnBeginEvent is an event that is emitted when a turn begins.
func (TurnBeginEvent) Type ¶
func (TurnBeginEvent) Type() string