@@ -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- 
2011func  DirectionChangeEventCallback (c  * WebSocketConnection , d  * AnyMessage ) {
2112return  // 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 )
2329if  ! ok  {
2430return 
2531}
2632
27- r , ok  :=  room .Rooms [data . Room ]
33+ r , ok  :=  room .Rooms [roomID ]
2834if  ! ok  {
2935return 
3036}
@@ -36,17 +42,17 @@ func DirectionChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
3642
3743now  :=  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  {
4248p .DirectionChangedAt  =  now 
4349}
4450
45- p .Direction  =  data . Direction 
51+ p .Direction  =  direction 
4652
4753distance  :=  int (math .Abs (float64 (p .DirectionChangeCoordinates .X - p .X )) +  math .Abs (float64 (p .DirectionChangeCoordinates .Y - p .Y )))
4854p .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}
0 commit comments