gameserver

package
v0.0.0-...-8ed97ff Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: ISC Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const InfiniteDuration = Duration(-1)

InfiniteDuration is a duration that represents infinity.

Variables

View Source
var ErrGameFull = hrt.NewHTTPError(400, "game already has two players")

ErrGameFull is an error that is returned when a game is full.

View Source
var ErrInvalidGameState = hrt.NewHTTPError(400, "invalid game state")

ErrInvalidMove is an error that is returned when a move is invalid.

View Source
var ErrInvalidMove = hrt.NewHTTPError(400, "invalid move")

ErrInvalidMove is an error that is returned when a move is invalid.

View Source
var ErrNotFound = hrt.NewHTTPError(404, "not found")

ErrNotFound is an error that is returned when an item is not found.

View Source
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

type Duration time.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

func (d Duration) MarshalText() ([]byte, error)

MarshalText marshals the duration into text.

func (Duration) String

func (d Duration) String() string

String returns the string representation of the duration.

func (Duration) ToDuration

func (d Duration) ToDuration() time.Duration

ToDuration converts the duration to a time.Duration.

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

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 GenerateGameID

func GenerateGameID() GameID

GenerateGameID generates a new game ID.

func (GameID) CreatedAt

func (g GameID) CreatedAt() time.Time

CreatedAt returns the time that the game ID was generated.

func (GameID) MarshalText

func (g GameID) MarshalText() ([]byte, error)

MarshalText marshals the game ID into text.

func (GameID) String

func (g GameID) String() string

String returns the string representation of the game ID.

func (*GameID) UnmarshalText

func (g *GameID) UnmarshalText(text []byte) error

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

type MoveSnapshot struct {
	Player scouts.Player
	Move   scouts.Move
	Time   time.Time
}

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

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

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

Jump to

Keyboard shortcuts

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