From 625e5b11faef32990ff54fe0aad3bfad4462e706 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 14 Dec 2022 22:19:47 +0100 Subject: [PATCH] Many changes. Handle rudimentary lobbies. Handle game with simple state machine. Flip one player's board. --- main.go | 5 +++ server/chess.go | 49 ---------------------- server/game.go | 105 +++++++++++++++++++++++++++++++++++++++++++++++ server/lobby.go | 33 +++++++++++++++ server/player.go | 19 +++++++++ server/server.go | 9 ++-- 6 files changed, 167 insertions(+), 53 deletions(-) delete mode 100644 server/chess.go create mode 100644 server/game.go create mode 100644 server/lobby.go create mode 100644 server/player.go diff --git a/main.go b/main.go index 78473da..0f44ac9 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( ) func main() { + http.HandleFunc("/", server.SocketHandler) err := http.ListenAndServe("localhost:8080", nil) @@ -16,3 +17,7 @@ func main() { log.Println("We ended") } + +func HandleFuncWrapper(a func()) { + a() +} diff --git a/server/chess.go b/server/chess.go deleted file mode 100644 index 17eeedc..0000000 --- a/server/chess.go +++ /dev/null @@ -1,49 +0,0 @@ -package server - -import ( - "log" - "math/rand" - - "github.com/gorilla/websocket" -) - -type chessGame struct { - id int -} - -func NewChessGame() *chessGame { - var game chessGame = chessGame{ - id: rand.Int(), - } - - return &game -} - -func (game *chessGame) handle(conn *websocket.Conn) { - for { - messageType, receivedMessage, err := conn.ReadMessage() - if err != nil { - log.Println("Error during message reading:", err) - break - } - - move, err := parseMove(string(receivedMessage)) - if err != nil { - log.Println("Game: ", game.id, err) - return - } - - log.Println("Game ", game.id, " move: ", move) - - messageToBeSent := moveToString(*move) - log.Println("About to send: ", messageToBeSent) - - err = conn.WriteMessage(messageType, []byte(messageToBeSent)) - if err != nil { - log.Println("Error during message writing:", err) - break - } - - } - -} diff --git a/server/game.go b/server/game.go new file mode 100644 index 0000000..a79fdc7 --- /dev/null +++ b/server/game.go @@ -0,0 +1,105 @@ +package server + +import ( + "log" + "math/rand" +) + +type chessGame struct { + id int + players [2]Player + currentTurnPlayer Player +} + +const ( + Player0_Move = 0 + Player1_Move = 1 + Check_Player_Change = 2 +) + +func NewChessGame(gameId int, players [2]Player) *chessGame { + var game chessGame = chessGame{ + players: players, + id: gameId, + } + + return &game +} + +func (game *chessGame) handle() { + + gameState := Player0_Move + game.currentTurnPlayer = game.players[0] + var move *chessMove + var messageType int + var receivedMessage []byte + + game.players[0].conn.WriteMessage(1, []byte("fb")) + + for { + + switch gameState { + case Player0_Move: + var err error + messageType, receivedMessage, err = game.players[0].conn.ReadMessage() + if err != nil { + log.Println("Error during message reading:", err) + return + } + move, err = parseMove(string(receivedMessage)) + if err != nil { + log.Println("Game: ", game.id, err) + return + } + gameState = Check_Player_Change + + case Player1_Move: + var err error + messageType, receivedMessage, err = game.players[1].conn.ReadMessage() + if err != nil { + log.Println("Error during message reading:", err) + return + } + move, err = parseMove(string(receivedMessage)) + if err != nil { + log.Println("Game: ", game.id, err) + return + } + gameState = Check_Player_Change + + case Check_Player_Change: + + if move.realMove { + if game.currentTurnPlayer == game.players[0] { + game.currentTurnPlayer = game.players[1] + gameState = Player1_Move + } else { + game.currentTurnPlayer = game.players[0] + gameState = Player0_Move + } + + err := game.currentTurnPlayer.conn.WriteMessage(messageType, []byte(receivedMessage)) + if err != nil { + log.Println("Error during message writing:", err) + break + } + } else { + if game.currentTurnPlayer == game.players[0] { + gameState = Player0_Move + } else { + gameState = Player1_Move + } + } + + } + + log.Println("GameState = ", gameState) + } +} + +func addPlayersToGame(players [2]Player) { + log.Printf("Adding players \n%s\n%s\nto new game", players[0].name, players[1].name) + game := NewChessGame(rand.Int(), players) + + go game.handle() +} diff --git a/server/lobby.go b/server/lobby.go new file mode 100644 index 0000000..b3edd53 --- /dev/null +++ b/server/lobby.go @@ -0,0 +1,33 @@ +package server + +import "log" + +type Lobby struct { + players []Player +} + +var lobbyInstance *Lobby = nil + +func GetLobby() *Lobby { + if lobbyInstance == nil { + lobbyInstance = newLobby() + } + + return lobbyInstance +} + +func newLobby() *Lobby { + return &Lobby{} +} + +func (lobby *Lobby) registerPlayer(player *Player) { + lobby.players = append(lobby.players, *player) + + log.Println(lobby) + + if len(lobby.players) == 2 { + player1 := lobby.players[0] + player2 := lobby.players[1] + addPlayersToGame([2]Player{player1, player2}) + } +} diff --git a/server/player.go b/server/player.go new file mode 100644 index 0000000..dbd8988 --- /dev/null +++ b/server/player.go @@ -0,0 +1,19 @@ +package server + +import "github.com/gorilla/websocket" + +type Player struct { + name string + conn *websocket.Conn +} + +func NewPlayer(name string, conn *websocket.Conn) *Player { + return &Player{ + name: name, + conn: conn, + } +} + +func (player *Player) movePlayerToGame(game chessGame) { + +} diff --git a/server/server.go b/server/server.go index fc97a3b..cea38b0 100644 --- a/server/server.go +++ b/server/server.go @@ -1,12 +1,15 @@ package server import ( + "fmt" "log" "net/http" "github.com/gorilla/websocket" ) +var player_number = 0 + var upgrader = websocket.Upgrader{} // use default options func SocketHandler(w http.ResponseWriter, r *http.Request) { @@ -17,8 +20,6 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) { log.Print("Error during connection upgrading:", err) return } - defer conn.Close() - defer log.Println("SocketHandler exited") // Check if first message is the passphrase msg_type, msg, err := conn.ReadMessage() @@ -33,6 +34,6 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) { conn.WriteMessage(msg_type, []byte("Password correct. Let's play")) } - var game = NewChessGame() - game.handle(conn) + GetLobby().registerPlayer(NewPlayer("Player"+fmt.Sprint(player_number), conn)) + player_number = player_number + 1 }