-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
macros.go
158 lines (145 loc) · 3.65 KB
/
macros.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
package gopher
import (
"bufio"
"fmt"
"github.com/hewiefreeman/GopherGameServer/core"
"os"
"strconv"
"strings"
)
func macroListener() {
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("[Gopher] Command: ")
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
stop := handleMacro(text)
if stop {
return
}
}
}
func handleMacro(macro string) bool {
if macro == "pause" {
Pause()
} else if macro == "resume" {
Resume()
} else if macro == "shutdown" {
ShutDown()
return true
} else if macro == "version" {
fmt.Println(version)
} else if macro == "roomcount" {
fmt.Println("Room count: ", core.RoomCount())
} else if macro == "usercount" {
fmt.Println("User count: ", core.UserCount())
} else if len(macro) >= 12 && macro[0:10] == "deleteroom" {
macroDeleteRoom(macro)
} else if len(macro) >= 9 && macro[0:7] == "newroom" {
macroNewRoom(macro)
} else if len(macro) >= 9 && macro[0:7] == "getuser" {
macroGetUser(macro)
} else if len(macro) >= 9 && macro[0:7] == "getroom" {
macroGetRoom(macro)
} else if len(macro) >= 6 && macro[0:4] == "kick" {
macroKick(macro)
}
return false
}
func macroKick(macro string) {
userName := macro[5:]
user, userErr := core.GetUser(userName)
if userErr != nil {
fmt.Println(userErr)
return
}
user.Kick()
fmt.Println("Kicked user '" + userName + "'")
}
func macroNewRoom(macro string) {
s := strings.Split(macro, " ")
if len(s) != 5 {
fmt.Println("newroom expects 4 parameters (name string, rType string, isPrivate bool, maxUsers int)")
return
}
isPrivate := false
if s[3] == "true" || s[3] == "t" {
isPrivate = true
}
maxUsers, err := strconv.Atoi(s[4])
if err != nil {
fmt.Println("maxUsers must be an integer")
return
}
_, roomErr := core.NewRoom(s[1], s[2], isPrivate, maxUsers, "")
if roomErr != nil {
fmt.Println(roomErr)
return
}
fmt.Println("Created room '" + s[1] + "'")
}
func macroDeleteRoom(macro string) {
s := strings.Split(macro, " ")
if len(s) != 2 {
fmt.Println("deleteroom expects 1 parameter (name string)")
return
}
room, roomErr := core.GetRoom(s[1])
if roomErr != nil {
fmt.Println(roomErr)
return
}
deleteErr := room.Delete()
if deleteErr != nil {
fmt.Println(deleteErr)
return
}
fmt.Println("Deleted room '" + s[1] + "'")
}
func macroGetUser(macro string) {
s := strings.Split(macro, " ")
if len(s) != 2 {
fmt.Println("getuser expects 1 parameter (name string)")
return
}
user, userErr := core.GetUser(s[1])
if userErr != nil {
fmt.Println(userErr)
return
}
fmt.Println("-- User '" + s[1] + "' --")
fmt.Println("Status:", user.Status())
fmt.Println("Guest:", user.IsGuest())
fmt.Println("Connections:")
conns := user.ConnectionIDs()
for i := 0; i < len(conns); i++ {
fmt.Println(" [ ID: '"+conns[i]+"', Room: '"+user.RoomIn(conns[i]).Name()+"', Vars:", user.GetVariables(nil, conns[i]), "]")
}
fmt.Println("Friends:", user.Friends())
fmt.Println("Database ID:", user.DatabaseID())
}
func macroGetRoom(macro string) {
s := strings.Split(macro, " ")
if len(s) != 2 {
fmt.Println("getroom expects 1 parameter (name string)")
return
}
room, roomErr := core.GetRoom(s[1])
if roomErr != nil {
fmt.Println(roomErr)
return
}
invList, _ := room.InviteList()
usrMap, _ := room.GetUserMap()
fmt.Println("-- Room '" + s[1] + "' --")
fmt.Println("Type:", room.Type())
fmt.Println("Private:", room.IsPrivate())
fmt.Println("Owner:", room.Owner())
fmt.Println("Max Users:", room.MaxUsers())
users := make([]string, 0, len(usrMap))
for name := range usrMap {
users = append(users, name)
}
fmt.Println("Users:", "("+strconv.Itoa(room.NumUsers())+")", users)
fmt.Println("Invite List:", invList)
}