Merge pull request 'hub_feauture' (#1) from hub_feauture into main
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m8s
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m8s
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
55
controllers/connection.go
Normal file
55
controllers/connection.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"server/models"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Hub) readLoop(conn *websocket.Conn) {
|
||||||
|
handMessage := models.Message{Type: MSG_WELCOME}
|
||||||
|
_ = conn.WriteMessage(websocket.BinaryMessage, handMessage.Encode())
|
||||||
|
|
||||||
|
var player *models.Player
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if player != nil {
|
||||||
|
h.removePlayer(player.ID)
|
||||||
|
log.Println("Player left:", player.ID)
|
||||||
|
}
|
||||||
|
conn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
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:
|
||||||
|
if player != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
player = h.handShake(msg.Payload, conn)
|
||||||
|
log.Println("Player joined:", player.ID)
|
||||||
|
|
||||||
|
case MSG_INPUT:
|
||||||
|
if player == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
reader := models.NewReader(msg.Payload)
|
||||||
|
x := reader.ReadF32()
|
||||||
|
y := reader.ReadF32()
|
||||||
|
h.updatePosition(x, y, player)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
controllers/hub.go
Normal file
26
controllers/hub.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"server/models"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Hub struct {
|
||||||
|
Players map[uint32]*models.Player
|
||||||
|
Mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHub() *Hub {
|
||||||
|
return &Hub{
|
||||||
|
Players: make(map[uint32]*models.Player),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) Start() {
|
||||||
|
go h.updateWorld()
|
||||||
|
http.HandleFunc("/ws", h.ws)
|
||||||
|
log.Println("Server listen port 8080")
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
}
|
||||||
43
controllers/players.go
Normal file
43
controllers/players.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"server/models"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Hub) handShake(msg []byte, conn *websocket.Conn) *models.Player {
|
||||||
|
reader := models.NewReader(msg)
|
||||||
|
newID := reader.ReadU32()
|
||||||
|
name := reader.ReadString()
|
||||||
|
|
||||||
|
player := &models.Player{
|
||||||
|
Entity: models.Entity{
|
||||||
|
Type: models.EntityPlayer,
|
||||||
|
ID: newID,
|
||||||
|
},
|
||||||
|
Name: name,
|
||||||
|
Conn: conn,
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Mu.Lock()
|
||||||
|
h.Players[player.ID] = player
|
||||||
|
h.Mu.Unlock()
|
||||||
|
|
||||||
|
return player
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) updatePosition(x, y float32, player *models.Player) {
|
||||||
|
h.Mu.Lock()
|
||||||
|
defer h.Mu.Unlock()
|
||||||
|
|
||||||
|
player.X = x
|
||||||
|
player.Y = y
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) removePlayer(id uint32) {
|
||||||
|
h.Mu.Lock()
|
||||||
|
defer h.Mu.Unlock()
|
||||||
|
|
||||||
|
delete(h.Players, id)
|
||||||
|
}
|
||||||
7
controllers/protocol.go
Normal file
7
controllers/protocol.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
const (
|
||||||
|
MSG_WELCOME = 0
|
||||||
|
MSG_INPUT = 1
|
||||||
|
MSG_SNAPSHOT = 2
|
||||||
|
)
|
||||||
46
controllers/world.go
Normal file
46
controllers/world.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"server/models"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Hub) broadcastSnapshot() {
|
||||||
|
h.Mu.RLock()
|
||||||
|
defer h.Mu.RUnlock()
|
||||||
|
|
||||||
|
w := models.Writer{}
|
||||||
|
w.WriteU16(uint16(len(h.Players)))
|
||||||
|
|
||||||
|
for _, p := range h.Players {
|
||||||
|
w.WriteU32(p.ID)
|
||||||
|
w.WriteU8(uint8(models.EntityPlayer))
|
||||||
|
w.WriteF32(p.X)
|
||||||
|
w.WriteF32(p.Y)
|
||||||
|
w.WriteF32(p.Z)
|
||||||
|
w.WriteF32(p.Yaw)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := models.Message{
|
||||||
|
Type: MSG_SNAPSHOT,
|
||||||
|
Version: 1,
|
||||||
|
Payload: w.Bytes(),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range h.Players {
|
||||||
|
if p.Conn != nil {
|
||||||
|
_ = p.Conn.WriteMessage(websocket.BinaryMessage, msg.Encode())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) updateWorld() {
|
||||||
|
ticker := time.NewTicker(50 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
h.broadcastSnapshot()
|
||||||
|
}
|
||||||
|
}
|
||||||
22
controllers/ws.go
Normal file
22
controllers/ws.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Hub) ws(w http.ResponseWriter, r *http.Request) {
|
||||||
|
upgrader := websocket.Upgrader{
|
||||||
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go h.readLoop(conn)
|
||||||
|
}
|
||||||
187
main.go
187
main.go
@@ -1,191 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"server/controllers"
|
||||||
"encoding/binary"
|
|
||||||
"log"
|
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
PacketHandshake uint8 = 0
|
|
||||||
PacketWorld uint8 = 1
|
|
||||||
|
|
||||||
TickRate = 30
|
|
||||||
Speed = 5.0
|
|
||||||
|
|
||||||
SpawnMinX = -5.0
|
|
||||||
SpawnMaxX = 5.0
|
|
||||||
SpawnMinZ = -5.0
|
|
||||||
SpawnMaxZ = 5.0
|
|
||||||
SpawnRadius = 1.5
|
|
||||||
)
|
|
||||||
|
|
||||||
type Player struct {
|
|
||||||
ID uint32
|
|
||||||
|
|
||||||
X float32
|
|
||||||
Y float32
|
|
||||||
Z float32
|
|
||||||
|
|
||||||
InputX float32
|
|
||||||
InputZ float32
|
|
||||||
|
|
||||||
Conn *websocket.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
upgrader = websocket.Upgrader{
|
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
|
||||||
}
|
|
||||||
|
|
||||||
players = make(map[uint32]*Player)
|
|
||||||
playersMu sync.Mutex
|
|
||||||
nextID uint32 = 1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rand.Seed(time.Now().UnixNano())
|
hub := controllers.NewHub()
|
||||||
|
hub.Start()
|
||||||
http.HandleFunc("/ws", handleWS)
|
|
||||||
|
|
||||||
go gameLoop()
|
|
||||||
|
|
||||||
log.Println("Server running on :8080")
|
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleWS(w http.ResponseWriter, r *http.Request) {
|
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
playersMu.Lock()
|
|
||||||
id := nextID
|
|
||||||
nextID++
|
|
||||||
|
|
||||||
x, z := randomSpawnPosition()
|
|
||||||
|
|
||||||
player := &Player{
|
|
||||||
ID: id,
|
|
||||||
X: x,
|
|
||||||
Y: 0,
|
|
||||||
Z: z,
|
|
||||||
Conn: conn,
|
|
||||||
}
|
|
||||||
players[id] = player
|
|
||||||
playersMu.Unlock()
|
|
||||||
|
|
||||||
{
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
binary.Write(buf, binary.LittleEndian, PacketHandshake)
|
|
||||||
binary.Write(buf, binary.LittleEndian, id)
|
|
||||||
conn.WriteMessage(websocket.BinaryMessage, buf.Bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("Player connected:", id, "spawn at", x, z)
|
|
||||||
|
|
||||||
go readLoop(player)
|
|
||||||
}
|
|
||||||
|
|
||||||
func randomSpawnPosition() (float32, float32) {
|
|
||||||
for i := 0; i < 20; i++ {
|
|
||||||
x := rand.Float32()*(SpawnMaxX-SpawnMinX) + SpawnMinX
|
|
||||||
z := rand.Float32()*(SpawnMaxZ-SpawnMinZ) + SpawnMinZ
|
|
||||||
|
|
||||||
if isSpawnFree(x, z) {
|
|
||||||
return x, z
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fallback
|
|
||||||
return 0, 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func isSpawnFree(x, z float32) bool {
|
|
||||||
for _, p := range players {
|
|
||||||
dx := p.X - x
|
|
||||||
dz := p.Z - z
|
|
||||||
if dx*dx+dz*dz < SpawnRadius*SpawnRadius {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func readLoop(p *Player) {
|
|
||||||
defer func() {
|
|
||||||
playersMu.Lock()
|
|
||||||
delete(players, p.ID)
|
|
||||||
playersMu.Unlock()
|
|
||||||
|
|
||||||
p.Conn.Close()
|
|
||||||
log.Println("Player disconnected:", p.ID)
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
_, data, err := p.Conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(data) < 8 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := bytes.NewReader(data)
|
|
||||||
binary.Read(buf, binary.LittleEndian, &p.InputX)
|
|
||||||
binary.Read(buf, binary.LittleEndian, &p.InputZ)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func gameLoop() {
|
|
||||||
ticker := time.NewTicker(time.Second / TickRate)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
dt := float32(1.0 / TickRate)
|
|
||||||
|
|
||||||
for range ticker.C {
|
|
||||||
update(dt)
|
|
||||||
broadcast()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func update(dt float32) {
|
|
||||||
playersMu.Lock()
|
|
||||||
defer playersMu.Unlock()
|
|
||||||
|
|
||||||
for _, p := range players {
|
|
||||||
p.X += p.InputX * Speed * dt
|
|
||||||
p.Z += p.InputZ * Speed * dt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func broadcast() {
|
|
||||||
playersMu.Lock()
|
|
||||||
defer playersMu.Unlock()
|
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
|
|
||||||
binary.Write(buf, binary.LittleEndian, PacketWorld)
|
|
||||||
binary.Write(buf, binary.LittleEndian, uint16(len(players)))
|
|
||||||
|
|
||||||
for _, p := range players {
|
|
||||||
binary.Write(buf, binary.LittleEndian, p.ID)
|
|
||||||
binary.Write(buf, binary.LittleEndian, p.X)
|
|
||||||
binary.Write(buf, binary.LittleEndian, p.Y)
|
|
||||||
binary.Write(buf, binary.LittleEndian, p.Z)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := buf.Bytes()
|
|
||||||
|
|
||||||
for _, p := range players {
|
|
||||||
p.Conn.WriteMessage(websocket.BinaryMessage, data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
19
models/entity.go
Normal file
19
models/entity.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type EntityType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
EntityPlayer EntityType = 1
|
||||||
|
EntityNPC EntityType = 2
|
||||||
|
EntityBullet EntityType = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
type Entity struct {
|
||||||
|
ID uint32
|
||||||
|
Type EntityType
|
||||||
|
|
||||||
|
X float32
|
||||||
|
Y float32
|
||||||
|
Z float32
|
||||||
|
Yaw float32
|
||||||
|
}
|
||||||
48
models/message.go
Normal file
48
models/message.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
Type uint16
|
||||||
|
Version uint16
|
||||||
|
Payload []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) Encode() []byte {
|
||||||
|
payloadLen := len(m.Payload)
|
||||||
|
if payloadLen > 0xFFFF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 8+payloadLen)
|
||||||
|
|
||||||
|
binary.LittleEndian.PutUint16(buf[0:2], m.Type)
|
||||||
|
binary.LittleEndian.PutUint16(buf[2:4], m.Version)
|
||||||
|
binary.LittleEndian.PutUint16(buf[4:6], uint16(payloadLen))
|
||||||
|
|
||||||
|
copy(buf[8:], m.Payload)
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
func Decode(data []byte) (*Message, error) {
|
||||||
|
if len(data) < 8 {
|
||||||
|
return nil, errors.New("message too short")
|
||||||
|
}
|
||||||
|
|
||||||
|
payloadLen := binary.LittleEndian.Uint16(data[4:6])
|
||||||
|
if len(data) != int(8+payloadLen) {
|
||||||
|
return nil, errors.New("invalid payload length")
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := &Message{
|
||||||
|
Type: binary.LittleEndian.Uint16(data[0:2]),
|
||||||
|
Version: binary.LittleEndian.Uint16(data[2:4]),
|
||||||
|
Payload: make([]byte, payloadLen),
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(msg.Payload, data[8:])
|
||||||
|
return msg, nil
|
||||||
|
}
|
||||||
9
models/player.go
Normal file
9
models/player.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/gorilla/websocket"
|
||||||
|
|
||||||
|
type Player struct {
|
||||||
|
Entity
|
||||||
|
Name string
|
||||||
|
Conn *websocket.Conn
|
||||||
|
}
|
||||||
50
models/reader.go
Normal file
50
models/reader.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Reader struct {
|
||||||
|
buf []byte
|
||||||
|
off int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReader(b []byte) *Reader {
|
||||||
|
return &Reader{buf: b}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) ReadU8() uint8 {
|
||||||
|
v := r.buf[r.off]
|
||||||
|
r.off++
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) ReadU16() uint16 {
|
||||||
|
v := binary.LittleEndian.Uint16(r.buf[r.off:])
|
||||||
|
r.off += 2
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) ReadU32() uint32 {
|
||||||
|
v := binary.LittleEndian.Uint32(r.buf[r.off:])
|
||||||
|
r.off += 4
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) ReadF32() float32 {
|
||||||
|
v := math.Float32frombits(binary.LittleEndian.Uint32(r.buf[r.off:]))
|
||||||
|
r.off += 4
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) ReadBool() bool {
|
||||||
|
return r.ReadU8() == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) ReadString() string {
|
||||||
|
l := r.ReadU16()
|
||||||
|
s := string(r.buf[r.off : r.off+int(l)])
|
||||||
|
r.off += int(l)
|
||||||
|
return s
|
||||||
|
}
|
||||||
49
models/writer.go
Normal file
49
models/writer.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Writer struct {
|
||||||
|
buf []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Writer) Bytes() []byte {
|
||||||
|
return w.buf
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Writer) WriteU8(v uint8) {
|
||||||
|
w.buf = append(w.buf, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Writer) WriteU16(v uint16) {
|
||||||
|
tmp := make([]byte, 2)
|
||||||
|
binary.LittleEndian.PutUint16(tmp, v)
|
||||||
|
w.buf = append(w.buf, tmp...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Writer) WriteU32(v uint32) {
|
||||||
|
tmp := make([]byte, 4)
|
||||||
|
binary.LittleEndian.PutUint32(tmp, v)
|
||||||
|
w.buf = append(w.buf, tmp...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Writer) WriteF32(v float32) {
|
||||||
|
tmp := make([]byte, 4)
|
||||||
|
binary.LittleEndian.PutUint32(tmp, math.Float32bits(v))
|
||||||
|
w.buf = append(w.buf, tmp...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Writer) WriteBool(v bool) {
|
||||||
|
if v {
|
||||||
|
w.WriteU8(1)
|
||||||
|
} else {
|
||||||
|
w.WriteU8(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Writer) WriteString(s string) {
|
||||||
|
w.WriteU16(uint16(len(s)))
|
||||||
|
w.buf = append(w.buf, []byte(s)...)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user