Fix the coloring of the last move after an invalid move was played.

This commit is contained in:
Marco 2024-01-09 19:07:36 +01:00
parent ff34e052dc
commit 582fa87224
4 changed files with 24 additions and 44 deletions

View File

@ -25,7 +25,7 @@ class ChessBoard extends StatelessWidget {
bool squareWasPartOfLastMove = false; bool squareWasPartOfLastMove = false;
if ((boardState.lastMove.to == ChessCoordinate(column, row) || if ((boardState.lastMove.to == ChessCoordinate(column, row) ||
boardState.lastMove.from == ChessCoordinate(column, row)) && boardState.lastMove.from == ChessCoordinate(column, row)) &&
boardState.positionAckdByServer) { boardState.colorLastMove) {
squareWasPartOfLastMove = true; squareWasPartOfLastMove = true;
} }

View File

@ -24,7 +24,6 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
on<OwnPieceMoved>(ownMoveHandler); on<OwnPieceMoved>(ownMoveHandler);
on<OwnPromotionPlayed>(ownPromotionHandler); on<OwnPromotionPlayed>(ownPromotionHandler);
on<InvalidMovePlayed>(invalidMoveHandler); on<InvalidMovePlayed>(invalidMoveHandler);
on<BoardStatusReceived>(boardStatusHandler);
} }
factory ChessBloc.getInstance() { factory ChessBloc.getInstance() {
@ -112,8 +111,14 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
tempPosition[move.from] = const ChessPiece.none(); tempPosition[move.from] = const ChessPiece.none();
emit( emit(
ChessBoardState(state.bottomColor, turnColor, tempPosition, move, false, ChessBoardState(
ChessCoordinate.none()), state.bottomColor,
turnColor,
tempPosition,
ChessPositionManager.getInstance().lastMove,
true,
ChessCoordinate.none(),
),
); );
} }
@ -138,31 +143,18 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
void invalidMoveHandler( void invalidMoveHandler(
InvalidMovePlayed event, Emitter<ChessBoardState> emit) { InvalidMovePlayed event, Emitter<ChessBoardState> emit) {
var move = ChessPositionManager.getInstance().lastMove;
emit( emit(
ChessBoardState( ChessBoardState(
state.bottomColor, state.bottomColor,
turnColor, turnColor,
ChessPositionManager.getInstance().currentPosition, ChessPositionManager.getInstance().currentPosition,
ChessMove.none(), move,
false, true,
event.squareInCheck, event.squareInCheck,
), ),
); );
} }
void boardStatusHandler(
BoardStatusReceived event, Emitter<ChessBoardState> emit) {
emit(
ChessBoardState(
event.myColor,
event.whoseTurn,
event.pos,
ChessMove.none(),
false,
ChessCoordinate.none(),
),
);
}
} }
class ChessBoardState { class ChessBoardState {
@ -170,7 +162,7 @@ class ChessBoardState {
final ChessColor newTurnColor; final ChessColor newTurnColor;
final ChessPosition position; final ChessPosition position;
final ChessMove lastMove; final ChessMove lastMove;
final bool positionAckdByServer; final bool colorLastMove;
final ChessCoordinate squareInCheck; final ChessCoordinate squareInCheck;
ChessBoardState._( ChessBoardState._(
@ -178,7 +170,7 @@ class ChessBoardState {
this.newTurnColor, this.newTurnColor,
this.position, this.position,
this.lastMove, this.lastMove,
this.positionAckdByServer, this.colorLastMove,
this.squareInCheck, this.squareInCheck,
); );

View File

@ -52,15 +52,3 @@ class InvalidMovePlayed extends ChessEvent {
required this.squareInCheck, required this.squareInCheck,
}); });
} }
class BoardStatusReceived extends ChessEvent {
final ChessPosition pos;
final ChessColor myColor;
final ChessColor whoseTurn;
BoardStatusReceived({
required this.pos,
required this.myColor,
required this.whoseTurn,
});
}

View File

@ -236,10 +236,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: path_provider_platform_interface name: path_provider_platform_interface
sha256: "94b1e0dd80970c1ce43d5d4e050a9918fce4f4a775e6142424c30a29a363265c" sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.1" version: "2.1.2"
path_provider_windows: path_provider_windows:
dependency: transitive dependency: transitive
description: description:
@ -324,10 +324,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: shared_preferences_platform_interface name: shared_preferences_platform_interface
sha256: d4ec5fc9ebb2f2e056c617112aa75dcf92fc2e4faaf2ae999caa297473f75d8a sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.3.1" version: "2.3.2"
shared_preferences_web: shared_preferences_web:
dependency: transitive dependency: transitive
description: description:
@ -433,26 +433,26 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: vector_graphics name: vector_graphics
sha256: "0f0c746dd2d6254a0057218ff980fc7f5670fd0fcf5e4db38a490d31eed4ad43" sha256: "18f6690295af52d081f6808f2f7c69f0eed6d7e23a71539d75f4aeb8f0062172"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.1.9+1" version: "1.1.9+2"
vector_graphics_codec: vector_graphics_codec:
dependency: transitive dependency: transitive
description: description:
name: vector_graphics_codec name: vector_graphics_codec
sha256: "0edf6d630d1bfd5589114138ed8fada3234deacc37966bec033d3047c29248b7" sha256: "531d20465c10dfac7f5cd90b60bbe4dd9921f1ec4ca54c83ebb176dbacb7bb2d"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.1.9+1" version: "1.1.9+2"
vector_graphics_compiler: vector_graphics_compiler:
dependency: transitive dependency: transitive
description: description:
name: vector_graphics_compiler name: vector_graphics_compiler
sha256: d24333727332d9bd20990f1483af4e09abdb9b1fc7c3db940b56ab5c42790c26 sha256: "03012b0a33775c5530576b70240308080e1d5050f0faf000118c20e6463bc0ad"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.1.9+1" version: "1.1.9+2"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description: