From c04407ae6760b9e0c6392650b7b0d92bb0656929 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 14 Dec 2022 23:17:31 +0100 Subject: [PATCH] Make chess work. Remove sending the new move in moveHandler, in order to prevent a loop move -> emit -> move ... Handle incoming moves. There still is a major bug. Sometimes random pieces move. --- lib/chess_bloc/chess_bloc.dart | 16 ++++++------- lib/chessapp/chess_app.dart | 3 +-- lib/chessapp/chess_square.dart | 18 ++++++++------ lib/chessapp/chess_utils.dart | 7 ++++++ lib/connection/ws_connection.dart | 39 +++++++++++++++++++++++++++---- 5 files changed, 61 insertions(+), 22 deletions(-) diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 2159475..12e6d99 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -6,11 +6,14 @@ import 'package:mchess/chessapp/chess_utils.dart'; import 'package:mchess/connection/ws_connection.dart'; import 'dart:developer'; +int message_index = 0; + class ChessBloc extends Bloc { static final ChessBloc _instance = ChessBloc._internal(); ChessBloc._internal() : super(ChessBoardState.init()) { on(moveHandler); + on(flipBoard); } factory ChessBloc.getInstance() { @@ -24,9 +27,6 @@ class ChessBloc extends Bloc { FutureOr moveHandler(PieceMoved event, Emitter emit) { Map newPosition = state.position; - ServerConnection.getInstance().send( - 'mv ${event.startSquare.toString()} ${event.endSquare.toString()}'); - newPosition[event.endSquare] = state.position[event.startSquare]!; newPosition[event.startSquare] = const ChessPiece.none(); @@ -42,11 +42,15 @@ class ChessBloc extends Bloc { PreCheckMove event, ) { ServerConnection.getInstance().send( - 'pc ${event.move.startSquare.toString()} ${event.move.endSquare.toString()}'); + '${message_index++} pc ${event.move.startSquare.toString()} ${event.move.endSquare.toString()}'); log('Pretending to check a move before you drop a piece'); return true; } + + void flipBoard(BoardFlippedEvent event, Emitter emit) { + emit(ChessBoardState(!state.flipped, state.turnColor, state.position)); + } } class ChessBoardState { @@ -112,8 +116,4 @@ class ChessBoardState { return ChessBoardState._(flipped, turnColor, position); } - - void flipBoard() { - flipped = !flipped; - } } diff --git a/lib/chessapp/chess_app.dart b/lib/chessapp/chess_app.dart index 99e7928..d4ee9f8 100644 --- a/lib/chessapp/chess_app.dart +++ b/lib/chessapp/chess_app.dart @@ -51,9 +51,8 @@ class ChessApp extends StatelessWidget { BlocBuilder( builder: (context, state) { return StreamBuilder( - stream: ServerConnection.getInstance().channel.stream, + stream: ServerConnection.getInstance().broadcast, builder: (context, snapshot) { - log(snapshot.data.toString()); return Text( style: const TextStyle(color: Colors.white), snapshot.data.toString()); diff --git a/lib/chessapp/chess_square.dart b/lib/chessapp/chess_square.dart index 4f0d430..dcd748e 100644 --- a/lib/chessapp/chess_square.dart +++ b/lib/chessapp/chess_square.dart @@ -6,6 +6,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess_bloc/chess_events.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; +import '../connection/ws_connection.dart'; import 'chess_utils.dart'; class ChessSquare extends StatelessWidget { @@ -84,17 +85,20 @@ class ChessSquare extends StatelessWidget { ), ); }, - onWillAccept: (move) { - move!.endSquare = coordinate; - final legalMove = - context.read().preCheckHandler(PreCheckMove(move: move)); - log('onWillAccept returns $legalMove'); - return legalMove; - }, + // onWillAccept: (move) { + // move!.endSquare = coordinate; + // final legalMove = + // context.read().preCheckHandler(PreCheckMove(move: move)); + // log('onWillAccept returns $legalMove'); + // return legalMove; + // }, onAccept: (move) { move.endSquare = coordinate; if (move.endSquare != move.startSquare) { + ServerConnection.getInstance().send( + '${message_index++} mv ${move.startSquare.toString()} ${move.endSquare.toString()}'); + context.read().add( PieceMoved( startSquare: move.startSquare, diff --git a/lib/chessapp/chess_utils.dart b/lib/chessapp/chess_utils.dart index 58aa0ae..a5322eb 100644 --- a/lib/chessapp/chess_utils.dart +++ b/lib/chessapp/chess_utils.dart @@ -42,6 +42,13 @@ class ChessCoordinate { ChessCoordinate(this.column, this.row); + factory ChessCoordinate.fromString(String coordString) { + var column = int.parse(coordString[0]); + var row = int.parse(coordString[1]); + + return ChessCoordinate(column, row); + } + ChessCoordinate.copyFrom(ChessCoordinate original) : column = original.column, row = original.row; diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 1edf0b2..6126df8 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -1,11 +1,15 @@ import 'dart:developer'; +import 'package:mchess/chess_bloc/chess_bloc.dart'; +import 'package:mchess/chess_bloc/chess_events.dart'; +import 'package:mchess/chessapp/chess_utils.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; class ServerConnection { - late WebSocketChannel channel; + late WebSocketChannel _channel; late bool wasConnected = false; late int counter = 0; + late Stream broadcast; static final ServerConnection _instance = ServerConnection._internal(); @@ -23,20 +27,45 @@ class ServerConnection { } void send(String message) { - channel.sink.add('$counter $message'); + _channel.sink.add(message); counter++; } void connect() { - if (wasConnected) channel.sink.close(); - channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080')); + if (wasConnected) _channel.sink.close(); + _channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080')); wasConnected = true; + broadcast = _channel.stream.asBroadcastStream(); + + broadcast.listen((data) { + log("Data received:"); + log(data); + + var receivedString = data.toString(); + + if (receivedString == "fb") { + ChessBloc.getInstance().add(BoardFlippedEvent()); + return; + } + + var moveStringList = receivedString.split(' '); + if (moveStringList[1] == ('mv')) { + var startSquare = ChessCoordinate.fromString(moveStringList[2]); + var endSquare = ChessCoordinate.fromString(moveStringList[3]); + + log('Move received : ${moveStringList[2]}:${moveStringList[3]}'); + + ChessBloc.getInstance() + .add(PieceMoved(startSquare: startSquare, endSquare: endSquare)); + } + }); + sendPassword(); } void sendPassword() { - channel.sink.add( + send( 'pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM'); } }