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 (
|
||||
@ -26,7 +27,9 @@ func NewGame() *Game {
|
||||
var game = Game{
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user