Fix resetting the board in case a new game is started.

This commit is contained in:
Marco 2023-06-08 20:23:00 +02:00
parent 0627a627d8
commit d55c7bbe1a
4 changed files with 41 additions and 24 deletions

View File

@ -1,7 +1,7 @@
import 'package:mchess/api/move.dart';
enum MessageType {
moveMessage,
move,
colorDetermined;
String toJson() => name;
@ -32,10 +32,13 @@ class ApiWebsocketMessage {
ret = ApiWebsocketMessage(
type: type, move: null, color: ApiColor.fromJson(json['color']));
break;
case MessageType.moveMessage:
case MessageType.move:
ret = ApiWebsocketMessage(
type: type, move: ApiMove.fromJson(json['move']), color: null);
}
return ret;
}
Map<String, dynamic> toJson() =>
{'messageType': type, 'move': move, 'color': color};
}

View File

@ -34,7 +34,9 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
}
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
emit(ChessBoardState.init());
ChessPosition.getInstance().resetToStartingPosition();
emit(ChessBoardState(ChessColor.white, ChessColor.white,
ChessPosition.getInstance().currentPosition));
}
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
@ -45,6 +47,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
void opponentMoveHandler(
OpponentPieceMoved event, Emitter<ChessBoardState> emit) {
log('opponentMoveHandler()');
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
var newPosition = ChessPosition.getInstance().currentPosition;
@ -59,10 +62,10 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
newPosition,
),
);
log('emitting new state with position $newPosition');
}
void ownMoveHandler(OwnPieceMoved event, Emitter<ChessBoardState> emit) {
log('ownMoveHandler()');
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
var start = ApiCoordinate(
@ -70,8 +73,8 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
var end =
ApiCoordinate(col: event.endSquare.column, row: event.endSquare.row);
var move = ApiMove(startSquare: start, endSquare: end);
var message = ApiWebsocketMessage(
type: MessageType.moveMessage, move: move, color: null);
var message =
ApiWebsocketMessage(type: MessageType.move, move: move, color: null);
ServerConnection.getInstance().send(jsonEncode(message));
@ -88,7 +91,6 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
newPosition,
),
);
log('emitting new state with position $newPosition');
}
}
@ -110,10 +112,11 @@ class ChessBoardState {
factory ChessBoardState.init() {
ChessColor bottomColor = ChessColor.white;
ChessColor turnColor = ChessColor.white;
Map<ChessCoordinate, ChessPiece> position =
ChessPosition.getInstance().currentPosition;
return ChessBoardState(bottomColor, turnColor, position);
ChessPosition.getInstance().resetToStartingPosition();
return ChessBoardState(
bottomColor, turnColor, ChessPosition.getInstance().currentPosition);
}
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {

View File

@ -5,10 +5,10 @@ typedef ChessPositionType = Map<ChessCoordinate, ChessPiece>;
typedef ChessMoveHistory = List<ChessMove>;
class ChessPosition {
static final ChessPosition _instance = ChessPosition._internal();
static ChessPosition _instance = ChessPosition._internal();
final ChessPositionType position;
ChessMoveHistory history = ChessMoveHistory.empty(growable: true);
static ChessMoveHistory history = ChessMoveHistory.empty(growable: true);
static ChessPosition getInstance() {
return _instance;
@ -17,6 +17,16 @@ class ChessPosition {
ChessPosition({required this.position});
factory ChessPosition._internal() {
return ChessPosition(position: _getStartingPosition());
}
ChessPositionType get currentPosition => position;
ChessMove? get lastMove {
if (history.isEmpty) return null;
return history.last;
}
static ChessPositionType _getStartingPosition() {
ChessPositionType pos = {};
for (int i = 1; i <= 8; i++) {
@ -60,13 +70,12 @@ class ChessPosition {
pos[ChessCoordinate(8, 1)] =
ChessPiece(ChessPieceName.whiteRook, ChessColor.white);
return ChessPosition(position: pos);
return pos;
}
ChessPositionType get currentPosition => position;
ChessMove? get lastMove {
if (history.isEmpty) return null;
return history.last;
void resetToStartingPosition() {
history = ChessMoveHistory.empty(growable: true);
_instance = ChessPosition(position: _getStartingPosition());
}
void recordMove(ChessCoordinate from, ChessCoordinate to) {

View File

@ -63,24 +63,26 @@ class ServerConnection {
switch (apiMessage.type) {
case MessageType.colorDetermined:
ChessBloc.getInstance().add(ColorDetermined(
myColor: ChessColor.fromApiColor(apiMessage.color!)));
handleIncomingColorDeterminedMessage(apiMessage);
break;
case MessageType.moveMessage:
case MessageType.move:
handleIncomingMoveMessage(apiMessage);
break;
}
}
void handleIncomingColorDeterminedMessage(ApiWebsocketMessage apiMessage) {
ChessBloc.getInstance().add(InitBoard());
ChessBloc.getInstance().add(
ColorDetermined(myColor: ChessColor.fromApiColor(apiMessage.color!)));
}
void handleIncomingMoveMessage(ApiWebsocketMessage apiMessage) {
var move = ChessMove.fromApiMove(apiMessage.move!);
if (move == ChessPosition.getInstance().lastMove) {
//This is our own move that got resent by the server. Do not process.
} else {
log('lastMove: from: ${ChessPosition.getInstance().lastMove?.from} to: ${ChessPosition.getInstance().lastMove?.to}');
log('constructed move: from: ${move.from} to: ${move.to}');
log('Move received : ${move.from.toAlphabetical()}->${move.to.toAlphabetical()}');
ChessBloc.getInstance()
.add(OpponentPieceMoved(startSquare: move.from, endSquare: move.to));
}