import 'dart:async'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess_bloc/chess_events.dart'; 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() { return ChessBloc(); } factory ChessBloc() { return _instance; } FutureOr moveHandler(PieceMoved event, Emitter emit) { Map newPosition = state.position; newPosition[event.endSquare] = state.position[event.startSquare]!; newPosition[event.startSquare] = const ChessPiece.none(); log('emitting new state with position $newPosition'); emit(ChessBoardState( state.flipped, ChessColor.black, newPosition, )); } bool preCheckHandler( PreCheckMove event, ) { ServerConnection.getInstance().send( '${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 { late bool flipped; final ChessColor turnColor; final Map position; ChessBoardState._(this.flipped, this.turnColor, this.position); factory ChessBoardState( bool flipped, ChessColor turnColor, Map position, ) { return ChessBoardState._(flipped, turnColor, position); } factory ChessBoardState.init() { bool flipped = false; ChessColor turnColor = ChessColor.white; Map position = {}; 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)] = ChessPiece(ChessPieceName.blackKing, ChessColor.black); 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); return ChessBoardState._(flipped, turnColor, position); } }