Make promotions work.

This commit is contained in:
Marco 2023-07-03 19:41:12 +02:00
parent 0f27fc6b4e
commit a5befed62c
16 changed files with 292 additions and 138 deletions

View File

@ -1,21 +1,30 @@
class ApiMove {
final ApiCoordinate startSquare;
final ApiCoordinate endSquare;
String? promotionToPiece;
const ApiMove({
ApiMove({
required this.startSquare,
required this.endSquare,
this.promotionToPiece,
});
factory ApiMove.fromJson(Map<String, dynamic> json) {
final startSquare = ApiCoordinate.fromJson(json['startSquare']);
final endSquare = ApiCoordinate.fromJson(json['endSquare']);
final promotionToPiece = json['promotionToPiece'];
return ApiMove(startSquare: startSquare, endSquare: endSquare);
return ApiMove(
startSquare: startSquare,
endSquare: endSquare,
promotionToPiece: promotionToPiece);
}
Map<String, dynamic> toJson() =>
{'startSquare': startSquare, 'endSquare': endSquare};
Map<String, dynamic> toJson() => {
'startSquare': startSquare,
'endSquare': endSquare,
'promotionToPiece': promotionToPiece
};
}
class ApiCoordinate {

View File

@ -58,6 +58,9 @@ class ApiWebsocketMessage {
return ret;
}
Map<String, dynamic> toJson() =>
{'messageType': type, 'move': move, 'color': color};
Map<String, dynamic> toJson() => {
'messageType': type,
'move': move,
'color': color,
};
}

View File

@ -19,7 +19,7 @@ class ChessApp extends StatelessWidget {
create: (context) => ChessBloc.getInstance(),
),
BlocProvider(
create: (context) => PromotionUiBloc.getInstance(),
create: (context) => PromotionBloc.getInstance(),
)
],
child: MaterialApp.router(

View File

@ -66,18 +66,20 @@ class ChessSquare extends StatelessWidget {
}
return true;
},
onAccept: (move) {
onAccept: (pieceDragged) {
// Replace the dummy value with the actual target of the move.
move.toSquare = coordinate;
pieceDragged.toSquare = coordinate;
if (isPromotionMove(move)) {
PromotionUiBloc.getInstance().add(PawnMovedToPromotionField(
endSquare: move.toSquare, colorMoved: ChessBloc.myColor!));
} else if (coordinate != move.fromSquare) {
if (isPromotionMove(pieceDragged)) {
var move = ChessMove(
from: pieceDragged.fromSquare, to: pieceDragged.toSquare);
PromotionBloc.getInstance().add(PawnMovedToPromotionField(
move: move, colorMoved: ChessBloc.myColor!));
} else if (coordinate != pieceDragged.fromSquare) {
ChessBloc.getInstance().add(OwnPieceMoved(
startSquare: move.fromSquare,
endSquare: move.toSquare,
piece: containedPiece!));
startSquare: pieceDragged.fromSquare,
endSquare: pieceDragged.toSquare,
piece: pieceDragged.movedPiece!));
}
},
builder: (context, candidateData, rejectedData) {

View File

@ -1,7 +1,6 @@
import 'dart:convert';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mchess/api/move.dart';
import 'package:mchess/api/websocket_message.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
import 'package:mchess/chess_bloc/chess_position.dart';
@ -22,7 +21,9 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
on<InitBoard>(initBoard);
on<ColorDetermined>(flipBoard);
on<ReceivedMove>(moveHandler);
on<ReceivedPromotion>(promotionHandler);
on<OwnPieceMoved>(ownMoveHandler);
on<OwnPromotionPlayed>(ownPromotionHandler);
on<InvalidMovePlayed>(invalidMoveHandler);
}
@ -64,14 +65,40 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
);
}
void promotionHandler(
ReceivedPromotion event, Emitter<ChessBoardState> emit) {
var pieceAtStartSquare = ChessPosition.getInstance().getPieceAt(
ChessCoordinate(event.startSquare.column, event.startSquare.row));
if (pieceAtStartSquare == null) {
log('received a promotion but piece on start square was empty');
return;
}
ChessPieceClass pieceClass = ChessPieceClass.none;
for (var piece in chessPiecesShortName.entries) {
if (piece.value.toLowerCase() == event.piece) {
pieceClass = piece.key.pieceClass;
break;
}
}
var newPosition = ChessPosition.getInstance().currentPosition;
newPosition[
ChessCoordinate(event.startSquare.column, event.startSquare.row)] =
const ChessPiece.none();
newPosition[ChessCoordinate(event.endSquare.column, event.endSquare.row)] =
ChessPiece(pieceClass, pieceAtStartSquare.color);
emit(ChessBoardState(
state.bottomColor,
turnColor,
newPosition,
));
}
void ownMoveHandler(OwnPieceMoved event, Emitter<ChessBoardState> emit) {
log('ownMoveHandler()');
var start = ApiCoordinate(
col: event.startSquare.column, row: event.startSquare.row);
var end =
ApiCoordinate(col: event.endSquare.column, row: event.endSquare.row);
var apiMove = ApiMove(startSquare: start, endSquare: end);
var apiMove =
ChessMove(from: event.startSquare, to: event.endSquare).toApiMove();
var apiMessage = ApiWebsocketMessage(
type: MessageType.move, move: apiMove, color: null, reason: null);
@ -92,6 +119,23 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
);
}
void ownPromotionHandler(
OwnPromotionPlayed event, Emitter<ChessBoardState> emit) {
var apiMove = event.move.toApiMove();
var shorNameForPiece = chessPiecesShortName[
ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor!)]!
.toLowerCase();