From cf12bc08c4a95db5a658e0966519a795ffde535e Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 30 Jan 2023 23:40:46 +0100 Subject: [PATCH] Add helper functions and fix the bug that replayed the move sent from the server. --- lib/chess_bloc/chess_position.dart | 18 +++++++++++++++- lib/connection/ws_connection.dart | 16 +++++++++----- lib/utils/chess_utils.dart | 34 ++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/lib/chess_bloc/chess_position.dart b/lib/chess_bloc/chess_position.dart index 464061f..e3f8070 100644 --- a/lib/chess_bloc/chess_position.dart +++ b/lib/chess_bloc/chess_position.dart @@ -1,12 +1,16 @@ import 'dart:developer'; +import 'package:flutter/material.dart'; import 'package:mchess/utils/chess_utils.dart'; typedef ChessPositionType = Map; +typedef ChessMoveHistory = List; class ChessPosition { static final ChessPosition _instance = ChessPosition._internal(); - late ChessPositionType position; + final ChessPositionType position; + + ChessMoveHistory history = ChessMoveHistory.empty(growable: true); static ChessPosition getInstance() { return _instance; @@ -62,11 +66,17 @@ class ChessPosition { } ChessPositionType get currentPosition => position; + ChessMove get lastMove => history.last; void recordMove(ChessCoordinate from, ChessCoordinate to) { position[to] = position[from] ?? const ChessPiece.none(); position[from] = const ChessPiece.none(); + + history.add(ChessMove(from: from, to: to)); + logPosition(position); + + logHistory(history); } void logPosition(ChessPositionType p) { @@ -86,4 +96,10 @@ class ChessPosition { log(logString); } + + void logHistory(ChessMoveHistory hist) { + hist.forEach((element) { + log('${element.from.toAlphabetical()} -> ${element.to.toAlphabetical()}'); + }); + } } diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index d967520..4070423 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -1,7 +1,7 @@ import 'dart:developer'; -import 'package:flutter/foundation.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/chess_events.dart'; +import 'package:mchess/chess_bloc/chess_position.dart'; import 'package:mchess/utils/chess_utils.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; @@ -64,11 +64,17 @@ class ServerConnection { if (splitString[0] == ('mv')) { var startSquare = ChessCoordinate.fromString(splitString[1]); var endSquare = ChessCoordinate.fromString(splitString[2]); + if (ChessMove(from: startSquare, to: endSquare) == + 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: $startSquare to: $endSquare)}'); + log('Move received : ${startSquare.toAlphabetical()}:${endSquare.toAlphabetical()}'); - log('Move received : ${splitString[1]}:${splitString[2]}'); - - ChessBloc.getInstance().add( - OpponentPieceMoved(startSquare: startSquare, endSquare: endSquare)); + ChessBloc.getInstance().add(OpponentPieceMoved( + startSquare: startSquare, endSquare: endSquare)); + } } }); } diff --git a/lib/utils/chess_utils.dart b/lib/utils/chess_utils.dart index df72cbd..47face4 100644 --- a/lib/utils/chess_utils.dart +++ b/lib/utils/chess_utils.dart @@ -91,6 +91,23 @@ class ChessCoordinate { return '$colStr$rowStr'; } + String toAlphabetical() { + Map columnMap = { + 1: "a", + 2: "b", + 3: "c", + 4: "d", + 5: "e", + 6: "f", + 7: "g", + 8: "h" + }; + String rowStr = row.toString(); + String colStr = columnMap[column]!; + + return '$colStr$rowStr'; + } + @override operator ==(other) { return other is ChessCoordinate && @@ -136,6 +153,23 @@ class ChessPiece extends StatelessWidget { } } +class ChessMove { + ChessCoordinate from; + ChessCoordinate to; + + ChessMove({required this.from, required this.to}); + + @override + operator ==(other) { + return other is ChessMove && other.from == from && other.to == to; + } + + @override + int get hashCode { + return hash2(from, to); + } +} + class PieceMovedFrom { ChessCoordinate fromSquare; ChessPiece? movedPiece;