introduce some simple methods to set/get a player's color
This commit is contained in:
parent
0f71743598
commit
e4ad872849
@ -58,8 +58,8 @@ func (game Game) GetPlayer2() *Player {
|
||||
}
|
||||
|
||||
func (game *Game) prepare() {
|
||||
game.players[0].color = types.White
|
||||
game.players[1].color = types.Black
|
||||
game.players[0].SetColor(types.White)
|
||||
game.players[1].SetColor(types.Black)
|
||||
|
||||
game.currentTurnPlayer = game.GetPlayer1()
|
||||
|
||||
@ -99,7 +99,7 @@ func (game *Game) Handle() {
|
||||
game.gameState = PlayerToMove
|
||||
|
||||
case PlayerToMove:
|
||||
log.Println("with ", game.currentTurnPlayer.GetPlayerColor(), " to move")
|
||||
log.Println("with ", game.currentTurnPlayer.GetColor(), " to move")
|
||||
receivedMove, err = game.currentTurnPlayer.ReadMove()
|
||||
if err != nil {
|
||||
log.Println("Error while reading message:", err)
|
||||
@ -182,12 +182,12 @@ func (game Game) notifyPlayersAboutGameStart() 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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -48,6 +48,14 @@ func (p *Player) SetWebsocketConnectionAndSendBoardState(
|
||||
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)) {
|
||||
// Todo: Fucking complicated
|
||||
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 {
|
||||
var pColor = p.color
|
||||
if p.color == "" { // we default to white if we do not know the color yet
|
||||
var pColor = p.GetColor()
|
||||
if p.GetColor() == "" { // we default to white if we do not know the color yet
|
||||
pColor = types.White
|
||||
}
|
||||
|
||||
@ -154,7 +162,3 @@ func (p *Player) readMessage() ([]byte, error) {
|
||||
|
||||
return msg, err
|
||||
}
|
||||
|
||||
func (p Player) GetPlayerColor() string {
|
||||
return string(p.color)
|
||||
}
|
||||
|
@ -16,6 +16,10 @@ func (c ChessColor) Opposite() ChessColor {
|
||||
}
|
||||
}
|
||||
|
||||
func (c ChessColor) String() string {
|
||||
return string(c)
|
||||
}
|
||||
|
||||
type AdditionalState struct {
|
||||
BlackKingMoved bool
|
||||
WhiteKingMoved bool
|
||||
|
Loading…
Reference in New Issue
Block a user