This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgameAdmin.go
375 lines (317 loc) · 7.95 KB
/
gameAdmin.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"encoding/json"
"errors"
"strconv"
"strings"
"sync"
"time"
"github.com/google/logger"
"github.com/gorilla/websocket"
)
// adminHandler takes care of starting the game and is used for spectating the events. Used by the web interface
type adminHandler struct {
gm *GameHandler
man *Managers
conn *websocket.Conn
qSend chan []byte
qRecv chan []byte
ticker *time.Ticker
mutex sync.Mutex
closeChan chan bool
}
func (admin *adminHandler) run() {
logger.Infof("Admin loop started")
logger.Infof("Game state: %v", admin.gm.Status)
go admin.readSocket()
go admin.writeSocket()
loggerTicker := time.NewTicker(500 * time.Millisecond)
for {
select {
case incoming := <-admin.qRecv:
logger.Infof("New admin message: %s", incoming)
admin.adminParseCommand(incoming)
break
case <-loggerTicker.C:
break
}
}
}
func (admin *adminHandler) adminParseCommand(jsonObj []byte) {
logger.Info("Got admin command")
c := EnvelopePartial{}
err := json.Unmarshal(jsonObj, &c)
if err != nil {
logger.Infof("Problem with admin command %v: %v", c, err)
}
switch c.Type {
case "config":
admin.adminParseConfigUpdates(&c.Message)
case "config_get":
admin.adminPushConfig()
case "init":
admin.gm.initGame()
case "start":
admin.gm.startGame()
case "pause":
admin.gm.pauseGame()
case "restart":
admin.gm.restartGame()
case "kick":
err := admin.kickPlayer(string(c.Message))
if err != nil {
logger.Error(err.Error())
admin.sendError(err.Error())
return
}
default:
logger.Errorf("Unable to parse message. Got %v", c.Type)
}
}
func (admin *adminHandler) adminParseConfigUpdates(jsonObj *json.RawMessage) {
logger.Infof("Admin Config update\n%v", jsonObj)
var c ConfigUpdate
err := json.Unmarshal(*jsonObj, &c)
if err != nil {
logger.Infof("Problem with admin command %v: %v", c, err)
}
logger.Infof("Got json config: %v", c)
ch := admin.gm.config
for _, config := range c.Configs {
logger.Infof("Config to check: %v", config)
switch config.Name {
case "minTurnUpdate":
value, err := strconv.Atoi(config.Value)
if err != nil {
admin.sendError("Unable to convert value to int")
break
}
if value > ch.MaxTurnUpdate {
admin.sendError("Min turn time is not lower than max turn time!")
break
}
ch.MinTurnUpdate = value
case "maxTurnUpdate":
value, err := strconv.Atoi(config.Value)
if err != nil {
admin.sendError("Unable to convert value to int")
break
}
if ch.MinTurnUpdate > value {
admin.sendError("Max turn time is lower than min turn time!")
break
}
ch.MaxTurnUpdate = value
case "mapSize":
parts := strings.Split(config.Value, "x")
if len(parts) != 2 {
admin.sendError("Did not find two elements when splitting on 'X'")
break
}
mapXStr, mapYStr := parts[0], parts[1]
mapX, err := strconv.Atoi(mapXStr)
if err != nil {
admin.sendError("Could not convert X elem to int")
break
}
if mapX < 3 && mapX != 0 {
admin.sendError("Map can not be under size 3")
break
}
mapY, err := strconv.Atoi(mapYStr)
if err != nil {
admin.sendError("Could not convert Y elem to int")
break
}
if mapY < 3 && mapY != 0 {
admin.sendError("Map can not be under size 3")
break
}
ch.mapSizeX = mapX
ch.mapSizeY = mapY
case "outerWalls":
value, err := strconv.Atoi(config.Value)
if err != nil {
admin.sendError("Unable to convert value to int")
break
}
ch.OuterWalls = value
default:
logger.Error("Unable to parse configLine %v", config.Name)
}
}
}
func (admin *adminHandler) adminPushConfig() {
logger.Info("Pushing config to frontend")
jsonString, err := json.Marshal(admin.gm.config)
if err != nil {
admin.sendError("Problems with creating message")
return
}
env := Envelope{
Type: "config_push",
Message: string(jsonString),
}
jsonString, err = json.Marshal(env)
if err != nil {
admin.sendError("Problems with creating message")
return
}
logger.Info(string(jsonString))
admin.qSend <- jsonString
}
// Pushes the gamestate to the frontend
func (admin *adminHandler) pushState() {
//logger.Info("Pushing players")
admin.gm.playersLock.Lock()
tmpPlayers := make(map[string]*Player)
for k := range admin.gm.players {
tmpPlayers[k.Username] = k
}
admin.gm.playersLock.Unlock()
jsonString, err := json.Marshal(tmpPlayers)
if err != nil {
admin.sendError("Problems with creating message")
return
}
env := Envelope{
Type: "players",
Message: string(jsonString),
}
jsonString, err = json.Marshal(env)
if err != nil {
admin.sendError("Problems with creating message")
return
}
admin.qSend <- jsonString
}
func (admin *adminHandler) kickPlayer(playerName string) error {
playerName = playerName[1 : len(playerName)-1]
logger.Infof("Kicking %v", playerName)
for player := range admin.gm.players {
if playerName == player.Username {
admin.gm.unregister <- player
return nil
}
}
logger.Infof("Unable to find target for kicking")
return errors.New("unable to find the player you want to kick")
}
func (admin *adminHandler) gameDone() {
msg := Envelope{
Type: "game_done",
Message: "",
}
jsonString, err := json.Marshal(msg)
if err != nil {
logger.Error("Problems with creating error message")
return
}
admin.qSend <- jsonString
}
func (admin *adminHandler) sendError(message string) {
msg := Envelope{
Type: "error",
Message: message,
}
jsonString, err := json.Marshal(msg)
if err != nil {
logger.Error("Problems with creating error message")
return
}
logger.Infof("Sending error: %s", message)
admin.qSend <- jsonString
}
func (admin *adminHandler) writeSocket() {
logger.Info("Write Socket")
admin.ticker = time.NewTicker(pingPeriod)
defer func() {
admin.ticker.Stop()
}()
for {
select {
case <-admin.closeChan:
return
case message, ok := <-admin.qSend:
if admin.conn == nil {
return
}
err := admin.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err != nil {
logger.Info("SetWrDeadline err")
}
if !ok {
// The hub closed the channel.
err := admin.conn.WriteMessage(websocket.CloseMessage, []byte{})
if err != nil {
logger.Info("WriteMessage err")
}
return
}
admin.mutex.Lock()
w, err := admin.conn.NextWriter(websocket.TextMessage)
admin.mutex.Unlock()
if err != nil {
logger.Info("Error with NextWriter")
return
}
_, err = w.Write(message)
if err != nil {
logger.Info("Error write")
}
// Add queued chat messages to the current websocket message.
n := len(admin.qSend)
for i := 0; i < n; i++ {
_, err := w.Write(<-admin.qSend)
if err != nil {
logger.Info("Error write")
}
}
if err := w.Close(); err != nil {
return
}
case <-admin.ticker.C:
//logger.Info("Admin ping ticker")
err := admin.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err != nil {
logger.Info("Error set deadline Ticker")
}
if err := admin.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return
}
}
}
}
func (admin *adminHandler) readSocket() {
logger.Info("Admin Read Socket")
err := admin.conn.SetReadDeadline(time.Now().Add(pongWait))
if err != nil {
logger.Info("Err set read Deadline")
}
admin.conn.SetPongHandler(
func(string) error {
err := admin.conn.SetReadDeadline(time.Now().Add(pongWait))
if err != nil {
logger.Infof("Err set deadline pong")
}
return nil
},
)
for {
_, message, err := admin.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure, websocket.CloseNoStatusReceived) {
logger.Infof("IsUnexpectedCloseError admin: %v", err)
} else {
// Admin disconnected
logger.Infof("Admin closed socket at %v . Removing connection", admin.conn.RemoteAddr())
admin.conn = nil
admin.ticker.Stop()
admin.closeChan <- true
}
break
}
logger.Info("Waiting for admin message")
admin.qRecv <- message
}
}