Use UUIDs to identify players.

This commit is contained in:
Marco 2023-04-18 22:19:28 +02:00
parent b4face1dca
commit 9c8f46c9a4
6 changed files with 18 additions and 13 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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.

View File

@ -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()

View File

@ -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,
}
}

View File

@ -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
}