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

@ -8,7 +8,8 @@ import (
type WebsocketMessage struct {
Type MessageType `json:"messageType"`
Move *types.Move `json:"move,omitempty"`
Color *types.ChessColor `json:"color,omitempty"`
TurnColor *types.ChessColor `json:"turnColor,omitempty"`
PlayerColor *types.ChessColor `json:"playerColor,omitempty"`
Reason *string `json:"reason,omitempty"`
Position *string `json:"position,omitempty"`
}
@ -16,6 +17,7 @@ type WebsocketMessage struct {
type MessageType string
const (
PositionMessage MessageType = "position"
MoveMessage MessageType = "move"
InvalidMoveMessage MessageType = "invalidMove"
ColorDetermined MessageType = "colorDetermined"
@ -36,7 +38,7 @@ func (m WebsocketMessage) IsValidMoveMessage() bool {
}
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) {

View File

@ -1,6 +1,7 @@
package chess
import (
"context"
"log"
"math/rand"
"mchess_server/api"
@ -8,6 +9,7 @@ import (
"github.com/google/uuid"
"github.com/samber/lo"
"nhooyr.io/websocket"
)
type Game struct {
@ -178,8 +180,12 @@ func (game Game) broadcastMove(move types.Move) error {
func (game *Game) playerDisconnected(p *Player) {
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
})
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 {
Uuid uuid.UUID
Conn *connection.Connection
InGame bool
color types.ChessColor
disconnectCallback func(p *Player)
}
@ -26,7 +25,6 @@ func NewPlayer(uuid uuid.UUID) *Player {
Uuid: uuid,
Conn: connection.NewConnection(
connection.WithContext(context.Background())),
InGame: false,
}
return player
@ -40,6 +38,16 @@ func (p *Player) SetWebsocketConnection(ctx context.Context, ws *websocket.Conn)
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)) {
// Todo: Fucking complicated
p.Conn.SetDisconnectCallback(p.PlayerDisconnectedCallback)
@ -50,6 +58,31 @@ func (p *Player) PlayerDisconnectedCallback() {
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 {
messageToSend, err := json.Marshal(api.WebsocketMessage{
Type: api.MoveMessage,

View File

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