Marco
c213d9b1f3
Now the client considers the position sent by the server to build the position on the board.
153 lines
4.4 KiB
Dart
153 lines
4.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:mchess/api/websocket_message.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 = ChessColor.white;
|
|
|
|
static ChessColor? getSidesColor() {
|
|
return myColor;
|
|
}
|
|
|
|
ChessBloc._internal() : super(ChessBoardState.init()) {
|
|
on<InitBoard>(initBoard);
|
|
on<ColorDetermined>(flipBoard);
|
|
on<ReceivedMove>(moveHandler);
|
|
on<ReceivedPosition>(positionHandler);
|
|
on<OwnPieceMoved>(ownMoveHandler);
|
|
on<OwnPromotionPlayed>(ownPromotionHandler);
|
|
on<InvalidMovePlayed>(invalidMoveHandler);
|
|
}
|
|
|
|
factory ChessBloc.getInstance() {
|
|
return ChessBloc();
|
|
}
|
|
|
|
factory ChessBloc() {
|
|
return _instance;
|
|
}
|
|
|
|
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
|
|
turnColor = ChessColor.white;
|
|
ChessPosition.getInstance().resetToStartingPosition();
|
|
emit(ChessBoardState(ChessColor.white, ChessColor.white,
|
|
ChessPosition.getInstance().currentPosition));
|
|
}
|
|
|
|
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
|
|
log("My Color is $myColor");
|
|
myColor = event.myColor;
|
|
emit(ChessBoardState(event.myColor, state.newTurnColor, state.position));
|
|
}
|
|
|
|
void moveHandler(ReceivedMove event, Emitter<ChessBoardState> emit) {
|
|
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
|
}
|
|
|
|
void positionHandler(
|
|
ReceivedPosition event,
|
|
Emitter<ChessBoardState> emit,
|
|
) {
|
|
turnColor = state.newTurnColor == ChessColor.white
|
|
? ChessColor.black
|
|
: ChessColor.white;
|
|
|
|
emit(ChessBoardState(state.bottomColor, turnColor, event.position));
|
|
}
|
|
|
|
void ownMoveHandler(OwnPieceMoved event, Emitter<ChessBoardState> emit) {
|
|
log('ownMoveHandler()');
|
|
var apiMove =
|
|
ChessMove(from: event.startSquare, to: event.endSquare).toApiMove();
|
|
var apiMessage = ApiWebsocketMessage(
|
|
type: MessageType.move,
|
|
move: apiMove,
|
|
color: null,
|
|
reason: null,
|
|
position: null,
|
|
);
|
|
|
|
ServerConnection.getInstance().send(jsonEncode(apiMessage));
|
|
|
|
//Temporary chess position until server responds with acknowledgement
|
|
var move = ChessMove.fromApiMove(apiMove);
|
|
var tempPosition = ChessPosition.getInstance().copyOfCurrentPosition;
|
|
tempPosition[move.to] = tempPosition[move.from] ?? const ChessPiece.none();
|
|
tempPosition[move.from] = const ChessPiece.none();
|
|
|
|
emit(
|
|
ChessBoardState(
|
|
state.bottomColor,
|
|
turnColor,
|
|
tempPosition,
|
|
),
|
|
);
|
|
}
|
|
|
|
void ownPromotionHandler(
|
|
OwnPromotionPlayed event, Emitter<ChessBoardState> emit) {
|
|
var apiMove = event.move.toApiMove();
|
|
var shorNameForPiece = chessPiecesShortName[
|
|
ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor!)]!;
|
|
apiMove.promotionToPiece = shorNameForPiece;
|
|
var message = ApiWebsocketMessage(
|
|
type: MessageType.move,
|
|
move: apiMove,
|
|
color: null,
|
|
reason: null,
|
|
position: null,
|
|
);
|
|
log(jsonEncode(message));
|
|
ServerConnection.getInstance().send(jsonEncode(message));
|
|
}
|
|
|
|
void invalidMoveHandler(
|
|
InvalidMovePlayed event, Emitter<ChessBoardState> emit) {
|
|
emit(
|
|
ChessBoardState(
|
|
state.bottomColor,
|
|
turnColor,
|
|
ChessPosition.getInstance().currentPosition,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
ChessPosition.getInstance().resetToStartingPosition();
|
|
|
|
return ChessBoardState(
|
|
bottomColor, turnColor, ChessPosition.getInstance().currentPosition);
|
|
}
|
|
|
|
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {
|
|
// for (int i = 0; i < 7; i++)
|
|
}
|
|
}
|