From 7da9b2dcb2a1df8a14c24084fb93fca8bc7dfcb3 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 18 Dec 2022 01:05:23 +0100 Subject: [PATCH] Players can now play against each other (if they behave) --- server/game.go | 38 +++++++++++++++++++++----------------- server/lobby.go | 6 +++--- server/server.go | 26 +++++++++++++------------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/server/game.go b/server/game.go index d176e71..4752caa 100644 --- a/server/game.go +++ b/server/game.go @@ -12,9 +12,9 @@ type chessGame struct { } const ( - Player0_Move = 0 - Player1_Move = 1 - Check_Player_Change = 2 + WhiteToMove = 0 + BlackToMove = 1 + CheckPlayerChange = 2 ) func NewChessGame(gameId int, players [2]Player) *chessGame { @@ -28,20 +28,24 @@ func NewChessGame(gameId int, players [2]Player) *chessGame { func (game *chessGame) handle() { - gameState := Player0_Move + gameState := WhiteToMove game.currentTurnPlayer = game.players[0] var move *chessMove var messageType int var receivedMessage []byte - game.players[1].conn.WriteMessage(1, []byte("fb")) + WriteMessageToPlayer(&game.players[0], []byte("bd init"), 1) + WriteMessageToPlayer(&game.players[1], []byte("bd init"), 1) + + WriteMessageToPlayer(&game.players[0], []byte("cl white"), 1) + WriteMessageToPlayer(&game.players[1], []byte("cl black"), 1) for { switch gameState { - case Player0_Move: + case WhiteToMove: var err error - messageType, receivedMessage, err = game.players[0].conn.ReadMessage() + messageType, receivedMessage, err = ReadMessageFromPlayer(&game.players[0]) if err != nil { log.Println("Error during message reading:", err) return @@ -53,11 +57,11 @@ func (game *chessGame) handle() { log.Println("Game: ", game.id, err) return } - gameState = Check_Player_Change + gameState = CheckPlayerChange - case Player1_Move: + case BlackToMove: var err error - messageType, receivedMessage, err = game.players[1].conn.ReadMessage() + messageType, receivedMessage, err = ReadMessageFromPlayer(&game.players[1]) if err != nil { log.Println("Error during message reading:", err) return @@ -69,29 +73,29 @@ func (game *chessGame) handle() { log.Println("Game: ", game.id, err) return } - gameState = Check_Player_Change + gameState = CheckPlayerChange - case Check_Player_Change: + case CheckPlayerChange: if move.realMove { if game.currentTurnPlayer.id == game.players[0].id { game.currentTurnPlayer = game.players[1] - gameState = Player1_Move + gameState = BlackToMove } else { game.currentTurnPlayer = game.players[0] - gameState = Player0_Move + gameState = WhiteToMove } - err := game.currentTurnPlayer.conn.WriteMessage(messageType, []byte(receivedMessage)) + err := WriteMessageToPlayer(&game.currentTurnPlayer, receivedMessage, messageType) if err != nil { log.Println("Error during message writing:", err) break } } else { if game.currentTurnPlayer.id == game.players[0].id { - gameState = Player0_Move + gameState = WhiteToMove } else { - gameState = Player1_Move + gameState = BlackToMove } } diff --git a/server/lobby.go b/server/lobby.go index b3edd53..bc871c2 100644 --- a/server/lobby.go +++ b/server/lobby.go @@ -25,9 +25,9 @@ func (lobby *Lobby) registerPlayer(player *Player) { log.Println(lobby) - if len(lobby.players) == 2 { - player1 := lobby.players[0] - player2 := lobby.players[1] + if len(lobby.players)%2 == 0 { + player1 := lobby.players[len(lobby.players)-2] + player2 := lobby.players[len(lobby.players)-1] addPlayersToGame([2]Player{player1, player2}) } } diff --git a/server/server.go b/server/server.go index fdd1532..984cf93 100644 --- a/server/server.go +++ b/server/server.go @@ -20,19 +20,19 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) { return } - // Check if first message is the passphrase - msg_type, msg, err := conn.ReadMessage() - if err != nil { - return - } - - if string(msg) != "pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM" { - conn.WriteMessage(msg_type, []byte("Wrong password. Connection refused")) - return - } else { - conn.WriteMessage(msg_type, []byte("Password correct. Let's play")) - } - GetLobby().registerPlayer(NewPlayer(player_number, conn)) player_number = player_number + 1 } + +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.Println("") + 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.Println("") + return msgType, msg, err +}