cg

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: MIT Imports: 15 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 CGVersion = "0.6"

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 IsStandardEvent

func IsStandardEvent(eventName EventName) bool

IsStandardEvent returns true if eventName is a standard event.

Types

type CallbackId

type CallbackId uuid.UUID

type ConnectEventData added in v0.8.0

type ConnectEventData struct {
	// The ID of the game to connect to.
	GameId string `json:"game_id"`
	// The ID of the player to connect to.
	PlayerId string `json:"player_id"`
	// The secret of the player to connect to.
	Secret string `json:"secret"`
}

type ConnectedEventData added in v0.8.0

type ConnectedEventData struct {
	// The username of the player.
	Username string `json:"username"`
}

type ErrorEventData added in v0.8.0

type ErrorEventData struct {
	// The error message.
	Message string `json:"message"`
}

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 EventName

type EventName string
const ConnectEvent EventName = "cg_connect"

The `cg_connect` event is used to associate a client with an existing player. This event is used after making changes to ones program and reconnecting to the game or when adding another client like a viewer in the webbrowser.

const ConnectedEvent EventName = "cg_connected"

The `cg_connected` event is sent to the socket that has connected.

const ErrorEvent EventName = "cg_error"

The error event is sent to the client that triggered the error. The error event should only be used for technical errors such as event deserialisation errors. If something in the game doesn’t work intentionally or a very specific error that requires handeling by the client occurs, a custom event should be used.

const InfoEvent EventName = "cg_info"

The `cg_info` event is sent to every player that joins, connects to or spectates a game and catches them up on things that may have happened before they were connected.

const JoinEvent EventName = "cg_join"

Join an existing game by ID.

const JoinedEvent EventName = "cg_joined"

The `cg_joined` event is used to send a secret to the player that just joined so that they can reconnect and add other clients. It also confirms that the game has been joined successfully.

const LeaveEvent EventName = "cg_leave"

The `cg_leave` event is used to leave a game which is the preferred way to exit a game in comparison to just disconnecting and never reconnecting. It is not required to send this event due to how hard it is to detect if the user has disconnected for good or is just re-writing their program.

const LeftEvent EventName = "cg_left"

The `cg_left` event is sent to everyone in the game when someone leaves it.

const NewPlayerEvent EventName = "cg_new_player"

The `new_player` event is sent to everyone in the game when someone joins it.

const SpectateEvent EventName = "cg_spectate"

The `cg_spectate` event is used to spectate a game. Spectators receive all public game events but cannot send any.

type EventWrapper added in v0.4.0

type EventWrapper struct {
	Origin string `json:"origin"`
	Event  Event  `json:"event"`
}

A wrapper struct around and event and its origin.

type InfoEventData added in v0.8.0

type InfoEventData struct {
	// The IDs of all players currently in the game mapped to their respective usernames.
	Players map[string]string `json:"players"`
}

type JoinEventData added in v0.8.0

type JoinEventData struct {
	// The ID of the game to join.
	GameId string `json:"game_id"`
	// The name of your new user.
	Username string `json:"username"`
}

type JoinedEventData added in v0.8.0

type JoinedEventData struct {
	// The player secret.
	Secret string `json:"secret"`
}

type LeaveEventData added in v0.8.0

type LeaveEventData struct {
}

type LeftEventData added in v0.8.0

type LeftEventData struct {
}

type NewPlayerEventData added in v0.8.0

type NewPlayerEventData struct {
	// The username of the newly joined player.
	Username string `json:"username"`
}

type OnEventCallback

type OnEventCallback func(origin string, event Event)

type Session

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

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 opens a new websocket connection with the CodeGame server listening at the URL (e.g. my-game.io) and returns a new Connection struct. 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.

func (*Socket) Create

func (s *Socket) Create(public bool) (string, error)

Create creates a new game on the server and returns the id of the created game.

func (*Socket) Join

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

Join sends a join_game event to the server and returns a Session object once it receives a joined_game and a player_secret event.

func (*Socket) Leave

func (s *Socket) Leave() error

Leave sends a leave_game event to the server and clears all non-standard event listeners. It also deletes the current session.

func (*Socket) NextEvent added in v0.4.0

func (s *Socket) NextEvent() (EventWrapper, 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 OnEventCallback) CallbackId

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

func (*Socket) Once added in v0.7.0

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

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

func (*Socket) RemoveCallback

func (s *Socket) RemoveCallback(id CallbackId)

RemoveCallback deletes the callback with the specified id.

func (*Socket) ResolveUsername added in v0.5.0

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

ResolveUsername returns the username associated with playerId.

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.

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(eventName EventName, eventData any) error

Send sends a new event to the server.

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.

type SpectateEventData added in v0.8.0

type SpectateEventData struct {
	// The ID of the game to spectate.
	GameId string `json:"game_id"`
}

Jump to

Keyboard shortcuts

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