Skip to content

Instantly share code, notes, and snippets.

@Tanami
Created July 18, 2021 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tanami/303084f9f04a0eedf2ff93ef8f2664ee to your computer and use it in GitHub Desktop.
Save Tanami/303084f9f04a0eedf2ff93ef8f2664ee to your computer and use it in GitHub Desktop.
Simple A2S player count collation.
package main
import (
"time"
"fmt"
"github.com/rumblefrog/go-a2s"
)
var ips = [...]string{
"27.100.36.6:27015",
"146.185.214.33:27015",
"144.48.37.118:27015",
"144.48.37.114:27015",
"148.251.11.171:1338",
"31.186.250.236:27015",
"31.186.250.4:27015",
"136.243.94.194:27055",
"136.243.94.194:27015",
"148.251.79.234:42069",
"144.76.225.235:27015",
"148.251.11.171:27319",
"95.172.92.222:27015",
"95.217.200.57:27016",
"95.217.200.57:27015",
"78.46.80.174:27215",
"124.18.10.119:11292",
"60.111.209.149:27018",
"46.174.52.164:27015",
"212.76.128.170:27045",
"46.174.50.224:27015",
"31.186.250.15:27015",
"82.12.239.145:27015",
"74.201.72.19:27015",
"162.248.92.17:27015",
"74.91.119.208:27015",
"92.38.148.25:27015",
"34.75.123.120:27015",
"162.248.92.42:27015",
"74.91.126.160:27015",
"74.201.72.19:27016",
"192.223.24.34:27015",
"74.91.113.126:27015",
"72.5.195.96:27015",
}
type set struct {
key string
val int
}
func process(addr string, newCount chan<- set) {
client, err := a2s.NewClient(addr, a2s.TimeoutOption(time.Second * 5))
if err != nil {
newCount <- set{addr, 0}
} else {
defer client.Close()
info, err := client.QueryInfo()
if err != nil {
newCount <- set{addr, 0}
} else {
newCount <- set{addr, int(info.Players - info.Bots)}
}
}
}
func main() {
newCount := make(chan set)
getSum := make(chan chan int)
go func() {
servers := make(map[string]int)
for _, addr := range ips {
servers[addr] = 0
}
for {
select {
case set := <- newCount:
servers[set.key] = set.val
case resp := <- getSum:
total := 0
for k := range servers {
total += servers[k]
}
resp <- total
}
}
}()
go func() {
ticker := time.NewTicker(time.Second * 10)
for ; true; <-ticker.C {
for _, addr := range ips {
go process(addr, newCount)
}
}
}()
go func() {
ticker := time.NewTicker(time.Second * 1)
for; true; <-ticker.C {
sumThing := make(chan int)
getSum <- sumThing
fmt.Printf("total global players: %d\n", <-sumThing)
}
}()
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment