cg

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

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

This version implements the CodeGame specification v0.1.

CodeGame v0.2

Index

Constants

This section is empty.

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")
)

Functions

func IsStandardEvent

func IsStandardEvent(eventName EventName) bool

Types

type CallbackId

type CallbackId uuid.UUID

type Event

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

func (*Event) UnmarshalData

func (e *Event) UnmarshalData(targetObjPtr interface{}) error

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

type EventConnectData

type EventConnectData struct {
	// The ID of 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 EventConnectedData

type EventConnectedData struct {
}

type EventCreateGameData

type EventCreateGameData struct {
	// If public is set to true, the game will be listed publicly.
	Public bool `json:"public"`
}

type EventCreatedGameData

type EventCreatedGameData struct {
	// The ID of the game that was created.
	GameId string `json:"game_id"`
}

type EventErrorData

type EventErrorData struct {
	// The reason the error occured.
	Reason string `json:"reason"`
}

type EventGameInfoData

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

type EventJoinGameData

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

type EventJoinedGameData

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

type EventLeaveGameData

type EventLeaveGameData struct {
}

type EventLeftGameData

type EventLeftGameData struct {
}

type EventName

type EventName string
const EventConnect EventName = "connect"

The `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 EventConnected EventName = "connected"

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

const EventCreateGame EventName = "create_game"

Create a new game.

const EventCreatedGame EventName = "created_game"

The `created_game` event is the response of the server to the client, which sent the create_game event.

const EventError EventName = "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 EventGameInfo EventName = "game_info"

The `game_info` event is sent to every player that joins or connects to a game.

const EventJoinGame EventName = "join_game"

Join an existing game by ID.

const EventJoinedGame EventName = "joined_game"

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

const EventLeaveGame EventName = "leave_game"

The `leave_game` 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 EventLeftGame EventName = "left_game"

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

const EventPlayerSecret EventName = "player_secret"

The `player_secret` event is used to send a secret to the player that just joined so that they can reconnect and add other clients.

type EventPlayerSecretData

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

type OnEventCallback

type OnEventCallback func(origin string, event Event)

type Session

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

func RestoreSession

func RestoreSession(name string) (Session, error)

func (Session) Remove

func (s Session) Remove() error

func (Session) Save

func (s Session) Save() error

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(game string, wsURL string) (*Socket, error)

NewSocket opens a new websocket connection with the CodeGame server listening at wsURL and returns a new Connection struct.

func (*Socket) Close

func (s *Socket) Close() error

Close closes the underlying websocket connection.

func (*Socket) Connect

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

Connect sends a connect_game event to the server and returns once it receives a connected_game event.

func (*Socket) Create

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

Create sends a create_game event to the server and returns the gameId on success.

func (*Socket) Emit

func (s *Socket) Emit(eventName EventName, eventData interface{}) error

Emit sends a new event to the server.

func (*Socket) GetUser

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

Returns the username associated with playerId.

func (*Socket) Join

func (s *Socket) Join(gameId, username string) (Session, 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.

func (*Socket) Listen

func (s *Socket) Listen() error

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

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) OnOnce

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

OnOnce 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.

Jump to

Keyboard shortcuts

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