code upkeep
This commit is contained in:
parent
8132e9d48d
commit
51d959b01f
@ -21,6 +21,10 @@ const (
|
|||||||
ColorDetermined MessageType = "colorDetermined"
|
ColorDetermined MessageType = "colorDetermined"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (m WebsocketMessage) IsValid() bool {
|
||||||
|
return m.IsValidMoveMessage()
|
||||||
|
}
|
||||||
|
|
||||||
func (m WebsocketMessage) IsValidMoveMessage() bool {
|
func (m WebsocketMessage) IsValidMoveMessage() bool {
|
||||||
if m.Type != MoveMessage {
|
if m.Type != MoveMessage {
|
||||||
return false
|
return false
|
||||||
|
@ -14,6 +14,7 @@ type Game struct {
|
|||||||
board Board
|
board Board
|
||||||
players []*Player
|
players []*Player
|
||||||
currentTurnPlayer *Player
|
currentTurnPlayer *Player
|
||||||
|
gameState int
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -24,9 +25,11 @@ const (
|
|||||||
|
|
||||||
func NewGame() *Game {
|
func NewGame() *Game {
|
||||||
var game = Game{
|
var game = Game{
|
||||||
id: uuid.New(),
|
id: uuid.New(),
|
||||||
board: newBoard(),
|
board: newBoard(),
|
||||||
|
gameState: PlayerToMove,
|
||||||
}
|
}
|
||||||
|
game.currentTurnPlayer = game.GetPlayer1()
|
||||||
game.board.Init()
|
game.board.Init()
|
||||||
|
|
||||||
return &game
|
return &game
|
||||||
@ -44,9 +47,7 @@ func (game Game) GetPlayer2() *Player {
|
|||||||
return game.players[1]
|
return game.players[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) Handle() {
|
func (game *Game) Prepare() {
|
||||||
defer game.killGame()
|
|
||||||
|
|
||||||
ok := game.waitForWebsocketConnections()
|
ok := game.waitForWebsocketConnections()
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
@ -56,38 +57,37 @@ func (game *Game) Handle() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
gameState := PlayerToMove
|
func (game *Game) Handle() {
|
||||||
game.currentTurnPlayer = game.GetPlayer1()
|
defer game.killGame()
|
||||||
var receivedMove types.Move
|
var receivedMove types.Move
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
switch game.gameState {
|
||||||
switch gameState {
|
|
||||||
case PlayerToMove:
|
case PlayerToMove:
|
||||||
receivedMove, err = game.currentTurnPlayer.ReadMove()
|
log.Println("with player ", game.currentTurnPlayer, " to move")
|
||||||
|
receivedMove, err := game.currentTurnPlayer.ReadMove()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error while reading message:", err)
|
log.Println("Error while reading message:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("Player ", game.currentTurnPlayer, " moved:\n", receivedMove)
|
log.Println("Player ", game.currentTurnPlayer, " moved:\n", receivedMove)
|
||||||
|
|
||||||
gameState = CheckMove
|
game.gameState = CheckMove
|
||||||
|
|
||||||
case CheckMove:
|
case CheckMove:
|
||||||
valid, ruleViolation := game.board.CheckAndPlay(receivedMove)
|
valid, ruleViolation := game.board.CheckAndPlay(receivedMove)
|
||||||
|
|
||||||
if valid {
|
if valid {
|
||||||
gameState = CheckPlayerChange
|
game.gameState = CheckPlayerChange
|
||||||
} else {
|
} else {
|
||||||
log.Println(err)
|
|
||||||
invalidMoveMessage, err := api.GetInvalidMoveMessage(receivedMove, ruleViolation.String())
|
invalidMoveMessage, err := api.GetInvalidMoveMessage(receivedMove, ruleViolation.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error marshalling 'colorDetermined' message for player 1", err)
|
log.Println("Error marshalling 'colorDetermined' message for player 1", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
game.currentTurnPlayer.writeMessage(invalidMoveMessage)
|
game.currentTurnPlayer.writeMessage(invalidMoveMessage)
|
||||||
gameState = PlayerToMove
|
game.gameState = PlayerToMove
|
||||||
}
|
}
|
||||||
case CheckPlayerChange:
|
case CheckPlayerChange:
|
||||||
if game.currentTurnPlayer.Uuid == game.players[0].Uuid {
|
if game.currentTurnPlayer.Uuid == game.players[0].Uuid {
|
||||||
@ -96,19 +96,15 @@ func (game *Game) Handle() {
|
|||||||
game.currentTurnPlayer = game.players[0]
|
game.currentTurnPlayer = game.players[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
err = game.broadcastMove(receivedMove)
|
err := game.broadcastMove(receivedMove)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error broadcasting move ", err)
|
log.Println("Error broadcasting move ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gameState = PlayerToMove
|
game.gameState = PlayerToMove
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("GameState = ", gameState)
|
|
||||||
if gameState == PlayerToMove {
|
|
||||||
log.Println("with player ", game.currentTurnPlayer, " to move")
|
|
||||||
}
|
}
|
||||||
|
log.Println("GameState = ", game.gameState)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,8 +122,8 @@ func (p *Pawn) HandleEnPassant(b *Board, move, lastMove types.Move) (bool, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if wasEnPassant { //play the move
|
if wasEnPassant { //play the move
|
||||||
delete(b.position, lastMove.EndSquare) // take opponent's pawn
|
delete(b.position, lastMove.EndSquare) // take opponent's pawn
|
||||||
delete(b.position, move.StartSquare) // move moving pawn
|
delete(b.position, move.StartSquare) // move moving pawn
|
||||||
b.position[move.EndSquare] = GetPieceForShortName(move.PieceMoved)
|
b.position[move.EndSquare] = GetPieceForShortName(move.PieceMoved)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,9 +38,9 @@ func (p *Player) SetConnection(ctx context.Context, conn *websocket.Conn) {
|
|||||||
|
|
||||||
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,
|
||||||
Move: &move,
|
Move: &move,
|
||||||
Position: &boardPosition,
|
Position: &boardPosition,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error while marshalling: ", err)
|
log.Println("Error while marshalling: ", err)
|
||||||
@ -73,7 +73,7 @@ func (p *Player) ReadMove() (types.Move, error) {
|
|||||||
return types.Move{}, err
|
return types.Move{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !msg.IsValidMoveMessage() {
|
if !msg.IsValid() {
|
||||||
return types.Move{}, errors.New("not a valid move")
|
return types.Move{}, errors.New("not a valid move")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ func newEmptyLobbyWithPassphrase() *Lobby {
|
|||||||
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
|
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
|
||||||
l.Game.AddPlayersToGame(player)
|
l.Game.AddPlayersToGame(player)
|
||||||
if l.IsFull() {
|
if l.IsFull() {
|
||||||
|
l.Game.Prepare()
|
||||||
go l.Game.Handle()
|
go l.Game.Handle()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ const (
|
|||||||
BlackQueenShortName PieceShortName = "q"
|
BlackQueenShortName PieceShortName = "q"
|
||||||
BlackKingShortName PieceShortName = "k"
|
BlackKingShortName PieceShortName = "k"
|
||||||
|
|
||||||
WhitePawnShortName PieceShortName = "P"
|
WhitePawnShortName PieceShortName = "P"
|
||||||
WhiteRookShortName PieceShortName = "R"
|
WhiteRookShortName PieceShortName = "R"
|
||||||
WhiteKnightShortName PieceShortName = "N"
|
WhiteKnightShortName PieceShortName = "N"
|
||||||
WhiteBishopShortName PieceShortName = "B"
|
WhiteBishopShortName PieceShortName = "B"
|
||||||
@ -32,6 +32,6 @@ func (p PieceShortName) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p PieceShortName) ToCommon() PieceShortName {
|
func (p PieceShortName) ToCommon() PieceShortName {
|
||||||
commonShortName := strings.ToLower(p.String())
|
commonShortName := strings.ToLower(p.String())
|
||||||
return PieceShortName(commonShortName)
|
return PieceShortName(commonShortName)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user