2023-06-02 21:28:40 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2022-11-13 00:03:09 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2023-06-02 21:28:40 +00:00
|
|
|
import 'package:mchess/api/move.dart';
|
2023-06-08 15:14:50 +00:00
|
|
|
import 'package:mchess/api/websocket_message.dart';
|
2022-11-13 13:27:00 +00:00
|
|
|
import 'package:mchess/chess_bloc/chess_events.dart';
|
2023-01-30 21:39:13 +00:00
|
|
|
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-12-13 02:43:05 +00:00
|
|
|
import 'dart:developer';
|
2022-11-13 00:03:09 +00:00
|
|
|
|
|
|
|
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|
|
|
static final ChessBloc _instance = ChessBloc._internal();
|
2023-01-30 21:39:13 +00:00
|
|
|
static ChessColor turnColor = ChessColor.white;
|
2022-12-21 22:14:53 +00:00
|
|
|
static ChessColor? myColor;
|
|
|
|
|
|
|
|
static ChessColor? getSidesColor() {
|
|
|
|
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);
|
2023-01-30 21:39:13 +00:00
|
|
|
on<OpponentPieceMoved>(opponentMoveHandler);
|
|
|
|
on<OwnPieceMoved>(ownMoveHandler);
|
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) {
|
2023-06-08 18:23:00 +00:00
|
|
|
ChessPosition.getInstance().resetToStartingPosition();
|
|
|
|
emit(ChessBoardState(ChessColor.white, ChessColor.white,
|
|
|
|
ChessPosition.getInstance().currentPosition));
|
2022-12-18 00:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
|
2022-12-21 22:14:53 +00:00
|
|
|
log("My Color is $myColor");
|
|
|
|
myColor = event.myColor;
|
2022-12-18 00:04:08 +00:00
|
|
|
emit(ChessBoardState(event.myColor, state.newTurnColor, state.position));
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:39:13 +00:00
|
|
|
void opponentMoveHandler(
|
|
|
|
OpponentPieceMoved event, Emitter<ChessBoardState> emit) {
|
2023-06-08 18:23:00 +00:00
|
|
|
log('opponentMoveHandler()');
|
2023-01-30 21:39:13 +00:00
|
|
|
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
|
|
|
var newPosition = ChessPosition.getInstance().currentPosition;
|
2022-11-13 01:42:10 +00:00
|
|
|
|
2023-01-30 21:39:13 +00:00
|
|
|
turnColor = state.newTurnColor == ChessColor.white
|
2022-12-18 00:04:08 +00:00
|
|
|
? ChessColor.black
|
|
|
|
: ChessColor.white;
|
2022-11-19 10:37:56 +00:00
|
|
|
|
2022-12-18 00:04:08 +00:00
|
|
|
emit(
|
|
|
|
ChessBoardState(
|
|
|
|
state.bottomColor,
|
2023-01-30 21:39:13 +00:00
|
|
|
turnColor,
|
2022-12-18 00:04:08 +00:00
|
|
|
newPosition,
|
|
|
|
),
|
|
|
|
);
|
2022-12-14 22:17:31 +00:00
|
|
|
}
|
2023-01-30 21:39:13 +00:00
|
|
|
|
|
|
|
void ownMoveHandler(OwnPieceMoved event, Emitter<ChessBoardState> emit) {
|
2023-06-08 18:23:00 +00:00
|
|
|
log('ownMoveHandler()');
|
2023-01-30 21:39:13 +00:00
|
|
|
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
|
|
|
|
2023-06-02 21:28:40 +00:00
|
|
|
var start = ApiCoordinate(
|
|
|
|
col: event.startSquare.column, row: event.startSquare.row);
|
|
|
|
var end =
|
|
|
|
ApiCoordinate(col: event.endSquare.column, row: event.endSquare.row);
|
|
|
|
var move = ApiMove(startSquare: start, endSquare: end);
|
2023-06-08 18:23:00 +00:00
|
|
|
var message =
|
|
|
|
ApiWebsocketMessage(type: MessageType.move, move: move, color: null);
|
2023-06-02 21:28:40 +00:00
|
|
|
|
2023-06-08 15:14:50 +00:00
|
|
|
ServerConnection.getInstance().send(jsonEncode(message));
|
2023-06-08 15:10:48 +00:00
|
|
|
|
|
|
|
turnColor = state.newTurnColor == ChessColor.white
|
|
|
|
? ChessColor.black
|
|
|
|
: ChessColor.white;
|
|
|
|
|
|
|
|
var newPosition = ChessPosition.getInstance().currentPosition;
|
|
|
|
|
|
|
|
emit(
|
|
|
|
ChessBoardState(
|
|
|
|
state.bottomColor,
|
|
|
|
turnColor,
|
|
|
|
newPosition,
|
|
|
|
),
|
|
|
|
);
|
2023-01-30 21:39:13 +00:00
|
|
|
}
|
2022-11-13 00:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ChessBoardState {
|
2022-12-18 00:04:08 +00:00
|
|
|
late ChessColor bottomColor;
|
|
|
|
final ChessColor newTurnColor;
|
2022-11-13 00:03:09 +00:00
|
|
|
final Map<ChessCoordinate, ChessPiece> position;
|
|
|
|
|
2022-12-18 00:04:08 +00:00
|
|
|
ChessBoardState._(this.bottomColor, this.newTurnColor, this.position);
|
2022-11-13 01:42:10 +00:00
|
|
|
|
|
|
|
factory ChessBoardState(
|
2022-12-18 00:04:08 +00:00
|
|
|
ChessColor bottomColor,
|
2022-11-13 01:42:10 +00:00
|
|
|
ChessColor turnColor,
|
|
|
|
Map<ChessCoordinate, ChessPiece> position,
|
|
|
|
) {
|
2022-12-18 00:04:08 +00:00
|
|
|
return ChessBoardState._(bottomColor, turnColor, position);
|
2022-11-13 01:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
factory ChessBoardState.init() {
|
2022-12-18 00:04:08 +00:00
|
|
|
ChessColor bottomColor = ChessColor.white;
|
2022-11-13 01:42:10 +00:00
|
|
|
ChessColor turnColor = ChessColor.white;
|
|
|
|
|
2023-06-08 18:23:00 +00:00
|
|
|
ChessPosition.getInstance().resetToStartingPosition();
|
|
|
|
|
|
|
|
return ChessBoardState(
|
|
|
|
bottomColor, turnColor, ChessPosition.getInstance().currentPosition);
|
2022-11-13 01:42:10 +00:00
|
|
|
}
|
2022-12-18 22:43:06 +00:00
|
|
|
|
|
|
|
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {
|
|
|
|
// for (int i = 0; i < 7; i++)
|
|
|
|
}
|
2022-11-13 00:03:09 +00:00
|
|
|
}
|