Transform communication of moves to api types.
This commit is contained in:
parent
78ddd4f90f
commit
e90fb7a0dc
11
api/move.go
Normal file
11
api/move.go
Normal 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
8
api/player_info.go
Normal 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"`
|
||||
}
|
@ -1 +0,0 @@
|
||||
package api_types
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
5
main.go
5
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)
|
||||
|
Loading…
Reference in New Issue
Block a user