-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.go
146 lines (139 loc) · 3.01 KB
/
dispatcher.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
package main
import (
"encoding/json"
"strconv"
"go.uber.org/zap"
)
const (
statusInternalServerError = "Internal Server Errror"
statusInvalidJSON = "Invalid JSON format"
)
func dispatch(c *ntcpclient, data []byte, packet typePacket) {
var err error
// Switch between packet types and dispatch them between the different op funcs
switch *packet.Type {
case "login":
var login loginPacket
err = json.Unmarshal(data, &login)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opLogin(c, login)
return
case "clienthello":
var clienthello xhelloPacket
err = json.Unmarshal(data, &clienthello)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opClienthello(c, clienthello)
return
case "agreeconn":
// Check if this client has already a agreed connection
if err = ntcpRegistry.Put(c); err != nil {
log.Warn("multiple connections for same user", zap.Error(err))
c.Respond("Connection already open - Please close any previous connections before initializing a new one.")
return
}
c.AgreeconnDone = true
c.Authenticated = true
c.Player = battle.Players[c.UserID]
c.RespondNil()
dist.GetClient(strconv.Itoa(c.UserID)).PushInfo(true, c.IP, c.SDK, c.SDKLink, c.OS, c.Log)
return
case "rotate":
var rotate rotatePacket
err = json.Unmarshal(data, &rotate)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opRotate(c, rotate)
return
case "move":
var move movePacket
err = json.Unmarshal(data, &move)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opMove(c, move)
return
case "radar":
var radar radarPacket
err = json.Unmarshal(data, &radar)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opRadar(c, radar)
return
case "scout":
var scout scoutPacket
err = json.Unmarshal(data, &scout)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opScout(c, scout)
return
case "environment":
var environment environmentPacket
err = json.Unmarshal(data, &environment)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
case "watch":
var watch watchPacket
err = json.Unmarshal(data, &watch)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opWatch(c, watch)
return
case "attack":
var attack attackPacket
err = json.Unmarshal(data, &attack)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opAttack(c, attack)
return
case "defend":
var defend defendPacket
err = json.Unmarshal(data, &defend)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opDefend(c, defend)
return
case "undefend":
var undefend undefendPacket
err = json.Unmarshal(data, &undefend)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opUndefend(c, undefend)
return
case "health":
var health healthPacket
err = json.Unmarshal(data, &health)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opHealth(c, health)
return
default:
c.CurType = "forbidden"
c.Respond("Invalid packet. '.type' unknown")
return
}
}