All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m19s
68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"log"
|
|
"server/models"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
func (h *Hub) readLoop(conn *websocket.Conn) {
|
|
var id uint32
|
|
log.Println("client ws connected")
|
|
|
|
defer func() {
|
|
conn.Close()
|
|
if id != 0 {
|
|
h.removeEntity(id)
|
|
log.Println("entity [player] removed:", id)
|
|
}
|
|
|
|
log.Println("client ws disconnected")
|
|
}()
|
|
|
|
for {
|
|
_, bytes, err := conn.ReadMessage()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
msg, err := models.Decode(bytes)
|
|
if err != nil {
|
|
log.Println(err)
|
|
continue
|
|
}
|
|
|
|
switch msg.Type {
|
|
|
|
case MSG_WELCOME:
|
|
reader := models.NewReader(msg.Payload)
|
|
log.Println("MSG_WELCOME received", &msg.Payload)
|
|
id = reader.ReadU32()
|
|
name := reader.ReadString()
|
|
|
|
if h.Entities[id] != nil {
|
|
h.ErrorMsg("Entity already exists", conn)
|
|
}
|
|
|
|
player := &models.Player{
|
|
BaseEntity: models.BaseEntity{ID: id},
|
|
Name: name,
|
|
}
|
|
|
|
h.addEntity(id, player)
|
|
h.Clients[id] = conn
|
|
log.Println("entity [player] added:", id)
|
|
|
|
case MSG_INPUT:
|
|
reader := models.NewReader(msg.Payload)
|
|
id := reader.ReadU32()
|
|
x := reader.ReadF32()
|
|
y := reader.ReadF32()
|
|
z := reader.ReadF32()
|
|
h.updateEntityPosition(x, y, z, id)
|
|
log.Println(h.Entities)
|
|
}
|
|
}
|
|
}
|