api

package
v0.8.8 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: BSD-3-Clause Imports: 21 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 (
	// CanvasColor is the initially / empty canvas colors value used for
	// Lobbydata objects.
	CanvasColor = game.RGBColor{R: 255, G: 255, B: 255}
	// SuggestedBrushSizes is suggested brush sizes value used for
	// Lobbydata objects. A unit test makes sure these values are ordered
	// and within the specified bounds.
	SuggestedBrushSizes = [4]uint8{8, 16, 24, 32}
)
View Source
var ErrLobbyNotExistent = errors.New("the requested lobby doesn't exist")
View Source
var (
	ErrPlayerNotConnected = errors.New("player not connected")
)

Functions

func GetIPAddressFromRequest

func GetIPAddressFromRequest(request *http.Request) string

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

func GetPlayer

func GetPlayer(lobby *game.Lobby, r *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 string, 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(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(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(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(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 SetUsersessionCookie

func SetUsersessionCookie(w http.ResponseWriter, player *game.Player)

SetUsersessionCookie takes the players usersession and sets it as a cookie.

func WriteObject added in v0.8.3

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

func WritePreparedMessage added in v0.8.3

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

Types

type LobbyData

type LobbyData struct {
	game.SettingBounds
	game.EditableLobbySettings

	LobbyID string `json:"lobbyId"`
	// DrawingBoardBaseWidth is the internal canvas width and is needed for
	// correctly up- / downscaling drawing instructions.
	DrawingBoardBaseWidth int `json:"drawingBoardBaseWidth"`
	// DrawingBoardBaseHeight is the internal canvas height and is needed for
	// correctly up- / downscaling drawing instructions.
	DrawingBoardBaseHeight int `json:"drawingBoardBaseHeight"`
	// MinBrushSize is the minimum amount of pixels the brush can draw in.
	MinBrushSize int `json:"minBrushSize"`
	// MaxBrushSize is the maximum amount of pixels the brush can draw in.
	MaxBrushSize int `json:"maxBrushSize"`
	// CanvasColor is the initially (empty) color of the canvas.
	CanvasColor game.RGBColor `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"`
}

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(lobby *game.Lobby) *LobbyData

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

func (LobbyData) MarshalEasyJSON added in v0.8.3

func (v LobbyData) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (LobbyData) MarshalJSON added in v0.8.3

func (v LobbyData) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*LobbyData) UnmarshalEasyJSON added in v0.8.3

func (v *LobbyData) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*LobbyData) UnmarshalJSON added in v0.8.3

func (v *LobbyData) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type LobbyEntries added in v0.8.3

type LobbyEntries []*LobbyEntry

func (LobbyEntries) MarshalEasyJSON added in v0.8.3

func (v LobbyEntries) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (LobbyEntries) MarshalJSON added in v0.8.3

func (v LobbyEntries) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*LobbyEntries) UnmarshalEasyJSON added in v0.8.3

func (v *LobbyEntries) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*LobbyEntries) UnmarshalJSON added in v0.8.3

func (v *LobbyEntries) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type LobbyEntry

type LobbyEntry struct {
	LobbyID         string     `json:"lobbyId"`
	Wordpack        string     `json:"wordpack"`
	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.

func (LobbyEntry) MarshalEasyJSON added in v0.8.3

func (v LobbyEntry) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (LobbyEntry) MarshalJSON added in v0.8.3

func (v LobbyEntry) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*LobbyEntry) UnmarshalEasyJSON added in v0.8.3

func (v *LobbyEntry) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*LobbyEntry) UnmarshalJSON added in v0.8.3

func (v *LobbyEntry) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

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, router chi.Router)

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