-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverlist.go
134 lines (123 loc) · 3.85 KB
/
serverlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"os"
"strings"
)
//ty to the awesome people at https://mholt.github.io/json-to-go/
type SrvList struct {
Total struct {
Servers int `json:"servers"`
Clients int `json:"clients"`
} `json:"total"`
TotalMax struct {
Servers int `json:"servers"`
Clients int `json:"clients"`
} `json:"total_max"`
List []struct {
Address string `json:"address"`
Clients int `json:"clients"`
ClientsList []string `json:"clients_list"`
ClientsMax int `json:"clients_max"`
Creative bool `json:"creative"`
Damage bool `json:"damage"`
Description string `json:"description"`
GameTime int `json:"game_time"`
Gameid string `json:"gameid"`
Lag float64 `json:"lag,omitempty"`
Name string `json:"name"`
Password bool `json:"password"`
Port int `json:"port"`
ProtoMax int `json:"proto_max"`
ProtoMin int `json:"proto_min"`
Pvp bool `json:"pvp"`
Uptime int `json:"uptime"`
URL string `json:"url,omitempty"`
Version string `json:"version"`
IP string `json:"ip"`
UpdateTime int `json:"update_time"`
Start int `json:"start"`
ClientsTop int `json:"clients_top"`
Updates int `json:"updates"`
TotalClients int `json:"total_clients"`
PopV float64 `json:"pop_v"`
Ping float64 `json:"ping"`
GeoContinent string `json:"geo_continent,omitempty"`
Dedicated bool `json:"dedicated,omitempty"`
Rollback bool `json:"rollback,omitempty"`
Mapgen string `json:"mapgen,omitempty"`
Privs string `json:"privs,omitempty"`
CanSeeFarNames bool `json:"can_see_far_names,omitempty"`
Mods []string `json:"mods,omitempty"`
ServerID string `json:"server_id,omitempty"`
Proto string `json:"proto,omitempty"`
} `json:"list"`
}
var CmdsWParam = []string{"address", "clients", "clients_list", "clients_max", "creative", "damage", "description", "game_time", "gameid", "lag", "name", "password",
"port", "proto_max", "proto_min", "pvp", "server_id", "uptime", "version", "ip", "update_time", "start", "clients_top", "dedicated", "rollback", "mapgen",
"privs", "can_see_far_names", "mods", "updates", "total_clients", "pop_v", "geo_continent", "ping"}
func main() {
args := os.Args[1:]
if len(args) == 0 {
log.Fatal("You must have an argument")
}
resp, err := http.Get("https://servers.minetest.net/list")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var srvlist *SrvList
json.Unmarshal(body, &srvlist)
var good = false
if len(args) == 1 {
arg := args[0]
if arg == "total_clients" {
log.Println(srvlist.Total.Clients)
good = true
} else if arg == "total_servers" {
log.Println(srvlist.Total.Servers)
good = true
} else if arg == "total_max_clients" {
log.Println(srvlist.TotalMax.Clients)
good = true
} else if arg == "total_max_servers" {
log.Println(srvlist.TotalMax.Servers)
good = true
} else if arg == "serverlist" {
log.Println(srvlist)
good = true
}
for _, command := range CmdsWParam {
if arg == command {
log.Println("You must put a servername after you say `" + command + "`")
good = true
}
}
}
if len(args) > 2 {
arg := args[0]
name := strings.Join(args[1:], " ")
servers := srvlist.List
for _, command := range CmdsWParam {
if arg == command {
for _, server := range servers {
if server.Name == name {
cmd := strings.Title(command)
log.Println(server[cmd]) //this bit not working
good = true
}
}
}
}
}
if !good {
log.Fatal("Invalid usage")
}
}