cg

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2022 License: MIT Imports: 19 Imported by: 10

Documentation

Overview

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

Index

Constants

View Source
const (
	DebugError   = "error"
	DebugWarning = "warning"
	DebugInfo    = "info"
	DebugTrace   = "trace"
)
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")
)

Functions

This section is empty.

Types

type Command added in v0.9.0

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

func (*Command) UnmarshalData added in v0.9.0

func (c *Command) UnmarshalData(targetObjPtr any) error

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

type CommandName added in v0.9.0

type CommandName string

type CommandWrapper added in v0.9.0

type CommandWrapper struct {
	Origin *Player
	Cmd    Command
}

type DebugSeverity added in v0.9.0

type DebugSeverity string

type Event

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

type EventName

type EventName string

type EventSender added in v0.3.1

type EventSender interface {
	Send(event EventName, data any) error
}

type EventWrapper added in v0.3.0

type EventWrapper struct {
	Player *Player
	Event  Event
}

type Game

type Game struct {
	Id string

	OnPlayerJoined          func(player *Player)
	OnPlayerLeft            func(player *Player)
	OnPlayerSocketConnected func(player *Player, socket *GameSocket)
	OnSpectatorConnected    func(socket *GameSocket)

	Log *Logger
	// contains filtered or unexported fields
}

func (*Game) Close added in v0.3.1

func (g *Game) Close() error

Stop the game, disconnect all players and remove it from the server.

func (*Game) GetPlayer added in v0.3.0

func (g *Game) GetPlayer(playerId string) (*Player, bool)

GetPlayer returns a player in the game by id.

func (*Game) NextCommand added in v0.9.0

func (g *Game) NextCommand() (CommandWrapper, bool)

NextCommand returns the next command in the queue or ok = false if there is none.

func (*Game) Running added in v0.3.0

func (g *Game) Running() bool

Returns true if the game has not already been closed.

func (*Game) Send added in v0.3.0

func (g *Game) Send(event EventName, data any) error

Send sends the event to all players currently in the game.

func (*Game) SetConfig added in v0.9.0

func (g *Game) SetConfig(config any)

Set game config data. This should be a struct of type GameConfig. It is required to call this function in order for some API endpoints to work.

func (*Game) WaitForNextCommand added in v0.9.0

func (g *Game) WaitForNextCommand() (CommandWrapper, bool)

WaitForNextCommand waits for and then returns the next command in the queue or ok = false if the game has been closed.

type GameSocket added in v0.9.0

type GameSocket struct {
	Id string
	// contains filtered or unexported fields
}

func (*GameSocket) Send added in v0.9.1

func (s *GameSocket) Send(event EventName, data any) error

Send sends the event the socket.

type Logger added in v0.9.0

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

func NewLogger added in v0.9.0

func NewLogger(printMessages bool) *Logger

func (*Logger) Close added in v0.9.0

func (l *Logger) Close() error

func (*Logger) Error added in v0.9.0

func (l *Logger) Error(format string, a ...any)

func (*Logger) ErrorData added in v0.9.0

func (l *Logger) ErrorData(data any, format string, a ...any)

func (*Logger) Info added in v0.9.0

func (l *Logger) Info(format string, a ...any)

func (*Logger) InfoData added in v0.9.0

func (l *Logger) InfoData(data any, format string, a ...any)

func (*Logger) Log added in v0.9.0

func (l *Logger) Log(severity DebugSeverity, data any, format string, a ...any)

func (*Logger) Trace added in v0.9.0

func (l *Logger) Trace(format string, a ...any)

func (*Logger) TraceData added in v0.9.0

func (l *Logger) TraceData(data any, format string, a ...any)

func (*Logger) Warning added in v0.9.0

func (l *Logger) Warning(format string, a ...any)

func (*Logger) WarningData added in v0.9.0

func (l *Logger) WarningData(data any, format string, a ...any)

type Player

type Player struct {
	Id       string
	Username string
	Secret   string

	Log *Logger
	// contains filtered or unexported fields
}

func (*Player) Leave added in v0.7.2

func (p *Player) Leave() error

Leave leaves the game.

func (*Player) Send

func (p *Player) Send(event EventName, data any) error

Send sends the event to all sockets currently connected to the player. Events are added to a queue in case there are no sockets. The next socket to connect to the player will then receive the missed events.

func (*Player) SocketCount added in v0.8.1

func (p *Player) SocketCount() int

SocketCount returns the amount of sockets currently connected to the player.

type Server

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

func NewServer

func NewServer(name string, config ServerConfig) *Server

func (*Server) Run

func (s *Server) Run(runGameFunc func(game *Game, config json.RawMessage))

Run starts the webserver and listens for new connections.

type ServerConfig

type ServerConfig struct {
	// The port to listen on for new websocket connections. (default: 80)
	Port int
	// The path to the CGE file for the current game.
	CGEFilepath string
	// All files in this direcory will be served.
	WebRoot string
	// The maximum number of allowed sockets per player (0 => unlimited).
	MaxSocketsPerPlayer int
	// The maximum number of allowed players per game (0 => unlimited).
	MaxPlayersPerGame int
	// The maximum number of allowed spectators per game (0 => unlimited).
	MaxSpectatorsPerGame int
	// The maximum number of games (0 => unlimited).
	MaxGames int
	// The time after which game with no connected sockets will be deleted. (0 => never)
	DeleteInactiveGameDelay time.Duration
	// The time after which a player without sockets will be kicked. (0 => never)
	KickInactivePlayerDelay time.Duration
	// The name of the game in snake_case.
	Name string
	// The name of the game that will be displayed to the user.
	DisplayName string
	// The version of the game.
	Version string
	// The description of the game.
	Description string
	// The URL to the code repository of the game.
	RepositoryURL string
	// The time after which an inactive websocket connection will be closed. (default: 15 minutes)
	WebsocketTimeout time.Duration
}

Jump to

Keyboard shortcuts

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