98 lines
2.8 KiB
Dart
98 lines
2.8 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:mchess/chess_bloc/chess_events.dart';
|
|
import 'package:mchess/chess_bloc/chess_position.dart';
|
|
import 'package:mchess/connection/ws_connection.dart';
|
|
import 'package:mchess/utils/chess_utils.dart';
|
|
import 'dart:developer';
|
|
|
|
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|
static final ChessBloc _instance = ChessBloc._internal();
|
|
static ChessColor turnColor = ChessColor.white;
|
|
static ChessColor? myColor;
|
|
|
|
static ChessColor? getSidesColor() {
|
|
return myColor;
|
|
}
|
|
|
|
ChessBloc._internal() : super(ChessBoardState.init()) {
|
|
on<InitBoard>(initBoard);
|
|
on<ColorDetermined>(flipBoard);
|
|
on<OpponentPieceMoved>(opponentMoveHandler);
|
|
on<OwnPieceMoved>(ownMoveHandler);
|
|
}
|
|
|
|
factory ChessBloc.getInstance() {
|
|
return ChessBloc();
|
|
}
|
|
|
|
factory ChessBloc() {
|
|
return _instance;
|
|
}
|
|
|
|
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
|
|
emit(ChessBoardState.init());
|
|
}
|
|
|
|
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
|
|
log("My Color is $myColor");
|
|
myColor = event.myColor;
|
|
emit(ChessBoardState(event.myColor, state.newTurnColor, state.position));
|
|
}
|
|
|
|
void opponentMoveHandler(
|
|
OpponentPieceMoved event, Emitter<ChessBoardState> emit) {
|
|
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
|
var newPosition = ChessPosition.getInstance().currentPosition;
|
|
|
|
turnColor = state.newTurnColor == ChessColor.white
|
|
? ChessColor.black
|
|
: ChessColor.white;
|
|
|
|
log('emitting new state with position $newPosition');
|
|
|
|
emit(
|
|
ChessBoardState(
|
|
state.bottomColor,
|
|
turnColor,
|
|
newPosition,
|
|
),
|
|
);
|
|
}
|
|
|
|
void ownMoveHandler(OwnPieceMoved event, Emitter<ChessBoardState> emit) {
|
|
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
|
|
|
ServerConnection.getInstance().send(
|
|
'mv ${event.startSquare.toString()} ${event.endSquare.toString()}');
|
|
}
|
|
}
|
|
|
|
class ChessBoardState {
|
|
late ChessColor bottomColor;
|
|
final ChessColor newTurnColor;
|
|
final Map<ChessCoordinate, ChessPiece> position;
|
|
|
|
ChessBoardState._(this.bottomColor, this.newTurnColor, this.position);
|
|
|
|
factory ChessBoardState(
|
|
ChessColor bottomColor,
|
|
ChessColor turnColor,
|
|
Map<ChessCoordinate, ChessPiece> position,
|
|
) {
|
|
return ChessBoardState._(bottomColor, turnColor, position);
|
|
}
|
|
|
|
factory ChessBoardState.init() {
|
|
ChessColor bottomColor = ChessColor.white;
|
|
ChessColor turnColor = ChessColor.white;
|
|
Map<ChessCoordinate, ChessPiece> position =
|
|
ChessPosition.getInstance().currentPosition;
|
|
|
|
return ChessBoardState(bottomColor, turnColor, position);
|
|
}
|
|
|
|
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {
|
|
// for (int i = 0; i < 7; i++)
|
|
}
|
|
}
|