Transform communication of moves to api types.

This commit is contained in:
Marco 2023-06-02 23:41:43 +02:00
parent 78ddd4f90f
commit e90fb7a0dc
7 changed files with 29 additions and 26 deletions

11
api/move.go Normal file
View File

@ -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"`
}

8
api/player_info.go Normal file
View File

@ -0,0 +1,8 @@
package api
import "github.com/google/uuid"
type PlayerInfo struct {
PlayerID uuid.UUID `json:"playerID"`
LobbyID uuid.UUID `json:"lobbyID"`
}

View File

@ -1 +0,0 @@
package api_types

View File

@ -1,15 +1 @@
package chess 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
}

View File

@ -1,6 +1,8 @@
package chess package chess
import ( import (
"encoding/json"
"local/m/mchess_server/api"
"log" "log"
"github.com/google/uuid" "github.com/google/uuid"
@ -44,14 +46,15 @@ func (game *Game) Handle() {
gameState := PlayerToMove gameState := PlayerToMove
game.currentTurnPlayer = game.getPlayer1() game.currentTurnPlayer = game.getPlayer1()
var move *Move var move api.Move
var receivedMessage []byte var receivedMessage []byte
var err error
for { for {
switch gameState { switch gameState {
case PlayerToMove: case PlayerToMove:
var err error
_, receivedMessage, err = game.currentTurnPlayer.ReadMessageFromPlayer() _, receivedMessage, err = game.currentTurnPlayer.ReadMessageFromPlayer()
if err != nil { if err != nil {
log.Println("Error while reading message:", err) log.Println("Error while reading message:", err)
@ -59,8 +62,8 @@ func (game *Game) Handle() {
// This means, the game just ends uncontrolled // This means, the game just ends uncontrolled
return return
} }
move, err = parseMove(string(receivedMessage)) err = json.Unmarshal(receivedMessage, &move)
log.Println("Player ", game.currentTurnPlayer, " moved: ", move) log.Println("Player ", game.currentTurnPlayer, " moved:\n", move)
if err != nil { if err != nil {
log.Println("Game: ", game.id, err) log.Println("Game: ", game.id, err)

View File

@ -16,11 +16,6 @@ type Player struct {
context context.Context context context.Context
} }
type PlayerInfo struct {
PlayerID uuid.UUID `json:"playerID"`
LobbyID uuid.UUID `json:"lobbyID"`
}
func NewPlayer(uuid uuid.UUID) *Player { func NewPlayer(uuid uuid.UUID) *Player {
return &Player{ return &Player{
Uuid: uuid, Uuid: uuid,

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"local/m/mchess_server/api"
"local/m/mchess_server/chess" "local/m/mchess_server/chess"
lobbies "local/m/mchess_server/lobby_registry" lobbies "local/m/mchess_server/lobby_registry"
"local/m/mchess_server/usher" "local/m/mchess_server/usher"
@ -52,7 +53,7 @@ func registerForRandomGame(c *gin.Context) {
usher.AddPlayerToLobbyAndStartGameIfFull(player, lobby) usher.AddPlayerToLobbyAndStartGameIfFull(player, lobby)
mut.Unlock() mut.Unlock()
info := chess.PlayerInfo{ info := api.PlayerInfo{
PlayerID: player.Uuid, PlayerID: player.Uuid,
LobbyID: lobby.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) log.Println("read from websocket: ", msgType, string(msg), err)
var info chess.PlayerInfo var info api.PlayerInfo
err = json.Unmarshal(msg, &info) err = json.Unmarshal(msg, &info)
if err != nil { if err != nil {
errorMessage := fmt.Sprintf("Unmarshaling message did not work: %s", err) errorMessage := fmt.Sprintf("Unmarshaling message did not work: %s", err)