api

package
v0.9.12 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package api the public APIs for both the HTTP and the WS endpoints. These are being used by the official client and can also be used by third party clients. On top of that this package contains some util code regarding http/ws that can be used by other packages. In order to register the endpoints you have to call SetupRoutes.

Index

Constants

This section is empty.

Variables

View Source
var ErrLobbyNotExistent = errors.New("the requested lobby doesn't exist")
View Source
var (
	ErrPlayerNotConnected = errors.New("player not connected")
)
View Source
var GameConstantsData = &GameConstants{
	DrawingBoardBaseWidth:  game.DrawingBoardBaseWidth,
	DrawingBoardBaseHeight: game.DrawingBoardBaseHeight,
	MinBrushSize:           game.MinBrushSize,
	MaxBrushSize:           game.MaxBrushSize,
	CanvasColor:            0,
	SuggestedBrushSizes:    SuggestedBrushSizes,
}
View Source
var SuggestedBrushSizes = [4]uint8{8, 16, 24, 32}

SuggestedBrushSizes is suggested brush sizes value used for Lobbydata objects. A unit test makes sure these values are ordered and within the specified bounds.

Functions

func GetDiscordInstanceId added in v0.9.0

func GetDiscordInstanceId(request *http.Request) string

func GetIPAddressFromRequest

func GetIPAddressFromRequest(request *http.Request) string

GetIPAddressFromRequest extracts the clients IP address from the request. This function respects forwarding headers.

func GetLobbyId added in v0.9.0

func GetLobbyId(request *http.Request) string

GetLobbyId returns either the lobby id from the URL or from a cookie.

func GetPlayer

func GetPlayer(lobby *game.Lobby, request *http.Request) *game.Player

GetPlayer returns the player object that matches the usersession in the supplied HTTP request and lobby. If no user session is set, we return nil.

func GetPlayername

func GetPlayername(request *http.Request) string

GetPlayername either retrieves the playername from a cookie, the URL form. If no preferred name can be found, we return an empty string.

func GetUserSession

func GetUserSession(request *http.Request) (uuid.UUID, error)

GetUserSession accesses the usersession from an HTTP request and returns the session. The session can either be in the cookie or in the header. If no session can be found, an empty string is returned.

func ParseBoolean

func ParseBoolean(valueName, value string) (bool, error)

ParseBoolean checks whether the given value is either "true" or "false". The checks are case-insensitive. If an empty string is supplied, false is returned. All other invalid input will return an error.

func ParseClientsPerIPLimit

func ParseClientsPerIPLimit(cfg *config.Config, value string) (int, error)

ParseClientsPerIPLimit checks whether the given value is an integer between the lower and upper bound of maximum clients per IP. All other invalid input, including empty strings, will return an error.

func ParseCustomWords

func ParseCustomWords(lowercaser cases.Caser, value string) ([]string, error)

ParseCustomWords checks whether the given value is a string containing comma separated values (or a single word). Empty strings will return an empty (nil) array and no error. An error is only returned if there are empty words. For example these wouldn't parse:

wordone,,wordtwo
,
wordone,

func ParseCustomWordsPerTurn added in v0.8.3

func ParseCustomWordsPerTurn(value string) (int, error)

ParseCustomWordsPerTurn checks whether the given value is an integer between 0 and 100. All other invalid input, including empty strings, will return an error.

func ParseDrawingTime

func ParseDrawingTime(cfg *config.Config, value string) (int, error)

ParseDrawingTime checks whether the given value is an integer between the lower and upper bound of drawing time. All other invalid input, including empty strings, will return an error.

func ParseLanguage

func ParseLanguage(value string) (*game.LanguageData, string, error)

ParseLanguage checks whether the given value is part of the game.SupportedLanguages array. The input is trimmed and lowercased.

func ParseMaxPlayers

func ParseMaxPlayers(cfg *config.Config, value string) (int, error)

ParseMaxPlayers checks whether the given value is an integer between the lower and upper bound of maximum players per lobby. All other invalid input, including empty strings, will return an error.

func ParsePlayerName

func ParsePlayerName(value string) (string, error)

