1. Remove leading counter from move message,

2. Only allow color to move when it's their turn.
This commit is contained in:
Marco 2022-12-21 23:14:53 +01:00
parent aaaf40bb2b
commit 0b7c3897e5
3 changed files with 39 additions and 21 deletions

View File

@ -5,7 +5,11 @@ import 'dart:developer';
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> { class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
static final ChessBloc _instance = ChessBloc._internal(); static final ChessBloc _instance = ChessBloc._internal();
late ChessColor? myColor; static ChessColor? myColor;
static ChessColor? getSidesColor() {
return myColor;
}
ChessBloc._internal() : super(ChessBoardState.init()) { ChessBloc._internal() : super(ChessBoardState.init()) {
on<InitBoard>(initBoard); on<InitBoard>(initBoard);
@ -26,6 +30,8 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
} }
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) { void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
log("My Color is $myColor");
myColor = event.myColor;
emit(ChessBoardState(event.myColor, state.newTurnColor, state.position)); emit(ChessBoardState(event.myColor, state.newTurnColor, state.position));
} }

View File

@ -67,21 +67,33 @@ class ChessSquare extends StatelessWidget {
color: color, color: color,
width: ChessSquare.pieceWidth, width: ChessSquare.pieceWidth,
height: ChessSquare.pieceWidth, height: ChessSquare.pieceWidth,
child: Draggable<ChessMove>( child: BlocBuilder<ChessBloc, ChessBoardState>(
/* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */ builder: (context, state) {
data: ChessMove(coordinate, coordinate, containedPiece), int allowDrags = 0;
feedback: FractionalTranslation( if (ChessBloc.myColor == null) {
translation: const Offset(-0.5, -0.75), allowDrags = 0;
child: SizedBox( } else {
height: draggableFdbSize, allowDrags = ChessBloc.myColor == state.newTurnColor ? 1 : 0;
width: draggableFdbSize, }
child: containedPiece),
), return Draggable<ChessMove>(
childWhenDragging: Container(), /* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */
dragAnchorStrategy: pointerDragAnchorStrategy, data: ChessMove(coordinate, coordinate, containedPiece),
child: containedPiece ?? Container(), maxSimultaneousDrags: allowDrags,
onDragCompleted: () {}, feedback: FractionalTranslation(
onDragStarted: () {}, 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) { if (move.endSquare != move.startSquare) {
ServerConnection.getInstance().send( ServerConnection.getInstance().send(
'${messageIndex++} mv ${move.startSquare.toString()} ${move.endSquare.toString()}'); 'mv ${move.startSquare.toString()} ${move.endSquare.toString()}');
} }
}, },
); );

View File

@ -67,11 +67,11 @@ class ServerConnection {
if (splitString[1] == "init") ChessBloc.getInstance().add(InitBoard()); if (splitString[1] == "init") ChessBloc.getInstance().add(InitBoard());
} }
if (splitString[1] == ('mv')) { if (splitString[0] == ('mv')) {
var startSquare = ChessCoordinate.fromString(splitString[2]); var startSquare = ChessCoordinate.fromString(splitString[1]);
var endSquare = ChessCoordinate.fromString(splitString[3]); var endSquare = ChessCoordinate.fromString(splitString[2]);
log('Move received : ${splitString[2]}:${splitString[3]}'); log('Move received : ${splitString[1]}:${splitString[2]}');
ChessBloc.getInstance() ChessBloc.getInstance()
.add(PieceMoved(startSquare: startSquare, endSquare: endSquare)); .add(PieceMoved(startSquare: startSquare, endSquare: endSquare));