From 38ac32e46009a82f3d23861ad586faf3131233dc Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 19 Nov 2022 13:25:00 +0100 Subject: [PATCH] Add move logic and helpers. --- go.mod | 4 ++-- main.go | 2 +- server/chess.go | 49 ++++++++++++++++++++++++++++++++++++++++ server/chess_move.go | 54 ++++++++++++++++++++++++++++++++++++++++++++ server/server.go | 18 ++++----------- 5 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 server/chess.go create mode 100644 server/chess_move.go diff --git a/go.mod b/go.mod index 4cfea72..446a51e 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ -module ws_server +module mchess_server go 1.19 -require github.com/gorilla/websocket v1.5.0 // indirect +require github.com/gorilla/websocket v1.5.0 diff --git a/main.go b/main.go index 0fa7ce3..78473da 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ package main import ( "log" + "mchess_server/server" "net/http" - "ws_server/server" ) func main() { diff --git a/server/chess.go b/server/chess.go new file mode 100644 index 0000000..17eeedc --- /dev/null +++ b/server/chess.go @@ -0,0 +1,49 @@ +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/chess_move.go b/server/chess_move.go new file mode 100644 index 0000000..56b2f88 --- /dev/null +++ b/server/chess_move.go @@ -0,0 +1,54 @@ +package server + +import ( + "errors" + "fmt" + "log" + "strings" +) + +type chessMove struct { + realMove bool + startSquare chessCoordinate + endSquare chessCoordinate +} + +type chessCoordinate struct { + col int + row int +} + +func parseMove(received string) (*chessMove, error) { + var move chessMove + + splitReceived := strings.Split(received, " ") + log.Println(splitReceived) + + realMoveString := splitReceived[1] + startSquareString := splitReceived[2] + endSquareString := splitReceived[3] + + if strings.Compare(realMoveString, "mv") == 0 { + move.realMove = true + } else if strings.Compare(realMoveString, "pc") == 0 { + move.realMove = false + } else { + return nil, errors.New("NEITHER MV OR PC RECEIVED AS FIRST BYTES") + } + + move.startSquare.col = int(([]rune(startSquareString)[0] - '0')) + move.startSquare.row = int(([]rune(startSquareString)[1] - '0')) + + move.endSquare.col = int(([]rune(endSquareString)[0] - '0')) + move.endSquare.row = int(([]rune(endSquareString)[1] - '0')) + + return &move, nil +} + +func moveToString(move chessMove) string { + + str := fmt.Sprint(move.startSquare.col) + fmt.Sprint(move.startSquare.row) + + " " + fmt.Sprint(move.endSquare.col) + fmt.Sprint(move.endSquare.row) + + return str +} diff --git a/server/server.go b/server/server.go index 2c22695..fae0b42 100644 --- a/server/server.go +++ b/server/server.go @@ -10,6 +10,8 @@ import ( var upgrader = websocket.Upgrader{} // use default options func SocketHandler(w http.ResponseWriter, r *http.Request) { + log.Println("SocketHandler invoked") + conn, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("Error during connection upgrading:", err) @@ -17,18 +19,6 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) { } defer conn.Close() - // The event loop - for { - messageType, message, err := conn.ReadMessage() - if err != nil { - log.Println("Error during message reading:", err) - break - } - log.Printf("Received: %s", message) - err = conn.WriteMessage(messageType, message) - if err != nil { - log.Println("Error during message writing:", err) - break - } - } + var game = NewChessGame() + game.handle(conn) }