From 43d8d77abcebb51fcc6cda9ceab9e010cdea63be Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 15 Aug 2023 00:39:10 +0200 Subject: [PATCH] Fix the current position in ChessPositionManager, now that we rely on position instead of detecting en passant ourselves. --- lib/chess_bloc/chess_bloc.dart | 23 +++++++++--------- lib/chess_bloc/chess_events.dart | 13 ++++------ lib/chess_bloc/chess_position.dart | 38 ++++++++++++++---------------- lib/connection/ws_connection.dart | 12 ++++++---- 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 15ea8f3..2144398 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -20,8 +20,7 @@ class ChessBloc extends Bloc { ChessBloc._internal() : super(ChessBoardState.init()) { on(initBoard); on(flipBoard); - on(moveHandler); - on(positionHandler); + on(moveAndPositionHandler); on(ownMoveHandler); on(ownPromotionHandler); on(invalidMoveHandler); @@ -37,9 +36,9 @@ class ChessBloc extends Bloc { void initBoard(InitBoard event, Emitter emit) { turnColor = ChessColor.white; - ChessPosition.getInstance().resetToStartingPosition(); + ChessPositionManager.getInstance().resetToStartingPosition(); emit(ChessBoardState(ChessColor.white, ChessColor.white, - ChessPosition.getInstance().currentPosition)); + ChessPositionManager.getInstance().currentPosition)); } void flipBoard(ColorDetermined event, Emitter emit) { @@ -49,13 +48,13 @@ class ChessBloc extends Bloc { } void moveHandler(ReceivedMove event, Emitter emit) { - ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare); } - void positionHandler( - ReceivedPosition event, + void moveAndPositionHandler( + ReceivedMove event, Emitter emit, ) { + ChessPositionManager.getInstance().recordMove(event.startSquare, event.endSquare, event.position); turnColor = state.newTurnColor == ChessColor.white ? ChessColor.black : ChessColor.white; @@ -79,7 +78,7 @@ class ChessBloc extends Bloc { //Temporary chess position until server responds with acknowledgement var move = ChessMove.fromApiMove(apiMove); - var tempPosition = ChessPosition.getInstance().copyOfCurrentPosition; + var tempPosition = ChessPositionManager.getInstance().copyOfCurrentPosition; tempPosition[move.to] = tempPosition[move.from] ?? const ChessPiece.none(); tempPosition[move.from] = const ChessPiece.none(); @@ -115,7 +114,7 @@ class ChessBloc extends Bloc { ChessBoardState( state.bottomColor, turnColor, - ChessPosition.getInstance().currentPosition, + ChessPositionManager.getInstance().currentPosition, ), ); } @@ -124,7 +123,7 @@ class ChessBloc extends Bloc { class ChessBoardState { late ChessColor bottomColor; final ChessColor newTurnColor; - final Map position; + final ChessPosition position; ChessBoardState._(this.bottomColor, this.newTurnColor, this.position); @@ -140,10 +139,10 @@ class ChessBoardState { ChessColor bottomColor = ChessColor.white; ChessColor turnColor = ChessColor.white; - ChessPosition.getInstance().resetToStartingPosition(); + ChessPositionManager.getInstance().resetToStartingPosition(); return ChessBoardState( - bottomColor, turnColor, ChessPosition.getInstance().currentPosition); + bottomColor, turnColor, ChessPositionManager.getInstance().currentPosition); } void logPosition(Map pos) { diff --git a/lib/chess_bloc/chess_events.dart b/lib/chess_bloc/chess_events.dart index 060e665..2e25e1f 100644 --- a/lib/chess_bloc/chess_events.dart +++ b/lib/chess_bloc/chess_events.dart @@ -4,16 +4,11 @@ import 'package:mchess/utils/chess_utils.dart'; abstract class ChessEvent {} class ReceivedMove extends ChessEvent { - final ChessCoordinate startSquare; - final ChessCoordinate endSquare; + final ChessCoordinate startSquare; + final ChessCoordinate endSquare; + final ChessPosition position; - ReceivedMove({required this.startSquare, required this.endSquare}); -} - -class ReceivedPosition extends ChessEvent { - final ChessPositionType position; - - ReceivedPosition({required this.position}); + ReceivedMove({required this.startSquare, required this.endSquare, required this.position}); } class OwnPieceMoved extends ChessEvent { diff --git a/lib/chess_bloc/chess_position.dart b/lib/chess_bloc/chess_position.dart index 0b56010..c392f74 100644 --- a/lib/chess_bloc/chess_position.dart +++ b/lib/chess_bloc/chess_position.dart @@ -3,29 +3,29 @@ import 'dart:developer'; import 'package:mchess/utils/chess_utils.dart'; typedef ChessMoveHistory = List; -typedef ChessPositionType = Map; +typedef ChessPosition = Map; -class ChessPosition { - static ChessPosition _instance = ChessPosition._internal(); +class ChessPositionManager { + static ChessPositionManager _instance = ChessPositionManager._internal(); static ChessMoveHistory history = ChessMoveHistory.empty(growable: true); - final ChessPositionType position; + ChessPosition position; - ChessPosition({required this.position}); + ChessPositionManager({required this.position}); - factory ChessPosition._internal() { - return ChessPosition(position: _getStartingPosition()); + factory ChessPositionManager._internal() { + return ChessPositionManager(position: _getStartingPosition()); } - ChessPositionType get copyOfCurrentPosition => Map.from(position); + ChessPosition get copyOfCurrentPosition => Map.from(position); - ChessPositionType get currentPosition => position; + ChessPosition get currentPosition => position; ChessMove? get lastMove { if (history.isEmpty) return null; return history.last; } - ChessPositionType fromPGNString(String pgn) { - ChessPositionType pos = {}; + ChessPosition fromPGNString(String pgn) { + ChessPosition pos = {}; List rowStrings; rowStrings = pgn.split('/'); @@ -53,7 +53,7 @@ class ChessPosition { } } - void logPosition(ChessPositionType p) { + void logPosition(ChessPosition p) { String logString = ''; for (int row = 8; row > 0; row--) { @@ -71,28 +71,26 @@ class ChessPosition { log(logString); } - void recordMove(ChessCoordinate from, ChessCoordinate to) { - position[to] = position[from] ?? const ChessPiece.none(); - position[from] = const ChessPiece.none(); + void recordMove(ChessCoordinate from, ChessCoordinate to, ChessPosition pos) { + position = pos; history.add(ChessMove(from: from, to: to)); logPosition(position); - logHistory(history); } void resetToStartingPosition() { history = ChessMoveHistory.empty(growable: true); - _instance = ChessPosition(position: _getStartingPosition()); + _instance = ChessPositionManager(position: _getStartingPosition()); } - static ChessPosition getInstance() { + static ChessPositionManager getInstance() { return _instance; } - static ChessPositionType _getStartingPosition() { - ChessPositionType pos = {}; + static ChessPosition _getStartingPosition() { + ChessPosition pos = {}; for (int i = 1; i <= 8; i++) { pos[ChessCoordinate(i, 7)] = diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 3c15568..530db78 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -88,12 +88,14 @@ class ServerConnection { var move = ChessMove.fromApiMove(apiMessage.move!); if (apiMessage.position != null) { - ChessBloc.getInstance().add(ReceivedPosition( - position: - ChessPosition.getInstance().fromPGNString(apiMessage.position!))); + ChessBloc.getInstance().add(ReceivedMove( + startSquare: move.from, + endSquare: move.to, + position: ChessPositionManager.getInstance() + .fromPGNString(apiMessage.position!))); + } else { + log('Error: no position received'); } - ChessBloc.getInstance() - .add(ReceivedMove(startSquare: move.from, endSquare: move.to)); } void handleInvalidMoveMessage(ApiWebsocketMessage apiMessage) {