/
types.go
287 lines (250 loc) · 5.5 KB
/
types.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"encoding/json"
"sync"
)
type (
// Game data
GameData struct {
sync.RWMutex
Users map[string]*UserValue
Objects map[string]Object
Scores Scores
PlayerStatistics PlayerStats
Flags Flags
}
// User value
UserValue struct {
Name string
X float32
Y float32
Z float32
Orientation float32
Health int
Team int
}
// Message container
Message struct {
Type int `yaml:"type"`
ID string `yaml:"id"`
Coordinates Coordinates `yaml:"coordinates"`
Orientation float32 `yaml:"orientation"`
Vel Coordinates `yaml:"vel"`
Health int `yaml:"health"`
Flag int `yaml:"flag"`
Action int `yaml:"action"`
Size float32 `yaml:"size"`
Name string `yaml:"name"`
}
// Store player coordinates (2d)
Coordinates struct {
X float32 `yaml:"x"`
Y float32 `yaml:"y"`
Z float32 `yaml:"z"`
}
// Store objects
Object struct {
Coordinates Coordinates `yaml:"coordinates"`
}
// Store game data
Scores struct {
Team1 float32 `yaml:"team1"`
Team2 float32 `yaml:"team2"`
}
// Player statistics
PlayerStats struct {
Team1 int
Team2 int
}
// Flag positions
Flags struct {
Team1 string
Team2 string
}
)
// Set the position and orientation of a player by ID
func (g *GameData) SetUserData(id string, x, y, z, orientation float32, name string) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
// Set health if not in memory
if _, ok := g.Users[id]; !ok {
g.Users[id] = &UserValue{
X: x,
Y: y,
Z: z,
Orientation: orientation,
Name: name,
Health: 100,
}
} else {
g.Users[id] = &UserValue{
X: x,
Y: y,
Z: z,
Orientation: orientation,
Name: name,
Health: g.Users[id].Health,
}
}
}
// Set the player's team by ID
func (g *GameData) SetTeam(id string, team int) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
g.Users[id].Team = team
}
// Change the user's health by a specified amount
func (g *GameData) UpdateHealth(id string, amount int) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
if g.Users[id].Health > 100 || g.Users[id].Health + amount > 100 {
g.Users[id].Health = 100
} else {
g.Users[id].Health += amount
}
}
// Set an object's position
func (g *GameData) SetObject(id string, x, y, z float32) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
g.Objects[id] = Object{
Coordinates: Coordinates{
X: x,
Y: y,
Z: z,
},
}
}
// Delete all of a users data from memory
func (g *GameData) DeleteUserData(id string) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
delete(g.Users, id)
}
// Delete an object's data from memory
func (g *GameData) DeleteObject(id string) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
delete(g.Objects, id)
}
// Add a new player to team 1
func (g *GameData) IncrementScoreTeam1() {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
g.Scores.Team1++
}
// Add a new player to team 2
func (g *GameData) IncrementScoreTeam2() {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
g.Scores.Team2++
}
// Assign a user to a team
func (g *GameData) AssignTeam() int {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
// Check if one team has more players
if g.PlayerStatistics.Team1 > g.PlayerStatistics.Team2 {
g.PlayerStatistics.Team2++
return 2
}
// Default to team 1
g.PlayerStatistics.Team1++
return 1
}
// Decrement the number of players on team 1
func (g *GameData) RemovePlayerTeam1() {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
g.PlayerStatistics.Team1--
}
// Decrement the number of players on team 2
func (g *GameData) RemovePlayerTeam2() {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
g.PlayerStatistics.Team2--
}
// Get all user data in JSON format
func (g *GameData) GetAllData() ([]byte, error) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
// Marshal to JSON
j, err := json.Marshal(struct {
Users map[string]*UserValue
Objects map[string]Object
Scores Scores
PlayerStatistics PlayerStats
}{
Users: g.Users,
Objects: g.Objects,
Scores: g.Scores,
PlayerStatistics: g.PlayerStatistics,
})
if err != nil {
return nil, err
}
return j, nil
}
// Get only the data the clients will need
// Excludes player statistics
func (g *GameData) GetPlayerData() ([]byte, error) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
// Marshal into JSON
j, err := json.Marshal(struct {
Users map[string]*UserValue
Objects map[string]Object
Scores Scores
}{
Users: g.Users,
Objects: g.Objects,
Scores: g.Scores,
})
if err != nil {
return nil, err
}
return j, nil
}
// Mark a flag as taken by a team
func (g *GameData) SetFlagTaken(id string, team int) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
// Set who has the flag based on team and ID
if team == 1 {
g.Flags.Team1 = id
} else if team == 2 {
g.Flags.Team2 = id
}
}
// Reset which team has the flag
func (g *GameData) ResetFlagTaken(team int) {
// Acquire lock to prevent race conditions
g.Lock()
defer g.Unlock()
if team == 1 {
g.Flags.Team1 = ""
} else if team == 2 {
g.Flags.Team2 = ""
}
}
// Deep equal for users
func (u UserValue) equals (u2 UserValue) bool {
if u.X != u2.X {return false}
if u.Y != u2.Y {return false}
if u.Z != u2.Z {return false}
return true
}