@@ -13,12 +13,16 @@ import (
13
13
"github.com/gorcon/rcon"
14
14
)
15
15
16
+ // Client represents a client to a Palword RCON server.
16
17
type Client struct {
17
18
address string
18
19
password string
19
20
conn * rcon.Conn
20
21
}
21
22
23
+ // NewClient creates a new [Client] with the given address and password. Note that this function does not attempt to
24
+ // connect to the RCON server, so the password is not validated at this time. Instead, the connection is established
25
+ // on-demand when a method on the [Client] is called.
22
26
func NewClient (address string , password string ) * Client {
23
27
client := & Client {
24
28
address : address ,
@@ -58,6 +62,8 @@ func (r *Client) executeWithRetry(command string, retry bool) (string, error) {
58
62
return strings .TrimSpace (response ), err
59
63
}
60
64
65
+ // KickPlayer instructs the server to ban the player with the given Steam ID from the server. The player must be online
66
+ // to be banned.
61
67
func (r * Client ) BanPlayer (steamID uint64 ) error {
62
68
response , err := r .executeWithRetry (fmt .Sprintf ("BanPlayer %d" , steamID ), true )
63
69
if err != nil {
@@ -68,6 +74,7 @@ func (r *Client) BanPlayer(steamID uint64) error {
68
74
return nil
69
75
}
70
76
77
+ // Broadcast displays the given message to all online players.
71
78
func (r * Client ) Broadcast (message string ) error {
72
79
response , err := r .executeWithRetry (fmt .Sprintf ("Broadcast %s" , message ), true )
73
80
if err != nil {
@@ -78,6 +85,7 @@ func (r *Client) Broadcast(message string) error {
78
85
return nil
79
86
}
80
87
88
+ // DoExit instructs the server to immediately exit.
81
89
func (r * Client ) DoExit () error {
82
90
response , err := r .executeWithRetry ("DoExit" , true )
83
91
if err != nil {
@@ -88,13 +96,15 @@ func (r *Client) DoExit() error {
88
96
return nil
89
97
}
90
98
99
+ // ServerInfo represents the information about the server returned by [Client.Info]().
91
100
type ServerInfo struct {
92
101
ServerName string
93
102
Version string
94
103
}
95
104
96
105
var infoRegex = regexp .MustCompile (`^Welcome to Pal Server\[v([\d\.]+)\]\s*(.*?)$` )
97
106
107
+ // Info returns information about the server.
98
108
func (r * Client ) Info () (* ServerInfo , error ) {
99
109
response , err := r .executeWithRetry ("Info" , true )
100
110
if err != nil {
@@ -110,6 +120,7 @@ func (r *Client) Info() (*ServerInfo, error) {
110
120
}, nil
111
121
}
112
122
123
+ // KickPlayer instructs the server to kick the player with the given Steam ID.
113
124
func (r * Client ) KickPlayer (steamID uint64 ) error {
114
125
response , err := r .executeWithRetry (fmt .Sprintf ("KickPlayer %d" , steamID ), true )
115
126
if err != nil {
@@ -120,6 +131,7 @@ func (r *Client) KickPlayer(steamID uint64) error {
120
131
return nil
121
132
}
122
133
134
+ // Save instructs the server to save the world to disk.
123
135
func (r * Client ) Save () error {
124
136
response , err := r .executeWithRetry ("Save" , true )
125
137
if err != nil {
@@ -130,12 +142,14 @@ func (r *Client) Save() error {
130
142
return nil
131
143
}
132
144
145
+ // Player is the representation of a single player.
133
146
type Player struct {
134
147
Name string
135
148
PlayerUID uint64
136
149
SteamID uint64
137
150
}
138
151
152
+ // ShowPlayers returns a list of all players that are currently online.
139
153
func (r * Client ) ShowPlayers () ([]Player , error ) {
140
154
players := []Player {}
141
155
response , err := r .executeWithRetry ("ShowPlayers" , true )
@@ -168,10 +182,13 @@ func (r *Client) ShowPlayers() ([]Player, error) {
168
182
return players , nil
169
183
}
170
184
185
+ // Shutdown instructs the server to shut down after the given number of seconds.
171
186
func (r * Client ) Shutdown (seconds int ) error {
172
187
return r .shutdown (fmt .Sprintf ("%d" , seconds ))
173
188
}
174
189
190
+ // ShutdownWithMessage instructs the server to shut down after the given number of seconds. The message will be
191
+ // displayed to all online players.
175
192
func (r * Client ) ShutdownWithMessage (seconds int , message string ) error {
176
193
return r .shutdown (fmt .Sprintf ("%d %s" , seconds , message ))
177
194
}
0 commit comments