Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5513d3e

Browse files
committed
partially rewrite gateway
1 parent 0196730 commit 5513d3e

File tree

13 files changed

+574
-51
lines changed

13 files changed

+574
-51
lines changed

‎go.mod‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ module github.com/blobs-io/blobsgame
33
go 1.13
44

55
require (
6-
github.com/gofiber/fiber v1.8.31
7-
github.com/gorilla/websocket v1.4.1 // indirect
8-
github.com/lib/pq v1.3.0
9-
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
6+
github.com/gofiber/fiber v1.9.2
7+
github.com/gofiber/websocket v0.2.0
8+
github.com/guregu/null v4.0.0+incompatible
9+
github.com/json-iterator/go v1.1.9 // indirect
10+
github.com/klauspost/compress v1.10.5 // indirect
11+
github.com/lib/pq v1.4.0
12+
github.com/valyala/fasttemplate v1.1.0 // indirect
13+
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5
1014
)

‎go.sum‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ github.com/gofiber/fiber v1.8.2 h1:WwN5FKoEgqPuUAJLbBbpfk9imr+LojwcWZJDz13PGt4=
1919
github.com/gofiber/fiber v1.8.2/go.mod h1:JD9ZZgxqitISS6yDcKP26BKR6Ul+z6UrZX7WlalC1sE=
2020
github.com/gofiber/fiber v1.8.31 h1:ztkn4jsM6sUK7f0af0MxCDj4/GSF8cT0dIaESFLEZRU=
2121
github.com/gofiber/fiber v1.8.31/go.mod h1:JD9ZZgxqitISS6yDcKP26BKR6Ul+z6UrZX7WlalC1sE=
22+
github.com/gofiber/fiber v1.9.0/go.mod h1:yQhhFUJprqnZVaEbd5h4ZqU+wb9vzP5imw7UbjGlDuQ=
23+
github.com/gofiber/fiber v1.9.2 h1:r7ZkAALBZWnw85nNRXkdtemZHFXTC9lduAbGUkK4Xww=
24+
github.com/gofiber/fiber v1.9.2/go.mod h1:o2YQgwJW8+Z16x8MTos4nYn8PD1RJpzu9fojiGqjSjI=
2225
github.com/gofiber/template v1.0.0 h1:Vf4Fby9zUWVQyY2y69KKyRHsEYlIE+Pxb25M+jiaEL0=
2326
github.com/gofiber/template v1.0.0/go.mod h1:+bij+R0NI6urTg2jtQvPj5wb2uWMxW9eYGsAN3QhnP0=
27+
github.com/gofiber/websocket v0.2.0 h1:9ic/QB20LGNZe96xVs2daFWreO3orn/fZv4WizMGvSE=
28+
github.com/gofiber/websocket v0.2.0/go.mod h1:OQFKOq+VHvdyrN9CnPGSnctNRzQoqaNaD7l0RLKcgBY=
2429
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
2530
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2631
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
@@ -29,16 +34,26 @@ github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
2934
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
3035
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
3136
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
37+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
38+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
39+
github.com/guregu/null v4.0.0+incompatible h1:4zw0ckM7ECd6FNNddc3Fu4aty9nTlpkkzH7dPn4/4Gw=
40+
github.com/guregu/null v4.0.0+incompatible/go.mod h1:ePGpQaN9cw0tj45IR5E5ehMvsFlLlQZAkkOXZurJ3NM=
3241
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
3342
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
3443
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
3544
github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs=
3645
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
46+
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
47+
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
48+
github.com/klauspost/compress v1.10.5 h1:7q6vHIqubShURwQz8cQK6yIe/xC3IF0Vm7TGfqjewrc=
49+
github.com/klauspost/compress v1.10.5/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
3750
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
3851
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
3952
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
4053
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
4154
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
55+
github.com/lib/pq v1.4.0 h1:TmtCFbH+Aw0AixwyttznSMQDgbR5Yed/Gg6S8Funrhc=
56+
github.com/lib/pq v1.4.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
4257
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
4358
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
4459
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
@@ -48,23 +63,31 @@ github.com/savsgio/gotils v0.0.0-20190714152828-365999d0a274 h1:F52t1X2ziOrMcQMV
4863
github.com/savsgio/gotils v0.0.0-20190714152828-365999d0a274/go.mod h1:w803/Fg1m0hrp1ZT9KNfQe4E4+WOMMFLcgzPvOcye10=
4964
github.com/savsgio/gotils v0.0.0-20200117113501-90175b0fbe3f h1:PgA+Olipyj258EIEYnpFFONrrCcAIWNUNoFhUfMqAGY=
5065
github.com/savsgio/gotils v0.0.0-20200117113501-90175b0fbe3f/go.mod h1:lHhJedqxCoHN+zMtwGNTXWmF0u9Jt363FYRhV6g0CdY=
66+
github.com/savsgio/gotils v0.0.0-20200413113635-8c468ce75cca h1:Qe7Mtuhjkk38HVpRtvWdziZJcwG3Qup1mfyvyOrcnyM=
67+
github.com/savsgio/gotils v0.0.0-20200413113635-8c468ce75cca/go.mod h1:TWNAOTaVzGOXq8RbEvHnhzA/A2sLZzgn0m6URjnukY8=
5168
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5269
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5370
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
5471
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
5572
github.com/valyala/fasthttp v1.4.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
5673
github.com/valyala/fasthttp v1.9.0 h1:hNpmUdy/+ZXYpGy0OBfm7K0UQTzb73W0T0U4iJIVrMw=
5774
github.com/valyala/fasthttp v1.9.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
75+
github.com/valyala/fasthttp v1.12.0 h1:TsB9qkSeiMXB40ELWWSRMjlsE+8IkqXHcs01y2d9aw0=
76+
github.com/valyala/fasthttp v1.12.0/go.mod h1:229t1eWu9UXTPmoUkbpN/fctKPBY4IJoFXQnxHGXy6E=
5877
github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
5978
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
6079
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
6180
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6281
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw=
6382
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
83+
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 h1:Q7tZBpemrlsc2I7IyODzhtallWRSm4Q0d09pL6XbQtU=
84+
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
6485
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
6586
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
6687
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
6788
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
89+
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
6890
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
6991
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
92+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7093
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

