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

View File

@ -67,9 +67,19 @@ class ChessSquare extends StatelessWidget {
color: color,
width: ChessSquare.pieceWidth,
height: ChessSquare.pieceWidth,
child: Draggable<ChessMove>(
child: BlocBuilder<ChessBloc, ChessBoardState>(
builder: (context, state) {
int allowDrags = 0;
if (ChessBloc.myColor == null) {
allowDrags = 0;
} else {
allowDrags = ChessBloc.myColor == state.newTurnColor ? 1 : 0;
}
return Draggable<ChessMove>(
/* 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(
@ -82,6 +92,8 @@ class ChessSquare extends StatelessWidget {
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()}');
}
},
);

View File

@ -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));