From e90fb7a0dc28a9a441de6ff66651e1f74be31041 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 2 Jun 2023 23:41:43 +0200 Subject: [PATCH] Transform communication of moves to api types. --- api/move.go | 11 +++++++++++ api/player_info.go | 8 ++++++++ api_types/player_info.go | 1 - chess/chess_move.go | 14 -------------- chess/game.go | 11 +++++++---- chess/player.go | 5 ----- main.go | 5 +++-- 7 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 api/move.go create mode 100644 api/player_info.go delete mode 100644 api_types/player_info.go diff --git a/api/move.go b/api/move.go new file mode 100644 index 0000000..e5ce602 --- /dev/null +++ b/api/move.go @@ -0,0 +1,11 @@ +package api + +type Coordinate struct { + Col int `json:"col"` + Row int `json:"row"` +} + +type Move struct { + StartSquare Coordinate `json:"startSquare"` + EndSquare Coordinate `json:"endSquare"` +} diff --git a/api/player_info.go b/api/player_info.go new file mode 100644 index 0000000..1a711ef --- /dev/null +++ b/api/player_info.go @@ -0,0 +1,8 @@ +package api + +import "github.com/google/uuid" + +type PlayerInfo struct { + PlayerID uuid.UUID `json:"playerID"` + LobbyID uuid.UUID `json:"lobbyID"` +} diff --git a/api_types/player_info.go b/api_types/player_info.go deleted file mode 100644 index 3fd1829..0000000 --- a/api_types/player_info.go +++ /dev/null @@ -1 +0,0 @@ -package api_types diff --git a/chess/chess_move.go b/chess/chess_move.go index 837d99c..e909c84 100644 --- a/chess/chess_move.go +++ b/chess/chess_move.go @@ -1,15 +1 @@ package chess - -type Move struct { - StartSquare Coordinate `json:"startSquare"` - EndSquare Coordinate `json:"endSquare"` -} - -type Coordinate struct { - Col int `json:"col"` - Row int `json:"row"` -} - -func parseMove(received string) (*Move, error) { - return &Move{}, nil -} diff --git a/chess/game.go b/chess/game.go index 1dc1146..e95e49a 100644 --- a/chess/game.go +++ b/chess/game.go @@ -1,6 +1,8 @@ package chess import ( + "encoding/json" + "local/m/mchess_server/api" "log" "github.com/google/uuid" @@ -44,14 +46,15 @@ func (game *Game) Handle() { gameState := PlayerToMove game.currentTurnPlayer = game.getPlayer1() - var move *Move + var move api.Move var receivedMessage []byte + var err error for { switch gameState { case PlayerToMove: - var err error + _, receivedMessage, err = game.currentTurnPlayer.ReadMessageFromPlayer() if err != nil { log.Println("Error while reading message:", err) @@ -59,8 +62,8 @@ func (game *Game) Handle() { // This means, the game just ends uncontrolled return } - move, err = parseMove(string(receivedMessage)) - log.Println("Player ", game.currentTurnPlayer, " moved: ", move) + err = json.Unmarshal(receivedMessage, &move) + log.Println("Player ", game.currentTurnPlayer, " moved:\n", move) if err != nil { log.Println("Game: ", game.id, err) diff --git a/chess/player.go b/chess/player.go index a271e8e..d54afc0 100644 --- a/chess/player.go +++ b/chess/player.go @@ -16,11 +16,6 @@ type Player struct { context context.Context } -type PlayerInfo struct { - PlayerID uuid.UUID `json:"playerID"` - LobbyID uuid.UUID `json:"lobbyID"` -} - func NewPlayer(uuid uuid.UUID) *Player { return &Player{ Uuid: uuid, diff --git a/main.go b/main.go index b61bba6..9e13e72 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "local/m/mchess_server/api" "local/m/mchess_server/chess" lobbies "local/m/mchess_server/lobby_registry" "local/m/mchess_server/usher" @@ -52,7 +53,7 @@ func registerForRandomGame(c *gin.Context) { usher.AddPlayerToLobbyAndStartGameIfFull(player, lobby) mut.Unlock() - info := chess.PlayerInfo{ + info := api.PlayerInfo{ PlayerID: player.Uuid, LobbyID: lobby.Uuid, } @@ -82,7 +83,7 @@ func waitForAndHandlePlayerID(ctx context.Context, conn websocket.Conn) { log.Println("read from websocket: ", msgType, string(msg), err) - var info chess.PlayerInfo + var info api.PlayerInfo err = json.Unmarshal(msg, &info) if err != nil { errorMessage := fmt.Sprintf("Unmarshaling message did not work: %s", err)