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