ParsePlayerName checks if the given value is a valid playername. Currently this only includes checkin whether the value is empty or only consists of whitespace character.

func ParseRounds

func ParseRounds(cfg *config.Config, value string) (int, error)

ParseRounds checks whether the given value is an integer between the lower and upper bound of rounds played. All other invalid input, including empty strings, will return an error.

func ParseScoreCalculation added in v0.8.19

func ParseScoreCalculation(value string) (game.ScoreCalculation, error)

func SetDiscordCookies added in v0.9.0

func SetDiscordCookies(w http.ResponseWriter, request *http.Request)

func SetGameplayCookies added in v0.9.0

func SetGameplayCookies(
	w http.ResponseWriter,
	request *http.Request,
	player *game.Player,
	lobby *game.Lobby,
)

SetGameplayCookies takes the players usersession and lobby id and sets them as a cookie.

func WriteObject added in v0.8.3

func WriteObject(player *game.Player, object any) error

func WritePreparedMessage added in v0.8.3

func WritePreparedMessage(player *game.Player, message *gws.Broadcaster) error

Types

type GameConstants added in v0.9.0

type GameConstants struct {
	// DrawingBoardBaseWidth is the internal canvas width and is needed for
	// correctly up- / downscaling drawing instructions.
	DrawingBoardBaseWidth uint16 `json:"drawingBoardBaseWidth"`
	// DrawingBoardBaseHeight is the internal canvas height and is needed for
	// correctly up- / downscaling drawing instructions.
	DrawingBoardBaseHeight uint16 `json:"drawingBoardBaseHeight"`
	// MinBrushSize is the minimum amount of pixels the brush can draw in.
	MinBrushSize uint8 `json:"minBrushSize"`
	// MaxBrushSize is the maximum amount of pixels the brush can draw in.
	MaxBrushSize uint8 `json:"maxBrushSize"`
	// CanvasColor is the initially (empty) color of the canvas.
	CanvasColor uint8 `json:"canvasColor"`
	// SuggestedBrushSizes are suggestions for the different brush sizes
	// that the user can choose between. These brushes are guaranteed to
	// be ordered from low to high and stay with the bounds.
	SuggestedBrushSizes [4]uint8 `json:"suggestedBrushSizes"`
}

GameConstants are values that are lobby-independent and can't be changed via settings, neither at compile time nor at startup time.

type LobbyData

type LobbyData struct {
	game.SettingBounds
	game.EditableLobbySettings
	*GameConstants
	IsWordpackRtl bool
}

LobbyData is the data necessary for correctly configuring a lobby. While unofficial clients will probably need all of these values, the official webclient doesn't use all of them as of now.

func CreateLobbyData

func CreateLobbyData(cfg *config.Config, lobby *game.Lobby) *LobbyData

CreateLobbyData creates a ready to use LobbyData object containing data from the passed Lobby.

type LobbyEntries added in v0.8.3

type LobbyEntries []*LobbyEntry

type LobbyEntry

type LobbyEntry struct {
	LobbyID         string     `json:"lobbyId"`
	Wordpack        string     `json:"wordpack"`
	Scoring         string     `json:"scoring"`
	State           game.State `json:"state"`
	PlayerCount     int        `json:"playerCount"`
	MaxPlayers      int        `json:"maxPlayers"`
	Round           int        `json:"round"`
	Rounds          int        `json:"rounds"`
	DrawingTime     int        `json:"drawingTime"`
	MaxClientsPerIP int        `json:"maxClientsPerIp"`
	CustomWords     bool       `json:"customWords"`
}

LobbyEntry is an API object for representing a join-able public lobby.

type V1Handler added in v0.8.5

type V1Handler struct {
	// contains filtered or unexported fields
}

func NewHandler added in v0.8.5

func NewHandler(cfg *config.Config) *V1Handler

func (*V1Handler) SetupRoutes added in v0.8.5

func (handler *V1Handler) SetupRoutes(rootPath string, register func(string, string, http.HandlerFunc))

SetupRoutes registers the /v1/ endpoints with the http package.

Jump to

Keyboard shortcuts

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