forked from packetflinger/q2admin-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
220 lines (178 loc) · 4.16 KB
/
parse.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
213
214
215
216
217
218
219
220
package main
import (
"encoding/hex"
"fmt"
"log"
"strconv"
)
/**
* Loop through all the data from the client
* and act accordingly
*/
func (cl *Client) ParseMessage() {
msg := &cl.Message
for {
if msg.index >= len(msg.buffer) {
break
}
switch b := ReadByte(msg); b {
case CMDPing:
Pong(cl)
case CMDPrint:
ParsePrint(cl)
case CMDMap:
ParseMap(cl)
case CMDPlayerList:
ParsePlayerlist(cl)
case CMDConnect:
ParseConnect(cl)
case CMDDisconnect:
ParseDisconnect(cl)
case CMDCommand:
ParseCommand(cl)
case CMDFrag:
ParseFrag(cl)
}
}
}
/**
* A player was fragged.
* Only two bytes are sent: the clientID of the victim,
* and of the attacker
*/
func ParseFrag(cl *Client) {
v := int(ReadByte(&cl.Message))
a := int(ReadByte(&cl.Message))
victim := cl.FindPlayer(v)
attacker := cl.FindPlayer(a)
if victim == nil {
return
}
log.Printf("[%s/FRAG] %d > %d\n", cl.Name, a, v)
if attacker == victim || attacker == nil {
victim.Suicides++
victim.Frags--
victim.Deaths++
} else {
attacker.Frags++
victim.Deaths++
}
}
/**
* Received a ping from a client, send a pong to show we're alive
*/
func Pong(cl *Client) {
if q2a.config.Debug > 1 {
log.Printf("[%s/PING]\n", cl.Name)
}
cl.PingCount++
WriteByte(SCMDPong, &cl.MessageOut)
// close to once per hour
if (cl.PingCount & 63) == 0 {
RotateKeys(cl)
}
}
/**
* A print was sent by the server.
* 1 byte: print level
* string: the actual message
*/
func ParsePrint(cl *Client) {
level := ReadByte(&cl.Message)
text := ReadString(&cl.Message)
// remove newline
stripped := text[0 : len(text)-1]
switch level {
case PRINT_CHAT:
cl.SendToWebsiteFeed(stripped, FeedChat)
LogChat(cl, text)
log.Printf("[%s/PRINT] (%d) %s\n", cl.Name, level, stripped)
case PRINT_MEDIUM:
ParseObituary(text)
}
}
/**
* A player connected to the a q2 server
*/
func ParseConnect(cl *Client) {
p := ParsePlayer(cl)
if p == nil {
return
}
info := UserinfoMap(p.Userinfo)
txt := fmt.Sprintf("[%s/CONNECT] %d|%s|%s|%s", cl.Name, p.ClientID, info["name"], info["ip"], p.Hash)
log.Printf("%s\n", txt)
LogEventToDatabase(cl.ID, LogTypeJoin, txt)
wstxt := fmt.Sprintf("[CONNECT] %s [%s]", info["name"], info["ip"])
cl.SendToWebsiteFeed(wstxt, FeedJoinPart)
cl.ApplyRules(p)
}
/**
* A player disconnected from a q2 server
*/
func ParseDisconnect(cl *Client) {
clientnum := int(ReadByte(&cl.Message))
if clientnum < 0 || clientnum > cl.MaxPlayers {
log.Printf("Invalid client number: %d\n%s\n", clientnum, hex.Dump(cl.Message.buffer))
return
}
pl := cl.FindPlayer(clientnum)
wstxt := fmt.Sprintf("[DISCONNECT] %s [%s]", pl.Name, pl.IP)
cl.SendToWebsiteFeed(wstxt, FeedJoinPart)
log.Printf("[%s/DISCONNECT] %d|%s\n", cl.Name, clientnum, pl.Name)
cl.RemovePlayer(clientnum)
}
/**
* Server told us what map is currently running. Typically happens
* when the map changes
*/
func ParseMap(cl *Client) {
mapname := ReadString(&cl.Message)
cl.CurrentMap = mapname
log.Printf("[%s/MAP] %s\n", cl.Name, cl.CurrentMap)
}
func ParseObituary(text string) {
log.Printf("Obit: %s\n", text)
}
func ParsePlayerlist(cl *Client) {
count := ReadByte(&cl.Message)
log.Printf("[%s/PLAYERLIST] %d\n", cl.Name, count)
for i := 0; i < int(count); i++ {
_ = ParsePlayer(cl)
}
}
func ParsePlayer(cl *Client) *Player {
clientnum := ReadByte(&cl.Message)
userinfo := ReadString(&cl.Message)
if int(clientnum) > cl.MaxPlayers {
log.Printf("WARNING: Invalid client number, ignoring\n")
return nil
}
info := UserinfoMap(userinfo)
port, _ := strconv.Atoi(info["port"])
fov, _ := strconv.Atoi(info["fov"])
newplayer := Player{
ClientID: int(clientnum),
Userinfo: userinfo,
UserinfoMap: info,
Name: info["name"],
IP: info["ip"],
Port: port,
FOV: fov,
ConnectTime: GetUnixTimestamp(),
}
LoadPlayerHash(&newplayer)
log.Printf("[%s/PLAYER] %d|%s|%s\n", cl.Name, clientnum, newplayer.Hash, userinfo)
cl.Players[newplayer.ClientID] = newplayer
cl.PlayerCount++
return &newplayer
}
func ParseCommand(cl *Client) {
cmd := ReadByte(&cl.Message)
switch cmd {
case PCMDTeleport:
cl.Teleport()
case PCMDInvite:
cl.Invite()
}
}