Skip to content

Commit 94ccfd1

Browse files
committed
coerce from map[string]interface{}
1 parent 30afbee commit 94ccfd1

File tree

7 files changed

+83
-63
lines changed

7 files changed

+83
-63
lines changed

http/gateway/coordinate_change.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ import (
88
"github.com/blobs-io/blobsgame/utils"
99
)
1010

11-
type CoordinateChangeEventData struct {
12-
X int `json:"x"`
13-
Y int `json:"y"`
14-
Room string `json:"room"`
15-
}
16-
1711
func CoordinateChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
18-
data, ok := d.Data.(CoordinateChangeEventData)
12+
roomID, ok := d.Data["room"].(string)
13+
if !ok {
14+
return
15+
}
16+
17+
x, ok := d.Data["x"].(int)
18+
if !ok {
19+
return
20+
}
21+
22+
y, ok := d.Data["y"].(int)
1923
if !ok {
2024
return
2125
}
2226

23-
r, ok := room.Rooms[data.Room]
27+
r, ok := room.Rooms[roomID]
2428
if !ok {
2529
return
2630
}
@@ -30,7 +34,7 @@ func CoordinateChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
3034
return
3135
}
3236

33-
xDrift, yDrift := math.Abs(float64(data.X-p.X)), math.Abs(float64(data.Y-p.Y))
37+
xDrift, yDrift := math.Abs(float64(x-p.X)), math.Abs(float64(y-p.Y))
3438

3539
if xDrift > utils.CoordinateDriftLimit {
3640
p.AntiCheatFlags += utils.Penalize(utils.ActionCoordinateDrift, int(xDrift))
@@ -46,20 +50,20 @@ func CoordinateChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
4650
}
4751

