code upkeep

This commit is contained in:
Marco 2023-10-12 21:03:12 +02:00
parent 8132e9d48d
commit 51d959b01f
6 changed files with 33 additions and 32 deletions

View File

@ -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

View File

@ -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)