Documentation ¶
Overview ¶
Package cg implements common server logic for connecting with a CodeGame clients and handling events.
Index ¶
- Constants
- Variables
- type Command
- type CommandName
- type CommandWrapper
- type DebugSeverity
- type Event
- type EventName
- type EventSender
- type EventWrapper
- type Game
- func (g *Game) Close() error
- func (g *Game) GetPlayer(playerId string) (*Player, bool)
- func (g *Game) NextCommand() (CommandWrapper, bool)
- func (g *Game) Running() bool
- func (g *Game) Send(event EventName, data any) error
- func (g *Game) SetConfig(config any)
- func (g *Game) WaitForNextCommand() (CommandWrapper, bool)
- type GameSocket
- type Logger
- func (l *Logger) Close() error
- func (l *Logger) Error(format string, a ...any)
- func (l *Logger) ErrorData(data any, format string, a ...any)
- func (l *Logger) Info(format string, a ...any)
- func (l *Logger) InfoData(data any, format string, a ...any)
- func (l *Logger) Log(severity DebugSeverity, data any, format string, a ...any)
- func (l *Logger) Trace(format string, a ...any)
- func (l *Logger) TraceData(data any, format string, a ...any)
- func (l *Logger) Warning(format string, a ...any)
- func (l *Logger) WarningData(data any, format string, a ...any)
- type Player
- type Server
- type ServerConfig
Constants ¶
const ( DebugError = "error" DebugWarning = "warning" DebugInfo = "info" DebugTrace = "trace" )
const CGVersion = "0.7"
Variables ¶
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
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 DebugSeverity ¶ added in v0.9.0
type DebugSeverity string
type Event ¶
type Event struct { Name EventName `json:"name"` Data json.RawMessage `json:"data"` }
type EventSender ¶ added in v0.3.1
type EventWrapper ¶ added in v0.3.0
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
Stop the game, disconnect all players and remove it from the server.
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) SetConfig ¶ added in v0.9.0
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 }
type Logger ¶ added in v0.9.0
type Logger struct {
// contains filtered or unexported fields
}
func (*Logger) Log ¶ added in v0.9.0
func (l *Logger) Log(severity DebugSeverity, 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) Send ¶
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
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
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 }