From 9c8f46c9a43779619a5a201bf77b6d7d50ec01e6 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 18 Apr 2023 22:19:28 +0200 Subject: [PATCH] Use UUIDs to identify players. --- go.mod | 1 + go.sum | 2 ++ main.go | 9 ++++----- server/game.go | 4 ++-- server/player.go | 11 +++++++---- server/server.go | 4 ++-- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 4e42c23..49247a5 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/goccy/go-json v0.10.0 // indirect + github.com/google/uuid v1.3.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect github.com/leodido/go-urn v1.2.1 // indirect diff --git a/go.sum b/go.sum index 21ffd93..5da78e0 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= diff --git a/main.go b/main.go index 4ef7444..be8050c 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/autotls" "github.com/gin-gonic/gin" + "github.com/google/uuid" "github.com/gorilla/websocket" ) @@ -15,8 +16,6 @@ var cert_path = "/etc/letsencrypt/live/chess.sw-gross.de/" var cert_file = cert_path + "fullchain.pem" var key_file = cert_path + "privkey.pem" -var player_number = 0 - var upgrader = websocket.Upgrader{} // album represents data about a record album. @@ -55,9 +54,9 @@ func main() { } func playRandomGame(c *gin.Context) { - server.GetLobby().RegisterPlayer(server.NewPlayer(player_number)) - player_number = player_number + 1 - c.IndentedJSON(http.StatusOK, player_number) + player_uuid := uuid.New() + server.GetLobby().RegisterPlayer(server.NewPlayer(player_uuid)) + c.IndentedJSON(http.StatusOK, player_uuid) } // getAlbums responds with the list of all albums as JSON. diff --git a/server/game.go b/server/game.go index ca95303..8eaedf0 100644 --- a/server/game.go +++ b/server/game.go @@ -65,7 +65,7 @@ func (game *chessGame) handle() { case CheckPlayerChange: if move.realMove { - if game.currentTurnPlayer.id == game.players[0].id { + if game.currentTurnPlayer.uuid == game.players[0].uuid { game.currentTurnPlayer = game.players[1] } else { game.currentTurnPlayer = game.players[0] @@ -97,7 +97,7 @@ func (game *chessGame) handle() { } func addPlayersToGame(players [2]Player) { - log.Printf("Adding players %d and %d to new game", players[0].id, players[1].id) + log.Printf("Adding players %d and %d to new game", players[0].uuid, players[1].uuid) game := NewChessGame(rand.Int(), players) go game.handle() diff --git a/server/player.go b/server/player.go index d8037e2..c29580b 100644 --- a/server/player.go +++ b/server/player.go @@ -1,14 +1,17 @@ package server -import "github.com/gorilla/websocket" +import ( + "github.com/google/uuid" + "github.com/gorilla/websocket" +) type Player struct { - id int + uuid uuid.UUID conn *websocket.Conn } -func NewPlayer(id int) *Player { +func NewPlayer(uuid uuid.UUID) *Player { return &Player{ - id: id, + uuid: uuid, } } diff --git a/server/server.go b/server/server.go index 0259492..a63dec9 100644 --- a/server/server.go +++ b/server/server.go @@ -28,14 +28,14 @@ func PlayHandler(w http.ResponseWriter, r *http.Request) { } func WriteMessageToPlayer(player *Player, msg []byte, msgType int) error { - log.Printf("Writing message: %s (with messagetype %d) to player %d", string(msg), msgType, player.id) + log.Printf("Writing message: %s (with messagetype %d) to player %d", string(msg), msgType, player.uuid) return player.conn.WriteMessage(msgType, msg) } func ReadMessageFromPlayer(player *Player) (messageType int, p []byte, err error) { msgType, msg, err := player.conn.ReadMessage() - log.Printf("Reading message: %s (with messagetype %d) from player %d", string(msg), msgType, player.id) + log.Printf("Reading message: %s (with messagetype %d) from player %d", string(msg), msgType, player.uuid) return msgType, msg, err }