allow-reconnect #1
@ -8,7 +8,8 @@ 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"`
|
||||||
|
PlayerColor *types.ChessColor `json:"playerColor,omitempty"`
|
||||||
Reason *string `json:"reason,omitempty"`
|
Reason *string `json:"reason,omitempty"`
|
||||||
Position *string `json:"position,omitempty"`
|
Position *string `json:"position,omitempty"`
|
||||||
}
|
}
|
||||||
@ -16,6 +17,7 @@ type WebsocketMessage struct {
|
|||||||
type MessageType string
|
type MessageType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
BoardStateMessage MessageType = "boardState"
|
||||||
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, PlayerColor: &color})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetInvalidMoveMessage(move types.Move, reason string) ([]byte, error) {
|
func GetInvalidMoveMessage(move types.Move, reason string) ([]byte, error) {
|
||||||
|
@ -12,7 +12,6 @@ func (b *Board) GetNonBlockedRowAndColumn(fromSquare types.Coordinate) []types.C
|
|||||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresRight, fromSquare)...)
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresRight, fromSquare)...)
|
||||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresAbove, fromSquare)...)
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresAbove, fromSquare)...)
|
||||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresBelow, fromSquare)...)
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresBelow, fromSquare)...)
|
||||||
|
|
||||||
return nonBlocked
|
return nonBlocked
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
package chess
|
package chess
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"mchess_server/api"
|
"mchess_server/api"
|
||||||
"mchess_server/types"
|
"mchess_server/types"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/samber/lo"
|
||||||
|
"nhooyr.io/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
@ -15,10 +18,13 @@ type Game struct {
|
|||||||
players []*Player
|
players []*Player
|
||||||
currentTurnPlayer *Player
|
currentTurnPlayer *Player
|
||||||
gameState int
|
gameState int
|
||||||
|
isBeingHandled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PlayerToMove = iota
|
Init = iota
|
||||||
|
Prepare
|
||||||
|
PlayerToMove
|
||||||
CheckMove
|
CheckMove
|
||||||
CheckPlayerChange
|
CheckPlayerChange
|
||||||
)
|
)
|
||||||
@ -27,7 +33,7 @@ func NewGame() *Game {
|
|||||||
var game = Game{
|