Skip to content

Commit 7c1293c

Browse files
committed
more gateway things
1 parent 40f46ea commit 7c1293c

File tree

7 files changed

+73
-49
lines changed

7 files changed

+73
-49
lines changed

http/gateway/coordinate_change.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import (
1010

1111
func CoordinateChangeEventCallback(c *WebSocketConnection, d *AnyMessage) {
1212
roomID, ok := d.Data["room"].(string)
13-
if !ok {
14-
return
15-
}
13+
var x, y int
1614

17-
x, ok := d.Data["x"].(int)
18-
if !ok {
15+
if temp, ok := d.Data["x"].(float64); ok {
16+
x = int(temp)
17+
} else {
1918
return
2019
}
2120

22-
y, ok := d.Data["y"].(int)
23-
if !ok {
21+
if temp, ok := d.Data["y"].(float64); ok {
22+
y = int(temp)
23+
} else {
2424
return
2525
}
2626

http/gateway/handler.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,19 @@ func handleHello(c *WebSocketConnection, d *AnyMessage) {
8888
Health: 100,
8989
X: rand.Intn(r.Map.MapSize.Width),
9090
Y: rand.Intn(r.Map.MapSize.Height),
91+
Conn: c.Conn,
9192
}
9293

9394
if err != nil && err.Error() == user.UserNotFound {
9495
p.Username = r.GenerateGuestName()
9596
p.BR = player.GuestBR
96-
p.Blob = player.BlobowoID
97+
p.Blob = user.StartBlob
9798
p.Guest = true
9899
p.Role = user.GuestRole
99100
} else if u != nil {
100101
p.Username = u.Username
101102
p.BR = u.BR
102-
// TODO: write a function that converts a blob string to int
103-
//p.Blob = u.ActiveBlob
104-
p.Blob = player.BlobowoID
103+
p.Blob = user.StartBlob
105104
p.Guest = false
106105
p.Role = u.Role
107106
p.Coins = u.Blobcoins
@@ -199,3 +198,24 @@ func (c *WebSocketConnection) HandleAntiCheatFlags(r *room.Room, flags int) bool
199198
}
200199
return false
201200
}
201+
202+
func WatchRoom(r *room.Room) {
203+
for {
204+
for _, p := range r.Players {
205+
conn, ok := connections[p.ID]
206+
if !ok {
207+
continue
208+
}
209+
210+
conn.Send(AnyMessage{
211+
Op: OpEvent,
212+
T: CoordinateChangeEvent,
213+
Data: map[string]interface{}{
214+
"players": r.Players,
215+
},
216+
})
217+
}
218+
219+
time.Sleep(time.Millisecond * 25)
220+
}
221+
}

main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/blobs-io/blobsgame/database"
10+
"github.com/blobs-io/blobsgame/http/gateway"
1011
"github.com/blobs-io/blobsgame/http/web"
1112
"github.com/blobs-io/blobsgame/models/gamemap"
1213
"github.com/blobs-io/blobsgame/models/room"
@@ -48,8 +49,9 @@ func main() {
4849
// Create rooms
4950
room.Rooms = make(map[string]*room.Room)
5051
for i := 0; i < 3; i++ {
51-
room.New(room.FFAMode)
52-
room.New(room.EliminationMode)
52+
go gateway.WatchRoom(room.New(room.FFAMode))
53+
54+
go gateway.WatchRoom(room.New(room.EliminationMode))
5355
}
5456
fmt.Printf("Created %d rooms\n", len(room.Rooms))
5557

models/item/item.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package item
22

33
const (
44
HealthItem = 0
5-
CoinItem = 1
6-
ItemWidth = 20
7-
ItemHeight = 20
5+
CoinItem = 1
6+
ItemWidth = 20.0
7+
ItemHeight = 20.0
88
)
99

1010
type Item struct {
11-
X int `json:"x"`
12-
Y int `json:"y"`
13-
Type uint8 `json:"type"`
14-
ID string `json:"id"`
15-
}
11+
X int `json:"x"`
12+
Y int `json:"y"`
13+
Type uint8 `json:"type"`
14+
ID string `json:"id"`
15+
}

models/player/player.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package player
22

33
import (
44
"time"
5+
6+
"github.com/gofiber/websocket"
57
)
68

79
const (
@@ -21,9 +23,6 @@ const (
2123
DirectionRight = 1
2224
DirectionDown = 2
2325
DirectionLeft = 3
24-
25-
// Blob IDs
26-
BlobowoID = 0
2726
)
2827

2928
type CoordinatesAny struct {
@@ -32,26 +31,27 @@ type CoordinatesAny struct {
3231
}
3332

3433
type Player struct {
35-
Username string `json:"username"`
36-
BR int `json:"br"`
37-
Blob uint8 `json:"blob"` // Blob ID
38-
Role int8 `json:"role"`
39-
ID string `json:"id"`
40-
LastNom int64 `json:"lastNom"`
41-
Direction uint8 `json:"direction"`
42-
DirectionChangeCoordinates CoordinatesAny `json:"directionChangeCoordinates"`
43-
DirectionChangedAt int64 `json:"directionChangedAt"`
44-
Guest bool `json:"guest"`
45-
Distance int `json:"distance"`
46-
Health uint8 `json:"health"`
47-
AntiCheatFlags int `json:"antiCheatFlags"`
48-
X int `json:"x"`
49-
Y int `json:"y"`
50-
LastRegeneration int64 `json:"lastRegeneration"`
51-
LastPing int64 `json:"lastPing"`
52-
Coins int `json:"coins"`
53-
Noms int `json:"noms"`
54-
XP int `json:"xp"`
34+
Username string `json:"username"`
35+
BR int `json:"br"`
36+
Blob string `json:"blob"`
37+
Role int8 `json:"role"`
38+
ID string `json:"id"`
39+
LastNom int64 `json:"lastNom"`
40+
Direction uint8 `json:"direction"`
41+
DirectionChangeCoordinates CoordinatesAny `json:"directionChangeCoordinates"`
42+
DirectionChangedAt int64 `json:"directionChangedAt"`
43+
Guest bool `json:"guest"`
44+
Distance int `json:"distance"`
45+
Health uint8 `json:"health"`
46+
AntiCheatFlags int `json:"antiCheatFlags"`
47+
X int `json:"x"`
48+
Y int `json:"y"`
49+
LastRegeneration int64 `json:"lastRegeneration"`
50+
LastPing int64 `json:"lastPing"`
51+
Coins int `json:"coins"`
52+
Noms int `json:"noms"`
53+
XP int `json:"xp"`
54+
Conn *websocket.Conn `json:"-"`
5555
}
5656

5757
func (p *Player) Update(br int, coins int, xp int) {

models/room/room.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,9 @@ func (r *Room) GetPlayerByUsername(username string) *player.Player {
110110
}
111111

112112
func (r *Room) GetPlayerByWebSocketID(id string) *player.Player {
113-
for i, p := range r.Players {
114-
if p.ID == id {
115-
return r.Players[i]
116-
}
113+
index := r.GetPlayerIndexByWebSocketID(id)
114+
if index > -1 {
115+
return r.Players[index]
117116
}
118117
return nil
119118
}

models/user/user.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ const (
6262
BlobNoAccess = "you cannot use this blob"
6363
DailyGiftFailed = "you have already requested your daily gift, come back later" // TODO: display time left
6464

65+
// Valid blobs
66+
Blobowo = "blobowo"
67+
6568
// Properties
6669
StartRating = 1000
6770
StartCoins = 0
68-
StartBlob = "blobowo"
71+
StartBlob = Blobowo
6972
StartXP = 0
7073
DailyCoins = 20
7174

0 commit comments

Comments
 (0)