From d55c7bbe1a5f84d822443658d9f950ddacf3e0ed Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 8 Jun 2023 20:23:00 +0200 Subject: [PATCH] Fix resetting the board in case a new game is started. --- lib/api/websocket_message.dart | 7 +++++-- lib/chess_bloc/chess_bloc.dart | 19 +++++++++++-------- lib/chess_bloc/chess_position.dart | 23 ++++++++++++++++------- lib/connection/ws_connection.dart | 16 +++++++++------- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/lib/api/websocket_message.dart b/lib/api/websocket_message.dart index d4eebbc..19cfd2c 100644 --- a/lib/api/websocket_message.dart +++ b/lib/api/websocket_message.dart @@ -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 toJson() => + {'messageType': type, 'move': move, 'color': color}; } diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 0571836..cdc45b0 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -34,7 +34,9 @@ class ChessBloc extends Bloc { } void initBoard(InitBoard event, Emitter emit) { - emit(ChessBoardState.init()); + ChessPosition.getInstance().resetToStartingPosition(); + emit(ChessBoardState(ChessColor.white, ChessColor.white, + ChessPosition.getInstance().currentPosition)); } void flipBoard(ColorDetermined event, Emitter emit) { @@ -45,6 +47,7 @@ class ChessBloc extends Bloc { void opponentMoveHandler( OpponentPieceMoved event, Emitter emit) { + log('opponentMoveHandler()'); ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare); var newPosition = ChessPosition.getInstance().currentPosition; @@ -59,10 +62,10 @@ class ChessBloc extends Bloc { newPosition, ), ); - log('emitting new state with position $newPosition'); } void ownMoveHandler(OwnPieceMoved event, Emitter emit) { + log('ownMoveHandler()'); ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare); var start = ApiCoordinate( @@ -70,8 +73,8 @@ class ChessBloc extends Bloc { 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 { 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 position = - ChessPosition.getInstance().currentPosition; - return ChessBoardState(bottomColor, turnColor, position); + ChessPosition.getInstance().resetToStartingPosition(); + + return ChessBoardState( + bottomColor, turnColor, ChessPosition.getInstance().currentPosition); } void logPosition(Map pos) { diff --git a/lib/chess_bloc/chess_position.dart b/lib/chess_bloc/chess_position.dart index 6ee2c1f..af3223a 100644 --- a/lib/chess_bloc/chess_position.dart +++ b/lib/chess_bloc/chess_position.dart @@ -5,10 +5,10 @@ typedef ChessPositionType = Map; typedef ChessMoveHistory = List; 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) { diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 1e7f9a8..53aa9f2 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -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)); }