-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
250 lines (224 loc) · 8.86 KB
/
messages.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package messages
import (
"encoding/json"
"github.com/cbodonnell/flywheel/pkg/kinematic"
)
const (
// UDPMessageBufferSize represents the maximum size of a message
UDPMessageBufferSize = 508
// TCPMessageBufferSize represents the maximum size of a message
TCPMessageBufferSize = 1460
)
// Message types
type MessageType uint8
const (
MessageTypeClientLogin MessageType = iota
MessageTypeServerLoginSuccess
MessageTypeServerLoginFailure
MessageTypeClientPing
MessageTypeServerPong
MessageTypeClientPlayerUpdate
MessageTypeServerGameUpdate
MessageTypeServerPlayerUpdate
MessageTypeServerNPCUpdate
MessageTypeClientSyncTime
MessageTypeServerSyncTime
MessageTypeServerPlayerConnect
MessageTypeServerPlayerDisconnect
MessageTypeServerNPCHit
MessageTypeServerNPCKill
MessageTypeServerPlayerHit
MessageTypeServerPlayerKill
)
func (m MessageType) String() string {
return [...]string{
"ClientLogin",
"ServerLoginSuccess",
"ServerLoginFailure",
"ClientPing",
"ServerPong",
"ClientPlayerUpdate",
"ServerGameUpdate",
"ServerPlayerUpdate",
"ServerNPCUpdate",
"ClientSyncTime",
"ServerSyncTime",
"ServerPlayerConnect",
"ServerPlayerDisconnect",
"ServerNPCHit",
"ServerNPCKill",
"ServerPlayerHit",
"ServerPlayerKill",
}[m]
}
// Message represents a generic message for serialization/deserialization
type Message struct {
ClientID uint32 `json:"clientID"`
Type MessageType `json:"type"`
Payload json.RawMessage `json:"payload"`
}
type ClientLogin struct {
Token string `json:"token"`
CharacterID int32 `json:"characterID"`
}
type ServerLoginSuccess struct {
ClientID uint32 `json:"clientID"`
}
type ServerLoginFailure struct {
Reason string `json:"reason"`
}
const (
// MaxPreviousUpdates is the maximum number of previous updates to send to the server
// TODO: determine a more appropriate size
MaxPreviousUpdates = 2
)
// TODO: split this into PlayerInput and ClientPlayerUpdate to differentiate the game and message types
// with a type field to differentiate between different input types (move, jump, fire, etc.)
type ClientPlayerUpdate struct {
// Timestamp is the time at which the update was generated by the client
Timestamp int64 `json:"timestamp"`
// InputX is the x-axis input from the client ranging from -1 to 1
InputX float64 `json:"inputX"`
// InputY is the y-axis input from the client ranging from -1 to 1
InputY float64 `json:"inputY"`
// InputJump is the jump input from the client
InputJump bool `json:"inputJump"`
// InputAttack1 is the attack 1 input from the client
InputAttack1 bool `json:"inputAttack1"`
// InputAttack2 is the attack 2 input from the client
InputAttack2 bool `json:"inputAttack2"`
// InputAttack3 is the attack 3 input from the client
InputAttack3 bool `json:"inputAttack3"`
// InputRespawn is the respawn input from the client
InputRespawn bool `json:"inputRespawn"`
// DeltaTime is the time since the last update as recorded by the client
DeltaTime float64 `json:"deltaTime"`
// PastUpdates is a list of past updates from the client to
// mitigate the effects of packet loss and out-of-order delivery
PastUpdates []*ClientPlayerUpdate `json:"previousUpdates"`
}
// ServerGameUpdate is a message sent by the server to update clients on the game state
type ServerGameUpdate struct {
// Timestamp is the time at which the update was generated by the server
Timestamp int64 `json:"timestamp"`
// Players maps client IDs to player states
Players map[uint32]*PlayerStateUpdate `json:"players"`
// NPCs maps enemy IDs to NPC states
NPCs map[uint32]*NPCStateUpdate `json:"npcs"`
}
// ServerPlayerUpdate is a message sent by the server to update clients on a player's state
type ServerPlayerUpdate struct {
// Timestamp is the time at which the update was generated by the server
Timestamp int64 `json:"timestamp"`
// ClientID is the ID of the player
ClientID uint32 `json:"clientID"`
// PlayerState is the state of the player
PlayerState *PlayerStateUpdate `json:"playerState"`
}
// ServerNPCUpdate is a message sent by the server to update clients on an NPC's state
type ServerNPCUpdate struct {
// Timestamp is the time at which the update was generated by the server
Timestamp int64 `json:"timestamp"`
// NPCID is the ID of the NPC
NPCID uint32 `json:"npcID"`
// NPCState is the state of the NPC
NPCState *NPCStateUpdate `json:"npcState"`
}
// PlayerStateUpdate is a message sent by the server to update clients on a player's state
type PlayerStateUpdate struct {
// LastProcessedTimestamp is the timestamp of the last processed update
LastProcessedTimestamp int64 `json:"lastProcessedTimestamp"`
// CharacterID is the ID of the player's character
CharacterID int32 `json:"characterID"`
// Name is the name of the player
Name string `json:"playerName"`
// Position is the position of the player
Position kinematic.Vector `json:"position"`
// Velocity is the velocity of the player
Velocity kinematic.Vector `json:"velocity"`
// IsOnGround is a flag indicating whether the player is on the ground
IsOnGround bool `json:"isOnGround"`
// IsAttacking is a flag indicating whether the player is attacking
IsAttacking bool `json:"isAttacking"`
// Animation is the current animation of the player
Animation uint8 `json:"animation"`
// AnimationFlip is a flag indicating whether the animation should be flipped
AnimationFlip bool `json:"animationFlip"`
// AnimationSequence is used to differentiate between different instances of the same animation
AnimationSequence uint8 `json:"animationSequence"`
// Hitpoints is the current hitpoints of the player
Hitpoints int16 `json:"hitpoints"`
}
// NPCStateUpdate is a message sent by the server to update clients on an NPC's state
type NPCStateUpdate struct {
// Position is the position of the NPC
Position kinematic.Vector `json:"position"`
// Velocity is the velocity of the NPC
Velocity kinematic.Vector `json:"velocity"`
// IsOnGround is a flag indicating whether the NPC is on the ground
IsOnGround bool `json:"isOnGround"`
// Animation is the current animation of the NPC
Animation uint8 `json:"animation"`
// AnimationFlip is a flag indicating whether the animation should be flipped
AnimationFlip bool `json:"animationFlip"`
// AnimationSequence is used to differentiate between different instances of the same animation
AnimationSequence uint8 `json:"animationSequence"`
// Hitpoints is the current hitpoints of the NPC
Hitpoints int16 `json:"hitpoints"`
}
// ClientSyncTime is a message sent by the client to request a time sync with the server
type ClientSyncTime struct {
// Timestamp is the time at which the sync request was generated by the client
Timestamp int64 `json:"timestamp"`
}
// ServerSyncTime is a message sent by the server in response to a time sync request
type ServerSyncTime struct {
// Timestamp is the time at which the sync response was generated by the server
Timestamp int64 `json:"timestamp"`
// ClientTimestamp is the time at which the sync request was generated by the client
ClientTimestamp int64 `json:"clientTimestamp"`
}
// ServerPlayerConnect is a message sent by the server to notify clients that a new player has connected
type ServerPlayerConnect struct {
// ClientID is the ID of the player that has connected
ClientID uint32 `json:"clientID"`
// PlayerState is the state of the player that has connected
PlayerState *PlayerStateUpdate `json:"playerState"`
}
// ServerPlayerDisconnect is a message sent by the server to notify clients that a player has disconnected
type ServerPlayerDisconnect struct {
// ClientID is the ID of the player that has disconnected
ClientID uint32 `json:"clientID"`
}
// ServerNPCHit is a message sent by the server to notify clients that an NPC has been hit
type ServerNPCHit struct {
// NPCID is the ID of the NPC that has been hit
NPCID uint32 `json:"npcID"`
// PlayerID is the ID of the player that hit the NPC
PlayerID uint32 `json:"playerID"`
// Damage is the amount of damage dealt to the NPC
Damage int16 `json:"damage"`
}
// ServerNPCKill is a message sent by the server to notify clients that an NPC has been killed
type ServerNPCKill struct {
// NPCID is the ID of the NPC that has been killed
NPCID uint32 `json:"npcID"`
// PlayerID is the ID of the player that killed the NPC
PlayerID uint32 `json:"playerID"`
}
// ServerPlayerHit is a message sent by the server to notify clients that a player has been hit
type ServerPlayerHit struct {
// PlayerID is the ID of the player that has been hit
PlayerID uint32 `json:"playerID"`
// NPCID is the ID of the NPC that hit the player
NPCID uint32 `json:"npcID"`
// Damage is the amount of damage dealt to the player
Damage int16 `json:"damage"`
}
// ServerPlayerKill is a message sent by the server to notify clients that a player has been killed
type ServerPlayerKill struct {
// PlayerID is the ID of the player that has been killed
PlayerID uint32 `json:"playerID"`
// NPCID is the ID of the NPC that killed the player
NPCID uint32 `json:"npcID"`
}