diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 8259099..f7222ce 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -5,7 +5,11 @@ import 'dart:developer'; class ChessBloc extends Bloc { static final ChessBloc _instance = ChessBloc._internal(); - late ChessColor? myColor; + static ChessColor? myColor; + + static ChessColor? getSidesColor() { + return myColor; + } ChessBloc._internal() : super(ChessBoardState.init()) { on(initBoard); @@ -26,6 +30,8 @@ class ChessBloc extends Bloc { } void flipBoard(ColorDetermined event, Emitter emit) { + log("My Color is $myColor"); + myColor = event.myColor; emit(ChessBoardState(event.myColor, state.newTurnColor, state.position)); } diff --git a/lib/chessapp/chess_square.dart b/lib/chessapp/chess_square.dart index 7889856..c1d0308 100644 --- a/lib/chessapp/chess_square.dart +++ b/lib/chessapp/chess_square.dart @@ -67,21 +67,33 @@ class ChessSquare extends StatelessWidget { color: color, width: ChessSquare.pieceWidth, height: ChessSquare.pieceWidth, - child: Draggable( - /* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */ - data: ChessMove(coordinate, coordinate, containedPiece), - feedback: FractionalTranslation( - translation: const Offset(-0.5, -0.75), - child: SizedBox( - height: draggableFdbSize, - width: draggableFdbSize, - child: containedPiece), - ), - childWhenDragging: Container(), - dragAnchorStrategy: pointerDragAnchorStrategy, - child: containedPiece ?? Container(), - onDragCompleted: () {}, - onDragStarted: () {}, + child: BlocBuilder( + builder: (context, state) { + int allowDrags = 0; + if (ChessBloc.myColor == null) { + allowDrags = 0; + } else { + allowDrags = ChessBloc.myColor == state.newTurnColor ? 1 : 0; + } + + return Draggable( + /* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */ + data: ChessMove(coordinate, coordinate, containedPiece), + maxSimultaneousDrags: allowDrags, + feedback: FractionalTranslation( + translation: const Offset(-0.5, -0.75), + child: SizedBox( + height: draggableFdbSize, + width: draggableFdbSize, + child: containedPiece), + ), + childWhenDragging: Container(), + dragAnchorStrategy: pointerDragAnchorStrategy, + child: containedPiece ?? Container(), + onDragCompleted: () {}, + onDragStarted: () {}, + ); + }, ), ); }, @@ -90,7 +102,7 @@ class ChessSquare extends StatelessWidget { if (move.endSquare != move.startSquare) { ServerConnection.getInstance().send( - '${messageIndex++} mv ${move.startSquare.toString()} ${move.endSquare.toString()}'); + 'mv ${move.startSquare.toString()} ${move.endSquare.toString()}'); } }, ); diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 4fa3f07..4ebcb09 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -67,11 +67,11 @@ class ServerConnection { if (splitString[1] == "init") ChessBloc.getInstance().add(InitBoard()); } - if (splitString[1] == ('mv')) { - var startSquare = ChessCoordinate.fromString(splitString[2]); - var endSquare = ChessCoordinate.fromString(splitString[3]); + if (splitString[0] == ('mv')) { + var startSquare = ChessCoordinate.fromString(splitString[1]); + var endSquare = ChessCoordinate.fromString(splitString[2]); - log('Move received : ${splitString[2]}:${splitString[3]}'); + log('Move received : ${splitString[1]}:${splitString[2]}'); ChessBloc.getInstance() .add(PieceMoved(startSquare: startSquare, endSquare: endSquare));