mchess-client/lib/chess_bloc/chess_bloc.dart

212 lines
5.6 KiB
Dart
Raw Normal View History

2023-06-02 21:28:40 +00:00
import 'dart:convert';
import 'dart:developer';
2023-06-02 21:28:40 +00:00
2022-11-13 00:03:09 +00:00
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';
2022-12-25 15:16:23 +00:00
import 'package:mchess/utils/chess_utils.dart';
2022-11-13 00:03:09 +00:00
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
static final ChessBloc _instance = ChessBloc._internal();
static ChessColor turnColor = ChessColor.white;
static ChessColor myColor = ChessColor.white;
static ChessColor getMyColor() {
return myColor;
}
2022-11-13 00:03:09 +00:00
ChessBloc._internal() : super(ChessBoardState.init()) {
2022-12-18 00:04:08 +00:00
on<InitBoard>(initBoard);
on<ColorDetermined>(flipBoard);
on<ReceivedBoardState>(moveAndPositionHandler);
on<OwnPieceMoved>(ownMoveHandler);
2023-07-03 17:41:12 +00:00
on<OwnPromotionPlayed>(ownPromotionHandler);
on<InvalidMovePlayed>(invalidMoveHandler);
2022-11-13 00:03:09 +00:00
}
factory ChessBloc.getInstance() {
return ChessBloc();
}
factory ChessBloc() {
return _instance;
}
2022-12-18 00:04:08 +00:00
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
turnColor = ChessColor.white;
ChessPositionManager.getInstance().resetToStartingPosition();
emit(
ChessBoardState(
ChessColor.white,
ChessColor.white,
ChessPositionManager.getInstance().currentPosition,
ChessMove.none(),
false,
ChessCoordinate.none(),
),
);
2022-12-18 00:04:08 +00:00
}
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
log("My Color is $myColor");
myColor = event.playerColor;
emit(
ChessBoardState(
event.playerColor,
state.newTurnColor,
state.position,
ChessMove.none(),
false,
ChessCoordinate.none(),
),
);
2022-12-18 00:04:08 +00:00
}
void moveAndPositionHandler(
ReceivedBoardState event, Emitter<ChessBoardState> emit) {
ChessMove? move;
2023-12-25 16:50:58 +00:00
if (event.startSquare != ChessCoordinate.none() &&
event.endSquare != ChessCoordinate.none()) {
move = ChessMove(from: event.startSquare!, to: event.endSquare!);
ChessPositionManager.getInstance()
.recordMove(event.startSquare, event.endSquare, event.position);
}
myColor = event.playerColor;
turnColor = event.turnColor;
emit(
ChessBoardState(
myColor,
2024-05-19 19:18:47 +00:00
event.turnColor,
event.position,
move,
true,
event.squareInCheck,
),
);
2023-07-03 17:41:12 +00:00
}
void ownMoveHandler(OwnPieceMoved event, Emitter<ChessBoardState> emit) {
log('ownMoveHandler()');
2023-07-03 17:41:12 +00:00
var apiMove =
ChessMove(from: event.startSquare, to: event.endSquare).toApiMove();
var apiMessage = ApiWebsocketMessage(
type: MessageType.move,
move: apiMove,
turnColor: null,
reason: null,
position: null,
squareInCheck: null,
playerColor: null,
);
2023-06-08 15:10:48 +00:00
ServerConnection.getInstance().send(jsonEncode(apiMessage));
2023-06-08 15:10:48 +00:00
2023-06-29 23:49:18 +00:00
//Temporary chess position until server responds with acknowledgement
var move = ChessMove.fromApiMove(apiMove);
var tempPosition = ChessPositionManager.getInstance().copyOfCurrentPosition;
tempPosition[move.to] = tempPosition[move.from] ?? const ChessPiece.none();
tempPosition[move.from] = const ChessPiece.none();
2023-06-08 15:10:48 +00:00
emit(
ChessBoardState(
state.bottomColor,
turnColor,
tempPosition,
ChessPositionManager.getInstance().lastMove,
true,
ChessCoordinate.none(),
),
2023-06-08 15:10:48 +00:00
);
}
2023-07-03 17:41:12 +00:00
void ownPromotionHandler(
OwnPromotionPlayed event, Emitter<ChessBoardState> emit) {
var apiMove = event.move.toApiMove();
var shorNameForPiece = chessPiecesShortName[
ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor)]!;
2023-07-03 17:41:12 +00:00
apiMove.promotionToPiece = shorNameForPiece;
var message = ApiWebsocketMessage(
type: MessageType.move,
move: apiMove,
turnColor: null,
2023-07-03 17:41:12 +00:00
reason: null,
position: null,
squareInCheck: null,
playerColor: null,
2023-07-03 17:41:12 +00:00
);
log(jsonEncode(message));
ServerConnection.getInstance().send(jsonEncode(message));
}
void invalidMoveHandler(
InvalidMovePlayed event, Emitter<ChessBoardState> emit) {
var move = ChessPositionManager.getInstance().lastMove;
2023-07-01 07:29:43 +00:00
emit(
ChessBoardState(
state.bottomColor,
turnColor,
ChessPositionManager.getInstance().currentPosition,
move,
true,
event.squareInCheck,
),
2023-07-01 07:29:43 +00:00
);
}
2022-11-13 00:03:09 +00:00
}
class ChessBoardState {
final ChessColor bottomColor;
2022-12-18 00:04:08 +00:00
final ChessColor newTurnColor;
final ChessPosition position;
final ChessMove lastMove;
final bool colorLastMove;
final ChessCoordinate squareInCheck;
2022-11-13 00:03:09 +00:00
ChessBoardState._(
this.bottomColor,
this.newTurnColor,
this.position,
this.lastMove,
this.colorLastMove,
this.squareInCheck,
);
factory ChessBoardState(
2022-12-18 00:04:08 +00:00
ChessColor bottomColor,
ChessColor turnColor,
ChessPosition position,
ChessMove? lastMove,
bool positionAckd,
ChessCoordinate squareInCheck,
) {
return ChessBoardState._(bottomColor, turnColor, position,
lastMove ?? ChessMove.none(), positionAckd, squareInCheck);
}
factory ChessBoardState.init() {
2022-12-18 00:04:08 +00:00
ChessColor bottomColor = ChessColor.white;
ChessColor turnColor = ChessColor.white;
ChessPositionManager.getInstance().resetToStartingPosition();
return ChessBoardState(
bottomColor,
turnColor,
ChessPositionManager.getInstance().currentPosition,
ChessMove.none(),
false,
ChessCoordinate.none(),
);
}
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {
// for (int i = 0; i < 7; i++)
}
2022-11-13 00:03:09 +00:00
}