2022-11-13 00:03:09 +00:00
|
|
|
import 'dart:async';
|
2022-11-13 13:27:00 +00:00
|
|
|
|
2022-11-13 00:03:09 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2022-11-13 13:27:00 +00:00
|
|
|
import 'package:mchess/chess_bloc/chess_events.dart';
|
2022-11-13 00:03:09 +00:00
|
|
|
import 'package:mchess/chessapp/chess_utils.dart';
|
2022-11-13 13:27:00 +00:00
|
|
|
import 'package:mchess/connection/ws_connection.dart';
|
2022-12-13 02:43:05 +00:00
|
|
|
import 'dart:developer';
|
2022-11-13 00:03:09 +00:00
|
|
|
|
2022-12-14 22:17:31 +00:00
|
|
|
int message_index = 0;
|
|
|
|
|
2022-11-13 00:03:09 +00:00
|
|
|
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|
|
|
static final ChessBloc _instance = ChessBloc._internal();
|
|
|
|
|
|
|
|
ChessBloc._internal() : super(ChessBoardState.init()) {
|
|
|
|
on<PieceMoved>(moveHandler);
|
2022-12-14 22:17:31 +00:00
|
|
|
on<BoardFlippedEvent>(flipBoard);
|
2022-11-13 00:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
factory ChessBloc.getInstance() {
|
|
|
|
return ChessBloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
factory ChessBloc() {
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2022-11-13 13:27:00 +00:00
|
|
|
FutureOr<void> moveHandler(PieceMoved event, Emitter<ChessBoardState> emit) {
|
2022-11-19 15:55:25 +00:00
|
|
|
Map<ChessCoordinate, ChessPiece> newPosition = state.position;
|
2022-11-13 01:42:10 +00:00
|
|
|
|
2022-11-13 02:24:42 +00:00
|
|
|
newPosition[event.endSquare] = state.position[event.startSquare]!;
|
2022-11-13 01:42:10 +00:00
|
|
|
newPosition[event.startSquare] = const ChessPiece.none();
|
|
|
|
|
2022-12-13 02:43:05 +00:00
|
|
|
log('emitting new state with position $newPosition');
|
2022-11-13 00:03:09 +00:00
|
|
|
emit(ChessBoardState(
|
2022-11-13 01:42:10 +00:00
|
|
|
state.flipped,
|
2022-11-13 00:03:09 +00:00
|
|
|
ChessColor.black,
|
2022-11-13 01:42:10 +00:00
|
|
|
newPosition,
|
2022-11-13 00:03:09 +00:00
|
|
|
));
|
|
|
|
}
|
2022-11-13 01:42:10 +00:00
|
|
|
|
2022-11-13 02:50:29 +00:00
|
|
|
bool preCheckHandler(
|
2022-11-13 01:42:10 +00:00
|
|
|
PreCheckMove event,
|
|
|
|
) {
|
2022-11-19 10:37:56 +00:00
|
|
|
ServerConnection.getInstance().send(
|
2022-12-14 22:17:31 +00:00
|
|
|
'${message_index++} pc ${event.move.startSquare.toString()} ${event.move.endSquare.toString()}');
|
2022-11-19 10:37:56 +00:00
|
|
|
|
2022-12-13 02:43:05 +00:00
|
|
|
log('Pretending to check a move before you drop a piece');
|
2022-11-13 02:50:29 +00:00
|
|
|
return true;
|
2022-11-13 01:42:10 +00:00
|
|
|
}
|
2022-12-14 22:17:31 +00:00
|
|
|
|
|
|
|
void flipBoard(BoardFlippedEvent event, Emitter<ChessBoardState> emit) {
|
|
|
|
emit(ChessBoardState(!state.flipped, state.turnColor, state.position));
|
|
|
|
}
|
2022-11-13 00:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ChessBoardState {
|
2022-12-13 02:35:01 +00:00
|
|
|
late bool flipped;
|
2022-11-13 00:03:09 +00:00
|
|
|
final ChessColor turnColor;
|
|
|
|
final Map<ChessCoordinate, ChessPiece> position;
|
|
|
|
|
2022-11-13 01:42:10 +00:00
|
|
|
ChessBoardState._(this.flipped, this.turnColor, this.position);
|
|
|
|
|
|
|
|
factory ChessBoardState(
|
|
|
|
bool flipped,
|
|
|
|
ChessColor turnColor,
|
|
|
|
Map<ChessCoordinate, ChessPiece> position,
|
|
|
|
) {
|
|
|
|
return ChessBoardState._(flipped, turnColor, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
factory ChessBoardState.init() {
|
|
|
|
bool flipped = false;
|
|
|
|
ChessColor turnColor = ChessColor.white;
|
|
|
|
Map<ChessCoordinate, ChessPiece> position = {};
|
2022-11-13 00:03:09 +00:00
|
|
|
|
2022-11-19 15:55:25 +00:00
|
|
|
for (int i = 0; i <= 8; i++) {
|
|
|
|
position[ChessCoordinate(i, 7)] =
|
|
|
|
ChessPiece(ChessPieceName.blackPawn, ChessColor.black);
|
|
|
|
position[ChessCoordinate(i, 2)] =
|
|
|
|
ChessPiece(ChessPieceName.whitePawn, ChessColor.white);
|
|
|
|
}
|
|
|
|
|
|
|
|
position[ChessCoordinate(1, 8)] =
|
|
|
|
ChessPiece(ChessPieceName.blackRook, ChessColor.black);
|
|
|
|
position[ChessCoordinate(2, 8)] =
|
|
|
|
ChessPiece(ChessPieceName.blackKnight, ChessColor.black);
|
|
|
|
position[ChessCoordinate(3, 8)] =
|
|
|
|
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
|
|
|
|
position[ChessCoordinate(4, 8)] =
|
|
|
|
ChessPiece(ChessPieceName.blackQueen, ChessColor.black);
|
|
|
|
position[ChessCoordinate(5, 8)] =
|
2022-11-13 02:42:30 +00:00
|
|
|
ChessPiece(ChessPieceName.blackKing, ChessColor.black);
|
2022-11-19 15:55:25 +00:00
|
|
|
position[ChessCoordinate(6, 8)] =
|
|
|
|
ChessPiece(ChessPieceName.blackBishop, ChessColor.black);
|
|
|
|
position[ChessCoordinate(7, 8)] =
|
|
|
|
ChessPiece(ChessPieceName.blackKnight, ChessColor.black);
|
|
|
|
position[ChessCoordinate(8, 8)] =
|
|
|
|
ChessPiece(ChessPieceName.blackRook, ChessColor.black);
|
|
|
|
|
|
|
|
position[ChessCoordinate(1, 1)] =
|
|
|
|
ChessPiece(ChessPieceName.whiteRook, ChessColor.black);
|
|
|
|
position[ChessCoordinate(2, 1)] =
|
|
|
|
ChessPiece(ChessPieceName.whiteKnight, ChessColor.black);
|
|
|
|
position[ChessCoordinate(3, 1)] =
|
|
|
|
ChessPiece(ChessPieceName.whiteBishop, ChessColor.black);
|
|
|
|
position[ChessCoordinate(4, 1)] =
|
|
|
|
ChessPiece(ChessPieceName.whiteQueen, ChessColor.black);
|
|
|
|
position[ChessCoordinate(5, 1)] =
|
|
|
|
ChessPiece(ChessPieceName.whiteKing, ChessColor.black);
|
|
|
|
position[ChessCoordinate(6, 1)] =
|
|
|
|
ChessPiece(ChessPieceName.whiteBishop, ChessColor.black);
|
|
|
|
position[ChessCoordinate(7, 1)] =
|
|
|
|
ChessPiece(ChessPieceName.whiteKnight, ChessColor.black);
|
|
|
|
position[ChessCoordinate(8, 1)] =
|
|
|
|
ChessPiece(ChessPieceName.whiteRook, ChessColor.black);
|
2022-11-13 01:42:10 +00:00
|
|
|
|
|
|
|
return ChessBoardState._(flipped, turnColor, position);
|
|
|
|
}
|
2022-11-13 00:03:09 +00:00
|
|
|
}
|