Fix tapping on opponent's piece #8

Merged
marco merged 1 commits from fix-tap-on-drag into master 2024-02-01 10:42:19 +00:00
5 changed files with 14 additions and 10 deletions
Showing only changes of commit 200393ac76 - Show all commits

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:mchess/chess/chess_square_outer_dragtarget.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/tap_bloc.dart';
import '../utils/chess_utils.dart';
@ -66,7 +67,8 @@ class _ChessSquareState extends State<ChessSquare> {
containedPiece: widget.containedPiece ?? const ChessPiece.none()),
);
if (widget.containedPiece == null) {
if (widget.containedPiece == null ||
widget.containedPiece!.color != ChessBloc.getMyColor()) {
return GestureDetector(
child: dragTarget,
onTap: () {

View File

@ -30,13 +30,13 @@ class ChessSquareOuterDragTarget extends StatelessWidget {
if (isPromotionMove(
details.data.movedPiece!.pieceClass,
ChessBloc.myColor!,
ChessBloc.getMyColor(),
details.data.toSquare,
)) {
var move = ChessMove(
from: details.data.fromSquare, to: details.data.toSquare);
PromotionBloc().add(PawnMovedToPromotionField(
move: move, colorMoved: ChessBloc.myColor!));
move: move, colorMoved: ChessBloc.myColor));
} else if (coordinate != details.data.fromSquare) {
ChessBloc().add(OwnPieceMoved(
startSquare: details.data.fromSquare,

View File

@ -11,9 +11,9 @@ import 'package:mchess/utils/chess_utils.dart';
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
static final ChessBloc _instance = ChessBloc._internal();
static ChessColor turnColor = ChessColor.white;
static ChessColor? myColor = ChessColor.white;
static ChessColor myColor = ChessColor.white;
static ChessColor? getSidesColor() {
static ChessColor getMyColor() {
return myColor;
}
@ -126,7 +126,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
OwnPromotionPlayed event, Emitter<ChessBoardState> emit) {
var apiMove = event.move.toApiMove();
var shorNameForPiece = chessPiecesShortName[
ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor!)]!;
ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor)]!;
apiMove.promotionToPiece = shorNameForPiece;
var message = ApiWebsocketMessage(
type: MessageType.move,

View File

@ -51,12 +51,12 @@ class TapBloc extends Bloc<TapEvent, TapState> {
state.firstSquareTapped != event.tapped) {
if (isPromotionMove(
state.pieceOnFirstTappedSquare!.pieceClass,
ChessBloc.myColor!,
ChessBloc.myColor,
event.tapped,
)) {
PromotionBloc().add(PawnMovedToPromotionField(
move: ChessMove(from: state.firstSquareTapped!, to: event.tapped),
colorMoved: ChessBloc.myColor!));
colorMoved: ChessBloc.myColor));
emit(TapState.init());
return;
} else {

View File

@ -168,8 +168,10 @@ class _LobbySelectorState extends State<LobbySelector> {
backgroundColor: Colors.amberAccent,
content: Text("mChess server is not responding. Try again or give up"),
);
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
if (mounted) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
return null;
}