‎http/gateway/coordinate_change.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package gateway
2+
3+
import (
4+
"math"
5+
6+
"github.com/blobs-io/blobsgame/models/room"
7+
"github.com/blobs-io/blobsgame/models/user"
8+
)
9+
10+
type CoordinateChangeEventData struct {
11+
X int `json:"x"`
12+
Y int `json:"y"`
13+
Room string `json:"room"`
14+
}
15+
16+
func CoordinateChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
17+
data, ok := d.Data.(CoordinateChangeEventData)
18+
if !ok {
19+
return
20+
}
21+
22+
r, ok := room.Rooms[data.Room]
23+
if !ok {
24+
return
25+
}
26+
27+
p := r.GetPlayerByWebSocketID(c.ID)
28+
if p == nil {
29+
return
30+
}
31+
32+
if math.Abs(float64(data.X-p.X)) > 50 {
33+
34+
}
35+
36+
if math.Abs(float64(data.Y-p.Y)) > 50 {
37+
38+
}
39+
40+
// TODO: kick if user has been flagged too many times
41+
42+
if p.Role != user.AdminRole {
43+
if data.X < 0 {
44+
data.X = 0
45+
} else if data.X > r.Map.MapSize.Width {
46+
data.X = r.Map.MapSize.Width
47+
}
48+
49+
if data.Y < 0 {
50+
data.Y = 0
51+
} else if data.Y > r.Map.MapSize.Height {
52+
data.Y = r.Map.MapSize.Height
53+
}
54+
}
55+
56+
// this might cause some problems later, not too sure yet
57+
p.X = data.X
58+
p.Y = data.Y
59+
}

‎http/gateway/direction_change.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package gateway
2+
3+
import (
4+
"math"
5+
"time"
6+
7+
"github.com/blobs-io/blobsgame/models/player"
8+
"github.com/blobs-io/blobsgame/models/room"
9+
)
10+
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+
20+
func DirectionChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
21+
return // currently disabled
22+
data, ok := d.Data.(DirectionChangeEventData)
23+
if !ok {
24+
return
25+
}
26+
27+
r, ok := room.Rooms[data.Room]
28+
if !ok {
29+
return
30+
}
31+
32+
p := r.GetPlayerByWebSocketID(c.ID)
33+
if p == nil {
34+
return
35+
}
36+
37+
now := time.Now().UnixNano() / int64(time.Millisecond)
38+
39+
if now-data.DirectionChangedAt < 5000 {
40+
p.DirectionChangedAt = data.DirectionChangedAt
41+
} else {
42+
p.DirectionChangedAt = now
43+
}
44+
45+
p.Direction = data.Direction
46+
47+
distance := int(math.Abs(float64(p.DirectionChangeCoordinates.X-p.X)) + math.Abs(float64(p.DirectionChangeCoordinates.Y-p.Y)))
48+
p.Distance += distance
49+
50+
p.DirectionChangeCoordinates.X = data.DirectionChangeCoordinates.X
51+
p.DirectionChangeCoordinates.Y = data.DirectionChangeCoordinates.Y
52+
}

