introduce some simple methods to set/get a player's color

This commit is contained in:
Marco 2024-05-11 22:38:42 +02:00
parent 0f71743598
commit e4ad872849
3 changed files with 19 additions and 11 deletions

View File

@ -58,8 +58,8 @@ func (game Game) GetPlayer2() *Player {
} }
func (game *Game) prepare() { func (game *Game) prepare() {
game.players[0].color = types.White game.players[0].SetColor(types.White)
game.players[1].color = types.Black game.players[1].SetColor(types.Black)
game.currentTurnPlayer = game.GetPlayer1() game.currentTurnPlayer = game.GetPlayer1()
@ -99,7 +99,7 @@ func (game *Game) Handle() {
game.gameState = PlayerToMove game.gameState = PlayerToMove
case PlayerToMove: case PlayerToMove:
log.Println("with ", game.currentTurnPlayer.GetPlayerColor(), " to move") log.Println("with ", game.currentTurnPlayer.GetColor(), " to move")
receivedMove, err = game.currentTurnPlayer.ReadMove() 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)
@ -182,12 +182,12 @@ func (game Game) notifyPlayersAboutGameStart() error {
} }
func (game Game) broadcastMove(move types.Move) error { func (game Game) broadcastMove(move types.Move) error {
err := game.GetPlayer1().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.color) err := game.GetPlayer1().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.GetColor())
if err != nil { if err != nil {
return err return err
} }
err = game.GetPlayer2().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.color) err = game.GetPlayer2().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.GetColor())
if err != nil { if err != nil {
return err return err
} }

View File

@ -48,6 +48,14 @@ func (p *Player) SetWebsocketConnectionAndSendBoardState(
p.SendBoardState(types.Move{}, boardPosition, turnColor) p.SendBoardState(types.Move{}, boardPosition, turnColor)
} }
func (p *Player) SetColor(color types.ChessColor) {
p.color = color
}
func (p *Player) GetColor() types.ChessColor {
return p.color
}
func (p *Player) SetDisconnectCallback(cb func(*Player)) { func (p *Player) SetDisconnectCallback(cb func(*Player)) {
// Todo: Fucking complicated // Todo: Fucking complicated
p.Conn.SetDisconnectCallback(p.PlayerDisconnectedCallback) p.Conn.SetDisconnectCallback(p.PlayerDisconnectedCallback)
@ -63,8 +71,8 @@ func (p *Player) IsInGame() bool {
} }
func (p *Player) SendBoardState(move types.Move, boardPosition string, turnColor types.ChessColor) error { func (p *Player) SendBoardState(move types.Move, boardPosition string, turnColor types.ChessColor) error {
var pColor = p.color var pColor = p.GetColor()
if p.color == "" { // we default to white if we do not know the color yet if p.GetColor() == "" { // we default to white if we do not know the color yet
pColor = types.White pColor = types.White
} }
@ -154,7 +162,3 @@ func (p *Player) readMessage() ([]byte, error) {
return msg, err return msg, err
} }
func (p Player) GetPlayerColor() string {
return string(p.color)
}

View File

@ -16,6 +16,10 @@ func (c ChessColor) Opposite() ChessColor {
} }
} }
func (c ChessColor) String() string {
return string(c)
}
type AdditionalState struct { type AdditionalState struct {
BlackKingMoved bool BlackKingMoved bool
WhiteKingMoved bool WhiteKingMoved bool