-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (176 loc) · 4.71 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"time"
"github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/chat"
"github.com/google/uuid"
"github.com/icza/gowut/gwu"
)
const (
background = "#000034"
background1 = "#08275e"
background2 = "#404f8c"
foreground = "#ffffff"
width = "600"
list_height = "400"
font_size = "40"
)
type serverResponse struct {
}
type sessHandler struct{}
func connect(ip string) {
}
type Config struct {
Port int
Address string
}
var (
config Config
)
type Status struct {
Description chat.Message
Players struct {
Max int
Online int
Sample []struct {
ID uuid.UUID
Name string
}
}
Version struct {
Name string
Protocol int
}
Favicon string
}
func style(c gwu.Comp) {
c.Style().SetBackground(background)
c.Style().SetColor(foreground)
}
func (h sessHandler) Created(s gwu.Session) {
win := gwu.NewWindow("app", "Ping Utility")
win.Style().SetBackground(background)
win.Style().SetColor(foreground)
win.Style().SetFullWidth()
win.SetHAlign(gwu.HACenter)
win.AddHeadHTML(`<body style = "background-color: #000034;">`)
tabpanel := gwu.NewTabPanel()
tabpanel.Style().SetWidth(width)
tabpanel.SetHAlign(gwu.HACenter)
//build input
inputpanel := gwu.NewPanel()
horizpanel := gwu.NewHorizontalPanel()
inputpanel.SetHAlign(gwu.HACenter)
inputpanel.Style().SetColor(foreground)
//address part
address_panel := gwu.NewPanel()
style(address_panel)
label := gwu.NewLabel("Enter server address:")
address_panel.Add(label)
inputfield := gwu.NewTextBox("")
address_panel.Add(inputfield)
horizpanel.Add(address_panel)
//port part
port_panel := gwu.NewPanel()
style(port_panel)
port_label := gwu.NewLabel("Enter server port:")
port_panel.Add(port_label)
portinputfield := gwu.NewTextBox("25565")
port_panel.Add(portinputfield)
horizpanel.Add(port_panel)
inputpanel.Add(horizpanel)
connectbutton := gwu.NewButton("Connect")
resultpanel := gwu.NewPanel()
connectbutton.AddEHandlerFunc(func(e gwu.Event) {
text := inputfield.Text()
inputfield.SetText("")
port := portinputfield.Text()
portinputfield.SetText("25565")
address := text + ":" + port
response, delay, err := bot.PingAndListTimeout(address, 5*time.Second)
if err != nil {
fmt.Println("Fail:", err)
e.MarkDirty(inputfield, portinputfield)
return
}
var decode Status
err = json.Unmarshal(response, &decode)
if err != nil {
fmt.Println("Fail unmarshaling: ", err)
}
//fmt.Println(decode)
fmt.Println("Delay: ", delay)
//build result tab
tabpanel.Remove(resultpanel)
resultpanel = gwu.NewPanel()
style(resultpanel)
resultpanel.SetHAlign(gwu.HACenter)
//favicon
favicon := gwu.NewImage("Favicon", decode.Favicon)
resultpanel.Add(favicon)
//blank space
resultpanel.Add(gwu.NewLabel(""))
//motd
resultpanel.Add(gwu.NewLabel(decode.Description.ClearString()))
//version string
resultpanel.Add(gwu.NewLabel("Version: " + chat.Text(decode.Version.Name).ClearString()))
//protocol
resultpanel.Add(gwu.NewLabel("Protocol: " + strconv.Itoa(decode.Version.Protocol)))
//online
resultpanel.Add(gwu.NewLabel("Online Players / Max Players: " + strconv.Itoa(decode.Players.Online) + "/" + strconv.Itoa(decode.Players.Max)))
//blank space
//playerlist
var players []string
playerlist := gwu.NewListBox(players)
//playerlist.Style().SetWidth("50")
uuids := ""
for index, player := range decode.Players.Sample {
index = index
players = append(players, player.Name)
uuids = uuids + "\n" + player.ID.String()
}
playerlist.SetValues(players)
playerlist.SetToolTip(uuids)
if len(players) > 0 {
resultpanel.Add(playerlist)
}
tabpanel.AddString("Result", resultpanel)
tabpanel.SetSelected(1)
e.MarkDirty(tabpanel)
}, gwu.ETypeClick)
inputpanel.Add(connectbutton)
//add stuff
tabpanel.AddString("Input", inputpanel)
title := gwu.NewLabel("Minecraft Ping Utility")
subtitle := gwu.NewLabel("Utility for geting info about Minecraft servers")
title.Style().SetFontSize("30")
win.Add(title)
win.Add(subtitle)
win.Add(tabpanel)
s.AddWin(win)
}
func (h sessHandler) Removed(s gwu.Session) {}
func main() {
var config Config
config.Address = "localhost"
config.Port = 8085
config_file, err := ioutil.ReadFile("config.json")
fmt.Println("File read error: ", err)
if err == nil {
err2 := json.Unmarshal(config_file, &config)
if err2 != nil {
fmt.Println("Error while unmarshalling: ", err2)
}
}
fmt.Println("Address: " + config.Address)
fmt.Println("Port: " + strconv.Itoa(config.Port))
fmt.Println("Starting")
server := gwu.NewServer("", config.Address+":"+strconv.Itoa(config.Port))
server.AddSessCreatorName("app", "Ping Utility")
server.AddSHandler(sessHandler{})
server.Start()
}