cg

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2022 License: MIT Imports: 17 Imported by: 1

Documentation

Overview

Package cg implements common client logic for connecting with a CodeGame server and handling events.

Index

Constants

View Source
const (
	DebugError   = "error"
	DebugWarning = "warning"
	DebugInfo    = "info"
	DebugTrace   = "trace"
)
View Source
const (
	Reset  color = "\x1b[0m"
	Red    color = "\x1b[31m"
	Green  color = "\x1b[32m"
	Yellow color = "\x1b[33m"
)
View Source
const CGVersion = "0.7"

Variables

View Source
var (
	ErrInvalidMessageType = errors.New("invalid message type")
	ErrEncodeFailed       = errors.New("failed to encode json object")
	ErrDecodeFailed       = errors.New("failed to decode event")
	ErrClosed             = errors.New("connection closed")
)

Functions

func FetchGameConfig added in v0.9.0

func FetchGameConfig[T any](socket *Socket, gameId string) (T, error)

FetchGameConfig fetches the game config from the server.

Types

type CallbackId

type CallbackId uuid.UUID

type Command added in v0.9.0

type Command struct {
	Name CommandName     `json:"name"`
	Data json.RawMessage `json:"data"`
}

type CommandName added in v0.9.0

type CommandName string

type DebugMessageCallback added in v0.9.0

type DebugMessageCallback func(severity DebugSeverity, message string, data string)

The data argument is empty if no data was included in the message.

type DebugSeverity added in v0.9.0

type DebugSeverity string

type DebugSocket added in v0.9.0

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

func NewDebugSocket added in v0.9.0

func NewDebugSocket(url string) *DebugSocket

func (*DebugSocket) Close added in v0.9.0

func (s *DebugSocket) Close() error

Close closes the underlying websocket connection.

func (*DebugSocket) DebugGame added in v0.9.0

func (s *DebugSocket) DebugGame(gameId string) error

DebugGame connects to the /api/games/{gameId}/debug endpoint on the server and listens for debug messages.

func (*DebugSocket) DebugPlayer added in v0.9.0

func (s *DebugSocket) DebugPlayer(gameId, playerId, playerSecret string) error

DebugPlayer connects to the /api/games/{gameId}/players/{playerId}/debug endpoint on the server and listens for debug messages.

func (*DebugSocket) DebugServer added in v0.9.0

func (s *DebugSocket) DebugServer() error

DebugServer connects to the /api/debug endpoint on the server and listens for debug messages.

func (*DebugSocket) OnMessage added in v0.9.0

func (s *DebugSocket) OnMessage(callback DebugMessageCallback) CallbackId

func (*DebugSocket) RemoveCallback added in v0.9.0

func (s *DebugSocket) RemoveCallback(id CallbackId)

func (*DebugSocket) SetSeverities added in v0.9.0

func (s *DebugSocket) SetSeverities(enableTrace, enableInfo, enableWarning, enableError bool)

SetSeverities enables/disables specific message severities. SetSeverities panics if it is called after calling DebugServer, DebugGame or DebugPlayer. When SetSeverities is never called all severities except trace are enabled.

func (*DebugSocket) URL added in v0.9.0

func (s *DebugSocket) URL() string

type Event

type Event struct {
	Name EventName       `json:"name"`
	Data json.RawMessage `json:"data"`
}

func (*Event) UnmarshalData

func (e *Event) UnmarshalData(targetObjPtr any) error

UnmarshalData decodes the event data into the struct pointed to by targetObjPtr.

type EventCallback added in v0.9.0

type EventCallback func(event Event)

type EventName

type EventName string

type Session

type Session struct {
	GameURL      string `json:"-"`
	Username     string `json:"-"`
	GameId       string `json:"game_id"`
	PlayerId     string `json:"player_id"`
	PlayerSecret string `json:"player_secret"`
}

type Socket

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

Socket represents the connection with a CodeGame server and handles events.

func NewSocket

func NewSocket(url string) (*Socket, error)

NewSocket creates a new Socket object ready to execute further actions. You can omit the protocol. NewSocket will determine the best protocol to use.

func (*Socket) Close

func (s *Socket) Close() error

Close closes the underlying websocket connection.

func (*Socket) Connect

func (s *Socket) Connect(gameId, playerId, playerSecret string) error

Connect connects to a game and player on the server. Connect panics if the socket is already connected to a game.

func (*Socket) CreateGame added in v0.9.0

func (s *Socket) CreateGame(public, protected bool, config any) (gameId, joinSecret string, err error)

CreateGame creates a new game on the server and returns the id of the created game and the join secret if protected == true.

func (*Socket) Join

func (s *Socket) Join(gameId, username, joinSecret string) error

Join creates a new player in the game and connects to it. Join panics if the socket is already connected to a game. Leave joinSecret empty if the game is not protected.

func (*Socket) NextEvent added in v0.4.0

func (s *Socket) NextEvent() (Event, bool, error)

NextEvent returns the next event in the queue or ok = false if there is none. Registered event listeners will be triggered.

func (*Socket) On

func (s *Socket) On(event EventName, callback EventCallback) CallbackId

On registers a callback that is triggered when the event is received.

func (*Socket) Once added in v0.7.0

func (s *Socket) Once(event EventName, callback EventCallback) CallbackId

Once registers a callback that is triggered only the first time the event is received.

func (*Socket) RemoveCallback

func (s *Socket) RemoveCallback(id CallbackId)

RemoveCallback deletes the callback with the specified id.

func (*Socket) RestoreSession added in v0.5.0

func (s *Socket) RestoreSession(username string) error

RestoreSession tries to restore the session and use it to reconnect to the game. RestoreSession panics if the socket is already connected to a game.

func (*Socket) RunEventLoop added in v0.4.0

func (s *Socket) RunEventLoop() error

RunEventLoop starts listening for events and triggers registered event listeners. Returns on close or error.

func (*Socket) Send added in v0.4.0

func (s *Socket) Send(name CommandName, data any) error

Send sends a new command to the server. Send panics if the socket is not connected to a player.

func (*Socket) Session added in v0.6.1

func (s *Socket) Session() Session

Session returns details of the current session.

func (*Socket) Spectate added in v0.6.0

func (s *Socket) Spectate(gameId string) error

Spectate joins the game as a spectator. Spectate panics if the socket is already connected to a game.

func (*Socket) Username added in v0.9.0

func (s *Socket) Username(playerId string) string

Username returns the username associated with playerId.

Jump to

Keyboard shortcuts

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