Documentation ¶
Overview ¶
Package cg implements common client logic for connecting with a CodeGame server and handling events.
Index ¶
- Constants
- Variables
- func FetchGameConfig[T any](socket *Socket, gameId string) (T, error)
- type CallbackId
- type Command
- type CommandName
- type DebugMessageCallback
- type DebugSeverity
- type DebugSocket
- func (s *DebugSocket) Close() error
- func (s *DebugSocket) DebugGame(gameId string) error
- func (s *DebugSocket) DebugPlayer(gameId, playerId, playerSecret string) error
- func (s *DebugSocket) DebugServer() error
- func (s *DebugSocket) OnMessage(callback DebugMessageCallback) CallbackId
- func (s *DebugSocket) RemoveCallback(id CallbackId)
- func (s *DebugSocket) SetSeverities(enableTrace, enableInfo, enableWarning, enableError bool)
- func (s *DebugSocket) URL() string
- type Event
- type EventCallback
- type EventName
- type Session
- type Socket
- func (s *Socket) Close() error
- func (s *Socket) Connect(gameId, playerId, playerSecret string) error
- func (s *Socket) CreateGame(public, protected bool, config any) (gameId, joinSecret string, err error)
- func (s *Socket) Join(gameId, username, joinSecret string) error
- func (s *Socket) NextEvent() (Event, bool, error)
- func (s *Socket) On(event EventName, callback EventCallback) CallbackId
- func (s *Socket) Once(event EventName, callback EventCallback) CallbackId
- func (s *Socket) RemoveCallback(id CallbackId)
- func (s *Socket) RestoreSession(username string) error
- func (s *Socket) RunEventLoop() error
- func (s *Socket) Send(name CommandName, data any) error
- func (s *Socket) Session() Session
- func (s *Socket) Spectate(gameId string) error
- func (s *Socket) Username(playerId string) string
Constants ¶
const ( DebugError = "error" DebugWarning = "warning" DebugInfo = "info" DebugTrace = "trace" )
const ( Reset color = "\x1b[0m" Red color = "\x1b[31m" Green color = "\x1b[32m" Yellow color = "\x1b[33m" )
const CGVersion = "0.7"
Variables ¶
Functions ¶
Types ¶
type CallbackId ¶
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 ¶
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 Socket ¶
type Socket struct {
// contains filtered or unexported fields
}
Socket represents the connection with a CodeGame server and handles events.
func NewSocket ¶
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) Connect ¶
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 ¶
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
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
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
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.