Fix resetting the board in case a new game is started.
This commit is contained in:
parent
0627a627d8
commit
d55c7bbe1a
@ -1,7 +1,7 @@
|
|||||||
import 'package:mchess/api/move.dart';
|
import 'package:mchess/api/move.dart';
|
||||||
|
|
||||||
enum MessageType {
|
enum MessageType {
|
||||||
moveMessage,
|
move,
|
||||||
colorDetermined;
|
colorDetermined;
|
||||||
|
|
||||||
String toJson() => name;
|
String toJson() => name;
|
||||||
@ -32,10 +32,13 @@ class ApiWebsocketMessage {
|
|||||||
ret = ApiWebsocketMessage(
|
ret = ApiWebsocketMessage(
|
||||||
type: type, move: null, color: ApiColor.fromJson(json['color']));
|
type: type, move: null, color: ApiColor.fromJson(json['color']));
|
||||||
break;
|
break;
|
||||||
case MessageType.moveMessage:
|
case MessageType.move:
|
||||||
ret = ApiWebsocketMessage(
|
ret = ApiWebsocketMessage(
|
||||||
type: type, move: ApiMove.fromJson(json['move']), color: null);
|
type: type, move: ApiMove.fromJson(json['move']), color: null);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() =>
|
||||||
|
{'messageType': type, 'move': move, 'color': color};
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,9 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
|
void initBoard(InitBoard event, Emitter<ChessBoardState> emit) {
|
||||||
emit(ChessBoardState.init());
|
ChessPosition.getInstance().resetToStartingPosition();
|
||||||
|
emit(ChessBoardState(ChessColor.white, ChessColor.white,
|
||||||
|
ChessPosition.getInstance().currentPosition));
|
||||||
}
|
}
|
||||||
|
|
||||||
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
|
void flipBoard(ColorDetermined event, Emitter<ChessBoardState> emit) {
|
||||||
@ -45,6 +47,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|||||||
|
|
||||||
void opponentMoveHandler(
|
void opponentMoveHandler(
|
||||||
OpponentPieceMoved event, Emitter<ChessBoardState> emit) {
|
OpponentPieceMoved event, Emitter<ChessBoardState> emit) {
|
||||||
|
log('opponentMoveHandler()');
|
||||||
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
||||||
var newPosition = ChessPosition.getInstance().currentPosition;
|
var newPosition = ChessPosition.getInstance().currentPosition;
|
||||||
|
|
||||||
@ -59,10 +62,10 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|||||||
newPosition,
|
newPosition,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
log('emitting new state with position $newPosition');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ownMoveHandler(OwnPieceMoved event, Emitter<ChessBoardState> emit) {
|
void ownMoveHandler(OwnPieceMoved event, Emitter<ChessBoardState> emit) {
|
||||||
|
log('ownMoveHandler()');
|
||||||
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
ChessPosition.getInstance().recordMove(event.startSquare, event.endSquare);
|
||||||
|
|
||||||
var start = ApiCoordinate(
|
var start = ApiCoordinate(
|
||||||
@ -70,8 +73,8 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|||||||
var end =
|
var end =
|
||||||
ApiCoordinate(col: event.endSquare.column, row: event.endSquare.row);
|
ApiCoordinate(col: event.endSquare.column, row: event.endSquare.row);
|
||||||
var move = ApiMove(startSquare: start, endSquare: end);
|
var move = ApiMove(startSquare: start, endSquare: end);
|
||||||
var message = ApiWebsocketMessage(
|
var message =
|
||||||
type: MessageType.moveMessage, move: move, color: null);
|
ApiWebsocketMessage(type: MessageType.move, move: move, color: null);
|
||||||
|
|
||||||
ServerConnection.getInstance().send(jsonEncode(message));
|
ServerConnection.getInstance().send(jsonEncode(message));
|
||||||
|
|
||||||
@ -88,7 +91,6 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|||||||
newPosition,
|
newPosition,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
log('emitting new state with position $newPosition');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,10 +112,11 @@ class ChessBoardState {
|
|||||||
factory ChessBoardState.init() {
|
factory ChessBoardState.init() {
|
||||||
ChessColor bottomColor = ChessColor.white;
|
ChessColor bottomColor = ChessColor.white;
|
||||||
ChessColor turnColor = ChessColor.white;
|
ChessColor turnColor = ChessColor.white;
|
||||||
Map<ChessCoordinate, ChessPiece> position =
|
|
||||||
ChessPosition.getInstance().currentPosition;
|
|
||||||
|
|
||||||
return ChessBoardState(bottomColor, turnColor, position);
|
ChessPosition.getInstance().resetToStartingPosition();
|
||||||
|
|
||||||
|
return ChessBoardState(
|
||||||
|
bottomColor, turnColor, ChessPosition.getInstance().currentPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {
|
void logPosition(Map<ChessCoordinate, ChessPiece> pos) {
|
||||||
|
@ -5,10 +5,10 @@ typedef ChessPositionType = Map<ChessCoordinate, ChessPiece>;
|
|||||||
typedef ChessMoveHistory = List<ChessMove>;
|
typedef ChessMoveHistory = List<ChessMove>;
|
||||||
|
|
||||||
class ChessPosition {
|
class ChessPosition {
|
||||||
static final ChessPosition _instance = ChessPosition._internal();
|
static ChessPosition _instance = ChessPosition._internal();
|
||||||
final ChessPositionType position;
|
final ChessPositionType position;
|
||||||
|
|
||||||
ChessMoveHistory history = ChessMoveHistory.empty(growable: true);
|
static ChessMoveHistory history = ChessMoveHistory.empty(growable: true);
|
||||||
|
|
||||||
static ChessPosition getInstance() {
|
static ChessPosition getInstance() {
|
||||||
return _instance;
|
return _instance;
|
||||||
@ -17,6 +17,16 @@ class ChessPosition {
|
|||||||
ChessPosition({required this.position});
|
ChessPosition({required this.position});
|
||||||
|
|
||||||
factory ChessPosition._internal() {
|
factory ChessPosition._internal() {
|
||||||
|
return ChessPosition(position: _getStartingPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
ChessPositionType get currentPosition => position;
|
||||||
|
ChessMove? get lastMove {
|
||||||
|
if (history.isEmpty) return null;
|
||||||
|
return history.last;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ChessPositionType _getStartingPosition() {
|
||||||
ChessPositionType pos = {};
|
ChessPositionType pos = {};
|
||||||
|
|
||||||
for (int i = 1; i <= 8; i++) {
|
for (int i = 1; i <= 8; i++) {
|
||||||
@ -60,13 +70,12 @@ class ChessPosition {
|
|||||||
pos[ChessCoordinate(8, 1)] =
|
pos[ChessCoordinate(8, 1)] =
|
||||||
ChessPiece(ChessPieceName.whiteRook, ChessColor.white);
|
ChessPiece(ChessPieceName.whiteRook, ChessColor.white);
|
||||||
|
|
||||||
return ChessPosition(position: pos);
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
ChessPositionType get currentPosition => position;
|
void resetToStartingPosition() {
|
||||||
ChessMove? get lastMove {
|
history = ChessMoveHistory.empty(growable: true);
|
||||||
if (history.isEmpty) return null;
|
_instance = ChessPosition(position: _getStartingPosition());
|
||||||
return history.last;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void recordMove(ChessCoordinate from, ChessCoordinate to) {
|
void recordMove(ChessCoordinate from, ChessCoordinate to) {
|
||||||
|
@ -63,24 +63,26 @@ class ServerConnection {
|
|||||||
|
|
||||||
switch (apiMessage.type) {
|
switch (apiMessage.type) {
|
||||||
case MessageType.colorDetermined:
|
case MessageType.colorDetermined:
|
||||||
ChessBloc.getInstance().add(ColorDetermined(
|
handleIncomingColorDeterminedMessage(apiMessage);
|
||||||
myColor: ChessColor.fromApiColor(apiMessage.color!)));
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MessageType.moveMessage:
|
case MessageType.move:
|
||||||
handleIncomingMoveMessage(apiMessage);
|
handleIncomingMoveMessage(apiMessage);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handleIncomingColorDeterminedMessage(ApiWebsocketMessage apiMessage) {
|
||||||
|
ChessBloc.getInstance().add(InitBoard());
|
||||||
|
ChessBloc.getInstance().add(
|
||||||
|
ColorDetermined(myColor: ChessColor.fromApiColor(apiMessage.color!)));
|
||||||
|
}
|
||||||
|
|
||||||
void handleIncomingMoveMessage(ApiWebsocketMessage apiMessage) {
|
void handleIncomingMoveMessage(ApiWebsocketMessage apiMessage) {
|
||||||
var move = ChessMove.fromApiMove(apiMessage.move!);
|
var move = ChessMove.fromApiMove(apiMessage.move!);
|
||||||
if (move == ChessPosition.getInstance().lastMove) {
|
if (move == ChessPosition.getInstance().lastMove) {
|
||||||
//This is our own move that got resent by the server. Do not process.
|
//This is our own move that got resent by the server. Do not process.
|
||||||
} else {
|
} else {
|
||||||
log('lastMove: from: ${ChessPosition.getInstance().lastMove?.from} to: ${ChessPosition.getInstance().lastMove?.to}');
|
|
||||||
log('constructed move: from: ${move.from} to: ${move.to}');
|
|
||||||
log('Move received : ${move.from.toAlphabetical()}->${move.to.toAlphabetical()}');
|
|
||||||
|
|
||||||
ChessBloc.getInstance()
|
ChessBloc.getInstance()
|
||||||
.add(OpponentPieceMoved(startSquare: move.from, endSquare: move.to));
|
.add(OpponentPieceMoved(startSquare: move.from, endSquare: move.to));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user