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 dac82b4

Browse files
committed
properly handle gateway close and add item_collect event
1 parent 5513d3e commit dac82b4

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

‎http/gateway/handler.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ func handleEvent(c *WebSocketConnection, d *AnyMessage) {
125125
case PlayerKickEvent:
126126
PlayerKickEventCallback(c, d)
127127
case NomKeyEvent:
128+
NomKeyEventCallback(c, d)
128129
case ItemCollectEvent:
130+
ItemCollectEventCallback(c, d)
129131
}
130132
}
131133

132134
func handleClose(c *WebSocketConnection) {
135+
c.Conn.Close()
133136
delete(connections, c.ID)
134137
}

‎http/gateway/item_collect.go‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package gateway
2+
3+
import (
4+
"math/rand"
5+
6+
"github.com/blobs-io/blobsgame/models/item"
7+
"github.com/blobs-io/blobsgame/models/room"
8+
)
9+
10+
type ItemCollectEventData struct {
11+
Room string `json:"room"`
12+
Item string `json:"item"`
13+
}
14+
15+
func ItemCollectEventCallback(c *WebSocketConnection, d *AnyMessage) {
16+
data, ok := d.Data.(ItemCollectEventData)
17+
if !ok {
18+
return
19+
}
20+
21+
r, ok := room.Rooms[data.Room]
22+
if !ok {
23+
return
24+
}
25+
26+
p := r.GetPlayerByWebSocketID(c.ID)
27+
if p == nil {
28+
return
29+
}
30+
31+
var targetItem *item.Item
32+
for i := range r.Items {
33+
currentItem := &r.Items[i]
34+
if currentItem.ID == data.Item &&
35+
p.X < (currentItem.X+item.ItemWidth) &&
36+
p.X > (currentItem.X-item.ItemWidth) &&
37+
p.Y < (currentItem.Y+item.ItemHeight) &&
38+
p.Y > (currentItem.Y-item.ItemHeight) {
39+
targetItem = currentItem
40+
}
41+
}
42+
43+
if targetItem == nil {
44+
return
45+
}
46+
47+
// TODO: create new item and append it to r.Items
48+
// and remove targetItem from array
49+
50+
switch targetItem.Type {
51+
case item.HealthItem:
52+
p.Health += uint8(rand.Intn(5) + 10)
53+
case item.CoinItem:
54+
// TODO: generate random coins
55+
}
56+
}

0 commit comments

Comments
(0)

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