From 5b6cb566ecf2863876e4d0bbcc2d4dcc5e6275f2 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 28 May 2023 17:43:05 +0200 Subject: [PATCH] Change lobby handling and websocket handling. --- main.go | 53 ++++++++++++++++++++++++++++++++---------------- server/game.go | 7 ++++++- server/lobby.go | 26 ++++++++++++------------ server/player.go | 5 +++-- 4 files changed, 57 insertions(+), 34 deletions(-) diff --git a/main.go b/main.go index dc4be65..9788631 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "local/m/mchess_server/server" "log" "net/http" @@ -25,7 +26,7 @@ func main() { router := gin.Default() router.GET("/api/random", registerForRandomGame) - router.POST("/api/ws", registerWebSocketConnection) + router.GET("/api/ws", registerWebSocketConnection) if hostname == "mbook" { log.Println("Starting service WITHOUT TLS") @@ -40,6 +41,8 @@ func registerForRandomGame(c *gin.Context) { player := server.NewPlayer(uuid.New()) server.GetLobby().RegisterPlayer(player) + log.Println("responding with player id ", player.Uuid) + c.IndentedJSON(http.StatusOK, server.PlayerInfo{ PlayerID: player.Uuid, }) @@ -47,23 +50,37 @@ func registerForRandomGame(c *gin.Context) { func registerWebSocketConnection(c *gin.Context) { // w http.ResponseWriter, r *http.Request - - var playerInfo server.PlayerInfo - - err := c.BindJSON(&playerInfo) + webSocketConn, err := websocket.Accept(c.Writer, c.Request, nil) if err != nil { - log.Printf("BindJSON has an error: (%e)", err) - } - - log.Println("playerID:", playerInfo.PlayerID) - - player, found := server.GetLobby()[playerInfo.PlayerID] - if found { - webSocketConn, err := websocket.Accept(c.Writer, c.Request, nil) - if err != nil { - log.Println(err) - return - } - player.Conn = *webSocketConn + log.Println(err) + return } + go waitForAndHandlePlayerID(c, *webSocketConn) +} + +func waitForAndHandlePlayerID(ctx context.Context, conn websocket.Conn) { + // var playerInfo server.PlayerInfo + + msgType, id, err := conn.Read(ctx) + + log.Println("read from websocket: ", msgType, id, err) + log.Println("id as string", string(id)) + + uuid, err := uuid.ParseBytes(id) + if err != nil { + log.Println(err) + conn.Close(websocket.StatusCode(400), err.Error()) + return + } + + lobby := server.GetLobby() + player, found := lobby[uuid] + if !found { + conn.Close(websocket.StatusCode(400), "player not found") + return + } + if player.Conn != nil { + player.Conn.Close(websocket.StatusCode(400), "closing existing connection") + } + player.Conn = &conn } diff --git a/server/game.go b/server/game.go index ffbd79a..f5bcbce 100644 --- a/server/game.go +++ b/server/game.go @@ -26,7 +26,6 @@ func NewGame() *Game { } func (game *Game) handle() { - defer log.Println("Game ", game.id, ": handle() ended") gameState := PlayerToMove @@ -88,7 +87,13 @@ func (game *Game) handle() { func (game *Game) addPlayersToGame(players [2]Player) { log.Printf("Adding players %s and %s to new game", players[0].Uuid.String(), players[1].Uuid.String()) + for _, player := range players { + player.InGame = true + } + game.players = players + + go game.handle() } func removePlayersFromLobby(players [2]Player) { diff --git a/server/lobby.go b/server/lobby.go index 71363ae..b9aca41 100644 --- a/server/lobby.go +++ b/server/lobby.go @@ -24,20 +24,20 @@ func newLobby() Lobby { func (lobby Lobby) RegisterPlayer(player *Player) { lobby[player.Uuid] = *player - if len(lobby)%2 == 0 { - var players [2]Player - var index int = 0 + var players []Player - for _, player := range lobby { - players[index] = player - index += 1 + for _, player := range lobby { + if !player.InGame { + players = append(players, player) } - - game := NewGame() - - game.addPlayersToGame(players) - - delete(lobby, players[0].Uuid) - delete(lobby, players[1].Uuid) } + + if len(players) < 2 { + return + } + + game := NewGame() + + game.addPlayersToGame([2]Player(players[:2])) + } diff --git a/server/player.go b/server/player.go index 49ac568..e4936aa 100644 --- a/server/player.go +++ b/server/player.go @@ -6,8 +6,9 @@ import ( ) type Player struct { - Uuid uuid.UUID - Conn websocket.Conn + Uuid uuid.UUID + Conn *websocket.Conn + InGame bool } type PlayerInfo struct {