Introduce method to send status of board and player

This commit is contained in:
Marco 2023-12-09 14:45:57 +01:00
parent cce0aa8162
commit 45fac78e06
4 changed files with 53 additions and 11 deletions

View File

@ -6,16 +6,18 @@ import (
) )
type WebsocketMessage struct { type WebsocketMessage struct {
Type MessageType `json:"messageType"` Type MessageType `json:"messageType"`
Move *types.Move `json:"move,omitempty"` Move *types.Move `json:"move,omitempty"`
Color *types.ChessColor `json:"color,omitempty"` TurnColor *types.ChessColor `json:"turnColor,omitempty"`
Reason *string `json:"reason,omitempty"` PlayerColor *types.ChessColor `json:"playerColor,omitempty"`
Position *string `json:"position,omitempty"` Reason *string `json:"reason,omitempty"`
Position *string `json:"position,omitempty"`
} }
type MessageType string type MessageType string
const ( const (
PositionMessage MessageType = "position"
MoveMessage MessageType = "move" MoveMessage MessageType = "move"
InvalidMoveMessage MessageType = "invalidMove" InvalidMoveMessage MessageType = "invalidMove"
ColorDetermined MessageType = "colorDetermined" ColorDetermined MessageType = "colorDetermined"
@ -36,7 +38,7 @@ func (m WebsocketMessage) IsValidMoveMessage() bool {
} }
func GetColorDeterminedMessage(color types.ChessColor) ([]byte, error) { func GetColorDeterminedMessage(color types.ChessColor) ([]byte, error) {
return json.Marshal(WebsocketMessage{Type: ColorDetermined, Color: &color}) return json.Marshal(WebsocketMessage{Type: ColorDetermined, TurnColor: &color})
} }
func GetInvalidMoveMessage(move types.Move, reason string) ([]byte, error) { func GetInvalidMoveMessage(move types.Move, reason string) ([]byte, error) {

View File

@ -1,6 +1,7 @@
package chess package chess
import ( import (
"context"
"log" "log"
"math/rand" "math/rand"
"mchess_server/api" "mchess_server/api"
@ -8,6 +9,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/samber/lo" "github.com/samber/lo"
"nhooyr.io/websocket"
) )
type Game struct { type Game struct {
@ -178,8 +180,12 @@ func (game Game) broadcastMove(move types.Move) error {
func (game *Game) playerDisconnected(p *Player) { func (game *Game) playerDisconnected(p *Player) {
log.Println(string(p.color), " disconnected") log.Println(string(p.color), " disconnected")
playerLeft := lo.Filter(game.players, func(player *Player, _ int) bool { playerStillInGame := lo.Filter(game.players, func(player *Player, _ int) bool {
return player.color != p.color return player.color != p.color
}) })
game.players = playerLeft game.players = playerStillInGame
}
func (game *Game) SetWebsocketConnectionFor(ctx context.Context, p *Player, ws *websocket.Conn) {
p.SetWebsocketConnectionAndSendBoardState(ctx, ws, game.board.PGN(), game.board.colorToMove)
} }

View File

@ -16,7 +16,6 @@ import (
type Player struct { type Player struct {
Uuid uuid.UUID Uuid uuid.UUID
Conn *connection.Connection Conn *connection.Connection
InGame bool
color types.ChessColor color types.ChessColor
disconnectCallback func(p *Player) disconnectCallback func(p *Player)
} }
@ -26,7 +25,6 @@ func NewPlayer(uuid uuid.UUID) *Player {
Uuid: uuid, Uuid: uuid,
Conn: connection.NewConnection( Conn: connection.NewConnection(
connection.WithContext(context.Background())), connection.WithContext(context.Background())),
InGame: false,
} }
return player return player
@ -40,6 +38,16 @@ func (p *Player) SetWebsocketConnection(ctx context.Context, ws *websocket.Conn)
p.Conn.SetWebsocketConnection(ws) p.Conn.SetWebsocketConnection(ws)
} }
func (p *Player) SetWebsocketConnectionAndSendBoardState(
ctx context.Context,
ws *websocket.Conn,
boardPosition string,
turnColor types.ChessColor,
) {
p.SetWebsocketConnection(ctx, ws)
p.SendPosition(boardPosition, turnColor)
}
func (p *Player) SetDisconnectCallback(cb func(*Player)) { func (p *Player) SetDisconnectCallback(cb func(*Player)) {
// Todo: Fucking complicated // Todo: Fucking complicated
p.Conn.SetDisconnectCallback(p.PlayerDisconnectedCallback) p.Conn.SetDisconnectCallback(p.PlayerDisconnectedCallback)
@ -50,6 +58,31 @@ func (p *Player) PlayerDisconnectedCallback() {
p.disconnectCallback(p) p.disconnectCallback(p)
} }
func (p *Player) SendPosition(boardPosition string, turnColor types.ChessColor) error {
var pColor = p.color
if p.color == "" { // we default to white if we do not know the color yet
pColor = types.White
}
messageToSend, err := json.Marshal(api.WebsocketMessage{
Type: api.PositionMessage,
TurnColor: &turnColor,
PlayerColor: &pColor,
Position: &boardPosition,
})
if err != nil {
log.Println("Error while marshalling: ", err)
return err
}
err = p.writeMessage(messageToSend)
if err != nil {
log.Println("Error during message writing:", err)
return err
}
return nil
}
func (p *Player) SendMoveAndPosition(move types.Move, boardPosition string) error { func (p *Player) SendMoveAndPosition(move types.Move, boardPosition string) error {
messageToSend, err := json.Marshal(api.WebsocketMessage{ messageToSend, err := json.Marshal(api.WebsocketMessage{
Type: api.MoveMessage, Type: api.MoveMessage,

View File

@ -151,6 +151,7 @@ func waitForAndHandlePlayerID(ctx context.Context, conn *websocket.Conn) {
lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(*info.LobbyID) lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(*info.LobbyID)
if lobby == nil { if lobby == nil {
conn.Close(websocket.StatusCode(400), "lobby not found") conn.Close(websocket.StatusCode(400), "lobby not found")
return
} }
player, found := lobby.GetPlayerByUUID(*info.PlayerID) player, found := lobby.GetPlayerByUUID(*info.PlayerID)
@ -161,6 +162,6 @@ func waitForAndHandlePlayerID(ctx context.Context, conn *websocket.Conn) {
if player.Conn.HasWebsocketConnection() { if player.Conn.HasWebsocketConnection() {
player.Conn.Close("closing existing connection") player.Conn.Close("closing existing connection")
} }
player.SetWebsocketConnection(ctx, conn) lobby.Game.SetWebsocketConnectionFor(ctx, player, conn)
log.Println("player after setting connection: ", player) log.Println("player after setting connection: ", player)
} }