‎http/gateway/handler.go‎

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,134 @@
11
package gateway
22

33
import (
4-
"github.com/gofiber/fiber"
4+
"fmt"
5+
"time"
6+
7+
"github.com/blobs-io/blobsgame/models/player"
8+
"github.com/blobs-io/blobsgame/models/room"
9+
"github.com/blobs-io/blobsgame/models/session"
10+
"github.com/blobs-io/blobsgame/models/user"
11+
"github.com/gofiber/websocket"
512
)
613

7-
func Handle(ctx *fiber.Ctx) {
8-
// TODO
9-
}
14+
type WebSocketConnection struct {
15+
Conn *websocket.Conn
16+
ID string
17+
}
18+
19+
var connections = make(map[string]WebSocketConnection, 0)
20+
21+
func Handle(c *websocket.Conn) {
22+
id, err := session.Generate(8)
23+
if err != nil {
24+
fmt.Println(err)
25+
return
26+
}
27+
sid := string(id)
28+
29+
ws := WebSocketConnection{
30+
Conn: c,
31+
ID: sid,
32+
}
33+
connections[sid] = ws
34+
35+
for {
36+
var msg AnyMessage
37+
err := c.ReadJSON(&msg)
38+
if err != nil {
39+
fmt.Println(err)
40+
handleClose(&ws)
41+
break
42+
}
43+
44+
switch msg.Op {
45+
case OpHello:
46+
handleHello(&ws, &msg)
47+
case OpHeartbeat:
48+
handleHeartbeat(&ws, &msg)
49+
case OpEvent:
50+
handleEvent(&ws, &msg)
51+
case OpClose:
52+
handleClose(&ws)
53+
break
54+
}
55+
}
56+
}
57+
58+
func handleHello(c *WebSocketConnection, d *AnyMessage) {
59+
data, ok := d.Data.(HelloPayload)
60+
if !ok {
61+
return
62+
}
63+
64+
r, ok := room.Rooms[data.Room]
65+
if !ok {
66+
return
67+
}
68+
69+
if len(r.Players) >= room.PlayerLimit {
70+
// TODO: send error message to client (too many players online)
71+
return
72+
}
73+
74+
if r.Mode == room.EliminationMode &&
75+
r.State != room.EliminationCountdownState &&
76+
r.State != room.EliminationWaitingState {
77+
// TODO: send error message to client (room is already in game)
78+
return
79+
}
80+
81+
// TODO: allow non-guests
82+
// get user by session id
83+
p := player.Player{
84+
Username: r.GenerateGuestName(),
85+
ID: c.ID,
86+
BR: player.GuestBR,
87+
Blob: player.BlobowoID,
88+
Guest: true,
89+
Health: 100,
90+
Role: user.GuestRole,
91+
// TODO: randomize coordinates
92+
X: 50,
93+
Y: 50,
94+
}
95+
r.Players = append(r.Players, p)
96+
97+
// TODO: send room data to client
98+
}
99+
100+
func handleHeartbeat(c *WebSocketConnection, d *AnyMessage) {
101+
data, ok := d.Data.(HeartbeatPayload)
102+
if !ok {
103+
return
104+
}
105+
106+
r, ok := room.Rooms[data.Room]
107+
if !ok {
108+
return
109+
}
110+
111+
p := r.GetPlayerByWebSocketID(c.ID)
112+
if p == nil {
113+
return
114+
}
115+
116+
p.LastPing = time.Now().UnixNano() / int64(time.Millisecond)
117+
}
118+
119+
func handleEvent(c *WebSocketConnection, d *AnyMessage) {
120+
switch d.T {
121+
case CoordinateChangeEvent:
122+
CoordinateChangeEventCallback(c, d)
123+
case DirectionChangeEvent:
124+
DirectionChangeEventCallback(c, d)
125+
case PlayerKickEvent:
126+
PlayerKickEventCallback(c, d)
127+
case NomKeyEvent:
128+
case ItemCollectEvent:
129+
}
130+
}
131+
132+
func handleClose(c *WebSocketConnection) {
133+
delete(connections, c.ID)
134+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /