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.
This commit is contained in:
Marco 2022-12-14 23:17:31 +01:00
parent 7198f591bd
commit c04407ae67
5 changed files with 61 additions and 22 deletions

View File

@ -6,11 +6,14 @@ import 'package:mchess/chessapp/chess_utils.dart';
import 'package:mchess/connection/ws_connection.dart'; import 'package:mchess/connection/ws_connection.dart';
import 'dart:developer'; import 'dart:developer';
int message_index = 0;
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> { class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
static final ChessBloc _instance = ChessBloc._internal(); static final ChessBloc _instance = ChessBloc._internal();
ChessBloc._internal() : super(ChessBoardState.init()) { ChessBloc._internal() : super(ChessBoardState.init()) {
on<PieceMoved>(moveHandler); on<PieceMoved>(moveHandler);
on<BoardFlippedEvent>(flipBoard);
} }
factory ChessBloc.getInstance() { factory ChessBloc.getInstance() {
@ -24,9 +27,6 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
FutureOr<void> moveHandler(PieceMoved event, Emitter<ChessBoardState> emit) { FutureOr<void> moveHandler(PieceMoved event, Emitter<ChessBoardState> emit) {
Map<ChessCoordinate, ChessPiece> newPosition = state.position; Map<ChessCoordinate, ChessPiece> newPosition = state.position;
ServerConnection.getInstance().send(
'mv ${event.startSquare.toString()} ${event.endSquare.toString()}');
newPosition[event.endSquare] = state.position[event.startSquare]!; newPosition[event.endSquare] = state.position[event.startSquare]!;
newPosition[event.startSquare] = const ChessPiece.none(); newPosition[event.startSquare] = const ChessPiece.none();
@ -42,11 +42,15 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
PreCheckMove event, PreCheckMove event,
) { ) {
ServerConnection.getInstance().send( 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'); log('Pretending to check a move before you drop a piece');
return true; return true;
} }
void flipBoard(BoardFlippedEvent event, Emitter<ChessBoardState> emit) {
emit(ChessBoardState(!state.flipped, state.turnColor, state.position));
}
} }
class ChessBoardState { class ChessBoardState {
@ -112,8 +116,4 @@ class ChessBoardState {
return ChessBoardState._(flipped, turnColor, position); return ChessBoardState._(flipped, turnColor, position);
} }
void flipBoard() {
flipped = !flipped;
}
} }

View File

@ -51,9 +51,8 @@ class ChessApp extends StatelessWidget {
BlocBuilder<ConnectionCubit, ConnectionCubitState>( BlocBuilder<ConnectionCubit, ConnectionCubitState>(
builder: (context, state) { builder: (context, state) {
return StreamBuilder( return StreamBuilder(
stream: ServerConnection.getInstance().channel.stream, stream: ServerConnection.getInstance().broadcast,
builder: (context, snapshot) { builder: (context, snapshot) {
log(snapshot.data.toString());
return Text( return Text(
style: const TextStyle(color: Colors.white), style: const TextStyle(color: Colors.white),
snapshot.data.toString()); snapshot.data.toString());

View File

@ -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_events.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart';
import '../connection/ws_connection.dart';
import 'chess_utils.dart'; import 'chess_utils.dart';
class ChessSquare extends StatelessWidget { class ChessSquare extends StatelessWidget {
@ -84,17 +85,20 @@ class ChessSquare extends StatelessWidget {
), ),
); );
}, },
onWillAccept: (move) { // onWillAccept: (move) {
move!.endSquare = coordinate; // move!.endSquare = coordinate;
final legalMove = // final legalMove =
context.read<ChessBloc>().preCheckHandler(PreCheckMove(move: move)); // context.read<ChessBloc>().preCheckHandler(PreCheckMove(move: move));
log('onWillAccept returns $legalMove'); // log('onWillAccept returns $legalMove');
return legalMove; // return legalMove;
}, // },
onAccept: (move) { onAccept: (move) {
move.endSquare = coordinate; move.endSquare = coordinate;
if (move.endSquare != move.startSquare) { if (move.endSquare != move.startSquare) {
ServerConnection.getInstance().send(
'${message_index++} mv ${move.startSquare.toString()} ${move.endSquare.toString()}');
context.read<ChessBloc>().add( context.read<ChessBloc>().add(
PieceMoved( PieceMoved(
startSquare: move.startSquare, startSquare: move.startSquare,

View File

@ -42,6 +42,13 @@ class ChessCoordinate {
ChessCoordinate(this.column, this.row); 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) ChessCoordinate.copyFrom(ChessCoordinate original)
: column = original.column, : column = original.column,
row = original.row; row = original.row;

View File

@ -1,11 +1,15 @@
import 'dart:developer'; 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'; import 'package:web_socket_channel/web_socket_channel.dart';
class ServerConnection { class ServerConnection {
late WebSocketChannel channel; late WebSocketChannel _channel;
late bool wasConnected = false; late bool wasConnected = false;
late int counter = 0; late int counter = 0;
late Stream broadcast;
static final ServerConnection _instance = ServerConnection._internal(); static final ServerConnection _instance = ServerConnection._internal();
@ -23,20 +27,45 @@ class ServerConnection {
} }
void send(String message) { void send(String message) {
channel.sink.add('$counter $message'); _channel.sink.add(message);
counter++; counter++;
} }
void connect() { void connect() {
if (wasConnected) channel.sink.close(); if (wasConnected) _channel.sink.close();
channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080')); _channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080'));
wasConnected = true; 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(); sendPassword();
} }
void sendPassword() { void sendPassword() {
channel.sink.add( send(
'pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM'); 'pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM');
} }
} }