Documentation
¶
Overview ¶
Package cg implements common client logic for connecting with a CodeGame server and handling events.
CodeGame v0.3
Index ¶
- Variables
- func IsStandardEvent(eventName EventName) bool
- type CallbackId
- type Event
- type EventConnectData
- type EventConnectedData
- type EventCreateData
- type EventCreatedData
- type EventErrorData
- type EventInfoData
- type EventJoinData
- type EventJoinedData
- type EventLeaveData
- type EventLeftData
- type EventName
- type EventNewPlayerData
- type OnEventCallback
- type Session
- type Socket
- func (s *Socket) Close() error
- func (s *Socket) Connect(gameId, playerId, secret string) error
- func (s *Socket) Create(public bool) (string, error)
- func (s *Socket) Emit(eventName EventName, eventData any) error
- func (s *Socket) GetUser(playerId string) string
- func (s *Socket) Join(gameId, username string) (Session, error)
- func (s *Socket) Leave() error
- func (s *Socket) Listen() error
- func (s *Socket) On(event EventName, callback OnEventCallback) CallbackId
- func (s *Socket) OnOnce(event EventName, callback OnEventCallback) CallbackId
- func (s *Socket) RemoveCallback(id CallbackId)
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func IsStandardEvent ¶
IsStandardEvent returns true if eventName is a standard event.
Types ¶
type CallbackId ¶
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 EventConnectData ¶
type EventConnectedData ¶
type EventConnectedData struct {
}
type EventCreateData ¶ added in v0.3.0
type EventCreateData struct {
// If public is set to true, the game will be listed publicly.
Public bool `json:"public"`
}
type EventCreatedData ¶ added in v0.3.0
type EventCreatedData 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 EventInfoData ¶ added in v0.3.0
type EventJoinData ¶ added in v0.3.0
type EventJoinedData ¶ added in v0.3.0
type EventJoinedData struct {
// The player secret.
Secret string `json:"secret"`
}
type EventLeaveData ¶ added in v0.3.0
type EventLeaveData struct {
}
type EventLeftData ¶ added in v0.3.0
type EventLeftData struct {
}
type EventName ¶
type EventName string
const EventConnect 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 EventConnected EventName = "cg_connected"
The `cg_connected` event is sent to the socket that has connected.
const EventCreate EventName = "cg_create"
Create a new game.
const EventCreated EventName = "cg_created"
The `cg_created` event is the response of the server to the client, which sent the create_game event.
const EventError 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 EventInfo EventName = "cg_info"
The `cg_info` event is sent to every player that joins or connects to a game and catches them up on things that may have happened before they were connected.
const EventJoin EventName = "cg_join"
Join an existing game by ID.
const EventJoined 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 EventLeave 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 EventLeft EventName = "cg_left"
The `cg_left` event is sent to everyone in the game when someone leaves it.
const EventNewPlayer EventName = "cg_new_player"
The `new_player` event is sent to everyone in the game when someone joins it.
type EventNewPlayerData ¶ added in v0.3.0
type EventNewPlayerData struct {
// The username of the newly joined player.
Username string `json:"username"`
}
type OnEventCallback ¶
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 ¶
type Socket ¶
type Socket struct {
// contains filtered or unexported fields
}
Socket represents the connection with a CodeGame server and handles events.
func NewSocket ¶
NewSocket opens a new websocket connection with the CodeGame server listening at wsURL and returns a new Connection struct.
func (*Socket) Connect ¶
Connect sends a connect_game event to the server and returns once it receives a connected_game event.
func (*Socket) Create ¶
Create sends a create_game event to the server and returns the gameId on success.
func (*Socket) Join ¶
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 ¶
Leave sends a leave_game event to the server and clears all non-standard event listeners.
func (*Socket) Listen ¶
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.