This commit is contained in:
Smile Rex
2026-01-21 19:14:05 +03:00
parent 280d9801d6
commit 9033264a15
5 changed files with 59 additions and 24 deletions

47
main.go
View File

@@ -4,7 +4,6 @@ import (
"log"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/websocket"
)
@@ -13,22 +12,47 @@ var upgrader = websocket.Upgrader{
}
func ServeWS(room *Room, w http.ResponseWriter, r *http.Request) {
conn, _ := upgrader.Upgrade(w, r, nil)
id := uuid.New().String()
player := &Player{
ID: id,
Conn: conn,
X: 180,
Y: 320,
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
room.Players[id] = player
var auth AuthMessage
if err := conn.ReadJSON(&auth); err != nil || auth.Type != "auth" {
conn.Close()
return
}
data, ok := VerifyTelegramInitData(auth.InitData, "7697757472:AAESD9HfkWwbIZe-HXR7IazUShr69hZTLmE")
if !ok {
conn.Close()
return
}
userID := data["user.id"]
username := data["user.username"]
if username == "" {
username = data["user.first_name"]
}
if username == "" {
username = "user_" + userID
}
player := &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": id,
"id": player.ID,
},
})
@@ -41,6 +65,7 @@ func readLoop(room *Room, player *Player) {
err := player.Conn.ReadJSON(&msg)
if err != nil {
delete(room.Players, player.ID)
player.Conn.Close()
return
}