Add move logic and helpers.
This commit is contained in:
parent
b6e0b58631
commit
38ac32e460
4
go.mod
4
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
|
||||
|
2
main.go
2
main.go
@ -2,8 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"mchess_server/server"
|
||||
"net/http"
|
||||
"ws_server/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
49
server/chess.go
Normal file
49
server/chess.go
Normal file
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
54
server/chess_move.go
Normal file
54
server/chess_move.go
Normal file
@ -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
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user