From 3a2de183a1cf68ed3820cccc70ad1ba70f853918 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 14 Dec 2022 23:16:51 +0100 Subject: [PATCH] Flip second player's board. Use player's id for comparison. --- server/chess_move.go | 2 -- server/game.go | 12 ++++++++---- server/player.go | 6 +++--- server/server.go | 3 +-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/server/chess_move.go b/server/chess_move.go index d940a20..c802915 100644 --- a/server/chess_move.go +++ b/server/chess_move.go @@ -2,7 +2,6 @@ package server import ( "errors" - "log" "strings" ) @@ -21,7 +20,6 @@ func parseMove(received string) (*chessMove, error) { var move chessMove splitReceived := strings.Split(received, " ") - log.Println(splitReceived) realMoveString := splitReceived[1] startSquareString := splitReceived[2] diff --git a/server/game.go b/server/game.go index a79fdc7..d176e71 100644 --- a/server/game.go +++ b/server/game.go @@ -34,7 +34,7 @@ func (game *chessGame) handle() { var messageType int var receivedMessage []byte - game.players[0].conn.WriteMessage(1, []byte("fb")) + game.players[1].conn.WriteMessage(1, []byte("fb")) for { @@ -47,6 +47,8 @@ func (game *chessGame) handle() { return } move, err = parseMove(string(receivedMessage)) + log.Println("Player 0 moved: ", move) + if err != nil { log.Println("Game: ", game.id, err) return @@ -61,6 +63,8 @@ func (game *chessGame) handle() { return } move, err = parseMove(string(receivedMessage)) + log.Println("Player 1 moved: ", move) + if err != nil { log.Println("Game: ", game.id, err) return @@ -70,7 +74,7 @@ func (game *chessGame) handle() { case Check_Player_Change: if move.realMove { - if game.currentTurnPlayer == game.players[0] { + if game.currentTurnPlayer.id == game.players[0].id { game.currentTurnPlayer = game.players[1] gameState = Player1_Move } else { @@ -84,7 +88,7 @@ func (game *chessGame) handle() { break } } else { - if game.currentTurnPlayer == game.players[0] { + if game.currentTurnPlayer.id == game.players[0].id { gameState = Player0_Move } else { gameState = Player1_Move @@ -98,7 +102,7 @@ func (game *chessGame) handle() { } func addPlayersToGame(players [2]Player) { - log.Printf("Adding players \n%s\n%s\nto new game", players[0].name, players[1].name) + log.Printf("Adding players \n%d\n%d\nto new game", players[0].id, players[1].id) game := NewChessGame(rand.Int(), players) go game.handle() diff --git a/server/player.go b/server/player.go index 3a60cbd..8f16c9c 100644 --- a/server/player.go +++ b/server/player.go @@ -3,13 +3,13 @@ package server import "github.com/gorilla/websocket" type Player struct { - name string + id int conn *websocket.Conn } -func NewPlayer(name string, conn *websocket.Conn) *Player { +func NewPlayer(id int, conn *websocket.Conn) *Player { return &Player{ - name: name, + id: id, conn: conn, } } diff --git a/server/server.go b/server/server.go index cea38b0..fdd1532 100644 --- a/server/server.go +++ b/server/server.go @@ -1,7 +1,6 @@ package server import ( - "fmt" "log" "net/http" @@ -34,6 +33,6 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) { conn.WriteMessage(msg_type, []byte("Password correct. Let's play")) } - GetLobby().registerPlayer(NewPlayer("Player"+fmt.Sprint(player_number), conn)) + GetLobby().registerPlayer(NewPlayer(player_number, conn)) player_number = player_number + 1 }