Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
example Go code to pull stats from an Arma 3 server via the RCON protocol
package main
import (
"flag"
"fmt"
steam "github.com/kidoman/go-steam"
"sort"
)
var addresses = []string{
"public.2-75thrangers.com:2303",
}
type SteamPlayers steam.PlayersInfoResponse
// implement the sort interface on a steam.PlayersInfoResponse-like type
// we can't add the sort interface to steam.PlayersInfoResponse because we're not part of the go-steam package, and
// so have to use SteamPlayers, which is a new type we created of type steam.PlayersInfoResponse for sort() purposes.
// len
func (d SteamPlayers) Len() int {
return len(d.Players)
}
// swap
func (d SteamPlayers) Swap(i, j int) {
d.Players[i], d.Players[j] = d.Players[j], d.Players[i]
}
// less
func (d SteamPlayers) Less(i, j int) bool {
return d.Players[i].Score < d.Players[j].Score
}
func main() {
// not really needed because i got lazy and put the IP in a slice as a global var <for shame>
flag.Parse()
// range through our list of IPs
for _, addr := range addresses {
// connect to the server
server, err := steam.Connect(addr)
if err != nil {
panic(err)
}
// save for later
defer server.Close()
// ping the server. if no ping, something is wrong
ping, err := server.Ping()
if err != nil {
fmt.Printf("steam: could not ping %v: %v", addr, err)
continue
}
// we're good so far. lets get the server info
info, err := server.Info()
if err != nil {
fmt.Printf("steam: could not get server info from %v: %v", addr, err)
continue
}
// still good, phew! lets get the player info
playersInfo, err := server.PlayersInfo()
if err != nil {
fmt.Printf("steam: could not get players info from %v: %v", addr, err)
continue
}
// we've got all the info we need from the server. lets print out some stuff.
fmt.Printf(" Server | %v\n", info.Name)
fmt.Printf(" Name | %s\n", info.Game)
fmt.Printf(" Ping | %s\n", ping)
fmt.Printf(" Type | %s\n", info.ServerType)
fmt.Printf("Version | %s\n", info.Version)
fmt.Printf("Players | %d/%d\n", info.Players, info.MaxPlayers)
fmt.Printf(" Map | %s\n", info.Map)
fmt.Printf(" IP | %s\n", addr)
// if there's at least 1x player, lets print out their info
if len(playersInfo.Players) > 0 {
fmt.Printf("--------|-----------------------------------------\n")
fmt.Printf(" Score | Time On | Player Name\n")
fmt.Printf("--------|-----------------------------------------\n")
var temp SteamPlayers
temp.Players = playersInfo.Players
sort.Sort(sort.Reverse(temp))
for _, player := range temp.Players {
fmt.Printf("%7d | %7d | %s\n", player.Score, int(player.Duration / 60), player.Name)
}
fmt.Printf("\n")
}
}
}
func must(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.