Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ae941df3b | ||
|
|
73cb832728 | ||
|
|
4e04de5581 | ||
| 68ba9bb2e2 | |||
|
|
b7d33889fd | ||
| 2fd959255d | |||
|
|
12e789b22e | ||
| 49d6fb9815 | |||
|
|
ca06471d9e | ||
|
|
9033264a15 | ||
| dde276f22f | |||
|
|
280d9801d6 | ||
|
|
cb586cd5ce |
@@ -1,67 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import "server/models"
|
|
||||||
|
|
||||||
func (h *Hub) addEntity(id uint32, entity models.Entity) {
|
|
||||||
h.Mu.Lock()
|
|
||||||
defer h.Mu.Unlock()
|
|
||||||
|
|
||||||
h.Entities[id] = entity
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Hub) updateEntityPosition(x, y, z float32, entity_id uint32) {
|
|
||||||
h.Mu.Lock()
|
|
||||||
defer h.Mu.Unlock()
|
|
||||||
|
|
||||||
h.Entities[entity_id].SetPosition(x, y, z)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Hub) updateEntityRotation(yaw float32, entity_id uint32) {
|
|
||||||
h.Mu.Lock()
|
|
||||||
defer h.Mu.Unlock()
|
|
||||||
|
|
||||||
h.Entities[entity_id].SetYaw(yaw)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Hub) removeEntity(id uint32) {
|
|
||||||
h.Mu.Lock()
|
|
||||||
defer h.Mu.Unlock()
|
|
||||||
|
|
||||||
delete(h.Entities, id)
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"server/models"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Hub struct {
|
|
||||||
Entities map[uint32]models.Entity
|
|
||||||
Clients map[uint32]*websocket.Conn
|
|
||||||
Mu sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHub() *Hub {
|
|
||||||
return &Hub{
|
|
||||||
Entities: make(map[uint32]models.Entity),
|
|
||||||
Clients: make(map[uint32]*websocket.Conn),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Hub) Start() {
|
|
||||||
go h.updateWorld()
|
|
||||||
http.HandleFunc("/ws", h.ws)
|
|
||||||
log.Println("Server listen port 8080")
|
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"server/models"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Hub) ErrorMsg(msg string, conn *websocket.Conn) {
|
|
||||||
wr := models.Writer{}
|
|
||||||
wr.WriteString(msg)
|
|
||||||
errMsg := &models.Message{Type: 9, Version: 1, Payload: wr.Bytes()}
|
|
||||||
conn.WriteMessage(websocket.BinaryMessage, errMsg.Encode())
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
const (
|
|
||||||
MSG_WELCOME = 0
|
|
||||||
MSG_INPUT = 1
|
|
||||||
MSG_SNAPSHOT = 2
|
|
||||||
MSG_EVENT = 3
|
|
||||||
)
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
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.Entities)))
|
|
||||||
|
|
||||||
for _, e := range h.Entities {
|
|
||||||
w.WriteU32(e.GetID())
|
|
||||||
w.WriteU8(uint8(e.GetType()))
|
|
||||||
x, y, z := e.GetPosition()
|
|
||||||
w.WriteF32(x)
|
|
||||||
w.WriteF32(y)
|
|
||||||
w.WriteF32(z)
|
|
||||||
w.WriteF32(e.GetYaw())
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := models.Message{
|
|
||||||
Type: MSG_SNAPSHOT,
|
|
||||||
Version: 1,
|
|
||||||
Payload: w.Bytes(),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, conn := range h.Clients {
|
|
||||||
if conn != nil {
|
|
||||||
_ = 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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module server
|
|||||||
|
|
||||||
go 1.25.0
|
go 1.25.0
|
||||||
|
|
||||||
require github.com/gorilla/websocket v1.5.3
|
require (
|
||||||
|
github.com/gorilla/websocket v1.5.3
|
||||||
|
github.com/telegram-mini-apps/init-data-golang v1.5.0
|
||||||
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -1,2 +1,4 @@
|
|||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/telegram-mini-apps/init-data-golang v1.5.0 h1:rtpsmQ/nihkicPvnrdRXmHHtTnPvG1FmxMRZJwMKPz0=
|
||||||
|
github.com/telegram-mini-apps/init-data-golang v1.5.0/go.mod h1:GG4HnRx9ocjD4MjjzOw7gf9Ptm0NvFbDr5xqnfFOYuY=
|
||||||
|
|||||||
100
main.go
100
main.go
@@ -1,10 +1,102 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"server/controllers"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"server/src/controllers"
|
||||||
|
"server/src/models"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
var upgrader = websocket.Upgrader{
|
||||||
hub := controllers.NewHub()
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
hub.Start()
|
}
|
||||||
|
|
||||||
|
func ServeWS(room *controllers.Room, w http.ResponseWriter, r *http.Request) {
|
||||||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to upgrade connection:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var auth models.AuthMessage
|
||||||
|
if err := conn.ReadJSON(&auth); err != nil || auth.Type != "auth" {
|
||||||
|
log.Println("Invalid auth message. Connection closed!")
|
||||||
|
conn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := controllers.VerifyTelegramInitData(auth.InitData, "7697757472:AAESD9HfkWwbIZe-HXR7IazUShr69hZTLmE")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("initData is empty. Connection closed!")
|
||||||
|
conn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userID := data.User.ID
|
||||||
|
username := data.User.Username
|
||||||
|
|
||||||
|
if username == "" {
|
||||||
|
username = data.User.FirstName
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Player %d connected!", userID)
|
||||||
|
|
||||||
|
player := &models.Player{
|
||||||
|
ID: userID,
|
||||||
|
Username: username,
|
||||||
|
Conn: conn,
|
||||||
|
X: 180,
|
||||||
|
Y: 320,
|
||||||
|
}
|
||||||
|
|
||||||
|
room.Players[player.ID] = player
|
||||||
|
|
||||||
|
player.Conn.WriteJSON(map[string]any{
|
||||||
|
"type": "init",
|
||||||
|
"payload": map[string]string{
|
||||||
|
"id": fmt.Sprintf("%d", player.ID),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
go readLoop(room, player)
|
||||||
|
}
|
||||||
|
|
||||||
|
func readLoop(room *controllers.Room, player *models.Player) {
|
||||||
|
defer func() {
|
||||||
|
log.Printf("Player %d disconnected!", player.ID)
|
||||||
|
delete(room.Players, player.ID)
|
||||||
|
player.Conn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
var msg models.StateMessage
|
||||||
|
err := player.Conn.ReadJSON(&msg)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch msg.Type {
|
||||||
|
case models.InputMsgType:
|
||||||
|
var input models.InputMessage = msg.Payload.(models.InputMessage)
|
||||||
|
player.X = input.DX
|
||||||
|
player.Y = input.DY
|
||||||
|
room.Input <- input
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
room := controllers.NewRoom()
|
||||||
|
|
||||||
|
go room.Run()
|
||||||
|
|
||||||
|
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ServeWS(room, w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Println("Server started on :8080")
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import "github.com/gorilla/websocket"
|
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
ID uint32
|
|
||||||
Conn *websocket.Conn
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
type EntityType uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
EntityPlayer EntityType = 1
|
|
||||||
EntityNPC EntityType = 2
|
|
||||||
EntityBullet EntityType = 3
|
|
||||||
)
|
|
||||||
|
|
||||||
type Entity interface {
|
|
||||||
GetID() uint32
|
|
||||||
GetType() EntityType
|
|
||||||
GetPosition() (float32, float32, float32)
|
|
||||||
SetPosition(float32, float32, float32)
|
|
||||||
GetYaw() float32
|
|
||||||
SetYaw(float32)
|
|
||||||
}
|
|
||||||
|
|
||||||
type BaseEntity struct {
|
|
||||||
ID uint32
|
|
||||||
Type EntityType
|
|
||||||
X, Y, Z float32
|
|
||||||
Yaw float32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEntity) GetID() uint32 {
|
|
||||||
return e.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEntity) GetType() EntityType {
|
|
||||||
return e.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEntity) GetPosition() (float32, float32, float32) {
|
|
||||||
return e.X, e.Y, e.Z
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEntity) SetPosition(x, y, z float32) {
|
|
||||||
e.X = x
|
|
||||||
e.Y = y
|
|
||||||
e.Z = z
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEntity) GetYaw() float32 {
|
|
||||||
return e.Yaw
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *BaseEntity) SetYaw(yaw float32) {
|
|
||||||
e.Yaw = yaw
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
type Player struct {
|
|
||||||
BaseEntity
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPlayer(id uint32, name string) *Player {
|
|
||||||
return &Player{
|
|
||||||
BaseEntity: BaseEntity{
|
|
||||||
ID: id,
|
|
||||||
Type: EntityPlayer,
|
|
||||||
X: 0,
|
|
||||||
Y: 0,
|
|
||||||
Z: 0,
|
|
||||||
Yaw: 0,
|
|
||||||
},
|
|
||||||
Name: name,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
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)...)
|
|
||||||
}
|
|
||||||
83
src/controllers/room.go
Normal file
83
src/controllers/room.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"server/src/models"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Room struct {
|
||||||
|
Players map[int64]*models.Player
|
||||||
|
Input chan models.InputMessage
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRoom() *Room {
|
||||||
|
return &Room{
|
||||||
|
Players: make(map[int64]*models.Player),
|
||||||
|
Input: make(chan models.InputMessage, 128),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Room) update() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case input := <-r.Input:
|
||||||
|
r.mu.Lock()
|
||||||
|
p := r.Players[input.PlayerID]
|
||||||
|
if p != nil {
|
||||||
|
p.DX = input.DX
|
||||||
|
p.DY = input.DY
|
||||||
|
}
|
||||||
|
r.mu.Unlock()
|
||||||
|
default:
|
||||||
|
goto DONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DONE:
|
||||||
|
|
||||||
|
// 2️⃣ двигаем игроков
|
||||||
|
r.mu.Lock()
|
||||||
|
for _, p := range r.Players {
|
||||||
|
p.X += p.DX * 4
|
||||||
|
p.Y += p.DY * 4
|
||||||
|
}
|
||||||
|
r.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Room) broadcast() {
|
||||||
|
r.mu.Lock()
|
||||||
|
|
||||||
|
state := make(map[int64]map[string]any, len(r.Players))
|
||||||
|
for id, p := range r.Players {
|
||||||
|
state[id] = map[string]any{
|
||||||
|
"x": p.X,
|
||||||
|
"y": p.Y,
|
||||||
|
"name": p.Username,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.mu.Unlock()
|
||||||
|
|
||||||
|
msg := models.StateMessage{
|
||||||
|
Type: "input",
|
||||||
|
Payload: map[string]any{
|
||||||
|
"players": state,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// отправляем БЕЗ mutex — важно
|
||||||
|
for _, p := range r.Players {
|
||||||
|
_ = p.Conn.WriteJSON(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Room) Run() {
|
||||||
|
ticker := time.NewTicker(time.Second / 30)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
r.update()
|
||||||
|
r.broadcast()
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/controllers/telegram.go
Normal file
16
src/controllers/telegram.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
initdata "github.com/telegram-mini-apps/init-data-golang"
|
||||||
|
)
|
||||||
|
|
||||||
|
func VerifyTelegramInitData(initData, botToken string) (initdata.InitData, error) {
|
||||||
|
expIn := 24 * time.Hour
|
||||||
|
err := initdata.Validate(initData, botToken, expIn)
|
||||||
|
if err != nil {
|
||||||
|
return initdata.InitData{}, err
|
||||||
|
}
|
||||||
|
return initdata.Parse(initData)
|
||||||
|
}
|
||||||
6
src/models/auth.go
Normal file
6
src/models/auth.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type AuthMessage struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
InitData string `json:"initData"`
|
||||||
|
}
|
||||||
14
src/models/player.go
Normal file
14
src/models/player.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Player struct {
|
||||||
|
ID int64
|
||||||
|
Username string
|
||||||
|
Conn *websocket.Conn
|
||||||
|
|
||||||
|
X, Y float64
|
||||||
|
DX, DY float64
|
||||||
|
}
|
||||||
19
src/models/protocol.go
Normal file
19
src/models/protocol.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type MsgType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
InputMsgType MsgType = "input"
|
||||||
|
ChatMsgType MsgType = "chat"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InputMessage struct {
|
||||||
|
PlayerID int64
|
||||||
|
DX float64 `json:"dx"`
|
||||||
|
DY float64 `json:"dy"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StateMessage struct {
|
||||||
|
Type MsgType `json:"type"`
|
||||||
|
Payload any `json:"payload"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user