Skip to content

Commit

Permalink
Add type and function docs (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
invliD authored Feb 18, 2024
1 parent b917928 commit 9ac3f62
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions rcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import (
"github.com/gorcon/rcon"
)

// Client represents a client to a Palword RCON server. Only use [NewClient] to instantiate a client.
type Client struct {
address string
password string
conn *rcon.Conn
}

// NewClient creates a new [Client] with the given address and password. Note that this function does not attempt to
// connect to the RCON server, so the password is not validated at this time. Instead, the connection is established
// on-demand when a method on the [Client] is called.
func NewClient(address string, password string) *Client {
client := &Client{
address: address,
Expand Down Expand Up @@ -58,6 +62,8 @@ func (r *Client) executeWithRetry(command string, retry bool) (string, error) {
return strings.TrimSpace(response), err
}

// KickPlayer instructs the server to ban the player with the given Steam ID from the server. The player must be online
// to be banned.
func (r *Client) BanPlayer(steamID uint64) error {
response, err := r.executeWithRetry(fmt.Sprintf("BanPlayer %d", steamID), true)
if err != nil {
Expand All @@ -68,6 +74,7 @@ func (r *Client) BanPlayer(steamID uint64) error {
return nil
}

// Broadcast displays the given message to all online players.
func (r *Client) Broadcast(message string) error {
response, err := r.executeWithRetry(fmt.Sprintf("Broadcast %s", message), true)
if err != nil {
Expand All @@ -78,6 +85,7 @@ func (r *Client) Broadcast(message string) error {
return nil
}

// DoExit instructs the server to immediately exit.
func (r *Client) DoExit() error {
response, err := r.executeWithRetry("DoExit", true)
if err != nil {
Expand All @@ -88,13 +96,15 @@ func (r *Client) DoExit() error {
return nil
}

// ServerInfo represents the information about the server returned by [Client.Info]().
type ServerInfo struct {
ServerName string
Version string
}

var infoRegex = regexp.MustCompile(`^Welcome to Pal Server\[v([\d\.]+)\]\s*(.*?)$`)

// Info returns information about the server.
func (r *Client) Info() (*ServerInfo, error) {
response, err := r.executeWithRetry("Info", true)
if err != nil {
Expand All @@ -110,6 +120,7 @@ func (r *Client) Info() (*ServerInfo, error) {
}, nil
}

// KickPlayer instructs the server to kick the player with the given Steam ID.
func (r *Client) KickPlayer(steamID uint64) error {
response, err := r.executeWithRetry(fmt.Sprintf("KickPlayer %d", steamID), true)
if err != nil {
Expand All @@ -120,6 +131,7 @@ func (r *Client) KickPlayer(steamID uint64) error {
return nil
}

// Save instructs the server to save the world to disk.
func (r *Client) Save() error {
response, err := r.executeWithRetry("Save", true)
if err != nil {
Expand All @@ -130,12 +142,14 @@ func (r *Client) Save() error {
return nil
}

// Player is the representation of a single player.
type Player struct {
Name string
PlayerUID uint64
SteamID uint64
}

// ShowPlayers returns a list of all players that are currently online.
func (r *Client) ShowPlayers() ([]Player, error) {
players := []Player{}
response, err := r.executeWithRetry("ShowPlayers", true)
Expand Down Expand Up @@ -168,10 +182,13 @@ func (r *Client) ShowPlayers() ([]Player, error) {
return players, nil
}

// Shutdown instructs the server to shut down after the given number of seconds.
func (r *Client) Shutdown(seconds int) error {
return r.shutdown(fmt.Sprintf("%d", seconds))
}

// ShutdownWithMessage instructs the server to shut down after the given number of seconds. The message will be
// displayed to all online players.
func (r *Client) ShutdownWithMessage(seconds int, message string) error {
return r.shutdown(fmt.Sprintf("%d %s", seconds, message))
}
Expand Down

0 comments on commit 9ac3f62

Please sign in to comment.