mchess-server/chess/free_squares.go
Marco ae3e73f711 Another step towards allowing reconnections
1. Implement thread-safe ringbuffer for websocket messages

This implements a ringbuffer that is used to decouple the raw websocket
connection from the messages that the game handler handles.

2. Change websocket handling

With this commit, we stop waiting for the websocket connection to be
established before the game starts.
Now, the Connection type is responsible for waiting for the websocket
connection before writing.

Some bugs are still happening:
	1. The rejoining client is not told the state of the board
	2. Invalid moves are not handled by the client (not sure why though)
	3. The still-connected client should be told, that the opponent
	   disconnected. Then the client should show the passphrase again

3. Introduce method to send status of board and player

4. Reconnect works (kind of)

With the right changes in the client, the reconnect works (but only for
the first time).

WARNING:
At the moment, we will create a new player whenever connection wants to
join a private game. This will also clear all the disconnect callbacks
that we set in the player.
2023-12-09 21:00:52 +01:00

83 lines
3.2 KiB
Go

package chess
import "mchess_server/types"
func (b *Board) GetNonBlockedRowAndColumn(fromSquare types.Coordinate) []types.Coordinate {
squaresLeft := fromSquare.GetStraightInDirection(types.Coordinate.Left)
squaresRight := fromSquare.GetStraightInDirection(types.Coordinate.Right)
squaresAbove := fromSquare.GetStraightInDirection(types.Coordinate.Up)
squaresBelow := fromSquare.GetStraightInDirection(types.Coordinate.Down)
nonBlocked := b.getNonBlockedConsecutive(squaresLeft, fromSquare)
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresRight, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresAbove, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresBelow, fromSquare)...)
return nonBlocked
}
func (b *Board) GetNonBlockedDiagonals(fromSquare types.Coordinate) []types.Coordinate {
rightUp := fromSquare.GetDiagonalInDirection(types.Coordinate.Right, types.Coordinate.Up)
leftUp := fromSquare.GetDiagonalInDirection(types.Coordinate.Left, types.Coordinate.Up)
leftDown := fromSquare.GetDiagonalInDirection(types.Coordinate.Left, types.Coordinate.Down)
rightDown := fromSquare.GetDiagonalInDirection(types.Coordinate.Right, types.Coordinate.Down)
nonBlocked := b.getNonBlockedConsecutive(rightUp, fromSquare)
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(leftUp, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(leftDown, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(rightDown, fromSquare)...)
return nonBlocked
}
func (b *Board) GetNonBlockedKingMoves(fromSquare types.Coordinate) []types.Coordinate {
return b.getNonBlockedRaw(fromSquare.GetAllKingMoves(), fromSquare)
}
func (b *Board) GetNonBlockedKnightMoves(fromSquare types.Coordinate) []types.Coordinate {
return b.getNonBlockedRaw(fromSquare.GetAllKnightMoves(), fromSquare)
}
// returns only those squares in a straight/diagonal that are not blocked
// until it hits a blocking piece (excluding same color, including diff color)
func (b *Board) getNonBlockedConsecutive(
squaresToCheck []types.Coordinate,
sourceSquare types.Coordinate,
) []types.Coordinate {
pieceOnSourceSquare := b.getPieceAt(sourceSquare)
nonBlocked := []types.Coordinate{}
for _, square := range squaresToCheck {
piece := b.getPieceAt(square)
if piece != nil {
if piece.GetColor() == pieceOnSourceSquare.GetColor() {
//We do not append squares with same-colored pieces
//and also not the squares behind it
break
}
// if there is an opposite colored piece we append it but
// stop appending the squares behind it
nonBlocked = append(nonBlocked, square)
break
}
nonBlocked = append(nonBlocked, square)
}
return nonBlocked
}
// returns any square in squaresToCheck that are not blocked
func (b *Board) getNonBlockedRaw(
squaresToCheck []types.Coordinate,
sourceSquare types.Coordinate,
) []types.Coordinate {
pieceOnSourceSquare := b.getPieceAt(sourceSquare)
nonBlocked := []types.Coordinate{}
for _, square := range squaresToCheck {
piece := b.getPieceAt(square)
if piece == nil || piece.GetColor() != pieceOnSourceSquare.GetColor() {
nonBlocked = append(nonBlocked, square)
}
}
return nonBlocked
}