4852
if p.Role != user.AdminRole {
49-
if data.X < 0 {
50-
data.X = 0
51-
} else if data.X > r.Map.MapSize.Width {
52-
data.X = r.Map.MapSize.Width
53+
if x < 0 {
54+
x = 0
55+
} else if x > r.Map.MapSize.Width {
56+
x = r.Map.MapSize.Width
5357
}
5458

55-
if data.Y < 0 {
56-
data.Y = 0
57-
} else if data.Y > r.Map.MapSize.Height {
58-
data.Y = r.Map.MapSize.Height
59+
if y < 0 {
60+
y = 0
61+
} else if y > r.Map.MapSize.Height {
62+
y = r.Map.MapSize.Height
5963
}
6064
}
6165

6266
// this might cause some problems later, not too sure yet
63-
p.X = data.X
64-
p.Y = data.Y
67+
p.X = x
68+
p.Y = y
6569
}

http/gateway/direction_change.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,29 @@ import (
88
"github.com/blobs-io/blobsgame/models/room"
99
)
1010

11-
type DirectionChangeEventData struct {
12-
X int `json:"x"`
13-
Y int `json:"y"`
14-
Room string `json:"room"`
15-
DirectionChangeCoordinates player.CoordinatesAny `json:"directionChangeCoordinates"`
16-
DirectionChangedAt int64 `json:"directionChangedAt"`
17-
Direction uint8 `json:"direction"`
18-
}
19-
2011
func DirectionChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
2112
return // currently disabled
22-
data, ok := d.Data.(DirectionChangeEventData)
13+
roomID, ok := d.Data["room"].(string)
14+
if !ok {
15+
return
16+
}
17+
18+
directionChangeCoordinates, ok := d.Data["directionChangeCoordinates"].(player.CoordinatesAny)
19+
if !ok {
20+
return
21+
}
22+
23+
directionChangedAt, ok := d.Data["directionChangedAt"].(int64)
24+
if !ok {
25+
return
26+
}
27+
28+
direction, ok := d.Data["direction"].(uint8)
2329
if !ok {
2430
return
2531
}
2632

27-
r, ok := room.Rooms[data.Room]
33+
r, ok := room.Rooms[roomID]
2834
if !ok {
2935
return
3036
}
@@ -36,17 +42,17 @@ func DirectionChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
3642

3743
now := time.Now().UnixNano() / int64(time.Millisecond)
3844

39-
if now-data.DirectionChangedAt < 5000 {
40-
p.DirectionChangedAt = data.DirectionChangedAt
45+
if now-directionChangedAt < 5000 {
46+
p.DirectionChangedAt = directionChangedAt
4147
} else {
4248
p.DirectionChangedAt = now
4349
}
4450

45-
p.Direction = data.Direction
51+
p.Direction = direction
4652

4753
distance := int(math.Abs(float64(p.DirectionChangeCoordinates.X-p.X)) + math.Abs(float64(p.DirectionChangeCoordinates.Y-p.Y)))
4854
p.Distance += distance
4955

50-
p.DirectionChangeCoordinates.X = data.DirectionChangeCoordinates.X
51-
p.DirectionChangeCoordinates.Y = data.DirectionChangeCoordinates.Y
56+
p.DirectionChangeCoordinates.X = directionChangeCoordinates.X
57+
p.DirectionChangeCoordinates.Y = directionChangeCoordinates.Y
5258
}

http/gateway/handler.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@ func Handle(c *websocket.Conn) {
5858
}
5959

6060
func handleHello(c *WebSocketConnection, d *AnyMessage) {
61-
data, ok := d.Data.(HelloPayload)
61+
roomID, ok := d.Data["room"].(string)
6262
if !ok {
6363
return
6464
}
65+
sessionID, ok := d.Data["session"].(string)
6566

66-
r, ok := room.Rooms[data.Room]
67+
fmt.Println("event data", d.Data)
68+
69+
r, ok := room.Rooms[roomID]
6770
if !ok {
6871
return
6972
}
73+
fmt.Println("room", r)
7074

7175
if len(r.Players) >= room.PlayerLimit {
7276
c.Kick(&r, RoomFullKick, "Too many players online")
@@ -80,7 +84,7 @@ func handleHello(c *WebSocketConnection, d *AnyMessage) {
8084
return
8185
}
8286

83-
u, err := user.GetUser(data.Session, user.UserSessionSearch)
87+
u, err := user.GetUser(sessionID, user.UserSessionSearch)
8488

8589
p := player.Player{
8690
ID: c.ID,
@@ -116,12 +120,12 @@ func handleHello(c *WebSocketConnection, d *AnyMessage) {
116120
}
117121

118122
func handleHeartbeat(c *WebSocketConnection, d *AnyMessage) {
119-
data, ok := d.Data.(HeartbeatPayload)
123+
roomID, ok := d.Data["room"].(string)
120124
if !ok {
121125
return
122126
}
123127

124-
r, ok := room.Rooms[data.Room]
128+
r, ok := room.Rooms[roomID]
125129
if !ok {
126130
return
127131
}

http/gateway/item_collect.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import (
77
"github.com/blobs-io/blobsgame/models/room"
88
)
99

10-
type ItemCollectEventData struct {
11-
Room string `json:"room"`
12-
Item string `json:"item"`
13-
}
14-
1510
func ItemCollectEventCallback(c *WebSocketConnection, d *AnyMessage) {
16-
data, ok := d.Data.(ItemCollectEventData)
11+
roomID, ok := d.Data["room"].(string)
12+
if !ok {
13+
return
14+
}
15+
16+
itemID, ok := d.Data["item"].(string)
1717
if !ok {
1818
return
1919
}
2020

21-
r, ok := room.Rooms[data.Room]
21+
r, ok := room.Rooms[roomID]
2222
if !ok {
2323
return
2424
}
@@ -31,7 +31,7 @@ func ItemCollectEventCallback(c *WebSocketConnection, d *AnyMessage) {
3131
var targetItem *item.Item
3232
for i := range r.Items {
3333
currentItem := &r.Items[i]
34-
if currentItem.ID == data.Item &&
34+
if currentItem.ID == itemID &&
3535
p.X < (currentItem.X+item.ItemWidth) &&
3636
p.X > (currentItem.X-item.ItemWidth) &&
3737
p.Y < (currentItem.Y+item.ItemHeight) &&

http/gateway/nom_key.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ import (
99
"github.com/blobs-io/blobsgame/utils"
1010
)
1111

12-
type NomKeyEventData struct {
13-
Room string `json:"room"`
14-
}
15-
1612
func NomKeyEventCallback(c *WebSocketConnection, d *AnyMessage) {
17-
data, ok := d.Data.(NomKeyEventData)
13+
roomID, ok := d.Data["room"].(string)
1814
if !ok {
1915
return
2016
}
2117

22-
r, ok := room.Rooms[data.Room]
18+
r, ok := room.Rooms[roomID]
2319
if !ok {
2420
return
2521
}
@@ -65,13 +61,13 @@ func NomKeyEventCallback(c *WebSocketConnection, d *AnyMessage) {
6561

6662
if !target.Guest && !p.Guest {
6763
result = utils.CalculateRatingDiff(p.BR, target.BR)
68-
if p.BR + result > player.BRLimit {
64+
if p.BR+result > player.BRLimit {
6965
p.BR = player.BRLimit
7066
} else {
7167
p.BR += result
7268
}
7369

74-
if target.BR - result < 0 {
70+
if target.BR-result < 0 {
7571
target.BR = 0
7672
} else {
7773
target.BR -= result

http/gateway/player_kick.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ type PlayerKickEventData struct {
1212
}
1313

1414
func PlayerKickEventCallback(c *WebSocketConnection, d *AnyMessage) {
15-
data, ok := d.Data.(PlayerKickEventData)
15+
roomID, ok := d.Data["room"].(string)
1616
if !ok {
1717
return
1818
}
1919

20-
r, ok := room.Rooms[data.Room]
20+
userID, ok := d.Data["user"].(string)
21+
if !ok {
22+
return
23+
}
24+
25+
reason, ok := d.Data["reason"].(string)
26+
if !ok {
27+
return
28+
}
29+
30+
r, ok := room.Rooms[roomID]
2131
if !ok {
2232
return
2333
}
@@ -32,7 +42,7 @@ func PlayerKickEventCallback(c *WebSocketConnection, d *AnyMessage) {
3242
return
3343
}
3444

35-
target := r.GetPlayerByUsername(data.User)
45+
target := r.GetPlayerByUsername(userID)
3646
if target == nil {
3747
return
3848
}
@@ -41,5 +51,5 @@ func PlayerKickEventCallback(c *WebSocketConnection, d *AnyMessage) {
4151
if !ok {
4252
return
4353
}
44-
conn.Kick(&r, ModKick, data.Reason)
54+
conn.Kick(&r, ModKick, reason)
4555
}

http/gateway/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ const (
3131
)
3232

3333
type AnyMessage struct {
34-
Op int `json:"op"`
35-
Data interface{} `json:"d"`
36-
T string `json:"t"`
34+
Op int `json:"op"`
35+
Data map[string]interface{} `json:"d"`
36+
T string `json:"t"`
3737
}
3838

3939
type HelloPayload struct {

0 commit comments

Comments
 (0)