forked from packetflinger/q2admin-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
212 lines (178 loc) · 4.74 KB
/
command.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
212
package main
import (
"errors"
"fmt"
"log"
"sort"
//"strings"
"time"
)
/**
* Player issued the teleport command.
*
* If a destination is supplied, just send the player there,
* else send a list of possibilities
*/
func (cl *Client) Teleport() {
pl := ReadByte(&cl.Message)
dest := ReadString(&cl.Message)
p := cl.FindPlayer(int(pl))
now := time.Now().Unix()
log.Printf("[%s/TELEPORT/%s] %s\n", p.Name, p.Name, dest)
if dest == "" {
listtime := now - p.LastTeleportList
if listtime < 30 {
txt := fmt.Sprintf("You can't list teleport destinations for %d more seconds\n", 30-listtime)
cl.SayPlayer(p, PRINT_HIGH, txt)
return
}
p.LastTeleportList = now
avail := TeleportAvailableReply()
cl.SayPlayer(p, PRINT_CHAT, avail)
cl.SayPlayer(p, PRINT_CHAT, "Active Servers\n")
line := ""
for _, c := range q2a.clients {
if len(c.Players) == 0 {
continue
}
players := ""
for _, p := range c.Players {
players = fmt.Sprintf("%s %s", players, p.Name)
}
line = fmt.Sprintf(" %-15s %-15s %s\n", c.Name, c.CurrentMap, players)
cl.SayPlayer(p, PRINT_CHAT, line)
}
return
}
s, err := FindTeleportDestination(dest)
p.LastTeleport = now
p.Teleports++
if err != nil {
log.Println("warning,", err)
cl.SayPlayer(p, PRINT_HIGH, "Unknown destination\n")
} else {
txt := fmt.Sprintf("Teleporting %s to %s [%s:%d]\n", p.Name, s.Name, s.IPAddress, s.Port)
cl.SayEveryone(PRINT_HIGH, txt)
st := fmt.Sprintf("connect %s:%d\n", s.IPAddress, s.Port)
cl.StuffPlayer(*p, st)
}
txt := fmt.Sprintf("TELEPORT [%d] %s", pl, p.Name)
LogEventToDatabase(cl.ID, LogTypeCommand, txt)
}
/**
* Resolve a teleport name to an ip:port
*/
func FindTeleportDestination(dest string) (*Client, error) {
for i, c := range q2a.clients {
if c.Name == dest {
return &q2a.clients[i], nil
}
}
return nil, errors.New("unknown destination")
}
func TeleportAvailableReply() string {
var allservers []string
for _, c := range q2a.clients {
if !c.Connected {
continue
}
allservers = append(allservers, c.Name)
}
// alphabetize the list
sort.Strings(allservers)
serverstr := "Available Servers:"
for _, s := range allservers {
serverstr = fmt.Sprintf("%s %s", serverstr, s)
}
serverstr = fmt.Sprintf("%s\n", serverstr)
return serverstr
}
/**
* Player issued an invite command.
*
* Broadcast the invite to all connected servers
*/
func (cl *Client) Invite() {
client := ReadByte(&cl.Message)
text := ReadString(&cl.Message)
p := cl.FindPlayer(int(client))
log.Printf("[%s/INVITE/%s] %s\n", cl.Name, p.Name, text)
now := time.Now().Unix()
invtime := now - p.LastInvite
if p.InvitesAvailable == 0 {
if invtime > 600 {
p.InvitesAvailable = 3
} else {
txt := fmt.Sprintf("You have no more invites available, wait %d seconds\n", 600-invtime)
cl.SayPlayer(p, PRINT_HIGH, txt)
return
}
} else {
if invtime < 30 {
txt := fmt.Sprintf("Invite used too recently, wait %d seconds\n", 30-invtime)
cl.SayPlayer(p, PRINT_HIGH, txt)
return
}
}
inv := fmt.Sprintf("%s invites you to play at %s (%s:%d)", p.Name, cl.Name, cl.IPAddress, cl.Port)
for _, s := range q2a.clients {
if s.Enabled && s.Connected {
s.SayEveryone(PRINT_CHAT, inv)
}
}
p.Invites++
p.LastInvite = now
p.InvitesAvailable--
}
func (cl *Client) ConsoleSay(print string) {
if print == "" {
return
}
txt := fmt.Sprintf("say %s\n", print)
WriteByte(SCMDCommand, &cl.MessageOut)
WriteString(txt, &cl.MessageOut)
}
/**
* Force a player to do a command
*/
func (cl *Client) StuffPlayer(p Player, cmd string) {
stuffcmd := fmt.Sprintf("sv !stuff CL %d %s\n", p.ClientID, cmd)
WriteByte(SCMDCommand, &cl.MessageOut)
WriteString(stuffcmd, &cl.MessageOut)
}
/**
* Temporarily prevent the player from talking
* using a negative number of seconds makes it
* permanent.
*/
func (cl *Client) MutePlayer(p *Player, seconds int) {
cmd := ""
if seconds < 0 {
cmd = fmt.Sprintf("sv !mute CL %d PERM\n", p.ClientID)
} else {
cmd = fmt.Sprintf("sv !mute CL %d %d", p.ClientID, seconds)
}
WriteByte(SCMDCommand, &cl.MessageOut)
WriteString(cmd, &cl.MessageOut)
player := cl.FindPlayer(p.ClientID)
txt := fmt.Sprintf("[%s/MUTE] %d|%s was muted", cl.Name, p.ClientID, player.Name)
LogEventToDatabase(cl.ID, LogTypeCommand, txt)
}
/**
*
*/
func (cl *Client) KickPlayer(p *Player, msg string) {
cmd := fmt.Sprintf("kick %d", p.ClientID)
WriteByte(SCMDCommand, &cl.MessageOut)
WriteString(cmd, &cl.MessageOut)
txt := fmt.Sprintf("KICK [%d] was kicked", p.ClientID)
LogEventToDatabase(cl.ID, LogTypeCommand, txt)
}
//
// Issue a command as if you were typing it into the console.
// Sanitize cmd before use
//
func (cl *Client) ConsoleCommand(cmd string) {
WriteByte(SCMDCommand, &cl.MessageOut)
WriteString(cmd, &cl.MessageOut)
}